KiCad PCB EDA Suite
test_altium_rule_transformer.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 (C) 2021 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, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
30 
32 
34 {
36 };
37 
38 
42 BOOST_FIXTURE_TEST_SUITE( AltiumRuleTransformer, ALTIUM_RULE_TRANSFORMER_FIXTURE )
43 
44 
45 BOOST_AUTO_TEST_CASE( AltiumRuleTokenizerEmptyInput )
46 {
47  ALTIUM_RULE_TOKENIZER tokenizer( "" );
48 
49  const ALTIUM_RULE_TOKEN& peek = tokenizer.Peek();
51  BOOST_CHECK_EQUAL( 0, peek.pos );
52 
53  const ALTIUM_RULE_TOKEN& next = tokenizer.Next();
55  BOOST_CHECK_EQUAL( 0, next.pos );
56 
57  const ALTIUM_RULE_TOKEN& peek2 = tokenizer.Peek();
59  BOOST_CHECK_EQUAL( 0, peek2.pos );
60 }
61 
62 BOOST_AUTO_TEST_CASE( AltiumRuleTokenizerOnlySpaces )
63 {
64  ALTIUM_RULE_TOKENIZER tokenizer( " " );
65 
66  const ALTIUM_RULE_TOKEN& peek = tokenizer.Peek();
68  BOOST_CHECK_EQUAL( 3, peek.pos );
69 
70  const ALTIUM_RULE_TOKEN& next = tokenizer.Next();
72  BOOST_CHECK_EQUAL( 3, next.pos );
73 
74  const ALTIUM_RULE_TOKEN& peek2 = tokenizer.Peek();
76  BOOST_CHECK_EQUAL( 3, peek2.pos );
77 }
78 
79 BOOST_AUTO_TEST_CASE( AltiumRuleTokenizerSingleCharIdentifier )
80 {
81  ALTIUM_RULE_TOKENIZER tokenizer( "a" );
82 
83  const ALTIUM_RULE_TOKEN& next = tokenizer.Next();
85  BOOST_CHECK_EQUAL( 0, next.pos );
86  BOOST_CHECK_EQUAL( "a", next.sValue );
87 
88  const ALTIUM_RULE_TOKEN& peek = tokenizer.Peek();
90  BOOST_CHECK_EQUAL( 1, peek.pos );
91 
92  const ALTIUM_RULE_TOKEN& next2 = tokenizer.Next();
94  BOOST_CHECK_EQUAL( 1, next2.pos );
95 }
96 
97 
99 {
100  wxString input;
101  std::vector<ALTIUM_RULE_TOKEN> exp_token;
102 };
103 
107 static const std::vector<ALTIUM_RULE_TOKENIZER_INPUT_OUTPUT> altium_rule_tokens_property = {
108  // Empty string
109  { "",
110  {
113  }
114  },
115  // Single Token
116  { "All",
117  {
118  { ALTIUM_RULE_TOKEN_KIND::IDENT, 0, "All" },
120  }
121  },
122  { "1234",
123  {
124  { ALTIUM_RULE_TOKEN_KIND::CONST_INT, 0, 1234L },
126  }
127  },
128  { "+1234",
129  {
130  { ALTIUM_RULE_TOKEN_KIND::CONST_INT, 0, 1234L },
132  }
133  },
134  { "-1234",
135  {
136  { ALTIUM_RULE_TOKEN_KIND::CONST_INT, 0, -1234L },
138  }
139  },
140  { "'1234'",
141  {
144  }
145  },
146  { "True",
147  {
150  }
151  },
152  { "true",
153  {
156  }
157  },
158  { "False",
159  {
162  }
163  },
164  { "false",
165  {
168  }
169  },
170  { "+",
171  {
174  }
175  },
176  { "-",
177  {
180  }
181  },
182  { "*",
183  {
186  }
187  },
188  { "/",
189  {
192  }
193  },
194  { "Div",
195  {
198  }
199  },
200  { "div",
201  {
204  }
205  },
206  { "Mod",
207  {
210  }
211  },
212  { "mod",
213  {
216  }
217  },
218  { "And",
219  {
222  }
223  },
224  { "and",
225  {
228  }
229  },
230  { "&&",
231  {
234  }
235  },
236  { "Or",
237  {
240  }
241  },
242  { "or",
243  {
246  }
247  },
248  { "||",
249  {
252  }
253  },
254  { "Xor",
255  {
258  }
259  },
260  { "xor",
261  {
264  }
265  },
266  { "Not",
267  {
270  }
271  },
272  { "not",
273  {
276  }
277  },
278  { "<",
279  {
282  }
283  },
284  { "<=",
285  {
288  }
289  },
290  { ">",
291  {
294  }
295  },
296  { ">=",
297  {
300  }
301  },
302  { "<>",
303  {
306  }
307  },
308  { "=",
309  {
312  }
313  },
314  { "Between",
315  {
318  }
319  },
320  { "between",
321  {
324  }
325  },
326  { "Like",
327  {
330  }
331  },
332  { "like",
333  {
336  }
337  },
338  // Multiple tokens
339  { "ab cd ef",
340  {
341  { ALTIUM_RULE_TOKEN_KIND::IDENT, 0, "ab" },
342  { ALTIUM_RULE_TOKEN_KIND::IDENT, 3, "cd" },
343  { ALTIUM_RULE_TOKEN_KIND::IDENT, 6, "ef" },
345  }
346  },
347  // Complex tests
348  { "InComponent('LEDS1') or InComponent('LEDS2')",
349  {
350  { ALTIUM_RULE_TOKEN_KIND::IDENT, 0, "InComponent" },
352  { ALTIUM_RULE_TOKEN_KIND::CONST_STRING, 12, "LEDS1" },
355  { ALTIUM_RULE_TOKEN_KIND::IDENT, 24, "InComponent" },
357  { ALTIUM_RULE_TOKEN_KIND::CONST_STRING, 36, "LEDS2" },
360  }
361  }
362 };
363 
367 BOOST_AUTO_TEST_CASE( AltiumRuleTokenizerParameterizedTest )
368 {
369  for( const auto& c : altium_rule_tokens_property )
370  {
371  BOOST_TEST_CONTEXT( wxString::Format( wxT( "'%s'" ), c.input ) )
372  {
373  ALTIUM_RULE_TOKENIZER tokenizer( c.input );
374 
375  for( const auto& expected : c.exp_token )
376  {
377  const ALTIUM_RULE_TOKEN& token = tokenizer.Next();
378  BOOST_CHECK( ( expected.kind == token.kind ) );
379  BOOST_CHECK_EQUAL( expected.pos, token.pos );
380  BOOST_CHECK_EQUAL( expected.iValue, token.iValue );
381  BOOST_CHECK_EQUAL( expected.fValue, token.fValue );
382  BOOST_CHECK_EQUAL( expected.sValue, token.sValue );
383  }
384  }
385  }
386 }
387 
388 BOOST_AUTO_TEST_SUITE_END()
CITER next(CITER it)
Definition: ptree.cpp:126
std::vector< ALTIUM_RULE_TOKEN > exp_token
const ALTIUM_RULE_TOKEN & Peek() const
BOOST_AUTO_TEST_CASE(AltiumRuleTokenizerEmptyInput)
Declares the struct as the Boost test fixture.
static const std::vector< ALTIUM_RULE_TOKENIZER_INPUT_OUTPUT > altium_rule_tokens_property
A list of valid test strings and the expected results.
BOOST_CHECK(v2.Cross(v1)==1)
const ALTIUM_RULE_TOKEN & Next()
#define BOOST_TEST_CONTEXT(A)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
ALTIUM_RULE_TOKEN_KIND kind