KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_altium_parser_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
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
24
26#include <boost/test/data/test_case.hpp>
27
29
34
35
39BOOST_FIXTURE_TEST_SUITE( AltiumParserUtils, ALTIUM_PARSER_UTILS_FIXTURE )
40
41
43{
44 wxString input;
45 wxString exp_result;
46 std::map<wxString, wxString> override;
47};
48
49std::ostream & operator<<(std::ostream & strm, SPECIAL_STRINGS_TO_KICAD const & data) {
50 return strm << "[" << data.input << " -> " << data.exp_result << "]";
51}
52
56static const std::vector<SPECIAL_STRINGS_TO_KICAD> sch_special_string_to_kicad_property = {
57 // Empty
58 { "", "", {} },
59 // No Special Strings
60 { "A", "A", {} },
61 { " A", " A", {} },
62 { "A ", "A ", {} },
63 { "A=B", "A=B", {} },
64 { "A=B+C", "A=B+C", {} },
65 { "A\nB", "A\nB", {} },
66 { "A\tB", "A\tB", {} },
67 { "This is a long text with spaces", "This is a long text with spaces", {} },
68 // Text format (underscore,...), TODO: add
69 // Escaping, TODO: add
70 { "+", "+", {} },
71 { "'", "'", {} },
72 { "'A'", "'A'", {} },
73 { "A+B", "A+B", {} },
74 { "A=B", "A=B", {} },
75 { "$", "$", {} },
76 { "{", "{", {} },
77 { "}", "}", {} },
78 { "${A}", "${A}", {} }, // TODO: correct substitution
79 // Simple special strings
80 { "=A", "${A}", {} },
81 { "=A", "${C}", { { "A", "C" } } },
82 { "=A_B", "${A_B}", {} },
83 // Combined special strings
84 { "=A+B", "${A}${B}", {} },
85 { "=A+B", "${A}${B}", {} },
86 { "=A+B", "${C}${B}", { { "A", "C" } } },
87 { "=A+B", "${C}${D}", { { "A", "C" }, { "B", "D" } } },
88 // Case-insensitive special strings
89 { "=A", "${C}", { { "A", "C" } } },
90 { "=a", "${C}", { { "A", "C" } } },
91 { "=AB", "${C}", { { "AB", "C" } } },
92 { "=aB", "${C}", { { "AB", "C" } } },
93 { "=Ab", "${C}", { { "AB", "C" } } },
94 { "=ab", "${C}", { { "AB", "C" } } },
95 // Special strings with text
96 { "='A'", "A", {} },
97 { "='This is a long text with spaces'", "This is a long text with spaces", {} },
98 { "='='", "=", {} },
99 { "='+'", "+", {} },
100 { "='$'", "$", {} },
101 { "='{'", "{", {} },
102 { "='}'", "}", {} },
103 { "='${A}'", "${A}", {} }, // TODO: correct substitution
104 { "='A'+'B'", "AB", {} },
105 { "='A'+' '", "A ", {} },
106 { "=' '+'B'", " B", {} },
107 { "='A'+B", "A${B}", {} },
108 { "='A'+\"B\"", "A${B}", {} },
109 { "='A' + \"B\"", "A${B}", {} },
110 { "=\"A\"+'B'", "${A}B", {} },
111 { "=\"A\" + 'B'", "${A}B", {} },
112 { "=A+'B'", "${A}B", {} },
113 { "=A+' '+B", "${A} ${B}", {} },
114 { "='A'+B+'C'+D", "A${B}C${D}", {} },
115 // Some special cases we do not know yet how to handle correctly. But we should not crash ;)
116 { "=+", "", {} },
117 { "=++", "", {} },
118 { "=+++", "", {} },
119 { "=B+", "${B}", {} },
120 { "=+B", "${B}", {} },
121 { "=B++", "${B}", {} },
122 { "=+B+", "${B}", {} },
123 { "=++B", "${B}", {} },
124 { " =", " =", {} },
125 { "= ", "", {} },
126 { "= A", "${A}", {} },
127 { "=A ", "${A}", {} },
128 { "='A'B", "A", {} },
129 { "=A'B'", "B", {} },
130 { "=A'B", "B", {} },
131 { "=A+ 'B'", "${A}B", {} },
132};
133
134
138BOOST_DATA_TEST_CASE( AltiumSchSpecialStringsToKiCadVariablesProperties,
139 boost::unit_test::data::make(sch_special_string_to_kicad_property),
140 data )
141{
142 wxString result = AltiumSchSpecialStringsToKiCadVariables( data.input, data.override );
143
144 // These are all valid
145 BOOST_CHECK_EQUAL( result, data.exp_result );
146}
147
148
152static const std::vector<SPECIAL_STRINGS_TO_KICAD> pcb_special_string_to_kicad_property = {
153 // Empty
154 { "", "", {} },
155 // No Special Strings
156 { "A", "A", {} },
157 { " A", " A", {} },
158 { "A ", "A ", {} },
159 { "A=B", "A=B", {} },
160 { "A=B+C", "A=B+C", {} },
161 { "A\nB", "A\nB", {} },
162 { "A\tB", "A\tB", {} },
163 { "This is a long text with spaces", "This is a long text with spaces", {} },
164 // Text format (underscore,...), TODO: add
165 // Escaping, TODO: add
166 { "'", "'", {} },
167 { "'A'", "'A'", {} },
168 { "$", "$", {} },
169 { "{", "{", {} },
170 { "}", "}", {} },
171 { "${A}", "${A}", {} }, // TODO: correct substitution
172 // Simple special strings starting with dot
173 { ".A", "${A}", {} },
174 { ".A", "${C}", { { "A", "C" } } },
175 { ".A_B", "${A_B}", {} },
176 // Concatenated special strings
177 { "'.A'", "${A}", {} },
178 { "'.A''.B'", "${A}${B}", {} },
179 { "'.A''.B'", "${C}${B}", { { "A", "C" } } },
180 { "'.A''.B'", "${CC}${D}", { { "A", "CC" }, { "B", "D" } } },
181 { "A='.A', B='.B'", "A=${A}, B=${B}", {} },
182 // Case-insensitive special strings
183 { ".A", "${C}", { { "A", "C" } } },
184 { ".a", "${C}", { { "A", "C" } } },
185 { ".AB", "${C}", { { "AB", "C" } } },
186 { ".aB", "${C}", { { "AB", "C" } } },
187 { ".Ab", "${C}", { { "AB", "C" } } },
188 { ".ab", "${C}", { { "AB", "C" } } },
189 // Some special cases we do not know yet how to handle correctly. But we should not crash ;)
190 { "''", "''", {} },
191 { " .", " .", {} },
192 { ". ", "${ }", {} },
193 { ". A", "${ A}", {} },
194 { ".A ", "${A }", {} },
195 { " .A", " .A", {} },
196 { "...", "${..}", {} },
197};
198
199
203BOOST_DATA_TEST_CASE( AltiumPcbSpecialStringsToKiCadStringsProperties,
204 boost::unit_test::data::make(pcb_special_string_to_kicad_property),
205 data )
206{
207 wxString result = AltiumPcbSpecialStringsToKiCadStrings( data.input, data.override );
208
209 // These are all valid
210 BOOST_CHECK_EQUAL( result, data.exp_result );
211}
212
214{
215 wxString input;
216 wxString expected;
217};
218
219std::ostream& operator<<( std::ostream& strm, PIN_DESIGNATOR_CASE const& data )
220{
221 return strm << "[" << data.input << " -> " << data.expected << "]";
222}
223
224// Verifies GitLab issue 23709 handling: Altium encodes pins tied to multiple physical
225// pads as a comma-separated designator string; KiCad expects stacked-pin bracket notation.
226static const std::vector<PIN_DESIGNATOR_CASE> pin_designator_cases = {
227 // Single-designator inputs return after whitespace trim only
228 { wxT( "" ), wxT( "" ) },
229 { wxT( "1" ), wxT( "1" ) },
230 { wxT( "A1" ), wxT( "A1" ) },
231 { wxT( "GND" ), wxT( "GND" ) },
232 { wxT( " 1 " ), wxT( "1" ) },
233 { wxT( " A1 " ), wxT( "A1" ) },
234
235 // Basic multi-designator conversion
236 { wxT( "1,2" ), wxT( "[1,2]" ) },
237 { wxT( "1,2,3" ), wxT( "[1,2,3]" ) },
238 { wxT( "A1,A2,A3" ), wxT( "[A1,A2,A3]" ) },
239
240 // Whitespace around tokens is trimmed
241 { wxT( "1, 2, 3" ), wxT( "[1,2,3]" ) },
242 { wxT( " 1 , 2 , 3 " ), wxT( "[1,2,3]" ) },
243
244 // Stray empty tokens (trailing comma, duplicate comma) are dropped
245 { wxT( "1,2," ), wxT( "[1,2]" ) },
246 { wxT( "1,,2" ), wxT( "[1,2]" ) },
247
248 // Degenerate inputs collapse back to a single bare token or to the original
249 { wxT( "1," ), wxT( "1" ) },
250 { wxT( "," ), wxT( "," ) },
251};
252
253
254BOOST_DATA_TEST_CASE( AltiumPinDesignatorToKiCadProperties,
255 boost::unit_test::data::make( pin_designator_cases ),
256 data )
257{
258 BOOST_CHECK_EQUAL( AltiumPinDesignatorToKiCad( data.input ), data.expected );
259}
260
wxString AltiumPinDesignatorToKiCad(const wxString &aDesignator)
Convert an Altium pin designator string to the equivalent KiCad pin number.
wxString AltiumSchSpecialStringsToKiCadVariables(const wxString &aString, const std::map< wxString, wxString > &aOverrides)
wxString AltiumPcbSpecialStringsToKiCadStrings(const wxString &aString, const std::map< wxString, wxString > &aOverrides)
Declares the struct as the Boost test fixture.
static const std::vector< PIN_DESIGNATOR_CASE > pin_designator_cases
BOOST_DATA_TEST_CASE(AltiumSchSpecialStringsToKiCadVariablesProperties, boost::unit_test::data::make(sch_special_string_to_kicad_property), data)
Test conversation from Altium Schematic Special String to a KiCad String with variables.
static const std::vector< SPECIAL_STRINGS_TO_KICAD > sch_special_string_to_kicad_property
A list of valid test strings and the expected results.
std::ostream & operator<<(std::ostream &strm, SPECIAL_STRINGS_TO_KICAD const &data)
static const std::vector< SPECIAL_STRINGS_TO_KICAD > pcb_special_string_to_kicad_property
A list of valid test strings and the expected results.
BOOST_AUTO_TEST_SUITE_END()
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")