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, see <https://www.gnu.org/licenses/>.
18 */
19
25
27
29#include <eeschema/sch_text.h>
30#include <layer_ids.h>
32#include <validators.h>
33#include <wx/filename.h>
34
35#include <set>
36#include <vector>
37
38// Function declarations of private methods to test
39int ReadKiCadUnitFrac( const std::map<wxString, wxString>& aProps,
40 const wxString& aKey );
41
43 ASCH_RECORD_ORIENTATION orientation );
44
45void AdjustTextForSymbolOrientation( SCH_TEXT* aText, const ASCH_SYMBOL& aSymbol );
46
47wxString AltiumWrapBusLabel( const wxString& aText );
48
49wxString AltiumDeriveSheetName( const wxString& aFilename, const std::set<wxString>& aExistingNames );
50
51
56
57
61BOOST_FIXTURE_TEST_SUITE( AltiumParserSch, ALTIUM_PARSER_SCH_FIXTURE )
62
64{
65 wxString input;
66 wxString input_frac;
68};
69
73static const std::vector<ALTIUM_TO_KICAD_UNIT_FRAC_CASE> altium_to_kicad_unit_frac = {
74 // Some simple values
75 { "0", "0", 0 },
76 { "1", "0", 2540 },
77 { "2", "0", 5080 },
78 { "-1", "0", -2540 },
79 { "-2", "0", -5080 },
80 // Decimal Places
81 { "0", "1", 0 },
82 { "0", "10", 0 },
83 { "0", "100", 0 },
84 { "0", "1000", 30 },
85 { "0", "10000", 250 },
86 { "1", "10000", 2790 },
87 { "0", "-1", 0 },
88 { "0", "-10", 0 },
89 { "0", "-100", 0 },
90 { "0", "-1000", -30 },
91 { "0", "-10000", -250 },
92 { "-1", "-10000", -2790 },
93 // Edge Cases
94 // Clamp bigger values
95 // imperial rounded units as input
96 // metric rounded units as input
97};
98
102BOOST_AUTO_TEST_CASE( PropertiesReadKiCadUnitFracConversation )
103{
104 for( const auto& c : altium_to_kicad_unit_frac )
105 {
107 wxString::Format( wxT( "%s FRAC %s -> %i" ), c.input, c.input_frac, c.exp_result ) )
108 {
109 std::map<wxString, wxString> properties = { { "TEST", c.input },
110 { "TEST_FRAC", c.input_frac } };
111
112 int result = ReadKiCadUnitFrac( properties, "TEST" );
113
114 // These are all valid
115 BOOST_CHECK_EQUAL( result, c.exp_result );
116 }
117 }
118}
119
120
122{
123 wxString input;
124 wxString expected;
125};
126
127static const std::vector<SHEET_NAME_SANITIZE_CASE> sheet_name_sanitize_cases = {
128 { wxT( "SimpleSheet" ), wxT( "SimpleSheet" ) },
129 { wxT( "POWER PROTECTION/MONITORING" ), wxT( "POWER PROTECTION_MONITORING" ) },
130 { wxT( "A/B/C" ), wxT( "A_B_C" ) },
131 { wxT( "/" ), wxT( "_" ) },
132 { wxT( "no_slash_here" ), wxT( "no_slash_here" ) },
133 { wxT( "trailing/" ), wxT( "trailing_" ) },
134 { wxT( "/leading" ), wxT( "_leading" ) },
135};
136
141BOOST_AUTO_TEST_CASE( SheetNameSlashSanitization )
142{
143 for( const auto& c : sheet_name_sanitize_cases )
144 {
145 BOOST_TEST_CONTEXT( wxString::Format( wxT( "'%s' -> '%s'" ), c.input, c.expected ) )
146 {
147 wxString sanitized = c.input;
148 sanitized.Replace( wxT( "/" ), wxT( "_" ) );
149
150 BOOST_CHECK_EQUAL( sanitized, c.expected );
151
152 wxString validationError = GetFieldValidationErrorMessage( FIELD_T::SHEET_NAME,
153 sanitized );
154 BOOST_CHECK_MESSAGE( validationError.empty(),
155 wxString::Format( wxT( "Sanitized name '%s' failed validation: %s" ),
156 sanitized, validationError ) );
157 }
158 }
159}
160
164BOOST_AUTO_TEST_CASE( SheetNameSlashRejection )
165{
166 wxString invalidName = wxT( "POWER PROTECTION/MONITORING" );
167 wxString validationError = GetFieldValidationErrorMessage( FIELD_T::SHEET_NAME, invalidName );
168
169 BOOST_CHECK_MESSAGE( (!validationError.empty()) ,
170 wxString::Format( "Sheet name with '/' should fail validation" ) );
171}
172
178BOOST_AUTO_TEST_CASE( ExtensionlessFilenameDetection )
179{
180 // Filenames WITH extensions should be detected
181 BOOST_CHECK( wxFileName( wxT( "6_power.SchDoc" ) ).HasExt() );
182 BOOST_CHECK( wxFileName( wxT( "my_sheet.SCHDOC" ) ).HasExt() );
183
184 // Filenames WITHOUT extensions should not be detected
185 BOOST_CHECK( !wxFileName( wxT( "6_power" ) ).HasExt() );
186 BOOST_CHECK( !wxFileName( wxT( "POWER MANAGEMENT" ) ).HasExt() );
187 BOOST_CHECK( !wxFileName( wxT( "" ) ).HasExt() );
188}
189
190
195BOOST_AUTO_TEST_CASE( ExtensionlessBaseNameMatching )
196{
197 wxFileName sheetFn( wxT( "6_power" ) );
198 bool extensionless = !sheetFn.HasExt();
199
200 BOOST_CHECK( extensionless );
201
202 wxFileName candidateA( wxT( "6_power.SchDoc" ) );
203 wxFileName candidateB( wxT( "6_POWER.SCHDOC" ) );
204 wxFileName candidateC( wxT( "7_other.SchDoc" ) );
205 wxFileName candidateD( wxT( "6_power.txt" ) );
206
207 // Should match same base name with .SchDoc extension (case-insensitive on both parts)
208 auto matches = [&]( const wxFileName& candidate )
209 {
210 return candidate.GetFullName().IsSameAs( sheetFn.GetFullName(), false )
211 || ( extensionless
212 && !sheetFn.GetName().empty()
213 && candidate.GetName().IsSameAs( sheetFn.GetName(), false )
214 && candidate.GetExt().IsSameAs( wxT( "SchDoc" ), false ) );
215 };
216
217 BOOST_CHECK( matches( candidateA ) );
218 BOOST_CHECK( matches( candidateB ) ); // Case-insensitive match on both name and ext
219 BOOST_CHECK( !matches( candidateC ) ); // Different base name
220 BOOST_CHECK( !matches( candidateD ) ); // Wrong extension
221}
222
223
228BOOST_AUTO_TEST_CASE( SheetNameDerivationFromFilename )
229{
230 auto deriveName = []( const wxString& aFilePath, const std::set<wxString>& aExistingNames )
231 {
232 wxString baseName = wxFileName( aFilePath ).GetName();
233 baseName.Replace( wxT( "/" ), wxT( "_" ) );
234
235 wxString sheetName = baseName;
236
237 for( int ii = 1; aExistingNames.count( sheetName ); ++ii )
238 sheetName = baseName + wxString::Format( wxT( "_%d" ), ii );
239
240 return sheetName;
241 };
242
243 BOOST_CHECK_EQUAL( deriveName( wxT( "6_power.SchDoc" ), {} ), wxT( "6_power" ) );
244 BOOST_CHECK_EQUAL( deriveName( wxT( "POWER.SchDoc" ), {} ), wxT( "POWER" ) );
245
246 // Deduplication when a name already exists
247 std::set<wxString> existing = { wxT( "6_power" ) };
248 BOOST_CHECK_EQUAL( deriveName( wxT( "6_power.SchDoc" ), existing ), wxT( "6_power_1" ) );
249
250 // Multiple duplicates
251 existing.insert( wxT( "6_power_1" ) );
252 BOOST_CHECK_EQUAL( deriveName( wxT( "6_power.SchDoc" ), existing ), wxT( "6_power_2" ) );
253}
254
255
256static ASCH_SYMBOL MakeTestSymbol( int aOrientation, bool aMirrored )
257{
258 std::map<wxString, wxString> props;
259 props[wxT( "RECORD" )] = wxT( "1" );
260 props[wxT( "ORIENTATION" )] = wxString::Format( wxT( "%d" ), aOrientation );
261 props[wxT( "ISMIRRORED" )] = aMirrored ? wxT( "T" ) : wxT( "F" );
262 props[wxT( "CURRENTPARTID" )] = wxT( "1" );
263 props[wxT( "PARTCOUNT" )] = wxT( "2" );
264 props[wxT( "DISPLAYMODECOUNT" )] = wxT( "1" );
265 props[wxT( "LOCATION.X" )] = wxT( "0" );
266 props[wxT( "LOCATION.Y" )] = wxT( "0" );
267 return ASCH_SYMBOL( props );
268}
269
270
271// Simulates the angle/justification portion of what OrientAndMirrorSymbolItems
272// does at render time. Position is not relevant for this test.
273static void SimulateRenderTransform( SCH_TEXT* aText, int aAltiumOrientation, bool aMirrored )
274{
275 int nRotations = aAltiumOrientation % 4;
276
277 for( int i = 0; i < nRotations; i++ )
278 aText->Rotate90( false );
279
280 if( aMirrored )
281 {
282 if( aText->GetTextAngle().IsHorizontal() )
283 aText->FlipHJustify();
284 else
285 aText->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -aText->GetVertJustify() ) );
286 }
287}
288
289
299
300
301static const std::vector<TEXT_ORIENT_CASE> text_orient_cases = {
302 // Horizontal text, left-justified, each symbol orientation
311
312 // Vertical text, left-justified, each symbol orientation
321
322 // Horizontal text, right-justified, each symbol orientation
331
332 // Mirrored cases
341
342 // LEFTWARDS text flips H-justify via SetTextPositioning
347
348 // DOWNWARDS text flips H-justify via SetTextPositioning
353};
354
355
361BOOST_AUTO_TEST_CASE( TextOrientationCompensation )
362{
363 for( size_t idx = 0; idx < text_orient_cases.size(); idx++ )
364 {
365 const TEXT_ORIENT_CASE& c = text_orient_cases[idx];
366
367 BOOST_TEST_CONTEXT( wxString::Format(
368 wxT( "case %zu: textOri=%d justification=%d symOri=%d mirror=%d" ),
369 idx, (int) c.textOrientation, (int) c.justification,
370 c.altiumSymOrientation, c.mirrored ? 1 : 0 ) )
371 {
372 SCH_TEXT textItem( { 0, 0 }, wxT( "TEST" ), LAYER_DEVICE );
373
375
377
378 AdjustTextForSymbolOrientation( &textItem, sym );
379
381
382 BOOST_CHECK_EQUAL( textItem.GetTextAngle(), c.expectedAngle );
383 BOOST_CHECK_EQUAL( textItem.GetHorizJustify(), c.expectedHJustify );
384 }
385 }
386}
387
388
394BOOST_AUTO_TEST_CASE( BusLabelWrapping )
395{
396 BOOST_CHECK_EQUAL( AltiumWrapBusLabel( wxT( "I2C" ) ), wxT( "{I2C}" ) );
397 BOOST_CHECK_EQUAL( AltiumWrapBusLabel( wxT( "SPI" ) ), wxT( "{SPI}" ) );
398 BOOST_CHECK_EQUAL( AltiumWrapBusLabel( wxT( "MOSI" ) ), wxT( "{MOSI}" ) );
399
400 // Already-formatted bus labels pass through unchanged (no double wrapping).
401 BOOST_CHECK_EQUAL( AltiumWrapBusLabel( wxT( "{SPI}" ) ), wxT( "{SPI}" ) );
402 BOOST_CHECK_EQUAL( AltiumWrapBusLabel( wxT( "DATA[0..7]" ) ), wxT( "DATA[0..7]" ) );
403 BOOST_CHECK_EQUAL( AltiumWrapBusLabel( wxT( "I2C{I2C}" ) ), wxT( "I2C{I2C}" ) );
404
405 // A net name with spaces or commas must stay a single bus member, not fan out into
406 // several. The wrapped form must parse back to exactly one member.
407 for( const wxString name : { wxT( "Net 1" ), wxT( "A,B" ) } )
408 {
409 wxString wrapped = AltiumWrapBusLabel( name );
410 std::vector<wxString> members;
411
412 BOOST_CHECK( NET_SETTINGS::ParseBusGroup( wrapped, nullptr, &members ) );
413 BOOST_CHECK_EQUAL( members.size(), 1u );
414 }
415}
416
417
423BOOST_AUTO_TEST_CASE( SheetNameDerivation )
424{
425 BOOST_CHECK_EQUAL( AltiumDeriveSheetName( wxT( "power.SchDoc" ), {} ), wxT( "power" ) );
426 BOOST_CHECK_EQUAL( AltiumDeriveSheetName( wxT( "my_sheet.SchDoc" ), {} ), wxT( "my_sheet" ) );
427 BOOST_CHECK_EQUAL( AltiumDeriveSheetName( wxT( "" ), {} ), wxT( "Sheet" ) );
428
429 std::set<wxString> existing = { wxT( "power" ) };
430 BOOST_CHECK_EQUAL( AltiumDeriveSheetName( wxT( "power.SchDoc" ), existing ), wxT( "power_1" ) );
431
432 existing.insert( wxT( "power_1" ) );
433 BOOST_CHECK_EQUAL( AltiumDeriveSheetName( wxT( "power.SchDoc" ), existing ), wxT( "power_2" ) );
434
435 BOOST_CHECK_EQUAL( AltiumDeriveSheetName( wxT( "" ), { wxT( "Sheet" ) } ), wxT( "Sheet_1" ) );
436}
437
438
const char * name
ASCH_RECORD_ORIENTATION
ASCH_LABEL_JUSTIFICATION
bool IsHorizontal() const
Definition eda_angle.h:141
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:94
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:378
void FlipHJustify()
Definition eda_text.h:247
virtual EDA_ANGLE GetTextAngle() const
Definition eda_text.h:178
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:242
static bool ParseBusGroup(const wxString &aGroup, wxString *name, std::vector< wxString > *aMemberList, size_t *aPrefixEnd=nullptr)
Parse a bus group label into the name and a list of components.
virtual void Rotate90(bool aClockwise)
Definition sch_text.cpp:255
static bool empty(const wxTextEntryBase *aCtrl)
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:445
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition eda_angle.h:444
@ LAYER_DEVICE
Definition layer_ids.h:488
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.
wxString AltiumDeriveSheetName(const wxString &aFilename, const std::set< wxString > &aExistingNames)
static void SimulateRenderTransform(SCH_TEXT *aText, int aAltiumOrientation, bool aMirrored)
wxString AltiumWrapBusLabel(const wxString &aText)
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.