KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_rule_document.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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <board.h>
34
35#include <wx/ffile.h>
36#include <wx/filename.h>
37
38
39namespace
40{
41
42const wxString TWO_CLEARANCES_AND_A_HOLE = wxS( "(version 2)\n"
43 "# header comment\n"
44 "(rule \"first\"\n"
45 "\t# keep this comment\n"
46 "\t(constraint clearance (min 0.3mm))\n"
47 "\t(condition \"A.NetClass == 'HV'\"))\n"
48 "(rule \"hole\"\n"
49 "\t(constraint hole_size (min 0.2mm)))\n"
50 "(rule \"second\"\n"
51 "\t(constraint clearance (min 0.5mm)))\n" );
52
53
54int countOf( const wxString& aText, const wxString& aNeedle )
55{
56 int count = 0;
57 size_t pos = aText.find( aNeedle );
58
59 while( pos != wxString::npos )
60 {
61 count++;
62 pos = aText.find( aNeedle, pos + 1 );
63 }
64
65 return count;
66}
67
68
69const wxString ONE_RULE_THREE_CONSTRAINTS = wxS( "(version 2)\n"
70 "(rule \"under_fpga\"\n"
71 "\t(constraint clearance (min 0.1mm))\n"
72 "\t(constraint hole_size (min 0.2mm))\n"
73 "\t(constraint via_diameter (min 0.4mm))\n"
74 "\t(condition \"A.intersectsArea('underFPGA')\"))\n" );
75
76} // namespace
77
78
79BOOST_AUTO_TEST_SUITE( DRC_RULE_DOCUMENT_TESTS )
80
81
82BOOST_AUTO_TEST_CASE( UneditedRulesRoundTripVerbatim )
83{
84 BOARD board;
85 DRC_RULE_DOCUMENT document( &board );
86
87 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
88
89 BOOST_REQUIRE_EQUAL( document.IndicesFor( MINIMUM_CLEARANCE ).size(), 2 );
90 BOOST_REQUIRE_EQUAL( document.IndicesFor( HOLE_SIZE ).size(), 1 );
91
92 // Adding and removing a rule sends the document through the saver with every rule unedited
93 DRC_RE_BASE_CONSTRAINT_DATA base( document.NextId(), wxS( "added" ) );
95
96 int added = document.Add( DRC_RE_LOADED_PANEL_ENTRY( MINIMUM_CLEARANCE, data, wxS( "added" ), wxEmptyString,
98 document.Remove( added );
99
100 BOOST_CHECK_EQUAL( document.GetText(), TWO_CLEARANCES_AND_A_HOLE );
101}
102
103
104BOOST_AUTO_TEST_CASE( AddingARuleLeavesTheOthersUntouched )
105{
106 BOARD board;
107 DRC_RULE_DOCUMENT document( &board );
108
109 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
110
111 DRC_RE_BASE_CONSTRAINT_DATA base( document.NextId(), wxS( "added" ) );
113 data->SetConstraintCode( wxS( "clearance" ) );
114 std::dynamic_pointer_cast<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>( data )->SetNumericInputValue( 0.7 );
115
116 document.Add( DRC_RE_LOADED_PANEL_ENTRY( MINIMUM_CLEARANCE, data, wxS( "added" ), wxEmptyString,
118
119 BOOST_CHECK( document.IsModified() );
120 BOOST_CHECK_EQUAL( document.IndicesFor( MINIMUM_CLEARANCE ).size(), 3 );
121
122 wxString text = document.GetText();
123
124 BOOST_CHECK( text.StartsWith( TWO_CLEARANCES_AND_A_HOLE ) );
125 BOOST_CHECK( text.Contains( wxS( "(rule added\n" ) ) );
126 BOOST_CHECK( text.Contains( wxS( "(constraint clearance (min 0.7mm))" ) ) );
127 BOOST_CHECK_EQUAL( countOf( text, wxS( "(rule " ) ), 4 );
128}
129
130
131BOOST_AUTO_TEST_CASE( AMergedRuleIsWrittenBackAsOneRule )
132{
133 BOARD board;
134 DRC_RULE_DOCUMENT document( &board );
135
136 // The two constraints of "merged" are listed on separate pages, so editing one has to write
137 // them back under one rule rather than one rule each
138 document.SetText( wxS( "(version 2)\n"
139 "(rule \"merged\"\n"
140 "\t(constraint clearance (min 0.3mm))\n"
141 "\t(constraint hole_size (min 0.2mm)))\n"
142 "(rule \"other\"\n"
143 "\t(constraint clearance (min 0.5mm)))\n" ) );
144
145 BOOST_REQUIRE_EQUAL( document.IndicesFor( MINIMUM_CLEARANCE ).size(), 2 );
146 BOOST_REQUIRE_EQUAL( document.IndicesFor( HOLE_SIZE ).size(), 1 );
147
148 int holeIndex = document.IndicesFor( HOLE_SIZE ).front();
149 DRC_RE_LOADED_PANEL_ENTRY hole = document.Entries()[holeIndex];
150
151 std::dynamic_pointer_cast<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>( hole.constraintData )
152 ->SetNumericInputValue( 0.25 );
153 document.UpdateEntry( holeIndex, hole );
154
155 wxString text = document.GetText();
156
157 BOOST_CHECK_EQUAL( countOf( text, wxS( "(rule " ) ), 2 );
158 BOOST_CHECK_EQUAL( countOf( text, wxS( "merged" ) ), 1 );
159 BOOST_CHECK_MESSAGE( text.Contains( wxS( "(constraint clearance (min 0.3mm))" ) )
160 && text.Contains( wxS( "(constraint hole_size (min 0.25mm))" ) ),
161 "the merged rule lost a constraint:\n" << text );
162}
163
164
165BOOST_AUTO_TEST_CASE( MoveSwapsWithTheNeighbourOfTheSameConstraint )
166{
167 BOARD board;
168 DRC_RULE_DOCUMENT document( &board );
169
170 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
171
172 std::vector<int> clearances = document.IndicesFor( MINIMUM_CLEARANCE );
173 BOOST_REQUIRE_EQUAL( clearances.size(), 2 );
174 BOOST_CHECK_EQUAL( document.Entries()[clearances[0]].ruleName, wxS( "first" ) );
175
176 BOOST_CHECK( !document.Move( clearances[0], -1, MINIMUM_CLEARANCE ) );
177 BOOST_CHECK( document.Move( clearances[1], -1, MINIMUM_CLEARANCE ) );
178
179 clearances = document.IndicesFor( MINIMUM_CLEARANCE );
180 BOOST_CHECK_EQUAL( document.Entries()[clearances[0]].ruleName, wxS( "second" ) );
181 BOOST_CHECK_EQUAL( document.Entries()[clearances[1]].ruleName, wxS( "first" ) );
182
183 wxString text = document.GetText();
184 BOOST_CHECK( text.find( wxS( "(rule \"second\"" ) ) < text.find( wxS( "(rule \"hole\"" ) ) );
185 BOOST_CHECK( text.find( wxS( "(rule \"hole\"" ) ) < text.find( wxS( "(rule \"first\"" ) ) );
186
187 // The last rule of a constraint has nowhere to go, and moving the other one down puts the
188 // pair back as they started.
189 BOOST_CHECK( !document.Move( clearances[1], 1, MINIMUM_CLEARANCE ) );
190 BOOST_CHECK( document.Move( clearances[0], 1, MINIMUM_CLEARANCE ) );
191
192 clearances = document.IndicesFor( MINIMUM_CLEARANCE );
193 BOOST_CHECK_EQUAL( document.Entries()[clearances[0]].ruleName, wxS( "first" ) );
194 BOOST_CHECK_EQUAL( document.Entries()[clearances[1]].ruleName, wxS( "second" ) );
195
196 text = document.GetText();
197 BOOST_CHECK( text.find( wxS( "(rule \"first\"" ) ) < text.find( wxS( "(rule \"hole\"" ) ) );
198 BOOST_CHECK( text.find( wxS( "(rule \"hole\"" ) ) < text.find( wxS( "(rule \"second\"" ) ) );
199}
200
201
202BOOST_AUTO_TEST_CASE( TextOnlyRuleListsUnderEachConstraintItSets )
203{
204 BOARD board;
205 DRC_RULE_DOCUMENT document( &board );
206
207 DRC_RE_BASE_CONSTRAINT_DATA base( document.NextId(), wxS( "combo" ) );
208 auto custom = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>( base );
209 custom->SetRuleText( wxS( "(constraint clearance (min 0.2mm))\n(constraint hole_size (min 0.3mm))" ) );
210
211 document.Add( DRC_RE_LOADED_PANEL_ENTRY( CUSTOM_RULE, custom, wxS( "combo" ), wxEmptyString, RPT_SEVERITY_UNDEFINED,
212 LSET() ) );
213
214 BOOST_CHECK_EQUAL( document.IndicesFor( MINIMUM_CLEARANCE ).size(), 1 );
215 BOOST_CHECK_EQUAL( document.IndicesFor( HOLE_SIZE ).size(), 1 );
216 BOOST_CHECK_EQUAL( document.IndicesFor( MINIMUM_VIA_DIAMETER ).size(), 0 );
217 BOOST_CHECK_EQUAL( document.IndicesFor( CUSTOM_RULE ).size(), 1 );
218}
219
220
221BOOST_AUTO_TEST_CASE( ImportingFromABoardWithoutARulesFileChangesNothing )
222{
223 BOARD board;
224 DRC_RULE_DOCUMENT document( &board );
225
226 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
227 wxString before = document.GetText();
228
229 wxFileName missing( wxFileName::CreateTempFileName( wxS( "qa_missing" ) ) );
230 wxRemoveFile( missing.GetFullPath() );
231 BOOST_REQUIRE( !missing.FileExists() );
232
233 document.LoadFrom( missing.GetFullPath() );
234
235 BOOST_CHECK_EQUAL( document.IndicesFor( MINIMUM_CLEARANCE ).size(), 2 );
236 BOOST_CHECK_EQUAL( document.GetText(), before );
237}
238
239
240BOOST_AUTO_TEST_CASE( ImportingFromABoardWithRulesReplacesThem )
241{
242 BOARD board;
243 DRC_RULE_DOCUMENT document( &board );
244
245 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
246
247 wxFileName other( wxFileName::CreateTempFileName( wxS( "qa_import_source" ) ) );
248 wxFFile file( other.GetFullPath(), wxS( "wb" ) );
249
250 BOOST_REQUIRE( file.IsOpened() );
251 file.Write( wxS( "(version 2)\n(rule \"imported\"\n\t(constraint clearance (min 0.9mm)))\n" ) );
252 file.Close();
253
254 document.LoadFrom( other.GetFullPath() );
255
256 BOOST_CHECK( document.IsModified() );
257 BOOST_CHECK_EQUAL( document.IndicesFor( MINIMUM_CLEARANCE ).size(), 1 );
258 BOOST_CHECK_EQUAL( document.IndicesFor( HOLE_SIZE ).size(), 0 );
259 BOOST_CHECK( document.GetText().Contains( wxS( "imported" ) ) );
260
261 wxRemoveFile( other.GetFullPath() );
262}
263
264
265BOOST_AUTO_TEST_CASE( TypographicQuotesInAFileOnDiskStillParse )
266{
267 BOARD board;
268 DRC_RULE_DOCUMENT document( &board );
269
270 // A rules file written outside KiCad can carry typographic quotes, which the parser does not
271 // accept, so reading one has to normalise them.
272 const wxString smartQuoted = L"(version 2)\n"
273 L"(rule \u201Coutside\u201D\n"
274 L"\t(constraint clearance (min 0.3mm))\n"
275 L"\t(condition \u201CA.NetClass == \u2018HV\u2019\u201D))\n";
276
277 wxFileName quoted( wxFileName::CreateTempFileName( wxS( "qa_smart_quotes" ) ) );
278 wxFFile file( quoted.GetFullPath(), wxS( "wb" ) );
279
280 BOOST_REQUIRE( file.IsOpened() );
281 file.Write( smartQuoted, wxConvUTF8 );
282 file.Close();
283
284 document.LoadFrom( quoted.GetFullPath() );
285
286 BOOST_CHECK( !document.HasUnparsedText() );
287 BOOST_CHECK_EQUAL( document.IndicesFor( MINIMUM_CLEARANCE ).size(), 1 );
288 BOOST_CHECK( document.GetText().Contains( wxS( "\"outside\"" ) ) );
289
290 wxRemoveFile( quoted.GetFullPath() );
291}
292
293
294BOOST_AUTO_TEST_CASE( ARangeWithOneEndIsNotGivenAnInventedBound )
295{
296 // An absent bound reads back as the largest int, so a rule that names one end of a range
297 // cannot go to a panel that writes both ends. Each of these reaches a different panel.
298 const std::vector<wxString> openEnded = {
299 wxS( "(constraint length (min 10mm))" ),
300 wxS( "(constraint length (min 10mm))\n\t(constraint skew (max 1mm))" ),
301 wxS( "(constraint skew (min 1mm))" ),
302 wxS( "(constraint diff_pair_uncoupled (min 1mm))" ),
303 };
304
305 for( const wxString& clauses : openEnded )
306 {
307 BOARD board;
308 DRC_RULE_DOCUMENT document( &board );
309
310 document.SetText( wxS( "(version 2)\n(rule \"open_ended\"\n\t" ) + clauses + wxS( ")\n" ) );
311
312 BOOST_REQUIRE_MESSAGE( !document.Entries().empty(), "nothing loaded for " << clauses );
313
314 DRC_RE_LOADED_PANEL_ENTRY edited = document.Entries()[0];
315 edited.ruleName = wxS( "renamed" );
316 document.UpdateEntry( 0, edited );
317
318 wxString text = document.GetText();
319
320 BOOST_CHECK_MESSAGE( !text.Contains( wxS( "2147" ) ),
321 "an absent bound was written back as the largest int, from " << clauses << ":\n"
322 << text );
323 BOOST_CHECK_MESSAGE( text.Contains( clauses ), "the clauses were rewritten, from " << clauses << ":\n"
324 << text );
325 }
326}
327
328
329BOOST_AUTO_TEST_CASE( ARangeWithBothEndsStillReachesItsPanel )
330{
331 BOARD board;
332 DRC_RULE_DOCUMENT document( &board );
333
334 document.SetText( wxS( "(version 2)\n"
335 "(rule \"both_ends\"\n"
336 "\t(constraint length (min 10mm) (max 20mm)))\n"
337 "(rule \"uncoupled\"\n"
338 "\t(constraint diff_pair_uncoupled (max 5mm)))\n"
339 "(rule \"skew\"\n"
340 "\t(constraint skew (max 1mm)))\n" ) );
341
342 BOOST_CHECK_EQUAL( document.IndicesFor( ABSOLUTE_LENGTH ).size(), 1 );
343 BOOST_CHECK_EQUAL( document.IndicesFor( ROUTING_DIFF_PAIR ).size(), 1 );
345 BOOST_CHECK_EQUAL( document.IndicesFor( CUSTOM_RULE ).size(), 0 );
346}
347
348
349BOOST_AUTO_TEST_CASE( AnEditedTextRuleKeepsItsIndent )
350{
351 BOARD board;
352 DRC_RULE_DOCUMENT document( &board );
353
354 document.SetText( wxS( "(version 2)\n"
355 "(rule \"min_only\"\n"
356 "\t(condition \"A.NetClass == 'Default'\")\n"
357 "\t(constraint track_width (min 0.2mm)))\n" ) );
358
359 BOOST_REQUIRE_EQUAL( document.IndicesFor( CUSTOM_RULE ).size(), 1 );
360
361 int index = document.IndicesFor( CUSTOM_RULE ).front();
362 DRC_RE_LOADED_PANEL_ENTRY edited = document.Entries()[index];
363
364 edited.ruleName = wxS( "renamed" );
365 document.UpdateEntry( index, edited );
366
367 wxString text = document.GetText();
368
369 BOOST_CHECK_MESSAGE( text.Contains( wxS( "(rule \"renamed\"\n"
370 "\t(condition \"A.NetClass == 'Default'\")\n"
371 "\t(constraint track_width (min 0.2mm)))" ) ),
372 "the body lost its indent:\n"
373 << text );
374}
375
376
377BOOST_AUTO_TEST_CASE( AWidthOrGapMissingABoundIsKeptAsWritten )
378{
379 // The width and gap panels write min, opt and max around one optimum, so a rule naming fewer
380 // would come back with every wider track or gap an error
381 const std::vector<wxString> partial = {
382 wxS( "(constraint track_width (min 0.2mm))" ),
383 wxS( "(constraint track_width (min 0.2mm) (max 0.4mm))" ),
384 wxS( "(constraint diff_pair_gap (min 0.1mm))" ),
385 };
386
387 for( const wxString& clauses : partial )
388 {
389 BOARD board;
390 DRC_RULE_DOCUMENT document( &board );
391
392 document.SetText( wxS( "(version 2)\n(rule \"partial\"\n\t" ) + clauses + wxS( ")\n" ) );
393
394 BOOST_REQUIRE_MESSAGE( !document.Entries().empty(), "nothing loaded for " << clauses );
395
396 DRC_RE_LOADED_PANEL_ENTRY edited = document.Entries()[0];
397 edited.ruleName = wxS( "renamed" );
398 document.UpdateEntry( 0, edited );
399
400 wxString text = document.GetText();
401
402 BOOST_CHECK_MESSAGE( text.Contains( clauses ), "the clauses were rewritten, from " << clauses << ":\n"
403 << text );
404 }
405}
406
407
408BOOST_AUTO_TEST_CASE( ASplitRuleKeepsThePredicateItsPartsShare )
409{
410 // One part of each takes the predicate out of the condition all of the parts are read with
411 const std::vector<std::pair<wxString, wxString>> rules = {
412 { wxS( "(constraint via_diameter (min 0.2mm))\n\t(constraint hole_size (min 0.1mm))" ),
413 wxS( "A.Via_Type == 'Micro' && A.NetClass == 'HS'" ) },
414 { wxS( "(constraint disallow via)" ), wxS( "A.Pad_Type == 'SMD' && A.NetClass == 'HS'" ) },
415 };
416
417 for( const auto& [clauses, condition] : rules )
418 {
419 for( size_t i = 0;; ++i )
420 {
421 BOARD board;
422 DRC_RULE_DOCUMENT document( &board );
423
424 document.SetText( wxS( "(version 2)\n(rule \"split\"\n\t" ) + clauses
425 + wxS( "\n\t(constraint clearance (min 0.15mm))\n\t(condition \"" ) + condition
426 + wxS( "\"))\n" ) );
427
428 if( i >= document.Entries().size() )
429 break;
430
431 // As the rule page applies an edit, with the condition its form holds
432 DRC_RE_LOADED_PANEL_ENTRY edited = document.Entries()[i];
433 edited.ruleName = wxS( "renamed" );
434 edited.condition = edited.constraintData->GetRuleCondition();
435 document.UpdateEntry( (int) i, edited );
436
437 wxString text = document.GetText();
438
439 BOOST_CHECK_MESSAGE( text.Contains( wxS( "(condition \"" ) + condition + wxS( "\")" ) ),
440 "editing part " << i << " changed the condition:\n"
441 << text );
442 }
443 }
444}
445
446
447BOOST_AUTO_TEST_CASE( AConditionThePanelCannotWriteBackIsKeptAsWritten )
448{
449 // A panel puts its predicate back in front of the rest, so only that form survives an edit
450 const std::vector<wxString> conditions = {
451 wxS( "A.Via_Type != 'Micro'" ),
452 wxS( "A.Via_Type == 'Micro' || A.Via_Type == 'Blind'" ),
453 wxS( "A.NetClass == 'HS' && A.Via_Type == 'Micro'" ),
454 wxS( "A.NetName == 'Micro'" ),
455 };
456
457 for( const wxString& condition : conditions )
458 {
459 BOARD board;
460 DRC_RULE_DOCUMENT document( &board );
461
462 document.SetText( wxS( "(version 2)\n(rule \"vias\"\n"
463 "\t(constraint via_diameter (min 0.2mm))\n"
464 "\t(constraint hole_size (min 0.1mm))\n"
465 "\t(condition \"" )
466 + condition + wxS( "\"))\n" ) );
467
468 BOOST_REQUIRE_EQUAL( document.Entries().size(), 1 );
469
470 DRC_RE_LOADED_PANEL_ENTRY edited = document.Entries()[0];
471 edited.ruleName = wxS( "renamed" );
472 edited.condition = edited.constraintData->GetRuleCondition();
473 document.UpdateEntry( 0, edited );
474
475 wxString text = document.GetText();
476
477 BOOST_CHECK_MESSAGE( text.Contains( wxS( "(condition \"" ) + condition + wxS( "\")" ) ),
478 "the condition was rewritten, from " << condition << ":\n"
479 << text );
480 }
481}
482
483
484BOOST_AUTO_TEST_CASE( AViaTypeChosenForASplitRuleNarrowsOnlyTheViaPart )
485{
486 BOARD board;
487 DRC_RULE_DOCUMENT document( &board );
488
489 document.SetText( ONE_RULE_THREE_CONSTRAINTS );
490
491 BOOST_REQUIRE_EQUAL( document.IndicesFor( VIA_STYLE ).size(), 1 );
492
493 int index = document.IndicesFor( VIA_STYLE ).front();
494 DRC_RE_LOADED_PANEL_ENTRY edited = document.Entries()[index];
495
496 std::static_pointer_cast<DRC_RE_VIA_STYLE_CONSTRAINT_DATA>( edited.constraintData )
497 ->SetViaType( VIA_STYLE_TYPE::MICRO );
498 edited.condition = edited.constraintData->GetRuleCondition();
499 document.UpdateEntry( index, edited );
500
501 wxString text = document.GetText();
502
503 BOOST_CHECK_MESSAGE(
504 text.Contains( wxS( "(condition \"A.Via_Type == 'Micro' && A.intersectsArea('underFPGA')\")" ) ),
505 "the via type was dropped:\n"
506 << text );
507
508 // The clearance part still applies to every item in the area
509 BOOST_CHECK_MESSAGE( text.Contains( wxS( "(condition \"A.intersectsArea('underFPGA')\")" ) ),
510 "the via type narrowed the other parts:\n"
511 << text );
512}
513
514
515BOOST_AUTO_TEST_CASE( RenamingOneConstraintRenamesTheWholeRule )
516{
517 BOARD board;
518 DRC_RULE_DOCUMENT document( &board );
519
520 document.SetText( ONE_RULE_THREE_CONSTRAINTS );
521
522 int clearance = document.IndicesFor( MINIMUM_CLEARANCE ).front();
523
524 BOOST_REQUIRE_EQUAL( document.IndicesFor( VIA_STYLE ).size(), 1 );
525
526 DRC_RE_LOADED_PANEL_ENTRY edited = document.Entries()[clearance];
527 edited.ruleName = wxS( "renamed" );
528 document.UpdateEntry( clearance, edited );
529
530 wxString text = document.GetText();
531
532 BOOST_CHECK_MESSAGE( countOf( text, wxS( "(rule " ) ) == 1, "the rule was split in two:\n" << text );
533 BOOST_CHECK_MESSAGE( text.Contains( wxS( "(rule renamed" ) ), "the new name was dropped:\n" << text );
534 BOOST_CHECK_MESSAGE( !text.Contains( wxS( "under_fpga\"\n" ) ), "the old name survived:\n" << text );
535 BOOST_CHECK_EQUAL( countOf( text, wxS( "(constraint " ) ), 3 );
536
537 BOOST_CHECK_EQUAL( document.Entries()[document.IndicesFor( VIA_STYLE ).front()].ruleName, wxS( "renamed" ) );
538}
539
540
541BOOST_AUTO_TEST_CASE( RulesAddedSeparatelyStayApartWhenTheyShareAName )
542{
543 BOARD board;
544 DRC_RULE_DOCUMENT document( &board );
545
547 {
548 DRC_RE_BASE_CONSTRAINT_DATA base( document.NextId(), wxS( "same name" ) );
549 auto data = DRC_RULE_EDITOR_UTILS::CreateConstraintData( type, base );
550 data->SetConstraintCode( DRC_RULE_EDITOR_UTILS::ConstraintToKicadDrc( type ) );
551 std::dynamic_pointer_cast<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>( data )->SetNumericInputValue( 0.3 );
552
553 document.Add( DRC_RE_LOADED_PANEL_ENTRY( type, data, wxS( "same name" ), wxEmptyString, RPT_SEVERITY_UNDEFINED,
554 LSET() ) );
555 }
556
557 wxString text = document.GetText();
558
559 BOOST_CHECK_MESSAGE( countOf( text, wxS( "(rule " ) ) == 2, "two unrelated rules were merged:\n" << text );
560}
561
562
563BOOST_AUTO_TEST_CASE( TextRuleMovesAmongTheRulesOfThePageItIsShownOn )
564{
565 BOARD board;
566 DRC_RULE_DOCUMENT document( &board );
567
568 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
569
570 DRC_RE_BASE_CONSTRAINT_DATA base( document.NextId(), wxS( "combo" ) );
571 auto custom = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>( base );
572 custom->SetRuleText( wxS( "(constraint clearance (min 0.2mm))\n(constraint hole_size (min 0.3mm))" ) );
573
574 int combo = document.Add( DRC_RE_LOADED_PANEL_ENTRY( CUSTOM_RULE, custom, wxS( "combo" ), wxEmptyString,
576
577 std::vector<int> clearances = document.IndicesFor( MINIMUM_CLEARANCE );
578 BOOST_REQUIRE_EQUAL( clearances.size(), 3 );
579 BOOST_CHECK_EQUAL( clearances[2], combo );
580
581 BOOST_CHECK( document.Move( combo, -1, MINIMUM_CLEARANCE ) );
582
583 clearances = document.IndicesFor( MINIMUM_CLEARANCE );
584 BOOST_CHECK_EQUAL( document.Entries()[clearances[1]].ruleName, wxS( "combo" ) );
585 BOOST_CHECK_EQUAL( document.Entries()[clearances[2]].ruleName, wxS( "second" ) );
586}
587
588
589BOOST_AUTO_TEST_CASE( ATextRuleIsListedOnceWhenTwoSectionsShareAConstraintCode )
590{
591 BOARD board;
592 DRC_RULE_DOCUMENT document( &board );
593
594 // The optimum sits outside the window, so no graphical panel can hold this rule.
595 document.SetText( wxS( "(version 2)\n"
596 "(rule \"byte2\"\n"
597 "\t(constraint length (min 39mm) (max 40mm) (opt 40.5mm))\n"
598 "\t(condition \"A.NetClass == 'DDR4'\"))\n" ) );
599
600 BOOST_REQUIRE_EQUAL( document.IndicesFor( CUSTOM_RULE ).size(), 1 );
601
602 BOOST_CHECK_MESSAGE( document.IndicesFor( ABSOLUTE_LENGTH ).size() == 1,
603 "a plain length rule belongs under absolute length" );
604 BOOST_CHECK_MESSAGE( document.IndicesFor( MATCHED_LENGTH_DIFF_PAIR ).empty(),
605 "the same rule was also listed under matched length on the same page" );
606}
607
608
609BOOST_AUTO_TEST_CASE( TextThatDoesNotParseIsKeptAsWritten )
610{
611 BOARD board;
612 DRC_RULE_DOCUMENT document( &board );
613
614 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
615
616 wxString broken = wxS( "(version 2)\n(rule \"unfinished\"\n\t(constraint clearance (min 0.3mm)" );
617 document.SetText( broken );
618
619 BOOST_CHECK( document.IsModified() );
620 BOOST_CHECK( document.HasUnparsedText() );
621 BOOST_CHECK_EQUAL( document.GetText(), broken );
622
623 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
624
625 BOOST_CHECK( !document.HasUnparsedText() );
626 BOOST_CHECK_EQUAL( document.IndicesFor( MINIMUM_CLEARANCE ).size(), 2 );
627 BOOST_CHECK( document.GetText().Contains( wxS( "(rule \"hole\"" ) ) );
628}
629
630
631BOOST_AUTO_TEST_CASE( ConstraintCodeMustEndAtADelimiter )
632{
633 wxString text = wxS( "(constraint clearance_under_fpga (min 1mm))\n(constraint clearance (min 2mm))" );
634
636 text.find( wxS( "(constraint clearance (" ) ) );
638 DRC_RULE_DOCUMENT::FindConstraintClause( wxS( "(constraint hole_size (min 1mm))" ), wxS( "hole" ) ),
639 wxString::npos );
640}
641
642
643BOOST_AUTO_TEST_CASE( DuplicateFollowsItsOriginalAsAnIndependentRule )
644{
645 BOARD board;
646 DRC_RULE_DOCUMENT document( &board );
647
648 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
649
650 std::vector<int> clearances = document.IndicesFor( MINIMUM_CLEARANCE );
651 BOOST_REQUIRE_EQUAL( clearances.size(), 2 );
652 BOOST_REQUIRE_EQUAL( document.Entries()[clearances[0]].ruleName, wxS( "first" ) );
653
654 int copy = document.Duplicate( clearances[0] );
655
656 BOOST_REQUIRE_EQUAL( copy, clearances[0] + 1 );
657 BOOST_CHECK( document.IsModified() );
658
659 clearances = document.IndicesFor( MINIMUM_CLEARANCE );
660 BOOST_REQUIRE_EQUAL( clearances.size(), 3 );
661 BOOST_CHECK_EQUAL( document.Entries()[clearances[0]].ruleName, wxS( "first" ) );
662 BOOST_CHECK_EQUAL( document.Entries()[clearances[1]].ruleName, wxS( "first 1" ) );
663
664 BOOST_CHECK_MESSAGE( document.Entries()[clearances[0]].constraintData
665 != document.Entries()[clearances[1]].constraintData,
666 "the copy shares its data with the original" );
667
668 auto original = std::dynamic_pointer_cast<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>(
669 document.Entries()[clearances[0]].constraintData );
670 auto duplicated = std::dynamic_pointer_cast<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>(
671 document.Entries()[clearances[1]].constraintData );
672
673 BOOST_REQUIRE( original );
674 BOOST_REQUIRE( duplicated );
675 BOOST_CHECK_CLOSE( duplicated->GetNumericInputValue(), original->GetNumericInputValue(), 0.0001 );
676 BOOST_CHECK_EQUAL( duplicated->GetRuleCondition(), original->GetRuleCondition() );
677
678 wxString text = document.GetText();
679
680 BOOST_CHECK_MESSAGE( countOf( text, wxS( "(rule " ) ) == 4, "the copy was not written once:\n" << text );
681 // A comment inside the rule travels with the copy. One outside the rules belongs to the
682 // file and does not.
683 BOOST_CHECK_MESSAGE( countOf( text, wxS( "# keep this comment" ) ) == 2,
684 "the copy lost the comment written inside the rule:\n"
685 << text );
686 BOOST_CHECK_MESSAGE( countOf( text, wxS( "# header comment" ) ) == 1,
687 "the file header was emitted more than once:\n"
688 << text );
689
690 DRC_RULE_DOCUMENT reread( &board );
691 reread.SetText( text );
692
693 BOOST_CHECK_MESSAGE( reread.IndicesFor( MINIMUM_CLEARANCE ).size() == 3, "the saved copy does not read back:\n"
694 << text );
695}
696
697
698BOOST_AUTO_TEST_CASE( DuplicatingACopyCountsOnFromTheOriginalName )
699{
700 BOARD board;
701 DRC_RULE_DOCUMENT document( &board );
702
703 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
704
705 int first = document.Duplicate( document.IndicesFor( MINIMUM_CLEARANCE ).front() );
706 BOOST_REQUIRE_GE( first, 0 );
707 BOOST_CHECK_EQUAL( document.Entries()[first].ruleName, wxS( "first 1" ) );
708
709 int second = document.Duplicate( first );
710 BOOST_REQUIRE_GE( second, 0 );
711 BOOST_CHECK_EQUAL( document.Entries()[second].ruleName, wxS( "first 2" ) );
712}
713
714
715BOOST_AUTO_TEST_CASE( RuleNamesAreUniqueWithinAConstraint )
716{
717 BOARD board;
718 DRC_RULE_DOCUMENT document( &board );
719
720 document.SetText( TWO_CLEARANCES_AND_A_HOLE );
721
722 std::vector<int> clearances = document.IndicesFor( MINIMUM_CLEARANCE );
723 int hole = document.IndicesFor( HOLE_SIZE ).front();
724
725 BOOST_CHECK( !document.IsRuleNameUnique( wxS( "second" ), MINIMUM_CLEARANCE, clearances[0] ) );
726 BOOST_CHECK( document.IsRuleNameUnique( wxS( "second" ), MINIMUM_CLEARANCE, clearances[1] ) );
727 BOOST_CHECK( document.IsRuleNameUnique( wxS( "second" ), HOLE_SIZE, hole ) );
728
729 BOOST_CHECK( !document.IsRuleNameUnique( wxS( "second" ), MINIMUM_CLEARANCE, -1 ) );
730 BOOST_CHECK( document.IsRuleNameUnique( wxS( "second" ), HOLE_SIZE, -1 ) );
731 BOOST_CHECK( document.IsRuleNameUnique( wxS( "third" ), MINIMUM_CLEARANCE, -1 ) );
732
733 BOOST_CHECK_EQUAL( document.UniqueRuleName( wxS( "Minimum clearance" ) ), wxS( "Minimum clearance 1" ) );
734 BOOST_CHECK_EQUAL( document.UniqueRuleName( wxS( "first" ) ), wxS( "first 1" ) );
735}
736
737
738BOOST_AUTO_TEST_CASE( SplitRuleIsListedOnceOnItsSiblingsPage )
739{
740 BOARD board;
741 DRC_RULE_DOCUMENT document( &board );
742
743 // The zone connection has no graphical panel, so the whole rule is kept as text
744 document.SetText( wxS( "(version 2)\n"
745 "(rule \"split\"\n"
746 "\t(constraint clearance (min 0.2mm))\n"
747 "\t(constraint zone_connection solid))\n" ) );
748
749 BOOST_REQUIRE_EQUAL( document.Entries().size(), 1 );
750
751 std::vector<int> clearances = document.IndicesFor( MINIMUM_CLEARANCE );
752
753 BOOST_REQUIRE_EQUAL( clearances.size(), 1 );
754 BOOST_CHECK_EQUAL( document.Entries()[clearances[0]].panelType, CUSTOM_RULE );
755}
756
757
758BOOST_AUTO_TEST_CASE( EveryBoardConstraintWithRulesResolvesToAPage )
759{
760 // These are the names PANEL_SETUP_CONSTRAINTS keys its fields by.
761 const std::vector<wxString> governed = { wxS( "min_clearance" ), wxS( "min_connection" ),
762 wxS( "min_track_width" ), wxS( "min_via_annular_width" ),
763 wxS( "min_via_diameter" ), wxS( "min_through_hole_diameter" ),
764 wxS( "min_hole_to_hole" ), wxS( "min_hole_clearance" ),
765 wxS( "min_silk_clearance" ), wxS( "min_text_height" ),
766 wxS( "min_silk_line_width" ), wxS( "min_copper_edge_clearance" ) };
767
768 DRC_PANEL_MATCHER matcher;
769
770 for( const wxString& setting : governed )
771 {
773
774 BOOST_CHECK_MESSAGE( PANEL_SETUP_CONSTRAINT_RULES::RulesCanOverride( setting, &constraint ),
775 "no rule constraint governs " << setting.ToStdString() );
776 BOOST_CHECK_MESSAGE( !PANEL_SETUP_CONSTRAINT_RULES::PageTitleFor( matcher.GetHomePanel( constraint ) ).IsEmpty(),
777 "no page title for " << setting.ToStdString() );
778 }
779
780 // Max error is a geometry tolerance. The groove width decides which board edge gaps a
781 // creepage path has to follow, so neither is a value a rule can override.
782 BOOST_CHECK( !PANEL_SETUP_CONSTRAINT_RULES::RulesCanOverride( wxS( "max_error" ), nullptr ) );
783 BOOST_CHECK( !PANEL_SETUP_CONSTRAINT_RULES::RulesCanOverride( wxS( "min_groove_width" ), nullptr ) );
784}
785
786
787BOOST_AUTO_TEST_CASE( EveryBoardDefaultIsWrittenByOnePageOnly )
788{
789 const std::vector<std::pair<wxString, CONSTRAINT_DEFAULT_FIELD>> edited =
791
792 // Two pages writing one value would make the result depend on which was left last
793 for( size_t i = 0; i < edited.size(); ++i )
794 {
795 for( size_t j = i + 1; j < edited.size(); ++j )
796 {
797 BOOST_CHECK_MESSAGE( edited[i].second.settingName != edited[j].second.settingName,
798 "two rule pages edit " << edited[i].second.settingName.ToStdString() );
799 BOOST_CHECK_MESSAGE( edited[i].second.member != edited[j].second.member,
800 "two rule pages write the member behind "
801 << edited[i].second.settingName.ToStdString() );
802 }
803 }
804
805 // A value a rule page edits names that page, so the page it left can link to it
806 for( const auto& [group, field] : edited )
807 {
808 BOOST_CHECK_MESSAGE( !PANEL_SETUP_CONSTRAINT_RULES::PageEditingDefault( field.settingName ).IsEmpty(),
809 "no page edits " << field.settingName.ToStdString() );
810 }
811}
812
813
814BOOST_AUTO_TEST_CASE( EveryConstraintWithARulePageHasItsDefaultThere )
815{
816 // Each of these is a whole board default for the constraint its rule page is about, even
817 // where the page it has always been set on holds it in a control with something else
818 for( const wxString& setting :
819 { wxS( "solder_mask_expansion" ), wxS( "solder_mask_min_width" ), wxS( "solder_paste_margin" ) } )
820 {
821 BOOST_CHECK_MESSAGE( !PANEL_SETUP_CONSTRAINT_RULES::PageEditingDefault( setting ).IsEmpty(),
822 "no rule page holds " << setting.ToStdString() );
823 }
824
825 // Max error is a geometry tolerance and the groove width feeds the creepage search, so
826 // neither is a constraint a rule page could hold
827 BOOST_CHECK( PANEL_SETUP_CONSTRAINT_RULES::PageEditingDefault( wxS( "max_error" ) ).IsEmpty() );
828 BOOST_CHECK( PANEL_SETUP_CONSTRAINT_RULES::PageEditingDefault( wxS( "min_groove_width" ) ).IsEmpty() );
829}
830
831
832BOOST_AUTO_TEST_CASE( EachDefaultTravelsWithItsImportGroup )
833{
834 using BDS = BOARD_DESIGN_SETTINGS;
835 using COPY = void ( * )( const BDS&, BDS& );
836
837 // Board Setup imports a group and commits its staged values with the same copy, so a value
838 // the copy misses is neither imported nor saved
839 const std::map<wxString, COPY> copies = { { wxS( "Constraints" ), &PANEL_SETUP_CONSTRAINTS::CopySettings },
840 { wxS( "Solder Mask/Paste" ),
842
843 auto travels = []( COPY aCopy, int BDS::* aMember )
844 {
845 BOARD from;
846 BOARD to;
847
848 from.GetDesignSettings().*aMember = to.GetDesignSettings().*aMember + 1234;
849 aCopy( from.GetDesignSettings(), to.GetDesignSettings() );
850
851 return to.GetDesignSettings().*aMember == from.GetDesignSettings().*aMember;
852 };
853
854 for( const auto& [group, field] : PANEL_SETUP_CONSTRAINT_RULES::EditedDefaults() )
855 {
856 auto it = copies.find( group );
857
858 BOOST_REQUIRE_MESSAGE( it != copies.end(), "no import carries group " << group.ToStdString() );
859 BOOST_CHECK_MESSAGE( travels( it->second, field.member ),
860 field.settingName.ToStdString() << " does not travel with " << group.ToStdString() );
861 }
862
863 // No rule page holds these two, so only the Constraints page carries them
864 BOOST_CHECK( travels( &PANEL_SETUP_CONSTRAINTS::CopySettings, &BDS::m_MaxError ) );
865 BOOST_CHECK( travels( &PANEL_SETUP_CONSTRAINTS::CopySettings, &BDS::m_MinGrooveWidth ) );
866}
867
868
869BOOST_AUTO_TEST_CASE( AConditionedDefaultSitsWithTheConstraintItNarrows )
870{
871 // A microvia carries its own minimum for two constraints that are not microvia specific, so
872 // those two values belong on the page holding the rules for the constraint they narrow
874 PANEL_SETUP_CONSTRAINT_RULES::PageEditingDefault( wxS( "min_via_diameter" ) ) );
876 PANEL_SETUP_CONSTRAINT_RULES::PageEditingDefault( wxS( "min_through_hole_diameter" ) ) );
877
878 BOOST_CHECK( !PANEL_SETUP_CONSTRAINT_RULES::PageEditingDefault( wxS( "min_microvia_diameter" ) ).IsEmpty() );
879 BOOST_CHECK( !PANEL_SETUP_CONSTRAINT_RULES::PageEditingDefault( wxS( "min_resolved_spokes" ) ).IsEmpty() );
880}
881
882
int index
Container for design settings for a BOARD object.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
BOARD_DESIGN_SETTINGS & GetDesignSettings()
Definition board.cpp:1318
Maps DRC rule constraints to appropriate editor panels.
DRC_RULE_EDITOR_CONSTRAINT_NAME GetHomePanel(DRC_CONSTRAINT_T aConstraintType)
The panel whose page lists a text rule that sets this constraint, or CUSTOM_RULE when no panel edits ...
The custom rules of one board, held in memory while Board Setup is open.
bool HasUnparsedText() const
True while the Custom Rules text could not be parsed.
void SetText(const wxString &aText)
Take the text of the Custom Rules page.
static size_t FindConstraintClause(const wxString &aText, const wxString &aCode)
Position of the constraint clause for aCode in rule text, or npos.
void UpdateEntry(int aIndex, const DRC_RE_LOADED_PANEL_ENTRY &aEdited)
Write an edited rule back.
std::vector< DRC_RE_LOADED_PANEL_ENTRY > & Entries()
std::vector< int > IndicesFor(DRC_RULE_EDITOR_CONSTRAINT_NAME aType) const
wxString UniqueRuleName(const wxString &aBase) const
void Remove(int aIndex)
int Add(const DRC_RE_LOADED_PANEL_ENTRY &aEntry)
void LoadFrom(const wxString &aPath)
Replace the rules with those of another board's file, as an import.
bool IsRuleNameUnique(const wxString &aName, DRC_RULE_EDITOR_CONSTRAINT_NAME aType, int aExceptIndex) const
wxString GetText() const
The rules as text, for the Custom Rules page.
int Duplicate(int aIndex)
bool Move(int aIndex, int aDelta, DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
Move a rule one step among the rules listed for aType.
static wxString ConstraintToKicadDrc(DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
Convert a constraint type into the keyword used in a .kicad_drc file.
static std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > CreateConstraintData(DRC_RULE_EDITOR_CONSTRAINT_NAME aType, const DRC_RE_BASE_CONSTRAINT_DATA &aBase)
Create the data object that holds a rule of the given constraint type, taking the identity fields fro...
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static void CopySettings(const BOARD_DESIGN_SETTINGS &aFrom, BOARD_DESIGN_SETTINGS &aTo)
Copies every value this page edits, which is what the Constraints import group carries.
static wxString PageEditingDefault(const wxString &aSetting)
The title of the page that edits a board default, or empty where no rule page edits one.
static bool RulesCanOverride(const wxString &aSetting, DRC_CONSTRAINT_T *aConstraint)
Whether rules can set a value other than this board default, and the DRC constraint that does it.
static wxString PageTitleFor(DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
The title of the page that edits rules of this type, or empty when no page does.
static std::vector< std::pair< wxString, CONSTRAINT_DEFAULT_FIELD > > EditedDefaults()
Every board default a rule page edits, each with the group it is imported with.
static void CopySettings(const BOARD_DESIGN_SETTINGS &aFrom, BOARD_DESIGN_SETTINGS &aTo)
Copies every value this page edits, which is what the Solder Mask/Paste import group carries.
@ NULL_CONSTRAINT
DRC_RULE_EDITOR_CONSTRAINT_NAME
@ ROUTING_DIFF_PAIR
@ ABSOLUTE_LENGTH
@ MINIMUM_CLEARANCE
@ MATCHED_LENGTH_DIFF_PAIR
@ MINIMUM_VIA_DIAMETER
@ RPT_SEVERITY_UNDEFINED
Represents a rule loaded from a .kicad_dru file and mapped to a panel.
wxString ruleName
wxString condition
std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > constraintData
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_CASE(UneditedRulesRoundTripVerbatim)
BOOST_AUTO_TEST_SUITE_END()
int clearance