KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_lib_id.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
21#include <boost/test/unit_test.hpp>
22
23#include <lib_id.h>
24#include <name_validation.h>
25
26
28
29
30BOOST_AUTO_TEST_CASE( ParseFullyQualified )
31{
32 LIB_ID id;
33 BOOST_CHECK( id.Parse( "Package_SO:DGG56" ) == -1 );
34 BOOST_CHECK_EQUAL( id.GetLibNickname(), "Package_SO" );
35 BOOST_CHECK_EQUAL( id.GetLibItemName(), "DGG56" );
36 BOOST_CHECK( id.IsValid() );
37 BOOST_CHECK( !id.IsLegacy() );
38 BOOST_CHECK( !id.empty() );
39}
40
41
43{
44 LIB_ID id;
45 BOOST_CHECK( id.Parse( "DGG56" ) == -1 );
46 BOOST_CHECK( id.GetLibNickname().empty() );
47 BOOST_CHECK_EQUAL( id.GetLibItemName(), "DGG56" );
48 BOOST_CHECK( !id.IsValid() );
49 BOOST_CHECK( id.IsLegacy() );
50 BOOST_CHECK( !id.empty() );
51}
52
53
54BOOST_AUTO_TEST_CASE( EqualityFullyQualified )
55{
56 LIB_ID a( wxT( "Package_SO" ), wxT( "DGG56" ) );
57 LIB_ID b( wxT( "Package_SO" ), wxT( "DGG56" ) );
58 LIB_ID c( wxT( "OtherLib" ), wxT( "DGG56" ) );
59
60 BOOST_CHECK( a == b );
61 BOOST_CHECK( a != c );
62}
63
64
65BOOST_AUTO_TEST_CASE( EqualityLegacyVsFullyQualified )
66{
67 LIB_ID legacy;
68 legacy.Parse( "DGG56" );
69
70 LIB_ID qualified( wxT( "Package_SO" ), wxT( "DGG56" ) );
71
72 // Standard equality requires both library and item name to match
73 BOOST_CHECK( legacy != qualified );
74
75 // Legacy matching (item name only) should match when we explicitly compare just item names
76 BOOST_CHECK( legacy.IsLegacy() );
77 BOOST_CHECK_EQUAL( legacy.GetLibItemName(), qualified.GetLibItemName() );
78}
79
80
81BOOST_AUTO_TEST_CASE( FormatRoundTrip )
82{
83 LIB_ID qualified( wxT( "Package_SO" ), wxT( "DGG56" ) );
84 BOOST_CHECK_EQUAL( qualified.Format().wx_str(), wxT( "Package_SO:DGG56" ) );
85
86 LIB_ID legacy;
87 legacy.Parse( "DGG56" );
88 BOOST_CHECK_EQUAL( legacy.Format().wx_str(), wxT( "DGG56" ) );
89
90 // Re-parse the formatted strings
91 LIB_ID reparsed;
92 reparsed.Parse( qualified.Format() );
93 BOOST_CHECK( reparsed == qualified );
94
95 LIB_ID reparsedLegacy;
96 reparsedLegacy.Parse( legacy.Format() );
97 BOOST_CHECK( reparsedLegacy.IsLegacy() );
98 BOOST_CHECK_EQUAL( reparsedLegacy.GetLibItemName(), legacy.GetLibItemName() );
99}
100
101
103{
105 BOOST_CHECK( empty.empty() );
106 BOOST_CHECK( !empty.IsValid() );
107 BOOST_CHECK( !empty.IsLegacy() );
108}
109
110
115BOOST_AUTO_TEST_CASE( SharedForbiddenListDrivesLibIdValidation )
116{
117 const wxString& libIdBad = GetLibIdForbiddenChars();
118
119 BOOST_REQUIRE( !libIdBad.IsEmpty() );
120
121 // Pin the exact sets so a silently dropped character is caught; the membership loops below
122 // only prove that whatever is in the list is rejected, not that the list stayed complete.
123 BOOST_CHECK( libIdBad == wxS( "<>\"\\:\t\n\r" ) );
124 BOOST_CHECK( GetLibFilenameForbiddenChars() == wxS( "<>\"\\:\t\n\r%$/" ) );
125
126 // Known members that must remain forbidden regardless of refactoring.
127 BOOST_CHECK( libIdBad.Find( wxUniChar( ':' ) ) != wxNOT_FOUND );
128 BOOST_CHECK( libIdBad.Find( wxUniChar( '"' ) ) != wxNOT_FOUND );
129
130 for( wxUniChar ch : libIdBad )
131 {
132 std::string name = "abc";
133 name += static_cast<char>( ch.GetValue() );
134
136 "LIB_ID accepted a shared-forbidden char 0x"
137 << std::hex << ch.GetValue() );
138 }
139}
140
141
147BOOST_AUTO_TEST_CASE( FilenameRulesSupersetOfLibId )
148{
149 const wxString& libId = GetLibIdForbiddenChars();
150 const wxString& fname = GetLibFilenameForbiddenChars();
151
152 for( wxUniChar ch : libId )
153 BOOST_CHECK( fname.Find( ch ) != wxNOT_FOUND );
154
155 for( wxUniChar ch : { wxUniChar( '%' ), wxUniChar( '$' ), wxUniChar( '/' ) } )
156 {
157 BOOST_CHECK( libId.Find( ch ) == wxNOT_FOUND );
158 BOOST_CHECK( fname.Find( ch ) != wxNOT_FOUND );
159
160 std::string name = "abc";
161 name += static_cast<char>( ch.GetValue() );
162
164 }
165}
166
167
const char * name
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
static int HasIllegalChars(const UTF8 &aLibItemName)
Examine aLibItemName for invalid LIB_ID item name characters.
Definition lib_id.cpp:189
UTF8 Format() const
Definition lib_id.cpp:132
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
bool IsLegacy() const
Definition lib_id.h:176
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
wxString wx_str() const
Definition utf8.cpp:41
static bool empty(const wxTextEntryBase *aCtrl)
const wxString & GetLibFilenameForbiddenChars()
Characters illegal in a footprint library filename.
Definition lib_id.cpp:40
const wxString & GetLibIdForbiddenChars()
Characters illegal in a LIB_ID item name.
Definition lib_id.cpp:33
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(ParseFullyQualified)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_CHECK_EQUAL(result, "25.4")