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
24
26
28#include <eeschema/sch_text.h>
29#include <layer_ids.h>
30#include <validators.h>
31#include <wx/filename.h>
32
33// Function declarations of private methods to test
34int ReadKiCadUnitFrac( const std::map<wxString, wxString>& aProps,
35 const wxString& aKey );
36
38 ASCH_RECORD_ORIENTATION orientation );
39
40void AdjustTextForSymbolOrientation( SCH_TEXT* aText, const ASCH_SYMBOL& aSymbol );
41
42
47
48
52BOOST_FIXTURE_TEST_SUITE( AltiumParserSch, ALTIUM_PARSER_SCH_FIXTURE )
53
55{
56 wxString input;
57 wxString input_frac;
59};
60
64static const std::vector<ALTIUM_TO_KICAD_UNIT_FRAC_CASE> altium_to_kicad_unit_frac = {
65 // Some simple values
66 { "0", "0", 0 },
67 { "1", "0", 2540 },
68 { "2", "0", 5080 },
69 { "-1", "0", -2540 },
70 { "-2", "0", -5080 },
71 // Decimal Places
72 { "0", "1", 0 },
73 { "0", "10", 0 },
74 { "0", "100", 0 },
75 { "0", "1000", 30 },
76 { "0", "10000", 250 },
77 { "1", "10000", 2790 },
78 { "0", "-1", 0 },
79 { "0", "-10", 0 },
80 { "0", "-100", 0 },
81 { "0", "-1000", -30 },
82 { "0", "-10000", -250 },
83 { "-1", "-10000", -2790 },
84 // Edge Cases
85 // Clamp bigger values
86 // imperial rounded units as input
87 // metric rounded units as input
88};
89
93BOOST_AUTO_TEST_CASE( PropertiesReadKiCadUnitFracConversation )
94{
95 for( const auto& c : altium_to_kicad_unit_frac )
96 {
98 wxString::Format( wxT( "%s FRAC %s -> %i" ), c.input, c.input_frac, c.exp_result ) )
99 {
100 std::map<wxString, wxString> properties = { { "TEST", c.input },
101 { "TEST_FRAC", c.input_frac } };
102
103 int result = ReadKiCadUnitFrac( properties, "TEST" );
104
105 // These are all valid
106 BOOST_CHECK_EQUAL( result, c.exp_result );
107 }
108 }
109}
110
111
113{
114 wxString input;
115 wxString expected;
116};
117
118static const std::vector<SHEET_NAME_SANITIZE_CASE> sheet_name_sanitize_cases = {
119 { wxT( "SimpleSheet" ), wxT( "SimpleSheet" ) },
120 { wxT( "POWER PROTECTION/MONITORING" ), wxT( "POWER PROTECTION_MONITORING" ) },
121 { wxT( "A/B/C" ), wxT( "A_B_C" ) },
122 { wxT( "/" ), wxT( "_" ) },
123 { wxT( "no_slash_here" ), wxT( "no_slash_here" ) },
124 { wxT( "trailing/" ), wxT( "trailing_" ) },
125 { wxT( "/leading" ), wxT( "_leading" ) },
126};
127
132BOOST_AUTO_TEST_CASE( SheetNameSlashSanitization )
133{
134 for( const auto& c : sheet_name_sanitize_cases )
135 {
136 BOOST_TEST_CONTEXT( wxString::Format( wxT( "'%s' -> '%s'" ), c.input, c.expected ) )
137 {
138 wxString sanitized = c.input;
139 sanitized.Replace( wxT( "/" ), wxT( "_" ) );
140
141 BOOST_CHECK_EQUAL( sanitized, c.expected );
142
143 wxString validationError = GetFieldValidationErrorMessage( FIELD_T::SHEET_NAME,
144 sanitized );
145 BOOST_CHECK_MESSAGE( validationError.empty(),
146 wxString::Format( wxT( "Sanitized name '%s' failed validation: %s" ),
147 sanitized, validationError ) );
148 }
149 }
150}
151
155BOOST_AUTO_TEST_CASE( SheetNameSlashRejection )
156{
157 wxString invalidName = wxT( "POWER PROTECTION/MONITORING" );
158 wxString validationError = GetFieldValidationErrorMessage( FIELD_T::SHEET_NAME, invalidName );
159
160 BOOST_CHECK_MESSAGE( (!validationError.empty()) ,
161 wxString::Format( "Sheet name with '/' should fail validation" ) );
162}
163
169BOOST_AUTO_TEST_CASE( ExtensionlessFilenameDetection )
170{
171 // Filenames WITH extensions should be detected
172 BOOST_CHECK( wxFileName( wxT( "6_power.SchDoc" ) ).HasExt() );
173 BOOST_CHECK( wxFileName( wxT( "my_sheet.SCHDOC" ) ).HasExt() );
174
175 // Filenames WITHOUT extensions should not be detected
176 BOOST_CHECK( !wxFileName( wxT( "6_power" ) ).HasExt() );
177 BOOST_CHECK( !wxFileName( wxT( "POWER MANAGEMENT" ) ).HasExt() );
178 BOOST_CHECK( !wxFileName( wxT( "" ) ).HasExt() );
179}
180
181
186BOOST_AUTO_TEST_CASE( ExtensionlessBaseNameMatching )
187{
188 wxFileName sheetFn( wxT( "6_power" ) );
189 bool extensionless = !sheetFn.HasExt();
190
191 BOOST_CHECK( extensionless );
192
193 wxFileName candidateA( wxT( "6_power.SchDoc" ) );
194 wxFileName candidateB( wxT( "6_POWER.SCHDOC" ) );
195 wxFileName candidateC( wxT( "7_other.SchDoc" ) );
196 wxFileName candidateD( wxT( "6_power.txt" ) );
197
198 // Should match same base name with .SchDoc extension (case-insensitive on both parts)
199 auto matches = [&]( const wxFileName& candidate )
200 {
201 return candidate.GetFullName().IsSameAs( sheetFn.GetFullName(), false )
202 || ( extensionless
203 && !sheetFn.GetName().empty()
204 && candidate.GetName().IsSameAs( sheetFn.GetName(), false )
205 && candidate.GetExt().IsSameAs( wxT( "SchDoc" ), false ) );
206 };
207
208 BOOST_CHECK( matches( candidateA ) );
209 BOOST_CHECK( matches( candidateB ) ); // Case-insensitive match on both name and ext
210 BOOST_CHECK( !matches( candidateC ) ); // Different base name
211 BOOST_CHECK( !matches( candidateD ) ); // Wrong extension
212}
213
214
219BOOST_AUTO_TEST_CASE( SheetNameDerivationFromFilename )
220{
221 auto deriveName = []( const wxString& aFilePath, const std::set<wxString>& aExistingNames )
222 {
223 wxString baseName = wxFileName( aFilePath ).GetName();
224 baseName.Replace( wxT( "/" ), wxT( "_" ) );
225
226 wxString sheetName = baseName;
227
228 for( int ii = 1; aExistingNames.count( sheetName ); ++ii )
229 sheetName = baseName + wxString::Format( wxT( "_%d" ), ii );
230
231 return sheetName;
232 };
233
234 BOOST_CHECK_EQUAL( deriveName( wxT( "6_power.SchDoc" ), {} ), wxT( "6_power" ) );
235 BOOST_CHECK_EQUAL( deriveName( wxT( "POWER.SchDoc" ), {} ), wxT( "POWER" ) );
236
237 // Deduplication when a name already exists
238 std::set<wxString> existing = { wxT( "6_power" ) };
239 BOOST_CHECK_EQUAL( deriveName( wxT( "6_power.SchDoc" ), existing ), wxT( "6_power_1" ) );
240
241 // Multiple duplicates
242 existing.insert( wxT( "6_power_1" ) );
243 BOOST_CHECK_EQUAL( deriveName( wxT( "6_power.SchDoc" ), existing ), wxT( "6_power_2" ) );
244}
245
246
247static ASCH_SYMBOL MakeTestSymbol( int aOrientation, bool aMirrored )
248{
249 std::map<wxString, wxString> props;
250 props[wxT( "RECORD" )] = wxT( "1" );
251 props[wxT( "ORIENTATION" )] = wxString::Format( wxT( "%d" ), aOrientation );
252 props[wxT( "ISMIRRORED" )] = aMirrored ? wxT( "T" ) : wxT( "F" );
253 props[wxT( "CURRENTPARTID" )] = wxT( "1" );
254 props[wxT( "PARTCOUNT" )] = wxT( "2" );
255 props[wxT( "DISPLAYMODECOUNT" )] = wxT( "1" );
256 props[wxT( "LOCATION.X" )] = wxT( "0" );
257 props[wxT( "LOCATION.Y" )] = wxT( "0" );
258 return ASCH_SYMBOL( props );
259}
260
261
262// Simulates the angle/justification portion of what OrientAndMirrorSymbolItems
263// does at render time. Position is not relevant for this test.
264static void SimulateRenderTransform( SCH_TEXT* aText, int aAltiumOrientation, bool aMirrored )
265{
266 int nRotations = ( aAltiumOrientation + 1 ) % 4;
267
268 for( int i = 0; i < nRotations; i++ )
269 aText->Rotate90( false );
270
271 if( aMirrored )
272 {
273 if( aText->GetTextAngle().IsHorizontal() )
274 aText->FlipHJustify();
275 else
276 aText->SetVertJustify( static_cast<GR_TEXT_V_ALIGN_T>( -aText->GetVertJustify() ) );
277 }
278}
279
280
290
291
292static const std::vector<TEXT_ORIENT_CASE> text_orient_cases = {
293 // Horizontal text, left-justified, each symbol orientation
302
303 // Vertical text, left-justified, each symbol orientation
312
313 // Horizontal text, right-justified, each symbol orientation
322
323 // Mirrored cases
332
333 // LEFTWARDS text flips H-justify via SetTextPositioning
338
339 // DOWNWARDS text flips H-justify via SetTextPositioning
344};
345
346
352BOOST_AUTO_TEST_CASE( TextOrientationCompensation )
353{
354 for( size_t idx = 0; idx < text_orient_cases.size(); idx++ )
355 {
356 const TEXT_ORIENT_CASE& c = text_orient_cases[idx];
357
358 BOOST_TEST_CONTEXT( wxString::Format(
359 wxT( "case %zu: textOri=%d justification=%d symOri=%d mirror=%d" ),
360 idx, (int) c.textOrientation, (int) c.justification,
361 c.altiumSymOrientation, c.mirrored ? 1 : 0 ) )
362 {
363 SCH_TEXT textItem( { 0, 0 }, wxT( "TEST" ), LAYER_DEVICE );
364
366
368
369 AdjustTextForSymbolOrientation( &textItem, sym );
370
372
373 BOOST_CHECK_EQUAL( textItem.GetTextAngle(), c.expectedAngle );
374 BOOST_CHECK_EQUAL( textItem.GetHorizJustify(), c.expectedHJustify );
375 }
376 }
377}
378
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:89
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:412
void FlipHJustify()
Definition eda_text.h:229
virtual EDA_ANGLE GetTextAngle() const
Definition eda_text.h:168
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:224
virtual void Rotate90(bool aClockwise)
Definition sch_text.cpp:257
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:464
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_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
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.