KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_string_utils.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21#define BOOST_TEST_NO_MAIN
22#include <boost/test/unit_test.hpp>
23#include <string_utils.h>
24
25BOOST_AUTO_TEST_SUITE( StringUtilsTests )
26
27// std::string overload: no illegal chars -> returns false and string unchanged
28BOOST_AUTO_TEST_CASE( ReplaceIllegalFileNameChars_StdString_NoChange )
29{
30 std::string name = "valid_filename-123._ok";
31 std::string original = name;
32
33 bool changed = ReplaceIllegalFileNameChars( name );
34
35 BOOST_TEST( changed == false );
36 BOOST_TEST( name == original );
37}
38
39// std::string overload: with illegal chars, default behavior -> percent-hex encoding
40BOOST_AUTO_TEST_CASE( ReplaceIllegalFileNameChars_StdString_PercentEncoding )
41{
42 // Contains several illegal chars: \ / ? * | " < >
43 std::string name = R"(bad\name/with?illegal*chars|"<>.txt)";
44 bool changed = ReplaceIllegalFileNameChars( name ); // aReplaceChar defaults to 0 -> percent-hex
45
46 BOOST_TEST( changed == true );
47
48 // Illegal characters should have been hex-encoded
49 BOOST_TEST( name.find( "%5c" ) != std::string::npos ); // backslash
50 BOOST_TEST( name.find( "%2f" ) != std::string::npos ); // forward slash
51 BOOST_TEST( name.find( "%3f" ) != std::string::npos ); // question mark
52 BOOST_TEST( name.find( "%2a" ) != std::string::npos ); // asterisk
53 BOOST_TEST( name.find( "%7c" ) != std::string::npos ); // vertical bar
54 BOOST_TEST( name.find( "%22" ) != std::string::npos ); // double quote
55 BOOST_TEST( name.find( "%3c" ) != std::string::npos ); // less-than
56 BOOST_TEST( name.find( "%3e" ) != std::string::npos ); // greater-than
57
58 // Non-illegal characters should remain unchanged
59 BOOST_TEST( name.find( ".txt" ) != std::string::npos );
60 BOOST_TEST( name.find( "bad" ) != std::string::npos );
61}
62
63// std::string overload: replacement character provided -> use that character
64BOOST_AUTO_TEST_CASE( ReplaceIllegalFileNameChars_StdString_ReplacementChar )
65{
66 std::string name = R"(a*bad?name|file)";
67 bool changed = ReplaceIllegalFileNameChars( name, '_' ); // replace illegal with underscore
68
69 BOOST_TEST( changed == true );
70 BOOST_TEST( name == "a_bad_name_file" );
71}
72
73// wxString overload: no illegal chars -> returns false and string unchanged
74BOOST_AUTO_TEST_CASE( ReplaceIllegalFileNameChars_WxString_NoChange )
75{
76 wxString name = "valid_filename-123._ok";
77 wxString original = name;
78
79 bool changed = ReplaceIllegalFileNameChars( name );
80
81 BOOST_TEST( changed == false );
82 BOOST_TEST( name == original );
83}
84
85// wxString overload: percent-hex encoding when aReplaceChar == 0
86BOOST_AUTO_TEST_CASE( ReplaceIllegalFileNameChars_WxString_PercentEncoding )
87{
88 wxString name = "bad\\name/with?illegal*chars|\"<>.txt";
89 bool changed = ReplaceIllegalFileNameChars( name ); // default -> percent-hex
90
91 BOOST_TEST( changed == true );
92
93 // Illegal characters should have been hex-encoded
94 BOOST_TEST( name.Contains( "%5c" ) ); // backslash
95 BOOST_TEST( name.Contains( "%2f" ) ); // forward slash
96 BOOST_TEST( name.Contains( "%3f" ) ); // question mark
97 BOOST_TEST( name.Contains( "%2a" ) ); // asterisk
98 BOOST_TEST( name.Contains( "%7c" ) ); // vertical bar
99 BOOST_TEST( name.Contains( "%22" ) ); // double quote
100 BOOST_TEST( name.Contains( "%3c" ) ); // less-than
101 BOOST_TEST( name.Contains( "%3e" ) ); // greater-than
102
103 // Non-illegal characters should remain unchanged
104 BOOST_TEST( name.Contains( ".txt" ) );
105 BOOST_TEST( name.Contains( "bad" ) );
106}
107
108// wxString overload: replacement character provided -> use that character
109BOOST_AUTO_TEST_CASE( ReplaceIllegalFileNameChars_WxString_ReplacementChar )
110{
111 wxString name = "a*bad?name|file";
112 bool changed = ReplaceIllegalFileNameChars( name, '_' );
113
114 BOOST_TEST( changed == true );
115 BOOST_TEST( name == "a_bad_name_file" );
116}
117
const char * name
bool ReplaceIllegalFileNameChars(std::string &aName, int aReplaceChar)
Checks aName for illegal file name characters.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_TEST(contains==c.ExpectedContains)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(ReplaceIllegalFileNameChars_StdString_NoChange)