KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_altium_parser_sch.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
28
30
32#include <eeschema/sch_text.h>
33#include <layer_ids.h>
34#include <validators.h>
35#include <wx/filename.h>
36
37// Function declarations of private methods to test
38int ReadKiCadUnitFrac( const std::map<wxString, wxString>& aProps,
39 const wxString& aKey );
40
42 ASCH_RECORD_ORIENTATION orientation );
43
44void AdjustTextForSymbolOrientation( SCH_TEXT* aText, const ASCH_SYMBOL& aSymbol );
45
46
51
52
56BOOST_FIXTURE_TEST_SUITE( AltiumParserSch, ALTIUM_PARSER_SCH_FIXTURE )
57
59{
60 wxString input;
61 wxString input_frac;
63};
64
68static const std::vector<ALTIUM_TO_KICAD_UNIT_FRAC_CASE> altium_to_kicad_unit_frac = {
69 // Some simple values
70 { "0", "0", 0 },
71 { "1", "0", 2540 },
72 { "2", "0", 5080 },
73 { "-1", "0", -2540 },
74 { "-2", "0", -5080 },
75 // Decimal Places
76 { "0", "1", 0 },
77 { "0", "10", 0 },
78 { "0", "100", 0 },
79 { "0", "1000", 30 },
80 { "0", "10000", 250 },
81 { "1", "10000", 2790 },
82 { "0", "-1", 0 },
83 { "0", "-10", 0 },
84 { "0", "-100", 0 },
85 { "0", "-1000", -30 },
86 { "0", "-10000", -250 },
87 { "-1", "-10000", -2790 },
88 // Edge Cases
89 // Clamp bigger values
90 // imperial rounded units as input
91 // metric rounded units as input
92};
93
97BOOST_AUTO_TEST_CASE( PropertiesReadKiCadUnitFracConversation )
98{
99 for( const auto& c : altium_to_kicad_unit_frac )
100 {
102 wxString::Format( wxT( "%s FRAC %s -> %i" ), c.input, c.input_frac, c.exp_result ) )
103 {
104 std::map<wxString, wxString> properties = { { "TEST", c.input },
105 { "TEST_FRAC", c.input_frac } };
106
107 int result = ReadKiCadUnitFrac( properties, "TEST" );
108
109 // These are all valid
110 BOOST_CHECK_EQUAL( result, c.exp_result );
111 }
112 }
113}
114
115
117{
118 wxString input;
119 wxString expected;
120};
121
122static const std::vector<SHEET_NAME_SANITIZE_CASE> sheet_name_sanitize_cases = {
123 { wxT( "SimpleSheet" ), wxT( "SimpleSheet" ) },
124 { wxT( "POWER PROTECTION/MONITORING" ), wxT( "POWER PROTECTION_MONITORING" ) },
125 { wxT( "A/B/C" ), wxT( "A_B_C" ) },
126 { wxT( "/" ), wxT( "_" ) },
127 { wxT( "no_slash_here" ), wxT( "no_slash_here" ) },
128 { wxT( "trailing/" ), wxT( "trailing_" ) },
129 { wxT( "/leading" ), wxT( "_leading" ) },
130};
131
136BOOST_AUTO_TEST_CASE( SheetNameSlashSanitization )
137{
138 for( const auto& c : sheet_name_sanitize_cases )
139 {
140 BOOST_TEST_CONTEXT( wxString::Format( wxT( "'%s' -> '%s'" ), c.input, c.expected ) )
141 {
142 wxString sanitized = c.input;
143 sanitized.Replace( wxT( "/" ), wxT( "_" ) );
144
145 BOOST_CHECK_EQUAL( sanitized, c.expected );
146
147 wxString validationError = GetFieldValidationErrorMessage( FIELD_T::SHEET_NAME,
148 sanitized );
149 BOOST_CHECK_MESSAGE( validationError.empty(),
150 wxString::Format( wxT( "Sanitized name '%s' failed validation: %s" ),
151 sanitized, validationError ) );
152 }
153 }
154}
155
159BOOST_AUTO_TEST_CASE( SheetNameSlashRejection )
160{
161 wxString invalidName = wxT( "POWER PROTECTION/MONITORING" );
162 wxString validationError = GetFieldValidationErrorMessage( FIELD_T::SHEET_NAME, invalidName );
163
164 BOOST_CHECK_MESSAGE( !validationError.empty(),
165 wxT( "Sheet name with '/' should fail validation" ) );
166}
167
173BOOST_AUTO_TEST_CASE( ExtensionlessFilenameDetection )
174{
175 // Filenames WITH extensions should be detected
176 BOOST_CHECK( wxFileName( wxT( "6_power.SchDoc" ) ).HasExt() );
177 BOOST_CHECK( wxFileName( wxT( "my_sheet.SCHDOC" ) ).HasExt() );
178
179 // Filenames WITHOUT extensions should not be detected
180 BOOST_CHECK( !wxFileName( wxT( "6_power" ) ).HasExt() );
181 BOOST_CHECK( !wxFileName( wxT( "POWER MANAGEMENT" ) ).HasExt() );
182 BOOST_CHECK( !wxFileName( wxT( "" ) ).HasExt() );
183}
184
185
190BOOST_AUTO_TEST_CASE( ExtensionlessBaseNameMatching )
191{
192 wxFileName sheetFn( wxT( "6_power" ) );
193 bool extensionless = !sheetFn.HasExt();
194
195 BOOST_CHECK( extensionless );
196
197 wxFileName candidateA( wxT( "6_power.SchDoc" ) );
198 wxFileName candidateB( wxT( "6_POWER.SCHDOC" ) );
199 wxFileName candidateC( wxT( "7_other.SchDoc" ) );
200 wxFileName candidateD( wxT( "6_power.txt" ) );
201
202 // Should match same base name with .SchDoc extension (case-insensitive on both parts)
203 auto matches = [&]( const wxFileName& candidate )
204 {
205 return candidate.GetFullName().IsSameAs( sheetFn.GetFullName(), false )
206 || ( extensionless
207 && !sheetFn.GetName().empty()
208 && candidate.GetName().IsSameAs( sheetFn.GetName(), false )
209 && candidate.GetExt().IsSameAs( wxT( "SchDoc" ), false ) );
210 };
211
212 BOOST_CHECK( matches( candidateA ) );
213 BOOST_CHECK( matches( candidateB ) ); // Case-insensitive match on both name and ext
214 BOOST_CHECK( !matches( candidateC ) ); // Different base name
215 BOOST_CHECK( !matches( candidateD ) ); // Wrong extension
216}
217
218
223BOOST_AUTO_TEST_CASE( SheetNameDerivationFromFilename )
224{
225 auto deriveName = []( const wxString& aFilePath, const std::set<wxString>& aExistingNames )
226 {
227 wxString baseName = wxFileName( aFilePath ).GetName();
228 baseName.Replace( wxT( "/" ), wxT( "_" ) );
229
230 wxString sheetName = baseName;
231
232 for( int ii = 1; aExistingNames.count( sheetName ); ++ii )
233 sheetName = baseName + wxString::Format( wxT( "_%d" ), ii );
234
235 return sheetName;
236 };
237
238 BOOST_CHECK_EQUAL( deriveName( wxT( "6_power.SchDoc" ), {} ), wxT( "6_power" ) );
239 BOOST_CHECK_EQUAL( deriveName( wxT( "POWER.SchDoc" ), {} ), wxT( "POWER" ) );
240
241 // Deduplication when a name already exists
242 std::set<wxString> existing = { wxT( "6_power" ) };
243 BOOST_CHECK_EQUAL( deriveName( wxT( "6_power.SchDoc" ), existing ), wxT( "6_power_1" ) );
244
245 // Multiple duplicates
246 existing.insert( wxT( "6_power_1" ) );
247 BOOST_CHECK_EQUAL( deriveName( wxT( "6_power.SchDoc" ), existing ), wxT( "6_power_2" ) );
248}
249
250
251static ASCH_SYMBOL MakeTestSymbol( int aOrientation, bool aMirrored )
252{
253 std::map<wxString, wxString> props;
254 props[wxT( "RECORD" )] = wxT( "1" );
255 props[wxT( "ORIENTATION" )] = wxString::Format( wxT( "%d" ), aOrientation );
256 props[wxT( "ISMIRRORED" )] = aMirrored ? wxT( "T" ) : wxT( "F" );
257 props[wxT( "CURRENTPARTID" )] = wxT( "1" );
258 props[wxT( "PARTCOUNT" )] = wxT( "2" );
259 props[wxT( "DISPLAYMODECOUNT" )] = wxT( "1" );
260 props[wxT( "LOCATION.X" )] = wxT( "0" );
261 props[wxT( "LOCATION.Y" )] = wxT( "0" );
262 return ASCH_SYMBOL( props );
263}
264
265
266// Simulates the angle/justification portion of what OrientAndMirrorSymbolItems
267// does at render time. Position is not relevant for this test.
268static void SimulateRenderTransform( SCH_TEXT* aText, int aAltiumOrientation, bool aMirrored )
269{
270 int nRotations = ( aAltiumOrientation + 1 ) % 4;
271
272 for( int i = 0; i < nRotations; i++ )
273 aText->Rotate90( false );
274
275 if( aMirrored )
276 {
277 if( aText->GetTextAngle().IsHorizontal() )
278 aText->FlipHJustify();
279 else
280 aText->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -aText->GetVertJustify() ) );
281 }
282}
283
284
294
295
296static const std::vector<TEXT_ORIENT_CASE> text_orient_cases = {
297 // Horizontal text, left-justified, each symbol orientation
306
307 // Vertical text, left-justified, each symbol orientation
316
317 // Horizontal text, right-justified, each symbol orientation
326
327 // Mirrored cases
336
337 // LEFTWARDS text flips H-justify via SetTextPositioning
342
343 // DOWNWARDS text flips H-justify via SetTextPositioning
348};
349
350
356BOOST_AUTO_TEST_CASE( TextOrientationCompensation )
357{
358 for( size_t idx = 0; idx < text_orient_cases.size(); idx++ )
359 {
360 const TEXT_ORIENT_CASE& c = text_orient_cases[idx];
361
362 BOOST_TEST_CONTEXT( wxString::Format(
363 wxT( "case %zu: textOri=%d justification=%d symOri=%d mirror=%d" ),
364 idx, (int) c.textOrientation, (int) c.justification,
365 c.altiumSymOrientation, c.mirrored ? 1 : 0 ) )
366 {
367 SCH_TEXT textItem( { 0, 0 }, wxT( "TEST" ), LAYER_DEVICE );
368
370
372
373 AdjustTextForSymbolOrientation( &textItem, sym );
374
376
377 BOOST_CHECK_EQUAL( textItem.GetTextAngle(), c.expectedAngle );
378 BOOST_CHECK_EQUAL( textItem.GetHorizJustify(), c.expectedHJustify );
379 }
380 }
381}
382
ASCH_RECORD_ORIENTATION
ASCH_LABEL_JUSTIFICATION
bool IsHorizontal() const
Definition eda_angle.h:142
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:80
const EDA_ANGLE & GetTextAngle() const
Definition eda_text.h:147
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:431
void FlipHJustify()
Definition eda_text.h:208
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:203
virtual void Rotate90(bool aClockwise)
Definition sch_text.cpp:213
static bool empty(const wxTextEntryBase *aCtrl)
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:408
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition eda_angle.h:407
@ LAYER_DEVICE
Definition layer_ids.h:466
Declares the struct as the Boost test fixture.
ASCH_RECORD_ORIENTATION textOrientation
ASCH_LABEL_JUSTIFICATION justification
GR_TEXT_H_ALIGN_T expectedHJustify
void AdjustTextForSymbolOrientation(SCH_TEXT *aText, const ASCH_SYMBOL &aSymbol)
static const std::vector< ALTIUM_TO_KICAD_UNIT_FRAC_CASE > altium_to_kicad_unit_frac
A list of valid internal unit conversation factors.
static const std::vector< TEXT_ORIENT_CASE > text_orient_cases
int ReadKiCadUnitFrac(const std::map< wxString, wxString > &aProps, const wxString &aKey)
static const std::vector< SHEET_NAME_SANITIZE_CASE > sheet_name_sanitize_cases
static ASCH_SYMBOL MakeTestSymbol(int aOrientation, bool aMirrored)
BOOST_AUTO_TEST_CASE(PropertiesReadKiCadUnitFracConversation)
Test conversation from Altium internal units into KiCad internal units using properties with FRAC.
static void SimulateRenderTransform(SCH_TEXT *aText, int aAltiumOrientation, bool aMirrored)
void SetTextPositioning(EDA_TEXT *text, ASCH_LABEL_JUSTIFICATION justification, ASCH_RECORD_ORIENTATION orientation)
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_CONTEXT("Test Clearance")
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
GR_TEXT_V_ALIGN_T
This is API surface mapped to common.types.VertialAlignment.
wxString GetFieldValidationErrorMessage(FIELD_T aFieldId, const wxString &aValue)
Return the error message if aValue is invalid for aFieldId.
Custom text control validator definitions.