KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue25223_rule_area_netclass_stale.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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26
27#include <boost/test/unit_test.hpp>
29#include <eeschema_test_utils.h>
30
31#include <wx/filefn.h>
32#include <wx/filename.h>
33#include <wx/stdpaths.h>
34
35#include <fstream>
36#include <string>
37
38#include <nlohmann/json.hpp>
39
40#include <project.h>
43#include <schematic.h>
46
47
48namespace
49{
50
51const std::string TP1_NET = "unconnected-(TP1-Pad1)";
52const std::string TP2_NET = "unconnected-(TP2-Pad1)";
53
54
55class RULE_AREA_NETCLASS_FIXTURE : public KI_TEST::SCHEMATIC_TEST_FIXTURE
56{
57public:
58 RULE_AREA_NETCLASS_FIXTURE()
59 {
60 wxString reserved = wxFileName::CreateTempFileName(
61 wxStandardPaths::Get().GetTempDir() + wxFileName::GetPathSeparator()
62 + wxS( "kicad_qa_25223_" ) );
63
64 BOOST_REQUIRE( !reserved.IsEmpty() );
65 wxRemoveFile( reserved );
66
67 m_tempDir.AssignDir( reserved );
68
69 BOOST_REQUIRE( m_tempDir.Mkdir() );
70
71 // The project gets saved during the test, so run against a copy
72 wxFileName source( KI_TEST::GetEeschemaTestDataDir() );
73 source.AppendDir( wxS( "issue25223" ) );
74
75 for( const std::string& ext : { FILEEXT::ProjectFileExtension,
77 {
78 wxFileName from( source.GetPath(), wxS( "proj" ), ext );
79
80 BOOST_REQUIRE( wxCopyFile( from.GetFullPath(), ProjFile( ext ).GetFullPath() ) );
81 }
82 }
83
84 ~RULE_AREA_NETCLASS_FIXTURE()
85 {
86 m_schematic.reset();
87
88 // Drop the project first so its lock file goes with it rather than after the directory
89 SettingsManager().UnloadProject( &SettingsManager().Prj(), false );
90
91 m_tempDir.Rmdir( wxPATH_RMDIR_RECURSIVE );
92 }
93
94 wxFileName ProjFile( const std::string& aExt ) const
95 {
96 return wxFileName( m_tempDir.GetPath(), wxS( "proj" ), aExt );
97 }
98
99 nlohmann::json ReadNetclassAssignments() const
100 {
101 wxFileName path = ProjFile( FILEEXT::ProjectFileExtension );
102 std::ifstream in( path.GetFullPath().fn_str(), std::ios::binary );
103
104 BOOST_REQUIRE( in );
105
106 nlohmann::json project = nlohmann::json::parse( in );
107
108 BOOST_REQUIRE( project.contains( "net_settings" ) );
109 BOOST_REQUIRE( project["net_settings"].contains( "netclass_assignments" ) );
110
111 return project["net_settings"]["netclass_assignments"];
112 }
113
114 wxFileName m_tempDir;
115};
116
117} // namespace
118
119
120BOOST_FIXTURE_TEST_SUITE( Issue25223RuleAreaNetclass, RULE_AREA_NETCLASS_FIXTURE )
121
122
123BOOST_AUTO_TEST_CASE( SavedProjectDropsStaleAssignment )
124{
125 BOOST_REQUIRE( ReadNetclassAssignments().contains( TP1_NET ) );
126
128
129 NET_SETTINGS& netSettings = *m_schematic->Project().GetProjectFile().m_NetSettings;
130
131 BOOST_REQUIRE( !netSettings.HasNetclassLabelAssignment( TP1_NET ) );
132 BOOST_REQUIRE( netSettings.HasNetclassLabelAssignment( TP2_NET ) );
133
134 SettingsManager().SaveProject();
135
136 nlohmann::json assignments = ReadNetclassAssignments();
137
138 BOOST_CHECK( !assignments.contains( TP1_NET ) );
139 BOOST_CHECK( assignments.contains( TP2_NET ) );
140
141 // Remove the assignment should now clear the existence
142 netSettings.ClearNetclassLabelAssignments();
143 SettingsManager().SaveProject();
144
145 assignments = ReadNetclassAssignments();
146
147 BOOST_CHECK( assignments.is_object() );
148 BOOST_CHECK( assignments.empty() );
149}
150
151
A generic fixture for loading schematics and associated settings for qa tests.
NET_SETTINGS stores various net-related settings in a project context.
void ClearNetclassLabelAssignments()
Clears all net name to netclasses assignments Calling user is responsible for resetting the effective...
bool HasNetclassLabelAssignment(const wxString &netName) const
Determines if a given net name has netclasses assigned.
static const std::string ProjectFileExtension
static const std::string KiCadSchematicFileExtension
PROJECT & Prj()
Definition kicad.cpp:727
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96
static void LoadSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxString &aFileName)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
Definition of file extensions used in Kicad.