KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue24858_variant_persistence.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 along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
31
33
34#include <memory>
35
36#include <lib_symbol.h>
37#include <sch_pin.h>
38#include <sch_symbol.h>
39#include <sch_screen.h>
40#include <sch_sheet.h>
41#include <sch_sheet_path.h>
42#include <schematic.h>
45#include <locale_io.h>
46
47#include <wx/file.h>
48#include <wx/filename.h>
49#include <wx/stdpaths.h>
50
51
53{
55 {
56 wxString tempDir = wxStandardPaths::Get().GetTempDir();
57 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + wxT( "test_issue24858.kicad_pro" );
58 m_tempFiles.push_back( projectPath );
59
60 m_settingsManager.LoadProject( projectPath.ToStdString() );
61 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
62 m_schematic->SetProject( &m_settingsManager.Prj() );
63 }
64
66 {
67 for( const wxString& file : m_tempFiles )
68 {
69 if( wxFileExists( file ) )
70 wxRemoveFile( file );
71 }
72
73 m_schematic.reset();
74 }
75
76 wxString tempFile()
77 {
78 wxString dir = wxStandardPaths::Get().GetTempDir();
79 wxString name = wxFileName::CreateTempFileName( dir + wxFileName::GetPathSeparator() + "issue24858" );
80 name += ".kicad_sch";
81 m_tempFiles.push_back( name );
82 return name;
83 }
84
87 {
88 m_schematic->CreateDefaultScreens();
89
90 m_sheet = m_schematic->GetTopLevelSheets()[0];
91 SCH_SCREEN* screen = m_sheet->GetScreen();
92 screen->SetFileName( "issue24858.kicad_sch" );
93
94 m_path.clear();
95 m_path.push_back( m_sheet );
96
97 m_libSym = std::make_unique<LIB_SYMBOL>( "R", nullptr );
98
99 SCH_PIN* pin = new SCH_PIN( m_libSym.get() );
100 pin->SetNumber( "1" );
102 pin->SetPosition( VECTOR2I( -508000, 0 ) );
103 m_libSym->AddDrawItem( pin );
104
105 m_symbol = new SCH_SYMBOL( *m_libSym, m_libSym->GetLibId(), &m_path, 0, 0, VECTOR2I( 15621000, 6223000 ) );
106 m_symbol->UpdatePins();
107
108 screen->Append( m_symbol );
109 }
110
111 wxString saveAndRead()
112 {
113 wxString fileName = tempFile();
114
116 io.SaveSchematicFile( fileName, m_sheet, m_schematic.get() );
117
118 wxFile f( fileName );
119 wxString contents;
120 f.ReadAll( &contents );
121
122 m_savedFileName = fileName;
123
124 return contents;
125 }
126
128 {
130
131 m_schematic->Reset();
132 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
134 m_schematic->AddTopLevelSheet( loaded );
135 m_schematic->RemoveTopLevelSheet( defaultSheet );
136 delete defaultSheet;
137
138 m_sheet = loaded;
139
140 SCH_SYMBOL* found = nullptr;
141
142 for( SCH_ITEM* item : loaded->GetScreen()->Items().OfType( SCH_SYMBOL_T ) )
143 found = static_cast<SCH_SYMBOL*>( item );
144
145 return found;
146 }
147
149 std::unique_ptr<SCHEMATIC> m_schematic;
150 std::unique_ptr<LIB_SYMBOL> m_libSym;
151 SCH_SHEET* m_sheet = nullptr;
155 std::vector<wxString> m_tempFiles;
156};
157
158
159BOOST_FIXTURE_TEST_SUITE( Issue24858VariantPersistence, ISSUE24858_FIXTURE )
160
161
162
167BOOST_AUTO_TEST_CASE( BaseValueSetCreatesNoEntry )
168{
169 placeSymbol();
170
171 wxString variantName = wxS( "Ghost" );
172
173 m_symbol->SetDNP( m_symbol->GetDNP(), &m_path, variantName );
174 m_symbol->SetExcludedFromSim( m_symbol->GetExcludedFromSim(), &m_path, variantName );
175 m_symbol->SetExcludedFromBOM( m_symbol->GetExcludedFromBOM(), &m_path, variantName );
176 m_symbol->SetExcludedFromPosFiles( m_symbol->GetExcludedFromPosFiles(), &m_path, variantName );
177
178 BOOST_CHECK( !m_symbol->GetVariant( m_path, variantName ).has_value() );
179}
180
181
187BOOST_AUTO_TEST_CASE( DiffLessEntryNotSerialized )
188{
189 placeSymbol();
190
191 SCH_SYMBOL_VARIANT ghost( wxS( "Ghost" ) );
192 ghost.InitializeAttributes( *m_symbol );
193 m_symbol->AddVariant( m_path, ghost );
194
195 wxString contents = saveAndRead();
196
197 BOOST_CHECK( !contents.Contains( wxS( "(variant" ) ) );
198}
199
200
204BOOST_AUTO_TEST_CASE( RealEntryStillSerialized )
205{
206 placeSymbol();
207
208 SCH_SYMBOL_VARIANT loaded( wxS( "Loaded" ) );
209 loaded.InitializeAttributes( *m_symbol );
210 loaded.m_DNP = !m_symbol->GetDNP();
211 m_symbol->AddVariant( m_path, loaded );
212
213 wxString contents = saveAndRead();
214
215 BOOST_CHECK( contents.Contains( wxS( "(variant" ) ) );
216 BOOST_CHECK( contents.Contains( wxS( "(name \"Loaded\")" ) ) );
217 BOOST_CHECK( contents.Contains( wxS( "dnp" ) ) );
218}
219
220
226BOOST_AUTO_TEST_CASE( DeletedVariantStaysDeleted )
227{
228 placeSymbol();
229
230 wxString variantName = wxS( "Ghost" );
231
232 // Real override, then the user deletes the variant.
233 m_symbol->SetDNP( !m_symbol->GetDNP(), &m_path, variantName );
234 BOOST_REQUIRE( m_symbol->GetVariant( m_path, variantName ).has_value() );
235
236 m_symbol->DeleteVariant( m_path, variantName );
237 BOOST_REQUIRE( !m_symbol->GetVariant( m_path, variantName ).has_value() );
238
239 // A later apply pass writes back the resolved value, which is now the base value.
240 m_symbol->SetDNP( m_symbol->GetDNP( &m_path, variantName ), &m_path, variantName );
241
242 saveAndRead();
243
244 SCH_SYMBOL* reloaded = reload();
245 BOOST_REQUIRE( reloaded );
246
247 for( const SCH_SYMBOL_INSTANCE& instance : reloaded->GetInstances() )
248 BOOST_CHECK( !instance.m_Variants.contains( variantName ) );
249
250 std::set<wxString> variantNames = m_sheet->GetScreen()->GetVariantNames();
251 BOOST_CHECK( !variantNames.contains( variantName ) );
252}
253
254
const char * name
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:221
A SCH_IO derivation for loading schematic files using the new s-expression file format.
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aSheet, SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_IO implementation knows about,...
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:115
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:139
Variant information for a schematic symbol.
void InitializeAttributes(const SCH_SYMBOL &aSymbol)
Schematic symbol object.
Definition sch_symbol.h:69
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:128
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:39
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
Regression tests for issue 24858, deleted design variants reappear after reload.
std::unique_ptr< SCHEMATIC > m_schematic
std::unique_ptr< LIB_SYMBOL > m_libSym
void placeSymbol()
Place a one-pin symbol on a fresh top sheet and set m_sheet, m_path and m_symbol.
A simple container for schematic symbol instance information.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(BaseValueSetCreatesNoEntry)
Setting a variant attribute to the value it already resolves to must not create an instance variant e...
KIBIS_PIN * pin
@ SCH_SYMBOL_T
Definition typeinfo.h:169
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683