KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue24915_job_variant_fields.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
31
32#include <boost/test/unit_test.hpp>
33
35
36#include <eeschema_helpers.h>
37#include <fields_data_model.h>
38#include <locale_io.h>
39#include <sch_reference_list.h>
40#include <sch_sheet_path.h>
41#include <sch_symbol.h>
42#include <schematic.h>
43
44
45namespace
46{
47
48struct ISSUE24915_FIXTURE
49{
50 void LoadFixture()
51 {
52 wxString schPath =
53 wxString::FromUTF8( KI_TEST::GetEeschemaTestDataDir() ) + wxS( "issue24217/issue24217.kicad_sch" );
54
55 m_schematic = EESCHEMA_HELPERS::LoadSchematic( schPath, true, true );
56 BOOST_REQUIRE( m_schematic != nullptr );
57
58 m_schematic->Hierarchy().GetSymbols( m_refs, SYMBOL_FILTER_NON_POWER, false );
59 BOOST_REQUIRE_EQUAL( m_refs.GetCount(), 3u );
60 }
61
62 const SCH_REFERENCE& FindRef( const wxString& aRef ) const
63 {
64 for( size_t i = 0; i < m_refs.GetCount(); ++i )
65 {
66 if( m_refs[i].GetRef() == aRef )
67 return m_refs[i];
68 }
69
70 BOOST_FAIL( "reference not found" );
71 return m_refs[0];
72 }
73
74 wxString ModelValue( FIELDS_EDITOR_GRID_DATA_MODEL& aModel, const wxString& aRef, const wxString& aField ) const
75 {
76 int col = aModel.GetFieldNameCol( aField );
77 BOOST_REQUIRE( col >= 0 );
78
79 DATA_MODEL_ROW group( FindRef( aRef ), GROUP_SINGLETON );
80 return aModel.GetValue( group, col );
81 }
82
83 // The export preview path, generated fields are re-resolved live instead of read
84 // from the data store.
85 wxString ExportValue( FIELDS_EDITOR_GRID_DATA_MODEL& aModel, const wxString& aRef, const wxString& aField ) const
86 {
87 int col = aModel.GetFieldNameCol( aField );
88 BOOST_REQUIRE( col >= 0 );
89
90 DATA_MODEL_ROW group( FindRef( aRef ), GROUP_SINGLETON );
91 return aModel.GetValue( group, col, wxS( ", " ), wxS( "-" ), true, false );
92 }
93
94 LOCALE_IO m_locale;
95 SCHEMATIC* m_schematic = nullptr;
96 SCH_REFERENCE_LIST m_refs;
97};
98
99} // namespace
100
101
102BOOST_FIXTURE_TEST_CASE( JobVariantDrivesFieldsDataStore, ISSUE24915_FIXTURE )
103{
104 LoadFixture();
105
106 // The failing scenario from the issue: the schematic sits on the default variant
107 // while the reopened job carries its own saved variant.
108 m_schematic->SetCurrentVariant( wxEmptyString );
109
110 FIELDS_EDITOR_GRID_DATA_MODEL jobModel( m_refs, nullptr );
111 jobModel.SetCurrentVariant( wxS( "H0" ) );
112 jobModel.AddColumn( wxS( "Value" ), wxS( "Value" ), false );
113 jobModel.AddColumn( wxS( "${DNP}" ), wxS( "DNP" ), false );
114
115 BOOST_CHECK_EQUAL( ModelValue( jobModel, wxS( "R1" ), wxS( "Value" ) ), wxS( "10k" ) );
116 BOOST_CHECK_EQUAL( ModelValue( jobModel, wxS( "R3" ), wxS( "Value" ) ), wxS( "2M" ) );
117 BOOST_CHECK_EQUAL( ModelValue( jobModel, wxS( "R2" ), wxS( "${DNP}" ) ), wxS( "1" ) );
118 BOOST_CHECK_EQUAL( ModelValue( jobModel, wxS( "R1" ), wxS( "${DNP}" ) ), wxS( "0" ) );
119
120 // The export path must follow the model's variant too, not the schematic's.
121 BOOST_CHECK_EQUAL( ExportValue( jobModel, wxS( "R2" ), wxS( "${DNP}" ) ), wxS( "DNP" ) );
122 BOOST_CHECK_EQUAL( ExportValue( jobModel, wxS( "R1" ), wxS( "${DNP}" ) ), wxEmptyString );
123 BOOST_CHECK_EQUAL( ExportValue( jobModel, wxS( "R1" ), wxS( "Value" ) ), wxS( "10k" ) );
124}
125
126
127BOOST_FIXTURE_TEST_CASE( SchematicVariantDoesNotLeakIntoDataStore, ISSUE24915_FIXTURE )
128{
129 LoadFixture();
130
131 // Control for the same contract from the other side: with H0 active on the
132 // schematic, a store populated for the default variant must show base values.
133 m_schematic->SetCurrentVariant( wxS( "H0" ) );
134
135 FIELDS_EDITOR_GRID_DATA_MODEL baseModel( m_refs, nullptr );
136 baseModel.AddColumn( wxS( "Value" ), wxS( "Value" ), false );
137 baseModel.AddColumn( wxS( "${DNP}" ), wxS( "DNP" ), false );
138
139 BOOST_CHECK_EQUAL( ModelValue( baseModel, wxS( "R1" ), wxS( "Value" ) ), wxS( "6k2" ) );
140 BOOST_CHECK_EQUAL( ModelValue( baseModel, wxS( "R3" ), wxS( "Value" ) ), wxS( "1M" ) );
141 BOOST_CHECK_EQUAL( ModelValue( baseModel, wxS( "R2" ), wxS( "${DNP}" ) ), wxS( "0" ) );
142}
static SCHEMATIC * LoadSchematic(const wxString &aFileName, bool aSetActive, bool aForceDefaultProject, PROJECT *aProject=nullptr, bool aCalculateConnectivity=true)
int GetFieldNameCol(const wxString &aFieldName) const
void AddColumn(const wxString &aFieldName, const wxString &aLabel, bool aAddedByUser)
wxString GetValue(int aRow, int aCol) override
void SetCurrentVariant(const wxString &aVariantName)
Set the current variant name for highlighting purposes.
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ SYMBOL_FILTER_NON_POWER
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_FIXTURE_TEST_CASE(JobVariantDrivesFieldsDataStore, ISSUE24915_FIXTURE)
BOOST_CHECK_EQUAL(result, "25.4")
GROUP_SINGLETON
Definition wx_grid.h:41