KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_variant_field_resolution.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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25#include "eeschema_test_utils.h"
26
27#include <sch_symbol.h>
28#include <sch_sheet.h>
29#include <sch_sheet_path.h>
30#include <lib_id.h>
32
33
35{
36public:
38 {
39 wxFileName fn( KI_TEST::GetEeschemaTestDataDir() );
40 fn.AppendDir( wxS( "variant_field_resolution" ) );
41 fn.SetName( wxS( "variant_field_resolution" ) );
43
44 LoadSchematic( fn.GetFullPath() );
45 }
46
48 {
49 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
50 SCH_SCREEN* screen = m_schematic->RootScreen();
51
52 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
53 {
54 SCH_SYMBOL* sym = static_cast<SCH_SYMBOL*>( item );
55
56 if( sym->GetRef( &hierarchy[0] ) == wxS( "R1" ) )
57 return sym;
58 }
59
60 return nullptr;
61 }
62
64 {
65 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
66 KIID_PATH path = hierarchy[0].Path();
67
68 auto& instances = const_cast<std::vector<SCH_SYMBOL_INSTANCE>&>(
69 aSymbol->GetInstances() );
70
71 for( SCH_SYMBOL_INSTANCE& inst : instances )
72 {
73 if( inst.m_Path == path )
74 return &inst;
75 }
76
77 return nullptr;
78 }
79
80 void SetupVariantWithSymbolOverride( SCH_SYMBOL* aSymbol, const wxString& aVariantName,
81 const wxString& aLibIdStr )
82 {
83 SCH_SYMBOL_INSTANCE* inst = GetMutableInstance( aSymbol );
84 BOOST_REQUIRE( inst );
85
86 SCH_SYMBOL_VARIANT& variant = inst->m_Variants[aVariantName];
87 LIB_ID libId;
88 BOOST_REQUIRE( libId.Parse( aLibIdStr ) == -1 );
89 variant.m_SymbolOverride = libId;
90 }
91
92 void SetVariantField( SCH_SYMBOL* aSymbol, const wxString& aVariantName,
93 const wxString& aFieldName, const wxString& aValue )
94 {
95 SCH_SYMBOL_INSTANCE* inst = GetMutableInstance( aSymbol );
96 BOOST_REQUIRE( inst );
97
98 inst->m_Variants[aVariantName].m_Fields[aFieldName] = aValue;
99 }
100
101 void ClearVariants( SCH_SYMBOL* aSymbol )
102 {
103 SCH_SYMBOL_INSTANCE* inst = GetMutableInstance( aSymbol );
104
105 if( inst )
106 inst->m_Variants.clear();
107
108 aSymbol->ClearCaches();
109 }
110};
111
112
114{
115 LoadTestSchematic();
116
117 SCH_SYMBOL* r1 = GetR1();
118 BOOST_REQUIRE( r1 );
119
120 // No variant returns base value
121 {
122 wxString value =
123 r1->GetFieldText( wxS( "Value" ), &m_schematic->Hierarchy()[0], wxEmptyString );
124 BOOST_CHECK_EQUAL( value, wxS( "100R" ) );
125 }
126
127 // Explicit variant field override takes precedence over the base value
128 {
129 ClearVariants( r1 );
130 SetVariantField( r1, wxS( "ExplicitOnly" ), wxS( "Value" ), wxS( "220R" ) );
131
132 wxString value = r1->GetFieldText( wxS( "Value" ), &m_schematic->Hierarchy()[0],
133 wxS( "ExplicitOnly" ) );
134 BOOST_CHECK_EQUAL( value, wxS( "220R" ) );
135 }
136
137 // Explicit override on multiple fields simultaneously
138 {
139 ClearVariants( r1 );
140 SetVariantField( r1, wxS( "Multi" ), wxS( "Value" ), wxS( "470R" ) );
141 SetVariantField( r1, wxS( "Multi" ), wxS( "MPN" ), wxS( "CUSTOM-MPN-123" ) );
142
143 wxString value = r1->GetFieldText( wxS( "Value" ), &m_schematic->Hierarchy()[0],
144 wxS( "Multi" ) );
145 wxString mpn =
146 r1->GetFieldText( wxS( "MPN" ), &m_schematic->Hierarchy()[0], wxS( "Multi" ) );
147
148 BOOST_CHECK_EQUAL( value, wxS( "470R" ) );
149 BOOST_CHECK_EQUAL( mpn, wxS( "CUSTOM-MPN-123" ) );
150 }
151
152 // Reference is always resolved from the base instance
153 {
154 ClearVariants( r1 );
155 SetVariantField( r1, wxS( "RefTest" ), wxS( "Value" ), wxS( "999R" ) );
156
157 wxString ref = r1->GetFieldText( wxS( "Reference" ), &m_schematic->Hierarchy()[0],
158 wxS( "RefTest" ) );
159 BOOST_CHECK_EQUAL( ref, wxS( "R1" ) );
160 }
161
162 // An unresolvable symbol override with no explicit field override falls back to the base
163 // value.
164 {
165 ClearVariants( r1 );
166 SetupVariantWithSymbolOverride( r1, wxS( "BadAlt" ),
167 wxS( "nonexistent_lib:no_such_symbol" ) );
168
169 wxString value = r1->GetFieldText( wxS( "Value" ), &m_schematic->Hierarchy()[0],
170 wxS( "BadAlt" ) );
171 BOOST_CHECK_EQUAL( value, wxS( "100R" ) );
172 }
173
174 // Non-existent variant name falls back to base
175 {
176 ClearVariants( r1 );
177
178 wxString value = r1->GetFieldText( wxS( "Value" ), &m_schematic->Hierarchy()[0],
179 wxS( "NoSuchVariant" ) );
180 BOOST_CHECK_EQUAL( value, wxS( "100R" ) );
181 }
182
183 // Footprint explicit override
184 {
185 ClearVariants( r1 );
186 SetVariantField( r1, wxS( "FPTest" ), wxS( "Footprint" ),
187 wxS( "Resistor_SMD:R_0805_2012Metric" ) );
188
189 wxString fp = r1->GetFieldText( wxS( "Footprint" ), &m_schematic->Hierarchy()[0],
190 wxS( "FPTest" ) );
191 BOOST_CHECK_EQUAL( fp, wxS( "Resistor_SMD:R_0805_2012Metric" ) );
192 }
193
194 // Footprint without variant override returns base
195 {
196 ClearVariants( r1 );
197 SetVariantField( r1, wxS( "NoFP" ), wxS( "Value" ), wxS( "330R" ) );
198
199 wxString fp = r1->GetFieldText( wxS( "Footprint" ), &m_schematic->Hierarchy()[0],
200 wxS( "NoFP" ) );
201 BOOST_CHECK_EQUAL( fp, wxS( "Resistor_SMD:R_0402_1005Metric" ) );
202 }
203
204 // Multiple variants resolve independently
205 {
206 ClearVariants( r1 );
207 SetVariantField( r1, wxS( "VarA" ), wxS( "Value" ), wxS( "1K" ) );
208 SetVariantField( r1, wxS( "VarB" ), wxS( "Value" ), wxS( "10K" ) );
209
210 wxString valueA =
211 r1->GetFieldText( wxS( "Value" ), &m_schematic->Hierarchy()[0], wxS( "VarA" ) );
212 wxString valueB =
213 r1->GetFieldText( wxS( "Value" ), &m_schematic->Hierarchy()[0], wxS( "VarB" ) );
214
215 BOOST_CHECK_EQUAL( valueA, wxS( "1K" ) );
216 BOOST_CHECK_EQUAL( valueB, wxS( "10K" ) );
217 }
218
219 // Clearing variants and re-adding must not resolve through a stale cache
220 {
221 ClearVariants( r1 );
222 SetVariantField( r1, wxS( "CacheA" ), wxS( "Value" ), wxS( "FIRST" ) );
223
224 wxString value1 = r1->GetFieldText( wxS( "Value" ), &m_schematic->Hierarchy()[0],
225 wxS( "CacheA" ) );
226 BOOST_CHECK_EQUAL( value1, wxS( "FIRST" ) );
227
228 ClearVariants( r1 );
229 SetVariantField( r1, wxS( "CacheA" ), wxS( "Value" ), wxS( "SECOND" ) );
230
231 wxString value2 = r1->GetFieldText( wxS( "Value" ), &m_schematic->Hierarchy()[0],
232 wxS( "CacheA" ) );
233 BOOST_CHECK_EQUAL( value2, wxS( "SECOND" ) );
234 }
235}
236
237
239{
240 LoadTestSchematic();
241
242 SCH_SYMBOL* r1 = GetR1();
243 BOOST_REQUIRE( r1 );
244
245 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
246 SCH_SHEET_PATH& sheet = hierarchy[0];
247
248 // Writing back the resolved value must not materialize an explicit override
249 {
250 ClearVariants( r1 );
251 SetupVariantWithSymbolOverride( r1, wxS( "RoundTrip" ), wxS( "variant_test_lib:R_10K" ) );
252
253 wxString resolved = r1->GetFieldText( wxS( "Value" ), &sheet, wxS( "RoundTrip" ) );
254 r1->SetFieldText( wxS( "Value" ), resolved, &sheet, wxS( "RoundTrip" ) );
255
256 std::optional<SCH_SYMBOL_VARIANT> variant = r1->GetVariant( sheet, wxS( "RoundTrip" ) );
257 BOOST_REQUIRE( variant.has_value() );
258 BOOST_CHECK( !variant->m_Fields.contains( wxS( "Value" ) ) );
259 }
260
261 // A real edit creates an override and reverting the edit erases it again
262 {
263 r1->SetFieldText( wxS( "Value" ), wxS( "EDITED" ), &sheet, wxS( "RoundTrip" ) );
264
265 std::optional<SCH_SYMBOL_VARIANT> variant = r1->GetVariant( sheet, wxS( "RoundTrip" ) );
266 BOOST_REQUIRE( variant.has_value() );
267 BOOST_CHECK( variant->m_Fields.contains( wxS( "Value" ) ) );
268
269 wxString baseValue = r1->GetFieldText( wxS( "Value" ), &sheet, wxEmptyString );
270 r1->SetFieldText( wxS( "Value" ), baseValue, &sheet, wxS( "RoundTrip" ) );
271
272 variant = r1->GetVariant( sheet, wxS( "RoundTrip" ) );
273 BOOST_REQUIRE( variant.has_value() );
274 BOOST_CHECK( !variant->m_Fields.contains( wxS( "Value" ) ) );
275 }
276}
277
278
280{
281 LoadTestSchematic();
282
283 SCH_SYMBOL* r1 = GetR1();
284 BOOST_REQUIRE( r1 );
285
286 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
287 SCH_SHEET_PATH& sheet = hierarchy[0];
288
289 BOOST_REQUIRE( !r1->GetField( FIELD_T::VALUE )->GetText().Contains( wxS( "${" ) ) );
290
291 ClearVariants( r1 );
292 SetVariantField( r1, wxS( "TextVars" ), wxS( "Value" ), wxS( "${MPN}-ALT" ) );
293 SetVariantField( r1, wxS( "TextVars" ), wxS( "Description" ), wxS( "${MPN}-DESC" ) );
294
295 const wxString mpn = r1->GetFieldText( wxS( "MPN" ), &sheet, wxEmptyString );
296 SCH_FIELD* description = r1->GetField( wxS( "Description" ) );
297
298 BOOST_REQUIRE( !mpn.IsEmpty() );
299 BOOST_REQUIRE( description );
300
301 BOOST_CHECK_EQUAL( r1->GetValue( &sheet, INTERNAL, wxS( "TextVars" ) ), mpn + wxS( "-ALT" ) );
302 BOOST_CHECK_EQUAL( description->GetShownText( &sheet, INTERNAL, wxS( "TextVars" ) ), mpn + wxS( "-DESC" ) );
303
304 m_schematic->AddVariant( wxS( "TextVars" ) );
305 SetVariantField( r1, wxS( "TextVars" ), wxS( "MPN" ), wxS( "REQUESTED-MPN" ) );
306 r1->GetField( FIELD_T::VALUE )->SetText( wxS( "${MPN}-BASE" ) );
307 description->SetText( wxS( "${MPN}-BASE-DESC" ) );
308
309 for( const wxString& active : { wxString(), wxString( "TextVars" ) } )
310 {
311 m_schematic->SetCurrentVariant( active );
312
313 for( const wxString& requested : { wxString(), wxString( "TextVars" ) } )
314 {
315 BOOST_TEST_CONTEXT( "active=" << active << ", requested=" << requested )
316 {
317 const wxString expectedMpn = requested.empty() ? mpn : wxString( "REQUESTED-MPN" );
318 const wxString expectedValue = expectedMpn + ( requested.empty() ? "-BASE" : "-ALT" );
319 const wxString expectedDescription = expectedMpn + ( requested.empty() ? "-BASE-DESC" : "-DESC" );
320 BOOST_CHECK_EQUAL( r1->GetField( FIELD_T::VALUE )->GetShownText( &sheet, INTERNAL, requested ),
321 expectedValue );
322 BOOST_CHECK_EQUAL( description->GetShownText( &sheet, INTERNAL, requested ), expectedDescription );
323 BOOST_CHECK_EQUAL( r1->GetValue( &sheet, INTERNAL, requested ), expectedValue );
324 }
325 }
326 }
327}
328
329
330BOOST_FIXTURE_TEST_CASE( SheetFieldVariantResolvesInsideSubsheet, KI_TEST::SCHEMATIC_TEST_FIXTURE )
331{
332 LoadSchematic( wxFileName( KI_TEST::GetEeschemaTestDataDir(), wxS( "issue13212" ),
334
335 SCH_SHEET_PATH childPath = m_schematic->Hierarchy()[1];
336 SCH_SHEET_PATH parentPath = childPath;
337 SCH_SHEET* sheet = childPath.Last();
338
339 parentPath.pop_back();
340 sheet->AddField( SCH_FIELD( sheet, FIELD_T::SHEET_USER, wxS( "Owner" ) ) )->SetText( wxS( "BASE" ) );
341
342 m_schematic->AddVariant( wxS( "Alt" ) );
343 sheet->SetFieldText( wxS( "Owner" ), wxS( "ALT" ), &parentPath, wxS( "Alt" ) );
344 m_schematic->SetCurrentVariant( wxS( "Alt" ) );
345
346 wxString token = wxS( "Owner" );
347 BOOST_REQUIRE( sheet->ResolveTextVar( &childPath, &token, 0 ) );
348 BOOST_CHECK_EQUAL( token, wxS( "ALT" ) );
349}
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
A generic fixture for loading schematics and associated settings for qa tests.
std::unique_ptr< SCHEMATIC > m_schematic
virtual void LoadSchematic(const wxFileName &aFn)
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
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString, int aDepth=0) const
void SetText(const wxString &aText) override
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
SCH_SHEET * Last() const
Return a pointer to the last SCH_SHEET of the list.
void pop_back()
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:50
void SetFieldText(SCH_FIELD *aField, const wxString &aFieldName, const wxString &aFieldText, const SCH_SHEET_PATH *aPath, const wxString &aVariantName)
SCH_FIELD * AddField(const SCH_FIELD &aField)
Add aFieldName to the list of fields.
bool ResolveTextVar(const SCH_SHEET_PATH *aPath, wxString *token, int aDepth=0) const
Resolve any references to system tokens supported by the sheet.
Variant information for a schematic symbol.
std::optional< LIB_ID > m_SymbolOverride
Alternate library symbol to substitute for the base symbol in this variant, if any.
Schematic symbol object.
Definition sch_symbol.h:76
void SetFieldText(SCH_FIELD *aField, const wxString &aFieldName, const wxString &aFieldText, const SCH_SHEET_PATH *aPath, const wxString &aVariantName)
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:136
void ClearCaches() override
std::optional< SCH_SYMBOL_VARIANT > GetVariant(const SCH_SHEET_PATH &aInstance, const wxString &aVariantName) const
const wxString GetValue(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const override
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
wxString GetFieldText(const SCH_FIELD *aField, const wxString &aFieldName, const SCH_SHEET_PATH *aPath, const wxString &aVariantName) const
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
void SetVariantField(SCH_SYMBOL *aSymbol, const wxString &aVariantName, const wxString &aFieldName, const wxString &aValue)
void SetupVariantWithSymbolOverride(SCH_SYMBOL *aSymbol, const wxString &aVariantName, const wxString &aLibIdStr)
SCH_SYMBOL_INSTANCE * GetMutableInstance(SCH_SYMBOL *aSymbol)
@ INTERNAL
Definition common.h:96
static const std::string KiCadSchematicFileExtension
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
static void LoadSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxString &aFileName, REPORTER *aReporter)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
A simple container for schematic symbol instance information.
std::map< wxString, SCH_SYMBOL_VARIANT > m_Variants
A list of symbol variants.
@ VALUE
Field Value of part, i.e. "3.3K".
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
std::string path
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_FIXTURE_TEST_CASE(VariantFieldResolution, TEST_VARIANT_FIELD_RESOLUTION_FIXTURE)
@ SCH_SYMBOL_T
Definition typeinfo.h:168
Definition of file extensions used in Kicad.