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, 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
29
30#include <boost/test/unit_test.hpp>
31#include <eeschema_test_utils.h>
32
34#include <sch_screen.h>
35#include <sch_sheet.h>
36#include <sch_symbol.h>
37#include <schematic.h>
38#include <kiid.h>
39#include <locale_io.h>
41
42#include <wx/filename.h>
43#include <wx/stdpaths.h>
44#include <wx/textfile.h>
45
46
48{
51 {
52 wxString tempDir = wxStandardPaths::Get().GetTempDir();
53 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + wxT( "test_project.kicad_pro" );
54 m_tempFiles.push_back( projectPath );
55
56 m_settingsManager.LoadProject( projectPath.ToStdString() );
57 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
59 m_schematic->SetProject( m_project );
60 }
61
63 {
64 for( const wxString& file : m_tempFiles )
65 {
66 if( wxFileExists( file ) )
67 wxRemoveFile( file );
68 }
69
70 m_schematic.reset();
71 }
72
73 wxString GetTempSchFile( const wxString& aPrefix )
74 {
75 wxString tempDir = wxStandardPaths::Get().GetTempDir();
76 wxString fileName = wxFileName::CreateTempFileName( tempDir + wxFileName::GetPathSeparator() + aPrefix );
77 fileName += wxT( ".kicad_sch" );
78 m_tempFiles.push_back( fileName );
79 return fileName;
80 }
81
83 std::unique_ptr<SCHEMATIC> m_schematic;
85 std::vector<wxString> m_tempFiles;
86};
87
88
89BOOST_FIXTURE_TEST_SUITE( ProjectNameInstances, PROJECT_NAME_FIXTURE )
90
91
92
96BOOST_AUTO_TEST_CASE( SymbolInstanceProjectName )
97{
99
100 m_schematic->CreateDefaultScreens();
101
102 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
103 BOOST_REQUIRE( !topSheets.empty() );
104
105 SCH_SCREEN* screen = topSheets[0]->GetScreen();
106 BOOST_REQUIRE( screen != nullptr );
107
108 // Create a symbol
109 SCH_SYMBOL* symbol = new SCH_SYMBOL();
110 symbol->SetLibId( LIB_ID( wxT( "Device" ), wxT( "R" ) ) );
111 symbol->SetPosition( VECTOR2I( 0, 0 ) );
112 symbol->GetField( FIELD_T::REFERENCE )->SetText( wxT( "R1" ) );
113 symbol->SetPrefix( wxT( "R" ) );
114 screen->Append( symbol );
115
116 // Build hierarchy and set the reference (this creates an instance
117 // with empty m_ProjectName, simulating the bug condition)
118 m_schematic->RefreshHierarchy();
119 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
120 BOOST_REQUIRE( !hierarchy.empty() );
121
122 SCH_SHEET_PATH& path = hierarchy[0];
123 symbol->SetRef( &path, wxT( "R1" ) );
124
125 // Verify the instance exists but has empty project name (the bug)
127 BOOST_REQUIRE( symbol->GetInstance( inst, path.Path() ) );
128 BOOST_CHECK( inst.m_ProjectName.IsEmpty() );
129
130 // Save the schematic
131 wxString fileName = GetTempSchFile( wxT( "test_projname" ) );
132
134 io.SaveSchematicFile( fileName, topSheets[0], m_schematic.get() );
135 BOOST_REQUIRE( wxFileExists( fileName ) );
136
137 BOOST_TEST_MESSAGE( "Saved file: " + fileName.ToStdString() );
138 BOOST_TEST_MESSAGE( "Project name: " + m_project->GetProjectName().ToStdString() );
139
140 // Read the saved file as text and check for project name
141 wxTextFile textFile;
142 BOOST_REQUIRE( textFile.Open( fileName ) );
143
144 bool foundProjectClause = false;
145 bool foundEmptyProject = false;
146
147 for( size_t i = 0; i < textFile.GetLineCount(); i++ )
148 {
149 wxString line = textFile.GetLine( i );
150
151 if( line.Contains( wxT( "(project \"test_project\"" ) ) )
152 foundProjectClause = true;
153
154 if( line.Contains( wxT( "(project \"\"" ) ) )
155 foundEmptyProject = true;
156 }
157
158 textFile.Close();
159
160 BOOST_CHECK_MESSAGE( foundProjectClause, "Saved file must contain (project \"test_project\")" );
161 BOOST_CHECK_MESSAGE( !foundEmptyProject, "Saved file must NOT contain (project \"\")" );
162}
163
164
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
Container for project specific data.
Definition project.h:65
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:76
void SetLibId(const LIB_ID &aName)
void SetPosition(const VECTOR2I &aPosition) override
Definition sch_symbol.h:868
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:239
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_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695