KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_variant.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
25
27#include <boost/test/unit_test.hpp>
28
29#include <board.h>
30#include <board_commit.h>
31#include <core/mirror.h>
32#include <footprint.h>
33#include <lib_id.h>
41#include <richio.h>
43#include <template_fieldnames.h>
44#include <tool/tool_manager.h>
45
46
47BOOST_AUTO_TEST_SUITE( Variant )
48
49
50
53BOOST_AUTO_TEST_CASE( FootprintVariantBasics )
54{
55 FOOTPRINT_VARIANT variant( "TestVariant" );
56
57 // Check default values
58 BOOST_CHECK_EQUAL( variant.GetName(), "TestVariant" );
59 BOOST_CHECK( !variant.GetDNP() );
60 BOOST_CHECK( !variant.GetExcludedFromBOM() );
61 BOOST_CHECK( !variant.GetExcludedFromSim() );
63 BOOST_CHECK( variant.GetFields().empty() );
64
65 // Test setting values
66 variant.SetDNP( true );
67 variant.SetExcludedFromBOM( true );
68 variant.SetExcludedFromSim( true );
69 variant.SetExcludedFromPosFiles( true );
70 variant.SetFieldValue( "Value", "100R" );
71
72 BOOST_CHECK( variant.GetDNP() );
76 BOOST_CHECK( variant.HasFieldValue( "Value" ) );
77 BOOST_CHECK_EQUAL( variant.GetFieldValue( "Value" ), "100R" );
78}
79
80
84BOOST_AUTO_TEST_CASE( BoardVariantRegistry )
85{
86 BOARD board;
87
88 // Initially no variants
89 BOOST_CHECK( board.GetVariantNames().empty() );
90 BOOST_CHECK( board.GetCurrentVariant().IsEmpty() );
91
92 // Add a variant
93 board.AddVariant( "Production" );
94 BOOST_CHECK( board.HasVariant( "Production" ) );
95 BOOST_CHECK_EQUAL( board.GetVariantNames().size(), 1 );
96
97 // Add another variant
98 board.AddVariant( "Debug" );
99 BOOST_CHECK( board.HasVariant( "Debug" ) );
100 BOOST_CHECK_EQUAL( board.GetVariantNames().size(), 2 );
101
102 // Test case insensitivity
103 BOOST_CHECK( board.HasVariant( "production" ) );
104 BOOST_CHECK( board.HasVariant( "PRODUCTION" ) );
105 BOOST_CHECK( board.HasVariant( "PrOdUcTiOn" ) );
106
107 // Set current variant
108 board.SetCurrentVariant( "Production" );
109 BOOST_CHECK_EQUAL( board.GetCurrentVariant(), "Production" );
110
111 // Set variant description
112 board.SetVariantDescription( "Production", "Standard production build" );
113 BOOST_CHECK_EQUAL( board.GetVariantDescription( "Production" ), "Standard production build" );
114
115 // Delete a variant
116 board.DeleteVariant( "Debug" );
117 BOOST_CHECK( !board.HasVariant( "Debug" ) );
118 BOOST_CHECK_EQUAL( board.GetVariantNames().size(), 1 );
119}
120
121
125BOOST_AUTO_TEST_CASE( FootprintDNPForVariant )
126{
127 BOARD board;
128 FOOTPRINT fp( &board );
129
130 // Add variants to board
131 board.AddVariant( "Production" );
132 board.AddVariant( "Debug" );
133
134 // Set base DNP to false
135 fp.SetAttributes( 0 );
136 BOOST_CHECK( !fp.GetDNPForVariant( wxEmptyString ) );
137 BOOST_CHECK( !fp.GetDNPForVariant( "Production" ) );
138
139 // Set base DNP to true
140 fp.SetAttributes( FP_DNP );
141 BOOST_CHECK( fp.GetDNPForVariant( wxEmptyString ) );
142 BOOST_CHECK( fp.GetDNPForVariant( "Production" ) );
143
144 // Now set Production variant to override DNP
145 FOOTPRINT_VARIANT prodVariant( "Production" );
146 prodVariant.SetDNP( false );
147 fp.SetVariant( prodVariant );
148
149 // Base is still DNP, but Production overrides to not DNP
150 BOOST_CHECK( fp.GetDNPForVariant( wxEmptyString ) );
151 BOOST_CHECK( !fp.GetDNPForVariant( "Production" ) );
152 BOOST_CHECK( !fp.GetDNPForVariant( "production" ) ); // Case insensitive
153 BOOST_CHECK( fp.GetDNPForVariant( "Debug" ) ); // No override, uses base
154
155 // Set Debug variant to also override
156 FOOTPRINT_VARIANT debugVariant( "Debug" );
157 debugVariant.SetDNP( true );
158 fp.SetVariant( debugVariant );
159
160 BOOST_CHECK( fp.GetDNPForVariant( "Debug" ) );
161}
162
163
167BOOST_AUTO_TEST_CASE( FootprintVariantTextVarResolution )
168{
169 BOARD board;
170 FOOTPRINT fp( &board );
171
172 board.AddVariant( "Production" );
173 board.SetCurrentVariant( "Production" );
174
175 fp.SetAttributes( 0 );
176
177 FOOTPRINT_VARIANT prodVariant( "Production" );
178 prodVariant.SetDNP( true );
179 prodVariant.SetExcludedFromSim( true );
180 fp.SetVariant( prodVariant );
181
182 wxString token = wxS( "DNP" );
183 BOOST_CHECK( fp.ResolveTextVar( &token ) );
184 BOOST_CHECK_EQUAL( token, wxS( "DNP" ) );
185
186 token = wxS( "DNP" );
187 BOOST_CHECK( fp.ResolveTextVar( &token, wxEmptyString ) );
188 BOOST_CHECK_EQUAL( token, wxEmptyString );
189
190 token = wxS( "EXCLUDE_FROM_SIM" );
191 BOOST_CHECK( fp.ResolveTextVar( &token ) );
192 BOOST_CHECK_EQUAL( token, wxS( "Excluded from simulation" ) );
193
194 token = wxS( "EXCLUDE_FROM_SIM" );
195 BOOST_CHECK( fp.ResolveTextVar( &token, wxEmptyString ) );
196 BOOST_CHECK_EQUAL( token, wxEmptyString );
197}
198
199
203BOOST_AUTO_TEST_CASE( FootprintBOMExclusionForVariant )
204{
205 BOARD board;
206 FOOTPRINT fp( &board );
207
208 board.AddVariant( "Production" );
209
210 // Set base exclude from BOM to false
211 fp.SetAttributes( 0 );
212 BOOST_CHECK( !fp.GetExcludedFromBOMForVariant( wxEmptyString ) );
213 BOOST_CHECK( !fp.GetExcludedFromBOMForVariant( "Production" ) );
214
215 // Set base exclude from BOM to true
217 BOOST_CHECK( fp.GetExcludedFromBOMForVariant( wxEmptyString ) );
218 BOOST_CHECK( fp.GetExcludedFromBOMForVariant( "Production" ) );
219
220 // Override for Production variant
221 FOOTPRINT_VARIANT prodVariant( "Production" );
222 prodVariant.SetExcludedFromBOM( false );
223 fp.SetVariant( prodVariant );
224
225 BOOST_CHECK( fp.GetExcludedFromBOMForVariant( wxEmptyString ) );
226 BOOST_CHECK( !fp.GetExcludedFromBOMForVariant( "Production" ) );
227}
228
229
233BOOST_AUTO_TEST_CASE( FootprintSimExclusionForVariant )
234{
235 BOARD board;
236 FOOTPRINT fp( &board );
237
238 board.AddVariant( "Production" );
239
240 // Set base exclude from simulation to false
241 fp.SetAttributes( 0 );
242 BOOST_CHECK( !fp.GetExcludedFromSimForVariant( wxEmptyString ) );
243 BOOST_CHECK( !fp.GetExcludedFromSimForVariant( "Production" ) );
244
245 // Set base exclude from simulation to true
247 BOOST_CHECK( fp.GetExcludedFromSimForVariant( wxEmptyString ) );
248 BOOST_CHECK( fp.GetExcludedFromSimForVariant( "Production" ) );
249
250 // Override for Production variant
251 FOOTPRINT_VARIANT prodVariant( "Production" );
252 prodVariant.SetExcludedFromSim( false );
253 fp.SetVariant( prodVariant );
254
255 BOOST_CHECK( fp.GetExcludedFromSimForVariant( wxEmptyString ) );
256 BOOST_CHECK( !fp.GetExcludedFromSimForVariant( "Production" ) );
257}
258
259
263BOOST_AUTO_TEST_CASE( FootprintPosFileExclusionForVariant )
264{
265 BOARD board;
266 FOOTPRINT fp( &board );
267
268 board.AddVariant( "Production" );
269
270 // Set base exclude from position files to false
271 fp.SetAttributes( 0 );
272 BOOST_CHECK( !fp.GetExcludedFromPosFilesForVariant( wxEmptyString ) );
273 BOOST_CHECK( !fp.GetExcludedFromPosFilesForVariant( "Production" ) );
274
275 // Set base exclude from position files to true
277 BOOST_CHECK( fp.GetExcludedFromPosFilesForVariant( wxEmptyString ) );
279
280 // Override for Production variant
281 FOOTPRINT_VARIANT prodVariant( "Production" );
282 prodVariant.SetExcludedFromPosFiles( false );
283 fp.SetVariant( prodVariant );
284
285 BOOST_CHECK( fp.GetExcludedFromPosFilesForVariant( wxEmptyString ) );
286 BOOST_CHECK( !fp.GetExcludedFromPosFilesForVariant( "Production" ) );
287}
288
289
293BOOST_AUTO_TEST_CASE( VariantCaseInsensitivity )
294{
295 BOARD board;
296 FOOTPRINT fp( &board );
297
298 board.AddVariant( "Production" );
299
300 // Set variant override with specific case
301 FOOTPRINT_VARIANT prodVariant( "Production" );
302 prodVariant.SetDNP( true );
303 fp.SetVariant( prodVariant );
304
305 // Access with different cases - all should find the same variant
306 BOOST_CHECK( fp.GetDNPForVariant( "Production" ) );
307 BOOST_CHECK( fp.GetDNPForVariant( "production" ) );
308 BOOST_CHECK( fp.GetDNPForVariant( "PRODUCTION" ) );
309 BOOST_CHECK( fp.GetDNPForVariant( "PrOdUcTiOn" ) );
310
311 // Board operations should also be case insensitive
312 BOOST_CHECK( board.HasVariant( "production" ) );
313 BOOST_CHECK( board.HasVariant( "PRODUCTION" ) );
314}
315
316
320BOOST_AUTO_TEST_CASE( EmptyVariantReturnsBase )
321{
322 BOARD board;
323 FOOTPRINT fp( &board );
324
325 // Set base DNP
326 fp.SetAttributes( FP_DNP );
327
328 // Empty variant should return base
329 BOOST_CHECK( fp.GetDNPForVariant( wxEmptyString ) );
330
331 // Add a variant but still check empty
332 board.AddVariant( "Production" );
333
334 FOOTPRINT_VARIANT prodVariant( "Production" );
335 prodVariant.SetDNP( false );
336 fp.SetVariant( prodVariant );
337
338 // Empty still returns base
339 BOOST_CHECK( fp.GetDNPForVariant( wxEmptyString ) );
340 BOOST_CHECK( !fp.GetDNPForVariant( "Production" ) );
341}
342
343
347BOOST_AUTO_TEST_CASE( UnknownVariantReturnsBase )
348{
349 BOARD board;
350 FOOTPRINT fp( &board );
351
352 // Set base DNP
353 fp.SetAttributes( FP_DNP );
354
355 board.AddVariant( "Production" );
356
357 FOOTPRINT_VARIANT prodVariant( "Production" );
358 prodVariant.SetDNP( false );
359 fp.SetVariant( prodVariant );
360
361 // Unknown variant should return base
362 BOOST_CHECK( fp.GetDNPForVariant( "NonExistentVariant" ) );
363 BOOST_CHECK( fp.GetDNPForVariant( "Debug" ) );
364}
365
366
367BOOST_AUTO_TEST_CASE( FootprintVariantCopyAssignment )
368{
369 BOARD board;
370 FOOTPRINT original( &board );
371
372 original.SetReference( "R1" );
373 original.SetValue( "10K" );
374
375 FOOTPRINT_VARIANT prodVariant( "Production" );
376 prodVariant.SetDNP( true );
377 prodVariant.SetExcludedFromBOM( true );
378 prodVariant.SetExcludedFromSim( true );
379 prodVariant.SetFieldValue( original.Value().GetName(), "22K" );
380 original.SetVariant( prodVariant );
381
382 FOOTPRINT copy( original );
383 const FOOTPRINT_VARIANT* copyVariant = copy.GetVariant( "Production" );
384 BOOST_REQUIRE( copyVariant );
385 BOOST_CHECK( copyVariant->GetDNP() );
386 BOOST_CHECK( copyVariant->GetExcludedFromBOM() );
387 BOOST_CHECK( copyVariant->GetExcludedFromSim() );
388 BOOST_CHECK_EQUAL( copyVariant->GetFieldValue( original.Value().GetName() ), "22K" );
389
390 FOOTPRINT assigned( &board );
391 assigned = original;
392
393 const FOOTPRINT_VARIANT* assignedVariant = assigned.GetVariant( "Production" );
394 BOOST_REQUIRE( assignedVariant );
395 BOOST_CHECK( assignedVariant->GetDNP() );
396 BOOST_CHECK( assignedVariant->GetExcludedFromBOM() );
397 BOOST_CHECK( assignedVariant->GetExcludedFromSim() );
398 BOOST_CHECK_EQUAL( assignedVariant->GetFieldValue( original.Value().GetName() ), "22K" );
399}
400
401
402BOOST_AUTO_TEST_CASE( FootprintFieldShownTextForVariant )
403{
404 BOARD board;
405 FOOTPRINT fp( &board );
406
407 board.AddVariant( "Production" );
408 board.SetCurrentVariant( "Production" );
409
410 fp.SetValue( "10K" );
411
412 FOOTPRINT_VARIANT prodVariant( "Production" );
413 prodVariant.SetFieldValue( fp.Value().GetName(), "22K" );
414 fp.SetVariant( prodVariant );
415
417
418 board.SetCurrentVariant( wxEmptyString );
420}
421
422
423BOOST_AUTO_TEST_CASE( BoardVariantTextVars )
424{
425 BOARD board;
426
427 board.AddVariant( "Production" );
428 board.SetVariantDescription( "Production", "Production build" );
429 board.SetCurrentVariant( "Production" );
430
431 wxString variantToken = wxT( "VARIANT" );
432 BOOST_CHECK( board.ResolveTextVar( &variantToken, 0 ) );
433 BOOST_CHECK_EQUAL( variantToken, "Production" );
434
435 wxString descToken = wxT( "VARIANT_DESC" );
436 BOOST_CHECK( board.ResolveTextVar( &descToken, 0 ) );
437 BOOST_CHECK_EQUAL( descToken, "Production build" );
438
439 board.SetCurrentVariant( wxEmptyString );
440 wxString defaultToken = wxT( "VARIANT" );
441 BOOST_CHECK( board.ResolveTextVar( &defaultToken, 0 ) );
442 BOOST_CHECK( defaultToken.IsEmpty() );
443}
444
445
446BOOST_AUTO_TEST_CASE( NetlistComponentVariantsParsing )
447{
448 const std::string netlist =
449 "(export (version 1)\n"
450 " (components\n"
451 " (comp (ref R1)\n"
452 " (value 10K)\n"
453 " (footprint Resistor_SMD:R_0603_1608Metric)\n"
454 " (libsource (lib Device) (part R))\n"
455 " (variants\n"
456 " (variant (name Alt)\n"
457 " (property (name dnp) (value 1))\n"
458 " (property (name exclude_from_bom) (value 0))\n"
459 " (property (name exclude_from_sim) (value 1))\n"
460 " (fields\n"
461 " (field (name Value) \"22K\")\n"
462 " (field (name Footprint) \"Resistor_SMD:R_0805_2012Metric\")\n"
463 " )\n"
464 " )\n"
465 " )\n"
466 " )\n"
467 " )\n"
468 ")\n";
469
470 STRING_LINE_READER reader( netlist, wxT( "variant_netlist" ) );
471 NETLIST parsedNetlist;
472 KICAD_NETLIST_PARSER parser( &reader, &parsedNetlist );
473
474 parser.Parse();
475
476 BOOST_REQUIRE_EQUAL( parsedNetlist.GetCount(), 1 );
477
478 COMPONENT* component = parsedNetlist.GetComponent( 0 );
479 const COMPONENT_VARIANT* variant = component->GetVariant( "Alt" );
480
481 BOOST_REQUIRE( variant );
482 BOOST_CHECK( variant->m_hasDnp );
483 BOOST_CHECK( variant->m_dnp );
485 BOOST_CHECK( !variant->m_excludedFromBOM );
487 BOOST_CHECK( variant->m_excludedFromSim );
488
489 auto valueIt = variant->m_fields.find( "Value" );
490 BOOST_CHECK( valueIt != variant->m_fields.end() );
491 BOOST_CHECK_EQUAL( valueIt->second, "22K" );
492
493 auto fpIt = variant->m_fields.find( "Footprint" );
494 BOOST_CHECK( fpIt != variant->m_fields.end() );
495 BOOST_CHECK_EQUAL( fpIt->second, "Resistor_SMD:R_0805_2012Metric" );
496}
497
498
502BOOST_AUTO_TEST_CASE( MultipleVariantsIndependent )
503{
504 BOARD board;
505
506 // Add multiple variants
507 board.AddVariant( "Variant1" );
508 board.AddVariant( "Variant2" );
509 board.SetVariantDescription( "Variant1", "First variant" );
510 board.SetVariantDescription( "Variant2", "Second variant" );
511
512 // Create footprint and set variant-specific properties
513 FOOTPRINT fp( &board );
514 fp.SetReference( "R1" );
515 fp.SetValue( "10K" );
516
517 FOOTPRINT_VARIANT variant1( "Variant1" );
518 variant1.SetDNP( true );
519 variant1.SetFieldValue( fp.Value().GetName(), "22K" );
520 fp.SetVariant( variant1 );
521
522 FOOTPRINT_VARIANT variant2( "Variant2" );
523 variant2.SetDNP( false );
524 variant2.SetFieldValue( fp.Value().GetName(), "47K" );
525 fp.SetVariant( variant2 );
526
527 // Verify both variants are independent
528 BOOST_CHECK( fp.GetDNPForVariant( "Variant1" ) );
529 BOOST_CHECK( !fp.GetDNPForVariant( "Variant2" ) );
530
531 const FOOTPRINT_VARIANT* v1 = fp.GetVariant( "Variant1" );
532 const FOOTPRINT_VARIANT* v2 = fp.GetVariant( "Variant2" );
533
534 BOOST_REQUIRE( v1 );
535 BOOST_REQUIRE( v2 );
536
537 BOOST_CHECK_EQUAL( v1->GetFieldValue( fp.Value().GetName() ), "22K" );
538 BOOST_CHECK_EQUAL( v2->GetFieldValue( fp.Value().GetName() ), "47K" );
539
540 // Verify descriptions are independent
541 BOOST_CHECK_EQUAL( board.GetVariantDescription( "Variant1" ), "First variant" );
542 BOOST_CHECK_EQUAL( board.GetVariantDescription( "Variant2" ), "Second variant" );
543}
544
545
549BOOST_AUTO_TEST_CASE( VariantFieldUnicodeAndSpecialChars )
550{
551 BOARD board;
552 FOOTPRINT fp( &board );
553
554 board.AddVariant( "UnicodeTest" );
555
556 fp.SetValue( "Default" );
557
558 // Unicode characters
559 FOOTPRINT_VARIANT unicodeVariant( "UnicodeTest" );
560 wxString unicodeValue = wxT( "1kΩ ±5% 日本語" );
561 unicodeVariant.SetFieldValue( fp.Value().GetName(), unicodeValue );
562 fp.SetVariant( unicodeVariant );
563
564 const FOOTPRINT_VARIANT* retrieved = fp.GetVariant( "UnicodeTest" );
565 BOOST_REQUIRE( retrieved );
566 BOOST_CHECK_EQUAL( retrieved->GetFieldValue( fp.Value().GetName() ), unicodeValue );
567
568 // Special characters
569 FOOTPRINT_VARIANT specialVariant( "SpecialChars" );
570 wxString specialChars = wxT( "R<1K>\"test\"'value'" );
571 specialVariant.SetFieldValue( fp.Value().GetName(), specialChars );
572 fp.SetVariant( specialVariant );
573
574 const FOOTPRINT_VARIANT* retrievedSpecial = fp.GetVariant( "SpecialChars" );
575 BOOST_REQUIRE( retrievedSpecial );
576 BOOST_CHECK_EQUAL( retrievedSpecial->GetFieldValue( fp.Value().GetName() ), specialChars );
577
578 // Unicode in variant description
579 wxString unicodeDesc = wxT( "Variante für Produktion — 测试" );
580 board.SetVariantDescription( "UnicodeTest", unicodeDesc );
581 BOOST_CHECK_EQUAL( board.GetVariantDescription( "UnicodeTest" ), unicodeDesc );
582}
583
584
588BOOST_AUTO_TEST_CASE( VariantDeletionClearsRegistry )
589{
590 BOARD board;
591
592 board.AddVariant( "Variant1" );
593 board.AddVariant( "Variant2" );
594 board.SetVariantDescription( "Variant1", "Description 1" );
595 board.SetCurrentVariant( "Variant1" );
596
597 BOOST_CHECK_EQUAL( board.GetVariantNames().size(), 2 );
598 BOOST_CHECK( board.HasVariant( "Variant1" ) );
599
600 // Delete the variant
601 board.DeleteVariant( "Variant1" );
602
603 // Verify it's gone
604 BOOST_CHECK_EQUAL( board.GetVariantNames().size(), 1 );
605 BOOST_CHECK( !board.HasVariant( "Variant1" ) );
606 BOOST_CHECK( board.HasVariant( "Variant2" ) );
607
608 // Current variant should be cleared since we deleted the current one
609 BOOST_CHECK( board.GetCurrentVariant().IsEmpty() || board.GetCurrentVariant() != "Variant1" );
610}
611
612
616BOOST_AUTO_TEST_CASE( RenameVariantPreservesData )
617{
618 BOARD board;
619
620 board.AddVariant( "OldName" );
621 board.SetVariantDescription( "OldName", "Test description" );
622 board.SetCurrentVariant( "OldName" );
623
624 // Rename the variant
625 board.RenameVariant( "OldName", "NewName" );
626
627 // Old name should be gone
628 BOOST_CHECK( !board.HasVariant( "OldName" ) );
629
630 // New name should exist with same properties
631 BOOST_CHECK( board.HasVariant( "NewName" ) );
632 BOOST_CHECK_EQUAL( board.GetVariantDescription( "NewName" ), "Test description" );
633
634 // Current variant should be updated if it was the renamed one
635 BOOST_CHECK_EQUAL( board.GetCurrentVariant(), "NewName" );
636}
637
638
642BOOST_AUTO_TEST_CASE( GetVariantNamesForUIFormat )
643{
644 BOARD board;
645
646 board.AddVariant( "Zebra" );
647 board.AddVariant( "Alpha" );
648 board.AddVariant( "Beta" );
649
650 wxArrayString names = board.GetVariantNamesForUI();
651
652 // Should have 4 entries (default + 3 variants)
653 BOOST_CHECK( names.GetCount() >= 4 );
654
655 // First should be the default placeholder
656 BOOST_CHECK( !names[0].IsEmpty() );
657
658 // Remaining should be sorted alphabetically
659 bool foundAlpha = false;
660 bool foundBeta = false;
661 bool foundZebra = false;
662
663 for( size_t i = 1; i < names.GetCount(); i++ )
664 {
665 if( names[i] == wxT( "Alpha" ) )
666 foundAlpha = true;
667 else if( names[i] == wxT( "Beta" ) )
668 foundBeta = true;
669 else if( names[i] == wxT( "Zebra" ) )
670 foundZebra = true;
671 }
672
673 BOOST_CHECK( foundAlpha );
674 BOOST_CHECK( foundBeta );
675 BOOST_CHECK( foundZebra );
676}
677
678
682BOOST_AUTO_TEST_CASE( VariantMultipleFlagsCombinations )
683{
684 BOARD board;
685 FOOTPRINT fp( &board );
686
687 board.AddVariant( "DNPOnly" );
688 board.AddVariant( "BOMOnly" );
689 board.AddVariant( "AllFlags" );
690 board.AddVariant( "NoFlags" );
691
692 // Set various flag combinations
693 FOOTPRINT_VARIANT dnpOnly( "DNPOnly" );
694 dnpOnly.SetDNP( true );
695 dnpOnly.SetExcludedFromBOM( false );
696 dnpOnly.SetExcludedFromSim( false );
697 dnpOnly.SetExcludedFromPosFiles( false );
698 fp.SetVariant( dnpOnly );
699
700 FOOTPRINT_VARIANT bomOnly( "BOMOnly" );
701 bomOnly.SetDNP( false );
702 bomOnly.SetExcludedFromBOM( true );
703 bomOnly.SetExcludedFromSim( false );
704 bomOnly.SetExcludedFromPosFiles( false );
705 fp.SetVariant( bomOnly );
706
707 FOOTPRINT_VARIANT allFlags( "AllFlags" );
708 allFlags.SetDNP( true );
709 allFlags.SetExcludedFromBOM( true );
710 allFlags.SetExcludedFromSim( true );
711 allFlags.SetExcludedFromPosFiles( true );
712 fp.SetVariant( allFlags );
713
714 FOOTPRINT_VARIANT noFlags( "NoFlags" );
715 noFlags.SetDNP( false );
716 noFlags.SetExcludedFromBOM( false );
717 noFlags.SetExcludedFromSim( false );
718 noFlags.SetExcludedFromPosFiles( false );
719 fp.SetVariant( noFlags );
720
721 // Verify each variant has correct flags
722 BOOST_CHECK( fp.GetDNPForVariant( "DNPOnly" ) );
723 BOOST_CHECK( !fp.GetExcludedFromBOMForVariant( "DNPOnly" ) );
724 BOOST_CHECK( !fp.GetExcludedFromSimForVariant( "DNPOnly" ) );
726
727 BOOST_CHECK( !fp.GetDNPForVariant( "BOMOnly" ) );
728 BOOST_CHECK( fp.GetExcludedFromBOMForVariant( "BOMOnly" ) );
729 BOOST_CHECK( !fp.GetExcludedFromSimForVariant( "BOMOnly" ) );
731
732 BOOST_CHECK( fp.GetDNPForVariant( "AllFlags" ) );
733 BOOST_CHECK( fp.GetExcludedFromBOMForVariant( "AllFlags" ) );
734 BOOST_CHECK( fp.GetExcludedFromSimForVariant( "AllFlags" ) );
736
737 BOOST_CHECK( !fp.GetDNPForVariant( "NoFlags" ) );
738 BOOST_CHECK( !fp.GetExcludedFromBOMForVariant( "NoFlags" ) );
739 BOOST_CHECK( !fp.GetExcludedFromSimForVariant( "NoFlags" ) );
741}
742
743
748BOOST_AUTO_TEST_CASE( ComponentVariantToFootprintTransfer )
749{
750 BOARD board;
751 FOOTPRINT fp( &board );
752
753 fp.SetReference( "R1" );
754 fp.SetValue( "10K" );
755
756 board.AddVariant( "Variant A" );
757 board.AddVariant( "Variant B" );
758
759 // Create a COMPONENT with variant data (simulating schematic data)
760 LIB_ID fpid( wxT( "Resistor_SMD" ), wxT( "R_0805_2012Metric" ) );
761 wxString reference = wxT( "R1" );
762 wxString value = wxT( "10K" );
764 std::vector<KIID> kiids;
765
766 COMPONENT component( fpid, reference, value, path, kiids );
767
768 // Add variant "Variant A" with DNP=true, ExcludedFromBOM=false, ExcludedFromPosFiles=true
769 // and a field override for Datasheet
770 COMPONENT_VARIANT variantA( "Variant A" );
771 variantA.m_dnp = true;
772 variantA.m_hasDnp = true;
773 variantA.m_excludedFromBOM = false;
774 variantA.m_hasExcludedFromBOM = true;
775 variantA.m_excludedFromSim = true;
776 variantA.m_hasExcludedFromSim = true;
777 variantA.m_excludedFromPosFiles = true;
778 variantA.m_hasExcludedFromPosFiles = true;
779 variantA.m_fields[wxT( "Datasheet" )] = wxT( "https://example.com/datasheet.pdf" );
780 component.AddVariant( variantA );
781
782 // Add variant "Variant B" with DNP=false, ExcludedFromBOM=true
783 COMPONENT_VARIANT variantB( "Variant B" );
784 variantB.m_dnp = false;
785 variantB.m_hasDnp = true;
786 variantB.m_excludedFromBOM = true;
787 variantB.m_hasExcludedFromBOM = true;
788 variantB.m_excludedFromSim = false;
789 variantB.m_hasExcludedFromSim = true;
790 variantB.m_excludedFromPosFiles = false;
791 variantB.m_hasExcludedFromPosFiles = true;
792 variantB.m_fields[wxT( "Value" )] = wxT( "22K" );
793 component.AddVariant( variantB );
794
795 // Transfer variant data from COMPONENT to FOOTPRINT (simulating applyComponentVariants)
796 for( const auto& [variantName, componentVariant] : component.GetVariants() )
797 {
798 FOOTPRINT_VARIANT* fpVariant = fp.AddVariant( variantName );
799 BOOST_REQUIRE( fpVariant );
800
801 if( componentVariant.m_hasDnp )
802 fpVariant->SetDNP( componentVariant.m_dnp );
803
804 if( componentVariant.m_hasExcludedFromBOM )
805 fpVariant->SetExcludedFromBOM( componentVariant.m_excludedFromBOM );
806
807 if( componentVariant.m_hasExcludedFromSim )
808 fpVariant->SetExcludedFromSim( componentVariant.m_excludedFromSim );
809
810 if( componentVariant.m_hasExcludedFromPosFiles )
811 fpVariant->SetExcludedFromPosFiles( componentVariant.m_excludedFromPosFiles );
812
813 for( const auto& [fieldName, fieldValue] : componentVariant.m_fields )
814 fpVariant->SetFieldValue( fieldName, fieldValue );
815 }
816
817 // Verify Variant A properties
818 const FOOTPRINT_VARIANT* fpVariantA = fp.GetVariant( "Variant A" );
819 BOOST_REQUIRE( fpVariantA );
820 BOOST_CHECK( fpVariantA->GetDNP() );
821 BOOST_CHECK( !fpVariantA->GetExcludedFromBOM() );
822 BOOST_CHECK( fpVariantA->GetExcludedFromSim() );
823 BOOST_CHECK( fpVariantA->GetExcludedFromPosFiles() );
824 BOOST_CHECK( fpVariantA->HasFieldValue( wxT( "Datasheet" ) ) );
825 BOOST_CHECK_EQUAL( fpVariantA->GetFieldValue( wxT( "Datasheet" ) ),
826 wxT( "https://example.com/datasheet.pdf" ) );
827
828 // Verify Variant B properties
829 const FOOTPRINT_VARIANT* fpVariantB = fp.GetVariant( "Variant B" );
830 BOOST_REQUIRE( fpVariantB );
831 BOOST_CHECK( !fpVariantB->GetDNP() );
832 BOOST_CHECK( fpVariantB->GetExcludedFromBOM() );
833 BOOST_CHECK( !fpVariantB->GetExcludedFromSim() );
834 BOOST_CHECK( !fpVariantB->GetExcludedFromPosFiles() );
835 BOOST_CHECK( fpVariantB->HasFieldValue( wxT( "Value" ) ) );
836 BOOST_CHECK_EQUAL( fpVariantB->GetFieldValue( wxT( "Value" ) ), wxT( "22K" ) );
837
838 // Verify variant-aware getters work
839 BOOST_CHECK( fp.GetDNPForVariant( "Variant A" ) );
840 BOOST_CHECK( !fp.GetDNPForVariant( "Variant B" ) );
841 BOOST_CHECK( !fp.GetExcludedFromBOMForVariant( "Variant A" ) );
842 BOOST_CHECK( fp.GetExcludedFromBOMForVariant( "Variant B" ) );
843 BOOST_CHECK( fp.GetExcludedFromSimForVariant( "Variant A" ) );
844 BOOST_CHECK( !fp.GetExcludedFromSimForVariant( "Variant B" ) );
847}
848
849
855BOOST_AUTO_TEST_CASE( ComponentVariantPartialOverride )
856{
857 BOARD board;
858 FOOTPRINT fp( &board );
859
860 fp.SetReference( "R1" );
861
862 // Set base footprint to have all attributes false
863 fp.SetDNP( false );
864 fp.SetExcludedFromBOM( false );
865 fp.SetExcludedFromSim( false );
866 fp.SetExcludedFromPosFiles( false );
867
868 board.AddVariant( "TestVariant" );
869
870 // Pre-populate the footprint variant with all true values (simulating old state)
871 FOOTPRINT_VARIANT initialVariant( "TestVariant" );
872 initialVariant.SetDNP( true );
873 initialVariant.SetExcludedFromBOM( true );
874 initialVariant.SetExcludedFromSim( true );
875 initialVariant.SetExcludedFromPosFiles( true );
876 fp.SetVariant( initialVariant );
877
878 // Create a component variant that only has explicit DNP override set
879 LIB_ID fpid( wxT( "Resistor_SMD" ), wxT( "R_0805_2012Metric" ) );
881 std::vector<KIID> kiids;
882 COMPONENT component( fpid, wxT( "R1" ), wxT( "10K" ), path, kiids );
883
884 COMPONENT_VARIANT partialVariant( "TestVariant" );
885 partialVariant.m_dnp = true;
886 partialVariant.m_hasDnp = true;
887 // The remaining m_has* flags are false (no explicit override)
888 component.AddVariant( partialVariant );
889
890 // Transfer properties, resetting non-overridden ones to base footprint values
891 for( const auto& [variantName, componentVariant] : component.GetVariants() )
892 {
893 FOOTPRINT_VARIANT* fpVariant = fp.GetVariant( variantName );
894 BOOST_REQUIRE( fpVariant );
895
896 // Apply explicit override or reset to base footprint value
897 bool targetDnp = componentVariant.m_hasDnp ? componentVariant.m_dnp : fp.IsDNP();
898 fpVariant->SetDNP( targetDnp );
899
900 bool targetBOM = componentVariant.m_hasExcludedFromBOM
901 ? componentVariant.m_excludedFromBOM
902 : fp.IsExcludedFromBOM();
903 fpVariant->SetExcludedFromBOM( targetBOM );
904
905 bool targetSim = componentVariant.m_hasExcludedFromSim
906 ? componentVariant.m_excludedFromSim
907 : fp.IsExcludedFromSim();
908 fpVariant->SetExcludedFromSim( targetSim );
909
910 bool targetPos = componentVariant.m_hasExcludedFromPosFiles
911 ? componentVariant.m_excludedFromPosFiles
913 fpVariant->SetExcludedFromPosFiles( targetPos );
914 }
915
916 // Verify: DNP should be true (m_hasDnp was true with value true)
917 BOOST_CHECK( fp.GetDNPForVariant( "TestVariant" ) );
918
919 // Verify: ExcludedFromBOM should be reset to base (false)
920 BOOST_CHECK( !fp.GetExcludedFromBOMForVariant( "TestVariant" ) );
921
922 // Verify: ExcludedFromSim should be reset to base (false)
923 BOOST_CHECK( !fp.GetExcludedFromSimForVariant( "TestVariant" ) );
924
925 // Verify: ExcludedFromPosFiles should be reset to base (false)
926 BOOST_CHECK( !fp.GetExcludedFromPosFilesForVariant( "TestVariant" ) );
927}
928
929
934BOOST_AUTO_TEST_CASE( VariantAttributeTransferWithReset )
935{
936 BOARD board;
937 FOOTPRINT fp( &board );
938 fp.SetReference( "R1" );
939 fp.SetFPID( LIB_ID( wxT( "Resistor_SMD" ), wxT( "R_0805" ) ) );
940
941 // Base footprint has no flags set
942 fp.SetDNP( false );
943 fp.SetExcludedFromBOM( false );
944 fp.SetExcludedFromSim( false );
945 fp.SetExcludedFromPosFiles( false );
946
947 board.AddVariant( "Variant A" );
948
949 // Step 1: Initial state - variant has all attributes set (simulating previous netlist update)
950 FOOTPRINT_VARIANT* fpVariant = fp.AddVariant( "Variant A" );
951 BOOST_REQUIRE( fpVariant );
952 fpVariant->SetDNP( true );
953 fpVariant->SetExcludedFromBOM( true );
954 fpVariant->SetExcludedFromSim( true );
955 fpVariant->SetExcludedFromPosFiles( true );
956
957 // Verify initial state
958 BOOST_CHECK( fp.GetDNPForVariant( "Variant A" ) );
959 BOOST_CHECK( fp.GetExcludedFromBOMForVariant( "Variant A" ) );
960 BOOST_CHECK( fp.GetExcludedFromSimForVariant( "Variant A" ) );
962
963 // Step 2: New netlist has variant with NO explicit attribute overrides
964 // This simulates the user removing all variant attribute overrides from schematic
965 COMPONENT_VARIANT componentVariant( "Variant A" );
966 // All m_has* flags are false by default (no explicit overrides)
967
968 // Step 3: Apply the fixed transfer logic (same as board_netlist_updater::applyComponentVariants)
969 // For isActive = true case with no explicit overrides, attributes should reset to base
970 bool targetDnp = componentVariant.m_hasDnp ? componentVariant.m_dnp : fp.IsDNP();
971 bool targetBOM = componentVariant.m_hasExcludedFromBOM ? componentVariant.m_excludedFromBOM
972 : fp.IsExcludedFromBOM();
973 bool targetSim = componentVariant.m_hasExcludedFromSim ? componentVariant.m_excludedFromSim
974 : fp.IsExcludedFromSim();
975 bool targetPos = componentVariant.m_hasExcludedFromPosFiles
976 ? componentVariant.m_excludedFromPosFiles
978
979 fpVariant->SetDNP( targetDnp );
980 fpVariant->SetExcludedFromBOM( targetBOM );
981 fpVariant->SetExcludedFromSim( targetSim );
982 fpVariant->SetExcludedFromPosFiles( targetPos );
983
984 // Step 4: Verify all attributes were reset to base footprint values (false)
985 BOOST_CHECK_MESSAGE( !fp.GetDNPForVariant( "Variant A" ),
986 "DNP should be reset to base value (false) when no explicit override" );
987 BOOST_CHECK_MESSAGE( !fp.GetExcludedFromBOMForVariant( "Variant A" ),
988 "ExcludedFromBOM should be reset to base value (false) when no override" );
989 BOOST_CHECK_MESSAGE( !fp.GetExcludedFromSimForVariant( "Variant A" ),
990 "ExcludedFromSim should be reset to base value (false) when no override" );
991 BOOST_CHECK_MESSAGE( !fp.GetExcludedFromPosFilesForVariant( "Variant A" ),
992 "ExcludedFromPosFiles should be reset to base (false) when no override" );
993}
994
995
1007BOOST_AUTO_TEST_CASE( VariantTestR2FootprintAttributeVerification )
1008{
1009 wxString dataPath = KI_TEST::GetPcbnewTestDataDir() + wxString( "variant_test/variant_test.kicad_pcb" );
1010
1011 PCB_IO_KICAD_SEXPR pcbIo;
1012 std::unique_ptr<BOARD> board = pcbIo.LoadBoard( dataPath );
1013
1014 BOOST_REQUIRE( board );
1015 BOOST_REQUIRE( board->HasVariant( "Variant A" ) );
1016
1017 // Find both R2 footprints and verify their variant data
1018 FOOTPRINT* r2_c1210 = nullptr; // Variant A's footprint (C_1210_3225Metric)
1019 FOOTPRINT* r2_c3640 = nullptr; // Base footprint (C_3640_9110Metric)
1020
1021 for( FOOTPRINT* fp : board->Footprints() )
1022 {
1023 if( fp->GetReference() == wxT( "R2" ) )
1024 {
1025 wxString fpName = fp->GetFPID().GetLibItemName();
1026
1027 if( fpName.Contains( wxT( "C_1210" ) ) )
1028 r2_c1210 = fp;
1029 else if( fpName.Contains( wxT( "C_3640" ) ) )
1030 r2_c3640 = fp;
1031 }
1032 }
1033
1034 // Verify we found both R2 footprints
1035 BOOST_TEST_MESSAGE( "Looking for R2 footprints in test data" );
1036 BOOST_REQUIRE_MESSAGE( r2_c1210, "Should find R2 with C_1210 footprint (variant footprint)" );
1037 BOOST_REQUIRE_MESSAGE( r2_c3640, "Should find R2 with C_3640 footprint (base footprint)" );
1038
1039 // Check C_1210 (Variant A's footprint) - this IS the active footprint for Variant A
1040 // The schematic has NO attribute overrides, so the PCB variant should also have no overrides
1041 // (or equivalently, values should match base footprint)
1042 const FOOTPRINT_VARIANT* c1210_variantA = r2_c1210->GetVariant( "Variant A" );
1043
1044 BOOST_TEST_MESSAGE( "R2 C_1210 (variant footprint) base attributes: DNP="
1045 << r2_c1210->IsDNP() << " ExcludedFromBOM=" << r2_c1210->IsExcludedFromBOM()
1046 << " ExcludedFromPosFiles=" << r2_c1210->IsExcludedFromPosFiles() );
1047
1048 if( c1210_variantA )
1049 {
1050 BOOST_TEST_MESSAGE( "R2 C_1210 Variant A attributes: DNP=" << c1210_variantA->GetDNP()
1051 << " ExcludedFromBOM=" << c1210_variantA->GetExcludedFromBOM()
1052 << " ExcludedFromPosFiles=" << c1210_variantA->GetExcludedFromPosFiles() );
1053
1054 // For the variant footprint, since schematic has NO attribute overrides,
1055 // variant attributes should match base footprint values (all false)
1056 BOOST_CHECK_MESSAGE( !c1210_variantA->GetDNP(),
1057 "C_1210 Variant A DNP should be false (no schematic override)" );
1058 BOOST_CHECK_MESSAGE( !c1210_variantA->GetExcludedFromBOM(),
1059 "C_1210 Variant A ExcludedFromBOM should be false (no override)" );
1060 BOOST_CHECK_MESSAGE( !c1210_variantA->GetExcludedFromPosFiles(),
1061 "C_1210 Variant A ExcludedFromPosFiles should be false (no override)" );
1062 }
1063 else
1064 {
1065 BOOST_TEST_MESSAGE( "R2 C_1210 has no Variant A data" );
1066 }
1067
1068 // Check C_3640 (base footprint) - this is NOT the active footprint for Variant A.
1069 // The netlist updater marks non-associated footprints as DNP for each variant where
1070 // they are not the active choice, so C_3640 must have DNP=true for Variant A.
1071 const FOOTPRINT_VARIANT* c3640_variantA = r2_c3640->GetVariant( "Variant A" );
1072
1073 BOOST_TEST_MESSAGE( "R2 C_3640 (base footprint) base attributes: DNP="
1074 << r2_c3640->IsDNP() << " ExcludedFromBOM=" << r2_c3640->IsExcludedFromBOM()
1075 << " ExcludedFromPosFiles=" << r2_c3640->IsExcludedFromPosFiles() );
1076
1077 BOOST_REQUIRE_MESSAGE( c3640_variantA,
1078 "C_3640 must have Variant A data to hide it when Variant A is active" );
1079
1080 BOOST_TEST_MESSAGE( "R2 C_3640 Variant A attributes: DNP=" << c3640_variantA->GetDNP()
1081 << " ExcludedFromBOM=" << c3640_variantA->GetExcludedFromBOM()
1082 << " ExcludedFromPosFiles=" << c3640_variantA->GetExcludedFromPosFiles() );
1083
1084 // C_3640 is NOT the active footprint for Variant A (C_1210 is), so it must be
1085 // marked DNP for Variant A so the 3D viewer and other consumers hide it correctly.
1086 BOOST_CHECK_MESSAGE( c3640_variantA->GetDNP(),
1087 "C_3640 Variant A DNP should be true (it is not active for Variant A)" );
1088}
1089
1090
1094BOOST_AUTO_TEST_CASE( VariantTestProjectLoad )
1095{
1096 wxString dataPath = KI_TEST::GetPcbnewTestDataDir() + wxString( "variant_test/variant_test.kicad_pcb" );
1097
1098 PCB_IO_KICAD_SEXPR pcbIo;
1099 std::unique_ptr<BOARD> board = pcbIo.LoadBoard( dataPath );
1100
1101 BOOST_REQUIRE( board );
1102
1103 // Verify the board has the "Variant A" variant registered
1104 BOOST_CHECK( board->HasVariant( "Variant A" ) );
1105
1106 // Find footprints and verify their variant properties
1107 // Based on the variant_test.kicad_pcb and schematic:
1108 // - R1 with Variant A having field override for Datasheet
1109 // - R2 (C_3640 base footprint) with Variant A having NO attribute overrides
1110 // (schematic has no explicit overrides, so PCB mirrors base values)
1111 // - R3 with Variant A having DNP (explicit schematic override)
1112
1113 for( FOOTPRINT* fp : board->Footprints() )
1114 {
1115 const wxString& ref = fp->GetReference();
1116
1117 if( ref == wxT( "R1" ) )
1118 {
1119 const FOOTPRINT_VARIANT* variantA = fp->GetVariant( "Variant A" );
1120
1121 if( variantA )
1122 {
1123 // R1 has a Datasheet field override in Variant A
1124 BOOST_CHECK( variantA->HasFieldValue( wxT( "Datasheet" ) ) );
1125 BOOST_CHECK_EQUAL( variantA->GetFieldValue( wxT( "Datasheet" ) ), wxT( "test" ) );
1126 }
1127 }
1128 else if( ref == wxT( "R2" ) )
1129 {
1130 wxString fpName = fp->GetFPID().GetLibItemName();
1131
1132 if( fpName.Contains( wxT( "C_3640" ) ) )
1133 {
1134 // C_3640 is the base (default) footprint for R2. When Variant A is active,
1135 // C_1210 is used instead, so C_3640 must be DNP for Variant A.
1136 const FOOTPRINT_VARIANT* variantA = fp->GetVariant( "Variant A" );
1137 BOOST_REQUIRE( variantA );
1138 BOOST_CHECK( variantA->GetDNP() );
1139 }
1140 else if( fpName.Contains( wxT( "C_1210" ) ) )
1141 {
1142 // C_1210 is the Variant A footprint for R2. It must be globally DNP
1143 // (so it's hidden in the default variant) and its Variant A entry must
1144 // be non-DNP (so it's visible when Variant A is active).
1145 BOOST_CHECK( fp->IsDNP() );
1146 const FOOTPRINT_VARIANT* variantA = fp->GetVariant( "Variant A" );
1147 BOOST_REQUIRE( variantA );
1148 BOOST_CHECK( !variantA->GetDNP() );
1149 }
1150 }
1151 else if( ref == wxT( "R3" ) )
1152 {
1153 const FOOTPRINT_VARIANT* variantA = fp->GetVariant( "Variant A" );
1154
1155 if( variantA )
1156 {
1157 // R3 has DNP in Variant A (explicit schematic override)
1158 BOOST_CHECK( variantA->GetDNP() );
1159 }
1160 }
1161 }
1162}
1163
1164
1171BOOST_AUTO_TEST_CASE( PosExportVariantValue )
1172{
1173 BOARD board;
1174
1175 board.AddVariant( "AltPop" );
1176
1177 FOOTPRINT* fp = new FOOTPRINT( &board );
1178 fp->SetReference( "R1" );
1179 fp->SetValue( "10K" );
1180
1181 FOOTPRINT_VARIANT altPopVariant( "AltPop" );
1182 altPopVariant.SetFieldValue( fp->Value().GetName(), "22K" );
1183 fp->SetVariant( altPopVariant );
1184
1185 board.Add( fp, ADD_MODE::INSERT );
1186
1187 auto runExport = [&]( const wxString& aVariant, bool aCsv ) -> std::string
1188 {
1189 PLACE_FILE_EXPORTER exporter( &board,
1190 true, // mm
1191 false, // all footprints
1192 false, // include TH
1193 false, // don't exclude DNP
1194 false, // don't exclude BOM
1195 true, // front
1196 true, // back
1197 aCsv, // format
1198 false, // no aux origin
1199 false ); // don't negate X
1200 exporter.SetVariant( aVariant );
1201 return exporter.GenPositionData();
1202 };
1203
1204 // ASCII format
1205 std::string defaultAscii = runExport( wxEmptyString, false );
1206 BOOST_CHECK( defaultAscii.find( "10K" ) != std::string::npos );
1207 BOOST_CHECK( defaultAscii.find( "22K" ) == std::string::npos );
1208
1209 std::string altPopAscii = runExport( wxS( "AltPop" ), false );
1210 BOOST_CHECK( altPopAscii.find( "22K" ) != std::string::npos );
1211 BOOST_CHECK( altPopAscii.find( "10K" ) == std::string::npos );
1212
1213 // CSV format
1214 std::string defaultCsv = runExport( wxEmptyString, true );
1215 BOOST_CHECK( defaultCsv.find( "10K" ) != std::string::npos );
1216 BOOST_CHECK( defaultCsv.find( "22K" ) == std::string::npos );
1217
1218 std::string altPopCsv = runExport( wxS( "AltPop" ), true );
1219 BOOST_CHECK( altPopCsv.find( "22K" ) != std::string::npos );
1220 BOOST_CHECK( altPopCsv.find( "10K" ) == std::string::npos );
1221
1222 // GenReportData should also respect the variant
1223 auto runReport = [&]( const wxString& aVariant ) -> std::string
1224 {
1225 PLACE_FILE_EXPORTER exporter( &board, true, false, false, false, false,
1226 true, true, false, false, false );
1227 exporter.SetVariant( aVariant );
1228 return exporter.GenReportData();
1229 };
1230
1231 std::string defaultReport = runReport( wxEmptyString );
1232 BOOST_CHECK( defaultReport.find( "10K" ) != std::string::npos );
1233 BOOST_CHECK( defaultReport.find( "22K" ) == std::string::npos );
1234
1235 std::string altPopReport = runReport( wxS( "AltPop" ) );
1236 BOOST_CHECK( altPopReport.find( "22K" ) != std::string::npos );
1237 BOOST_CHECK( altPopReport.find( "10K" ) == std::string::npos );
1238}
1239
1240
1250BOOST_AUTO_TEST_CASE( ExcessVariantsCleanedWhenNetlistEmpty )
1251{
1252 BOARD board;
1253 FOOTPRINT fp( &board );
1254 fp.SetReference( "C1" );
1255 fp.SetFPID( LIB_ID( wxT( "Capacitor_SMD" ), wxT( "C_0805" ) ) );
1256 fp.SetDNP( false );
1257 fp.SetExcludedFromBOM( false );
1258 fp.SetExcludedFromPosFiles( false );
1259
1260 board.AddVariant( "TestVariant" );
1261
1262 // Simulate a previous netlist update that applied DNP=true for this variant
1263 FOOTPRINT_VARIANT* fpVariant = fp.AddVariant( "TestVariant" );
1264 BOOST_REQUIRE( fpVariant );
1265 fpVariant->SetDNP( true );
1266 fpVariant->SetExcludedFromBOM( true );
1267
1268 BOOST_CHECK( fp.GetVariant( "TestVariant" ) != nullptr );
1269 BOOST_CHECK( fp.GetDNPForVariant( "TestVariant" ) );
1270 BOOST_CHECK( fp.GetExcludedFromBOMForVariant( "TestVariant" ) );
1271
1272 // Simulate what applyComponentVariants does when the netlist has no variant
1273 // data for this component (all variant properties now match the base).
1274 // With the fix, the function no longer returns early on empty variants,
1275 // so the excess-variants cleanup runs.
1276
1277 std::set<wxString> excessVariants;
1278
1279 for( const auto& [variantName, _] : fp.GetVariants() )
1280 excessVariants.insert( variantName );
1281
1282 // No netlist variants to process, so nothing is erased from excessVariants.
1283 // All footprint variants are excess.
1284 BOOST_CHECK_EQUAL( excessVariants.size(), 1 );
1285 BOOST_CHECK( excessVariants.count( "TestVariant" ) == 1 );
1286
1287 for( const wxString& excess : excessVariants )
1288 fp.DeleteVariant( excess );
1289
1290 BOOST_CHECK_MESSAGE( fp.GetVariant( "TestVariant" ) == nullptr,
1291 "Stale variant must be removed when netlist has no variant data" );
1292 BOOST_CHECK_MESSAGE( fp.GetVariants().empty(),
1293 "All variants should be cleaned up" );
1294}
1295
1296
1301BOOST_AUTO_TEST_CASE( ExcessVariantsSelectiveCleanup )
1302{
1303 BOARD board;
1304 FOOTPRINT fp( &board );
1305 fp.SetReference( "U1" );
1306 fp.SetFPID( LIB_ID( wxT( "Package_SO" ), wxT( "SOIC-8" ) ) );
1307 fp.SetDNP( false );
1308
1309 board.AddVariant( "Production" );
1310 board.AddVariant( "Debug" );
1311
1312 // Both variants were previously applied to the footprint
1313 FOOTPRINT_VARIANT* prodVariant = fp.AddVariant( "Production" );
1314 BOOST_REQUIRE( prodVariant );
1315 prodVariant->SetDNP( true );
1316
1317 FOOTPRINT_VARIANT* debugVariant = fp.AddVariant( "Debug" );
1318 BOOST_REQUIRE( debugVariant );
1319 debugVariant->SetExcludedFromBOM( true );
1320
1321 BOOST_CHECK_EQUAL( fp.GetVariants().size(), 2 );
1322
1323 // Simulate netlist update where only "Production" has variant data.
1324 // "Debug" variant properties now match the base so it was omitted from the netlist.
1325 std::set<wxString> excessVariants;
1326
1327 for( const auto& [variantName, _] : fp.GetVariants() )
1328 excessVariants.insert( variantName );
1329
1330 // Erase variants that ARE in the netlist
1331 excessVariants.erase( "Production" );
1332
1333 BOOST_CHECK_EQUAL( excessVariants.size(), 1 );
1334 BOOST_CHECK( excessVariants.count( "Debug" ) == 1 );
1335
1336 for( const wxString& excess : excessVariants )
1337 fp.DeleteVariant( excess );
1338
1339 BOOST_CHECK_MESSAGE( fp.GetVariant( "Production" ) != nullptr,
1340 "Production variant should be preserved (in netlist)" );
1341 BOOST_CHECK_MESSAGE( fp.GetVariant( "Debug" ) == nullptr,
1342 "Debug variant should be removed (not in netlist)" );
1343 BOOST_CHECK_EQUAL( fp.GetVariants().size(), 1 );
1344}
1345
1346
1360BOOST_AUTO_TEST_CASE( Issue23298_BaseFootprintHiddenInNonDefaultVariant )
1361{
1362 // The updater dereferences the board's project, so the board needs one.
1363 SETTINGS_MANAGER settingsManager;
1364 settingsManager.LoadProject( "" );
1365
1366 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
1367 board->SetProject( &settingsManager.Prj() );
1368 board->AddVariant( "Variant A" );
1369
1370 LIB_ID baseFpid;
1371 BOOST_REQUIRE_EQUAL( baseFpid.Parse( wxS( "TestLib:C_3640" ) ), -1 );
1372
1373 LIB_ID variantFpid;
1374 BOOST_REQUIRE_EQUAL( variantFpid.Parse( wxS( "TestLib:C_1210" ) ), -1 );
1375
1376 // R2 has one footprint per variant sharing the RefDes: C_3640 for the default variant and
1377 // C_1210 for Variant A. Reproduce the board state the netlist updater starts from.
1378 FOOTPRINT* baseFp = new FOOTPRINT( board.get() );
1379 baseFp->SetReference( "R2" );
1380 baseFp->SetFPID( baseFpid );
1381 board->Add( baseFp );
1382
1383 FOOTPRINT* variantFp = new FOOTPRINT( board.get() );
1384 variantFp->SetReference( "R2" );
1385 variantFp->SetFPID( variantFpid );
1386 board->Add( variantFp );
1387
1388 // Schematic side: R2 with base footprint C_3640 and a Variant A that reassigns C_1210 and keeps
1389 // it populated (the non-base footprint is otherwise flagged globally DNP by the updater). The
1390 // netlist must also declare the variant, otherwise the updater reconciles it away.
1392 netlist.AddVariant( "Variant A" );
1393
1394 COMPONENT* component = new COMPONENT( baseFpid, "R2", "10uF", KIID_PATH(),
1395 std::vector<KIID>{ KIID() } );
1396
1397 COMPONENT_VARIANT variantA( "Variant A" );
1398 variantA.m_fields[GetDefaultFieldName( FIELD_T::FOOTPRINT, UNTRANSLATED )] = variantFpid.Format().wx_str();
1399 variantA.m_dnp = false;
1400 variantA.m_hasDnp = true;
1401 component->AddVariant( variantA );
1402 netlist.AddComponent( component );
1403
1404 TOOL_MANAGER toolMgr;
1405 toolMgr.SetEnvironment( board.get(), nullptr, nullptr, nullptr, nullptr );
1406 toolMgr.RegisterTool( new KI_TEST::DUMMY_TOOL() );
1407
1408 BOARD_NETLIST_UPDATER updater( &toolMgr, board.get() );
1409 updater.SetIsDryRun( false );
1410 updater.SetWillDrag( false );
1411 updater.SetReplaceFootprints( false );
1412 updater.SetDeleteUnusedFootprints( false );
1413
1414 BOOST_REQUIRE( updater.UpdateNetlist( netlist ) );
1415
1416 // Default variant: base FP visible, variant FP hidden.
1417 BOOST_CHECK_MESSAGE( !baseFp->GetDNPForVariant( wxEmptyString ),
1418 "Base FP must be visible in the default variant" );
1419 BOOST_CHECK_MESSAGE( variantFp->GetDNPForVariant( wxEmptyString ),
1420 "Variant A FP must be hidden in the default variant (globally DNP)" );
1421
1422 // Variant A active: variant FP visible, base FP hidden. The base-FP check is the #23298 fix;
1423 // the variant-FP check guards the single-pass ordering.
1424 BOOST_CHECK_MESSAGE( !variantFp->GetDNPForVariant( wxT( "Variant A" ) ),
1425 "Variant A FP must be visible when Variant A is active" );
1426 BOOST_CHECK_MESSAGE( baseFp->GetDNPForVariant( wxT( "Variant A" ) ),
1427 "Base FP must be hidden when Variant A is active (issue #23298)" );
1428}
1429
1430
1435BOOST_AUTO_TEST_CASE( ExchangeFootprintPreservesVariantsAndUnits )
1436{
1437 BOARD board;
1438 TOOL_MANAGER mgr;
1439 mgr.SetEnvironment( &board, nullptr, nullptr, nullptr, nullptr );
1440 KI_TEST::DUMMY_TOOL* dummyTool = new KI_TEST::DUMMY_TOOL();
1441 mgr.RegisterTool( dummyTool );
1442
1443 board.AddVariant( "Prototype" );
1444
1445 FOOTPRINT* existing = new FOOTPRINT( &board );
1446 existing->SetFPID( LIB_ID( "Resistor_SMD", "R_0805_2012Metric" ) );
1447 existing->SetReference( "R1" );
1448 existing->SetValue( "10k" );
1449
1450 FOOTPRINT_VARIANT variant( "Prototype" );
1451 variant.SetDNP( true );
1452 variant.SetFieldValue( "MPN", "RC0805FR-071KL" );
1453 existing->SetVariant( variant );
1454
1456 unit.m_unitName = "A";
1457 unit.m_pins = { "1", "2" };
1458 existing->SetUnitInfo( { unit } );
1459
1460 board.Add( existing );
1461 board.BuildConnectivity();
1462
1463 // The library copy, as a fresh load gives it: the same footprint with no board data.
1464 FOOTPRINT* updated = new FOOTPRINT( &board );
1465 updated->SetFPID( existing->GetFPID() );
1466
1467 BOARD_COMMIT commit( dummyTool );
1468
1469 // The defaults are every reset option enabled, the most aggressive update on offer.
1470 board.ExchangeFootprint( existing, updated, commit, FLIP_DIRECTION::TOP_BOTTOM, true );
1471 commit.Push( wxT( "Update Footprint" ) );
1472
1473 const FOOTPRINT_VARIANT* kept = updated->GetVariant( "Prototype" );
1474
1475 BOOST_REQUIRE_MESSAGE( kept, "The Prototype variant must survive a footprint update" );
1476 BOOST_CHECK( kept->GetDNP() );
1477 BOOST_CHECK_EQUAL( kept->GetFieldValue( "MPN" ), "RC0805FR-071KL" );
1478 BOOST_CHECK( updated->GetDNPForVariant( "Prototype" ) );
1479
1480 BOOST_REQUIRE_EQUAL( updated->GetUnitInfo().size(), 1u );
1481 BOOST_CHECK_EQUAL( updated->GetUnitInfo()[0].m_unitName, "A" );
1482 BOOST_CHECK_EQUAL( updated->GetUnitInfo()[0].m_pins.size(), 2u );
1483}
1484
1485
General utilities for PCB file IO for QA programs.
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
Update the BOARD with a new netlist.
bool UpdateNetlist(NETLIST &aNetlist)
Update the board's components according to the new netlist.
void SetWillDrag(bool aWillDrag)
void SetIsDryRun(bool aEnabled)
Enable dry run mode (just report, no changes to PCB).
void SetDeleteUnusedFootprints(bool aEnabled)
void SetReplaceFootprints(bool aEnabled)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
void SetCurrentVariant(const wxString &aVariant)
Definition board.cpp:3016
const std::vector< wxString > & GetVariantNames() const
Definition board.h:533
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1548
wxArrayString GetVariantNamesForUI() const
Return the variant names for UI display.
Definition board.cpp:3208
void DeleteVariant(const wxString &aVariantName)
Definition board.cpp:3061
bool BuildConnectivity(PROGRESS_REPORTER *aReporter=nullptr)
Build or rebuild the board connectivity database for the board, especially the list of connected item...
Definition board.cpp:369
bool ResolveTextVar(wxString *token, int aDepth) const
Definition board.cpp:708
void ExchangeFootprint(FOOTPRINT *aExisting, FOOTPRINT *aNew, BOARD_COMMIT &aCommit, FLIP_DIRECTION aFlipDirection, bool aMatchPadPositions, bool aDeleteExtraTexts=true, bool aResetTextLayers=true, bool aResetTextEffects=true, bool aResetTextPositions=true, bool aResetTextContent=true, bool aResetFabricationAttrs=true, bool aResetClearanceOverrides=true, bool aReset3DModels=true, bool aResetTransform=false, bool *aUpdated=nullptr, bool *aShifted=nullptr)
Replace aExisting with aNew, preserving connectivity and metadata.
bool HasVariant(const wxString &aVariantName) const
Definition board.cpp:3042
void AddVariant(const wxString &aVariantName)
Definition board.cpp:3048
wxString GetVariantDescription(const wxString &aVariantName) const
Definition board.cpp:3172
wxString GetCurrentVariant() const
Definition board.h:530
void RenameVariant(const wxString &aOldName, const wxString &aNewName)
Definition board.cpp:3089
void SetVariantDescription(const wxString &aVariantName, const wxString &aDescription)
Definition board.cpp:3191
Store all of the related component information found in a netlist.
void AddVariant(const COMPONENT_VARIANT &aVariant)
const COMPONENT_VARIANT * GetVariant(const wxString &aVariantName) const
const CASE_INSENSITIVE_MAP< COMPONENT_VARIANT > & GetVariants() const
Variant information for a footprint.
Definition footprint.h:178
wxString GetName() const
Definition footprint.h:189
bool HasFieldValue(const wxString &aFieldName) const
Definition footprint.h:237
void SetExcludedFromPosFiles(bool aExclude)
Definition footprint.h:202
wxString GetFieldValue(const wxString &aFieldName) const
Get a field value override for this variant.
Definition footprint.h:209
const std::map< wxString, wxString > & GetFields() const
Definition footprint.h:242
bool GetExcludedFromSim() const
Definition footprint.h:198
bool GetExcludedFromBOM() const
Definition footprint.h:195
void SetExcludedFromSim(bool aExclude)
Definition footprint.h:199
void SetDNP(bool aDNP)
Definition footprint.h:193
bool GetExcludedFromPosFiles() const
Definition footprint.h:201
bool GetDNP() const
Definition footprint.h:192
void SetFieldValue(const wxString &aFieldName, const wxString &aValue)
Set a field value override for this variant.
Definition footprint.h:224
void SetExcludedFromBOM(bool aExclude)
Definition footprint.h:196
const CASE_INSENSITIVE_MAP< FOOTPRINT_VARIANT > & GetVariants() const
Get all variants.
Definition footprint.h:1011
void SetFPID(const LIB_ID &aFPID)
Definition footprint.h:418
bool IsDNP() const
Definition footprint.h:947
bool IsExcludedFromBOM() const
Definition footprint.h:929
void SetDNP(bool aDNP=true)
Definition footprint.h:948
bool GetExcludedFromSimForVariant(const wxString &aVariantName) const
Get the exclude-from-simulation status for a specific variant.
void SetExcludedFromSim(bool aExclude=true)
Definition footprint.h:939
void SetExcludedFromBOM(bool aExclude=true)
Definition footprint.h:930
const std::vector< FP_UNIT_INFO > & GetUnitInfo() const
Definition footprint.h:909
void SetAttributes(int aAttributes)
Definition footprint.h:510
PCB_FIELD & Value()
read/write accessors:
Definition footprint.h:831
void SetExcludedFromPosFiles(bool aExclude=true)
Definition footprint.h:921
const FOOTPRINT_VARIANT * GetVariant(const wxString &aVariantName) const
Get a variant by name.
bool ResolveTextVar(wxString *aToken, int aDepth=0) const
Resolve any references to system tokens supported by the component.
void DeleteVariant(const wxString &aVariantName)
Delete a variant by name.
const LIB_ID & GetFPID() const
Definition footprint.h:417
void SetReference(const wxString &aReference)
Definition footprint.h:799
bool IsExcludedFromPosFiles() const
Definition footprint.h:920
bool IsExcludedFromSim() const
Definition footprint.h:938
void SetValue(const wxString &aValue)
Definition footprint.h:822
void SetUnitInfo(const std::vector< FP_UNIT_INFO > &aUnits)
Definition footprint.h:908
bool GetDNPForVariant(const wxString &aVariantName) const
Get the DNP status for a specific variant.
void SetVariant(const FOOTPRINT_VARIANT &aVariant)
Add or update a variant.
bool GetExcludedFromPosFilesForVariant(const wxString &aVariantName) const
Get the exclude-from-position-files status for a specific variant.
FOOTPRINT_VARIANT * AddVariant(const wxString &aVariantName)
Add a new variant with the given name.
bool GetExcludedFromBOMForVariant(const wxString &aVariantName) const
Get the exclude-from-BOM status for a specific variant.
The parser for reading the KiCad s-expression netlist format.
void Parse()
Function Parse parse the full netlist.
Definition kiid.h:46
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
UTF8 Format() const
Definition lib_id.cpp:132
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
unsigned GetCount() const
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
wxString GetShownText(RESOLUTION_CONTEXT aContext, int aDepth=0) const override
Return the string actually shown after processing of the base text.
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
A PCB_IO derivation for saving and loading Pcbnew s-expression formatted files.
std::unique_ptr< BOARD > LoadBoard(const wxString &aFileName, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr)
Load information from some input file format that this PCB_IO implementation knows about into new BOA...
Definition pcb_io.cpp:72
The ASCII format of the kicad place file is:
std::string GenPositionData()
build a string filled with the position data
void SetVariant(const wxString &aVariant)
Set the variant name for variant-aware export.
std::string GenReportData()
build a string filled with the pad report data This report does not used options aForceSmdItems,...
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true, bool aOverrideLock=false)
Load a project or sets up a new project with a specified path.
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition richio.h:225
Master controller class:
void RegisterTool(TOOL_BASE *aTool)
Add a tool to the manager set and sets it up.
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
wxString wx_str() const
Definition utf8.cpp:41
@ INTERNAL
Definition common.h:96
#define _(s)
@ FP_DNP
Definition footprint.h:92
@ FP_EXCLUDE_FROM_POS_FILES
Definition footprint.h:88
@ FP_EXCLUDE_FROM_BOM
Definition footprint.h:89
@ FP_EXCLUDE_FROM_SIM
Definition footprint.h:93
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:28
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
bool m_hasExcludedFromPosFiles
nlohmann::ordered_map< wxString, wxString > m_fields
std::vector< wxString > m_pins
Definition footprint.h:905
wxString GetDefaultFieldName(FIELD_T aFieldId, TRANSLATION aTranslation)
Return a default symbol field name for a mandatory field type.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ UNTRANSLATED
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_SUITE_END()
std::string netlist
std::string path
VECTOR3I v1(5, 5, 5)
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_AUTO_TEST_CASE(FootprintVariantBasics)
Test FOOTPRINT_VARIANT class basic operations.
VECTOR2I v2(1, 0)