KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_project_name_instances.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
26#include <boost/test/unit_test.hpp>
27#include <eeschema_test_utils.h>
28
30#include <sch_screen.h>
31#include <sch_sheet.h>
32#include <sch_symbol.h>
33#include <schematic.h>
34#include <kiid.h>
35#include <locale_io.h>
37
38#include <wx/filename.h>
39#include <wx/stdpaths.h>
40#include <wx/textfile.h>
41
42
44{
47 {
48 wxString tempDir = wxStandardPaths::Get().GetTempDir();
49 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + wxT( "test_project.kicad_pro" );
50 m_tempFiles.push_back( projectPath );
51
52 m_settingsManager.LoadProject( projectPath.ToStdString() );
53 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
55 m_schematic->SetProject( m_project );
56 }
57
59 {
60 for( const wxString& file : m_tempFiles )
61 {
62 if( wxFileExists( file ) )
63 wxRemoveFile( file );
64 }
65
66 m_schematic.reset();
67 }
68
69 wxString GetTempSchFile( const wxString& aPrefix )
70 {
71 wxString tempDir = wxStandardPaths::Get().GetTempDir();
72 wxString fileName = wxFileName::CreateTempFileName( tempDir + wxFileName::GetPathSeparator() + aPrefix );
73 fileName += wxT( ".kicad_sch" );
74 m_tempFiles.push_back( fileName );
75 return fileName;
76 }
77
79 std::unique_ptr<SCHEMATIC> m_schematic;
81 std::vector<wxString> m_tempFiles;
82};
83
84
85BOOST_FIXTURE_TEST_SUITE( ProjectNameInstances, PROJECT_NAME_FIXTURE )
86
87
88
92BOOST_AUTO_TEST_CASE( SymbolInstanceProjectName )
93{
95
96 m_schematic->CreateDefaultScreens();
97
98 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
99 BOOST_REQUIRE( !topSheets.empty() );
100
101 SCH_SCREEN* screen = topSheets[0]->GetScreen();
102 BOOST_REQUIRE( screen != nullptr );
103
104 // Create a symbol
105 SCH_SYMBOL* symbol = new SCH_SYMBOL();
106 symbol->SetLibId( LIB_ID( wxT( "Device" ), wxT( "R" ) ) );
107 symbol->SetPosition( VECTOR2I( 0, 0 ) );
108 symbol->GetField( FIELD_T::REFERENCE )->SetText( wxT( "R1" ) );
109 symbol->SetPrefix( wxT( "R" ) );
110 screen->Append( symbol );
111
112 // Build hierarchy and set the reference (this creates an instance
113 // with empty m_ProjectName, simulating the bug condition)
114 m_schematic->RefreshHierarchy();
115 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
116 BOOST_REQUIRE( !hierarchy.empty() );
117
118 SCH_SHEET_PATH& path = hierarchy[0];
119 symbol->SetRef( &path, wxT( "R1" ) );
120
121 // Verify the instance exists but has empty project name (the bug)
123 BOOST_REQUIRE( symbol->GetInstance( inst, path.Path() ) );
124 BOOST_CHECK( inst.m_ProjectName.IsEmpty() );
125
126 // Save the schematic
127 wxString fileName = GetTempSchFile( wxT( "test_projname" ) );
128
130 io.SaveSchematicFile( fileName, topSheets[0], m_schematic.get() );
131 BOOST_REQUIRE( wxFileExists( fileName ) );
132
133 BOOST_TEST_MESSAGE( "Saved file: " + fileName.ToStdString() );
134 BOOST_TEST_MESSAGE( "Project name: " + m_project->GetProjectName().ToStdString() );
135
136 // Read the saved file as text and check for project name
137 wxTextFile textFile;
138 BOOST_REQUIRE( textFile.Open( fileName ) );
139
140 bool foundProjectClause = false;
141 bool foundEmptyProject = false;
142
143 for( size_t i = 0; i < textFile.GetLineCount(); i++ )
144 {
145 wxString line = textFile.GetLine( i );
146
147 if( line.Contains( wxT( "(project \"test_project\"" ) ) )
148 foundProjectClause = true;
149
150 if( line.Contains( wxT( "(project \"\"" ) ) )
151 foundEmptyProject = true;
152 }
153
154 textFile.Close();
155
156 BOOST_CHECK_MESSAGE( foundProjectClause, "Saved file must contain (project \"test_project\")" );
157 BOOST_CHECK_MESSAGE( !foundEmptyProject, "Saved file must NOT contain (project \"\")" );
158}
159
160
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
Container for project specific data.
Definition project.h:62
void SetText(const wxString &aText) override
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,...
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
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...
Schematic symbol object.
Definition sch_symbol.h:69
void SetLibId(const LIB_ID &aName)
void SetPosition(const VECTOR2I &aPosition) override
Definition sch_symbol.h:886
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
bool GetInstance(SCH_SYMBOL_INSTANCE &aInstance, const KIID_PATH &aSheetPath, bool aTestFromEnd=false) const
void SetPrefix(const wxString &aPrefix)
Definition sch_symbol.h:232
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
std::vector< FAB_LAYER_COLOR > dummy
std::vector< wxString > m_tempFiles
wxString GetTempSchFile(const wxString &aPrefix)
std::unique_ptr< SCHEMATIC > m_schematic
A simple container for schematic symbol instance information.
@ REFERENCE
Field Reference of part, i.e. "IC21".
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683