KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue25209_derived_body_styles.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
24
26
27#include <lib_symbol.h>
28#include <sch_io/sch_io_mgr.h>
30
31#include <wx/filename.h>
32
33
35{
36public:
38 {
39 m_tempDir = wxFileName::CreateTempFileName( wxS( "kicad_test_" ) );
40 wxRemoveFile( m_tempDir );
41 wxFileName::Mkdir( m_tempDir );
42
43 m_libPath = wxFileName( m_tempDir, wxS( "test_lib.kicad_sym" ) ).GetFullPath();
44 }
45
47 {
48 if( wxFileName::DirExists( m_tempDir ) )
49 wxFileName::Rmdir( m_tempDir, wxPATH_RMDIR_RECURSIVE );
50 }
51
52 const wxString& GetLibPath() const { return m_libPath; }
53
54private:
55 wxString m_tempDir;
56 wxString m_libPath;
57};
58
59
60BOOST_FIXTURE_TEST_SUITE( DerivedSymbolBodyStyles, DERIVED_BODY_STYLES_FIXTURE )
61
62
63
69BOOST_AUTO_TEST_CASE( BodyStylesResolveThroughParent )
70{
71 LIB_SYMBOL root( wxS( "Root" ) );
72 LIB_SYMBOL derived( wxS( "Derived" ) );
73
74 derived.SetParent( &root );
75
77
78 // What the dialog used to do to a derived symbol
79 derived.SetHasDeMorganBodyStyles( true );
80
81 BOOST_CHECK( !derived.HasDeMorganBodyStyles() );
83
84 derived.SetBodyStyleNames( { wxS( "Bogus1" ), wxS( "Bogus2" ), wxS( "Bogus3" ) } );
85
87
88 // The root gains De Morgan styles, and the derived symbol follows it
89 root.SetBodyStyleCount( 2, false, true );
90 root.SetHasDeMorganBodyStyles( true );
91
92 BOOST_CHECK( derived.HasDeMorganBodyStyles() );
93 BOOST_CHECK( derived.IsMultiBodyStyle() );
95
96 // Custom body styles resolve the same way, names included
97 root.SetHasDeMorganBodyStyles( false );
98 root.SetBodyStyleNames( { wxS( "Standard" ), wxS( "Alternate" ), wxS( "Simplified" ) } );
99
100 BOOST_CHECK( !derived.HasDeMorganBodyStyles() );
101 BOOST_CHECK_EQUAL( derived.GetBodyStyleCount(), 3 );
102 BOOST_CHECK_EQUAL( derived.GetBodyStyleDescription( 3, false ), wxS( "Simplified" ) );
103
104 // Draw items common to every body style carry a body style of 0, which must not index
105 // one before the start of the names
106 BOOST_CHECK_EQUAL( derived.GetBodyStyleDescription( 0, false ), wxS( "?" ) );
107 BOOST_CHECK_EQUAL( root.GetBodyStyleDescription( 0, false ), wxS( "?" ) );
108}
109
110
116BOOST_AUTO_TEST_CASE( UnitMetadataResolvesThroughParent )
117{
118 LIB_SYMBOL root( wxS( "Root" ) );
119 LIB_SYMBOL derived( wxS( "Derived" ) );
120
121 derived.SetParent( &root );
122
123 root.SetUnitCount( 4, true );
124 root.LockUnits( true );
125 root.GetUnitDisplayNames()[2] = wxS( "Buffer" );
126
127 // GetUnitCount() always resolved through the parent; these did not
128 BOOST_CHECK_EQUAL( derived.GetUnitCount(), 4 );
129 BOOST_CHECK( derived.IsMultiUnit() );
130 BOOST_CHECK( derived.UnitsLocked() );
131 BOOST_CHECK_EQUAL( derived.GetUnitDisplayName( 2, false ), wxS( "Buffer" ) );
132
133 // Units the root has not named still fall back to the sub-reference letter
134 BOOST_CHECK_EQUAL( derived.GetUnitDisplayName( 3, false ), wxS( "C" ) );
135}
136
137
142BOOST_AUTO_TEST_CASE( BodyStylesSurviveRoundTrip )
143{
144 {
145 LIB_SYMBOL root( wxS( "Root" ) );
146 root.SetBodyStyleCount( 2, false, true );
147 root.SetHasDeMorganBodyStyles( true );
148
149 LIB_SYMBOL derived( wxS( "Derived" ) );
150 derived.SetParent( &root );
151
152 // The dialog's write-back for the "single body style" radio button
153 derived.SetBodyStyleCount( 1, false, false );
154 derived.SetHasDeMorganBodyStyles( false );
155 derived.SetBodyStyleNames( {} );
156
157 BOOST_CHECK_EQUAL( derived.GetBodyStyleCount(), 2 );
158
159 IO_RELEASER<SCH_IO> plugin( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
160 plugin->CreateLibrary( GetLibPath() );
161 plugin->SaveSymbol( GetLibPath(), std::make_unique<LIB_SYMBOL>( root ) );
162 plugin->SaveSymbol( GetLibPath(), std::make_unique<LIB_SYMBOL>( derived ) );
163 plugin->SaveLibrary( GetLibPath() );
164 }
165
166 IO_RELEASER<SCH_IO> plugin( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
167 LIB_SYMBOL* loadedRoot = plugin->LoadSymbol( GetLibPath(), wxS( "Root" ) );
168 LIB_SYMBOL* loadedDerived = plugin->LoadSymbol( GetLibPath(), wxS( "Derived" ) );
169
170 BOOST_REQUIRE( loadedRoot );
171 BOOST_REQUIRE( loadedDerived );
172
173 // LoadSymbol does not link parents without a library table
174 loadedDerived->SetParent( loadedRoot );
175
176 BOOST_CHECK( loadedRoot->HasDeMorganBodyStyles() );
177 BOOST_CHECK( loadedDerived->HasDeMorganBodyStyles() );
178 BOOST_CHECK_EQUAL( loadedDerived->GetBodyStyleCount(), 2 );
179}
180
181
Define a library symbol object.
Definition lib_symbol.h:119
bool UnitsLocked() const
Check whether symbol units are interchangeable.
std::map< int, wxString > & GetUnitDisplayNames()
Definition lib_symbol.h:870
bool IsMultiBodyStyle() const override
Definition lib_symbol.h:894
void SetBodyStyleNames(const std::vector< wxString > &aBodyStyleNames)
Definition lib_symbol.h:905
void LockUnits(bool aLockUnits)
Set interchangeable the property for symbol units.
Definition lib_symbol.h:365
void SetParent(LIB_SYMBOL *aParent=nullptr)
void SetUnitCount(int aCount, bool aDuplicateDrawItems)
Set the units per symbol count.
void SetHasDeMorganBodyStyles(bool aFlag)
Definition lib_symbol.h:904
void SetBodyStyleCount(int aCount, bool aDuplicateDrawItems, bool aDuplicatePins)
Set the number of body styles for the symbol.
bool HasDeMorganBodyStyles() const override
bool IsMultiUnit() const override
Definition lib_symbol.h:890
int GetBodyStyleCount() const override
The body styles are a property of the drawings, which a derived symbol inherits from its root symbol ...
wxString GetBodyStyleDescription(int aBodyStyle, bool aLabel) const override
int GetUnitCount() const override
wxString GetUnitDisplayName(int aUnit, bool aLabel) const override
Return the user-defined display name for aUnit for symbols with units.
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(BodyStylesResolveThroughParent)
A derived symbol owns no drawings, so its body styles are its root symbol's.
BOOST_CHECK_EQUAL(result, "25.4")