From 346f8138140f0f8a592c27cadb266e49fb51b0cf Mon Sep 17 00:00:00 2001 From: John Beard Date: Mon, 25 Feb 2019 10:40:00 +0000 Subject: [PATCH] QA: Add WX_FILENAME split tests Add some tests of the filename splitting of WX_FILENAME objects. Interestingly, the result of GetFuillPath on an empty path is likely wrong as it will imply an absolute path from /. But this is probably not a used code path. Document the current behaviour as passing the test. This unit tests covers the code reported by Coverity 183884 and 183894 (improper use of negative). The find_last_of can return "npos" (-1), but it's stored as a size_t. This is correct according the WX docs, and the substr() method it is passed to can handle npos too. --- qa/common/CMakeLists.txt | 1 + qa/common/test_wx_filename.cpp | 129 +++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 qa/common/test_wx_filename.cpp diff --git a/qa/common/CMakeLists.txt b/qa/common/CMakeLists.txt index 229edab2cc..6e811da076 100644 --- a/qa/common/CMakeLists.txt +++ b/qa/common/CMakeLists.txt @@ -47,6 +47,7 @@ set( common_srcs test_title_block.cpp test_utf8.cpp test_wildcards_and_files_ext.cpp + test_wx_filename.cpp libeval/test_numeric_evaluator.cpp diff --git a/qa/common/test_wx_filename.cpp b/qa/common/test_wx_filename.cpp new file mode 100644 index 0000000000..6dfdad0b73 --- /dev/null +++ b/qa/common/test_wx_filename.cpp @@ -0,0 +1,129 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2019 KiCad Developers, see CHANGELOG.TXT for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/** + * @file + * Test suite for WX_FILNAME + */ + +#include + +// Code under test +#include + +/** + * Declare the test suite + */ +BOOST_AUTO_TEST_SUITE( WxFilename ) + + +struct WX_FILENAME_SPLIT_CASE +{ + // Ctor params + std::string m_path; + std::string m_name; + + // Split results + std::string m_exp_name; + std::string m_exp_full_name; + std::string m_exp_path; + std::string m_exp_full_path; +}; + + +// clang-format off +static const std::vector split_cases = { + { + "", + "", + "", + "", + "", + "/", // This doesn't look right... + }, + { + "", + "name.ext", + "name", + "name.ext", + "", + "/name.ext", // This doesn't look right... + }, + { + "/tmp/example", + "", + "", + "", + "/tmp/example", + "/tmp/example/", + }, + { + "/tmp/example", + "name.ext", + "name", + "name.ext", + "/tmp/example", + "/tmp/example/name.ext", + }, + { + "/tmp/example", + "name", // no extension + "name", + "name", + "/tmp/example", + "/tmp/example/name", + }, + { + "/tmp/example", + "name.ext1.ext2", // two extensions + "name.ext1", // remove the first one + "name.ext1.ext2", + "/tmp/example", + "/tmp/example/name.ext1.ext2", + }, +}; +// clang-format on + +/** + * Check the various split cases work correctly + */ +BOOST_AUTO_TEST_CASE( Split ) +{ + for( const auto& c : split_cases ) + { + std::stringstream ss; + ss << c.m_path << ", " << c.m_name; + BOOST_TEST_CONTEXT( ss.str() ) + { + // Const: all methods called must be const + const WX_FILENAME wx_fn( c.m_path, c.m_name ); + + BOOST_CHECK_EQUAL( c.m_exp_name, wx_fn.GetName() ); + BOOST_CHECK_EQUAL( c.m_exp_full_name, wx_fn.GetFullName() ); + BOOST_CHECK_EQUAL( c.m_exp_path, wx_fn.GetPath() ); + BOOST_CHECK_EQUAL( c.m_exp_full_path, wx_fn.GetFullPath() ); + } + } +} + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file