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
25
BOOST_AUTO_TEST_SUITE
( StringUtilsTests )
26
27
// std::string overload: no illegal chars -> returns false and string unchanged
28
BOOST_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
40
BOOST_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
64
BOOST_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
74
BOOST_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
86
BOOST_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
109
BOOST_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
118
BOOST_AUTO_TEST_CASE
( ConvertPathToFileUri_UnixAbsolutePath )
119
{
120
// /tmp exists on all Unix CI machines
121
BOOST_TEST
(
ConvertPathToFileUri
( wxS(
"/tmp"
),
nullptr
) == wxString(
"file:///tmp"
) );
122
}
123
124
BOOST_AUTO_TEST_CASE
( ConvertPathToFileUri_AlreadyHasScheme )
125
{
126
BOOST_TEST
(
ConvertPathToFileUri
( wxS(
"https://example.com"
),
nullptr
) == wxString(
"https://example.com"
) );
127
BOOST_TEST
(
ConvertPathToFileUri
( wxS(
"file:///path"
),
nullptr
) == wxString(
"file:///path"
) );
128
}
129
130
BOOST_AUTO_TEST_CASE
( ConvertPathToFileUri_Empty )
131
{
132
BOOST_TEST
(
ConvertPathToFileUri
( wxEmptyString,
nullptr
) == wxString(
""
) );
133
}
134
135
BOOST_AUTO_TEST_CASE
( ConvertPathToFileUri_Tilde )
136
{
137
BOOST_TEST
(
ConvertPathToFileUri
( wxS(
"~"
),
nullptr
) == wxString(
"~"
) );
138
}
139
140
BOOST_AUTO_TEST_CASE
( ConvertPathToFileUri_NonexistentPath )
141
{
142
BOOST_TEST
(
ConvertPathToFileUri
( wxS(
"/nonexistent/file.pdf"
),
nullptr
)
143
== wxString(
"/nonexistent/file.pdf"
) );
144
}
145
146
BOOST_AUTO_TEST_CASE
( ConvertPathToFileUri_PlainText )
147
{
148
// Non-path strings should pass through unchanged
149
BOOST_TEST
(
ConvertPathToFileUri
( wxS(
"LM358"
),
nullptr
) == wxString(
"LM358"
) );
150
BOOST_TEST
(
ConvertPathToFileUri
( wxS(
"100nF"
),
nullptr
) == wxString(
"100nF"
) );
151
}
152
153
BOOST_AUTO_TEST_SUITE_END
()
name
const char * name
Definition
DXF_plotter.cpp:65
ConvertPathToFileUri
wxString ConvertPathToFileUri(const wxString &aPath, const PROJECT *aProject)
Convert a file path to a file:// URI.
Definition
string_utils.cpp:1561
ReplaceIllegalFileNameChars
bool ReplaceIllegalFileNameChars(std::string &aName, int aReplaceChar)
Checks aName for illegal file name characters.
Definition
string_utils.cpp:1329
string_utils.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
Definition
test_api_enums.cpp:71
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_TEST
BOOST_TEST(contains==c.ExpectedContains)
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(ReplaceIllegalFileNameChars_StdString_NoChange)
Definition
test_string_utils.cpp:28
src
qa
tests
common
test_string_utils.cpp
Generated on Sat May 16 2026 00:07:11 for KiCad PCB EDA Suite by
1.13.2