KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_connectivity_loader.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
21#include <boost/test/data/test_case.hpp>
22#include <qa_utils/file_utils.h>
24
25#include <algorithm>
26#include <map>
27#include <fstream>
28
29#include <bus_alias.h>
30#include <locale_io.h>
31#include <project.h>
33#include <schematic.h>
34#include <sch_screen.h>
35#include <sch_sheet.h>
38
39
40BOOST_DATA_TEST_CASE( DeletedProjectAliasesStayDeletedWhenLegacySheetsReopen,
41 boost::unit_test::data::make( { false, true } ), oldEmptyTable )
42{
43 LOCALE_IO locale;
44 KI_TEST::SCOPED_TEMP_DIR directory( "connectivity-alias-reopen" );
45 const wxString source = KI_TEST::GetEeschemaTestDataDir() + "netlists/hierarchy_aliases/";
46
47 for( const wxString& filename : { wxString( "hierarchy_aliases.kicad_pro" ),
48 wxString( "hierarchy_aliases.kicad_sch" ),
49 wxString( "sub1.kicad_sch" ), wxString( "sub2.kicad_sch" ) } )
50 {
51 BOOST_REQUIRE( wxCopyFile( source + filename, directory.PathStr() + "/" + filename ) );
52 }
53
54 const wxString projectFile = directory.PathStr() + "/hierarchy_aliases.kicad_pro";
55 const wxString schematicFile = directory.PathStr() + "/hierarchy_aliases.kicad_sch";
56
57 if( oldEmptyTable )
58 {
59 // Older writers emitted an empty table even without opening the schematic
60 std::ifstream input( projectFile.ToStdString() );
61 nlohmann::json document = nlohmann::json::parse( input );
62 input.close();
63 document["meta"]["version"] = 3;
64 document["schematic"]["bus_aliases"] = nlohmann::json::object();
65 std::ofstream output( projectFile.ToStdString() );
66 output << document.dump( 2 );
67 output.close();
68 BOOST_REQUIRE( output.good() );
69 }
70
71 std::vector<wxString> retainedMembers;
72 const auto load = [&]( PROJECT& project )
73 {
74 auto schematic = std::make_unique<SCHEMATIC>( &project );
75 schematic->Reset();
76 SCH_SHEET* empty = schematic->GetTopLevelSheet( 0 );
78 SCH_SHEET* root = io.LoadSchematicFile( schematicFile, schematic.get() );
79 schematic->AddTopLevelSheet( root );
80 schematic->RemoveTopLevelSheet( empty );
81 delete empty;
82 BOOST_REQUIRE_MESSAGE( io.GetError().IsEmpty(), io.GetError() );
83 return schematic;
84 };
85
86 {
87 SETTINGS_MANAGER settings;
88 BOOST_REQUIRE( settings.LoadProject( projectFile ) );
89 BOOST_REQUIRE( settings.Prj().GetProjectFile().SaveToFile( directory.PathStr(), true ) );
90 }
91
92 {
93 SETTINGS_MANAGER settings;
94 BOOST_REQUIRE( settings.LoadProject( projectFile ) );
95 // Project migration can write defaults before legacy sheet aliases are imported
96 BOOST_REQUIRE( settings.Prj().GetProjectFile().SaveToFile( directory.PathStr(), true ) );
97 auto schematic = load( settings.Prj() );
98 BOOST_REQUIRE( schematic->GetBusAlias( "ALIAS1" ) );
99 BOOST_REQUIRE( schematic->GetBusAlias( "ALIAS2" ) );
100 schematic->SetBusAliases( {} );
101 BOOST_REQUIRE( settings.Prj().GetProjectFile().SaveToFile( directory.PathStr() ) );
102 }
103
104 {
105 SETTINGS_MANAGER settings;
106 BOOST_REQUIRE( settings.LoadProject( projectFile ) );
107 BOOST_REQUIRE( settings.Prj().GetProjectFile().m_BusAliases.empty() );
108 auto schematic = load( settings.Prj() );
109 BOOST_CHECK( schematic->GetAllBusAliases().empty() );
110 BOOST_CHECK( settings.Prj().GetProjectFile().m_BusAliases.empty() );
111
112 auto sheets = schematic->RootScreen()->Items().OfType( SCH_SHEET_T );
113 BOOST_REQUIRE( sheets.begin() != sheets.end() );
114 std::unique_ptr<SCH_SHEET> appended(
115 static_cast<SCH_SHEET*>( ( *sheets.begin() )->Duplicate( false ) ) );
116 appended->SetScreen( nullptr );
117 appended->SetFileName( source + "sub1.kicad_sch" );
119 BOOST_REQUIRE( io.LoadSchematicFile( appended->GetFileName(), schematic.get(), appended.get() )
120 == appended.get() );
121 BOOST_REQUIRE_MESSAGE( io.GetError().IsEmpty(), io.GetError() );
122 schematic->AddTopLevelSheet( appended.release() );
123 auto imported = schematic->GetBusAlias( "ALIAS2" );
124 BOOST_REQUIRE( imported );
125 BOOST_CHECK( !schematic->GetBusAlias( "ALIAS1" ) );
126 auto changed = imported->Clone();
127 retainedMembers = imported->Members();
128 std::reverse( retainedMembers.begin(), retainedMembers.end() );
129 BOOST_REQUIRE( retainedMembers != imported->Members() );
130 changed->SetMembers( retainedMembers );
131 schematic->SetBusAliases( { changed } );
132 BOOST_REQUIRE( settings.Prj().GetProjectFile().SaveToFile( directory.PathStr(), true ) );
133 }
134
135 {
136 SETTINGS_MANAGER settings;
137 BOOST_REQUIRE( settings.LoadProject( projectFile ) );
138 auto schematic = load( settings.Prj() );
139 BOOST_REQUIRE( schematic->GetBusAlias( "ALIAS2" ) );
140 BOOST_CHECK( schematic->GetBusAlias( "ALIAS2" )->Members() == retainedMembers );
141 BOOST_CHECK( !schematic->GetBusAlias( "ALIAS1" ) );
142
144 const std::map<std::string, UTF8> properties{ { "hierarchical_sheet_load", "" } };
145 std::unique_ptr<SCH_SHEET> imported( io.LoadSchematicFile(
146 source + "hierarchy_aliases.kicad_sch", schematic.get(), nullptr, &properties ) );
147 BOOST_REQUIRE( imported );
148 BOOST_REQUIRE_MESSAGE( io.GetError().IsEmpty(), io.GetError() );
149 schematic->AddTopLevelSheet( imported.release() );
150 BOOST_REQUIRE( schematic->GetBusAlias( "ALIAS1" ) );
151 BOOST_REQUIRE( schematic->GetBusAlias( "ALIAS2" ) );
152 BOOST_CHECK( schematic->GetBusAlias( "ALIAS2" )->Members() != retainedMembers );
153 }
154}
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
std::map< wxString, std::vector< wxString > > m_BusAliases
Bus alias definitions for the schematic project.
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then writes the contents of the JSON document to a file.
Container for project specific data.
Definition project.h:63
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:201
A SCH_IO derivation for loading schematic files using the new s-expression file format.
const wxString & GetError() const override
Return an error string to the caller.
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,...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
static bool empty(const wxTextEntryBase *aCtrl)
std::string source
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
BOOST_DATA_TEST_CASE(DeletedProjectAliasesStayDeletedWhenLegacySheetsReopen, boost::unit_test::data::make({ false, true }), oldEmptyTable)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
@ SCH_SHEET_T
Definition typeinfo.h:171