KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_lib_tree_model_adapter.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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <eda_pattern_match.h>
23#include <lib_id.h>
24#include <lib_tree_item.h>
25#include <lib_tree_model.h>
27#include <project.h>
29#include <wx/tokenzr.h>
30
31
38{
39public:
41 LIB_TREE_MODEL_ADAPTER( nullptr, wxT( "pinned" ), aSettings )
42 {
43 }
44
46
47 int LoadedWidth( const wxString& aColumn ) const
48 {
49 auto it = m_colWidths.find( aColumn );
50 return it != m_colWidths.end() ? it->second : -1;
51 }
52};
53
54
61{
62public:
63 TEST_LIB_TREE_ITEM( const wxString& aLibNickname, const wxString& aName,
64 const wxString& aKeywords, const wxString& aDesc ) :
65 m_libNickname( aLibNickname ),
66 m_name( aName ),
67 m_keywords( aKeywords ),
68 m_desc( aDesc )
69 {}
70
71 LIB_ID GetLIB_ID() const override { return LIB_ID( m_libNickname, m_name ); }
72 wxString GetName() const override { return m_name; }
73 wxString GetLibNickname() const override { return m_libNickname; }
74 wxString GetDesc() override { return m_desc; }
75
76 std::vector<SEARCH_TERM>& GetSearchTerms() override
77 {
78 m_searchTerms.clear();
79 m_searchTerms.emplace_back( SEARCH_TERM( m_libNickname, 4 ) );
80 m_searchTerms.emplace_back( SEARCH_TERM( m_name, 8, true ) );
81 m_searchTerms.emplace_back( SEARCH_TERM( GetLIB_ID().Format(), 16, true ) );
82
83 wxStringTokenizer tokenizer( m_keywords, wxT( " \t\r\n" ), wxTOKEN_STRTOK );
84
85 while( tokenizer.HasMoreTokens() )
86 m_searchTerms.emplace_back( SEARCH_TERM( tokenizer.GetNextToken(), 4 ) );
87
88 m_searchTerms.emplace_back( SEARCH_TERM( m_keywords, 1 ) );
89 m_searchTerms.emplace_back( SEARCH_TERM( m_desc, 1 ) );
90
91 return m_searchTerms;
92 }
93
94private:
95 wxString m_libNickname;
96 wxString m_name;
97 wxString m_keywords;
98 wxString m_desc;
99 std::vector<SEARCH_TERM> m_searchTerms;
100};
101
102
103BOOST_AUTO_TEST_SUITE( LibTreeModelAdapter )
104
105
111BOOST_AUTO_TEST_CASE( CorruptPersistedColumnWidthIgnoredOnLoad )
112{
114 settings.column_widths[ wxT( "Item" ) ] = 427218649;
115 settings.column_widths[ wxT( "Description" ) ] = 450;
116
118
119 // The corrupt width must not overwrite the default; the legitimate one must be applied.
120 BOOST_CHECK_EQUAL( adapter->LoadedWidth( wxT( "Item" ) ), 300 );
121 BOOST_CHECK_EQUAL( adapter->LoadedWidth( wxT( "Description" ) ), 450 );
122
123 adapter->DecRef();
124}
125
126
138BOOST_AUTO_TEST_CASE( SingleLetterSearchMatchesDeviceNameOverKeywordCollision )
139{
141
142 LIB_TREE_NODE_LIBRARY& device = root.AddLib( wxT( "Device" ), wxT( "Basic devices" ) );
143
144 TEST_LIB_TREE_ITEM r( wxT( "Device" ), wxT( "R" ), wxT( "R res resistor" ), wxT( "Resistor" ) );
145 TEST_LIB_TREE_ITEM l( wxT( "Device" ), wxT( "L" ), wxT( "inductor choke coil reactor magnetic" ),
146 wxT( "Inductor" ) );
147 TEST_LIB_TREE_ITEM c( wxT( "Device" ), wxT( "C" ), wxT( "cap capacitor" ), wxT( "Unpolarized capacitor" ) );
148 TEST_LIB_TREE_ITEM d( wxT( "Device" ), wxT( "D" ), wxT( "diode" ), wxT( "Diode" ) );
149
150 device.AddItem( &r );
151 device.AddItem( &l );
152 device.AddItem( &c );
153 device.AddItem( &d );
154
155 LIB_TREE_NODE_LIBRARY& potLib = root.AddLib( wxT( "Potentiometer_Digital" ), wxT( "Digital potentiometers" ) );
156 TEST_LIB_TREE_ITEM digiPot( wxT( "Potentiometer_Digital" ), wxT( "AD5293" ), wxT( "R POT" ),
157 wxT( "Digital potentiometer 1024 pos (SPI), TSSOP-14" ) );
158 potLib.AddItem( &digiPot );
159
160 LIB_TREE_NODE_LIBRARY& usbLib = root.AddLib( wxT( "Interface_USB" ), wxT( "USB interface ICs" ) );
161
162 // "USB Type C PD Sink" is AP33771's real ki_keywords field (the "C" of "Type C" tokenizes
163 // to a standalone, query-colliding "C"); "Converter Controller CMOS Clock capacitor charger
164 // coupled" are appended from other real shipped keyword fields (see e.g. buck/boost
165 // converter and MCU symbols) to model a busier real-world part clearing Device:C's score.
166 TEST_LIB_TREE_ITEM usbPd( wxT( "Interface_USB" ), wxT( "AP33771" ),
167 wxT( "USB Type C PD Sink Converter Controller CMOS Clock capacitor "
168 "charger coupled" ),
169 wxT( "USB Type-C PD Sink Controller, QFN-24" ) );
170 usbLib.AddItem( &usbPd );
171
172 LIB_TREE_NODE_LIBRARY& logicLib = root.AddLib( wxT( "74xGxx" ), wxT( "Little logic" ) );
173 TEST_LIB_TREE_ITEM dFlipFlop( wxT( "74xGxx" ), wxT( "74AUC1G74" ), wxT( "Single D Flip-Flop D CMOS" ),
174 wxT( "Single D Flip-Flop, Low-Voltage CMOS" ) );
175 logicLib.AddItem( &dFlipFlop );
176
177 // Mirrors LIB_TREE_MODEL_ADAPTER::AssignIntrinsicRanks(): rebuild search terms for the
178 // libraries, then for the items nested inside each of them.
179 root.AssignIntrinsicRanks( {} );
180
181 for( std::unique_ptr<LIB_TREE_NODE>& lib : root.m_Children )
182 lib->AssignIntrinsicRanks( {} );
183
184 for( const wxString& letter : { wxString( wxT( "r" ) ), wxString( wxT( "l" ) ),
185 wxString( wxT( "c" ) ), wxString( wxT( "d" ) ) } )
186 {
187 std::vector<std::unique_ptr<EDA_COMBINED_MATCHER>> matchers;
188 matchers.emplace_back( std::make_unique<EDA_COMBINED_MATCHER>( letter, CTX_LIBITEM ) );
189
190 root.UpdateScore( matchers, nullptr );
191 root.SortNodes( true );
192
193 BOOST_REQUIRE( !root.m_Children.empty() );
194
195 LIB_TREE_NODE* topLib = root.m_Children[0].get();
196
197 BOOST_REQUIRE( !topLib->m_Children.empty() );
198
199 LIB_TREE_NODE* topItem = topLib->m_Children[0].get();
200
201 BOOST_TEST_CONTEXT( "search letter '" << letter << "'" )
202 {
203 BOOST_CHECK_EQUAL( topLib->m_Name, wxString( wxT( "Device" ) ) );
204 BOOST_CHECK_EQUAL( topItem->m_Name, letter.Upper() );
205 BOOST_CHECK( topItem->m_ExactMatch );
206 }
207 }
208}
209
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
A mix-in to provide polymorphism between items stored in libraries (symbols, aliases and footprints).
LIB_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, const wxString &aPinnedKey, APP_SETTINGS_BASE::LIB_TREE &aSettingsStruct)
Create the adapter.
std::map< wxString, int > m_colWidths
Node type: library.
LIB_TREE_NODE_ITEM & AddItem(LIB_TREE_ITEM *aItem)
Construct a new alias node, add it to this library, and return it.
Node type: root.
LIB_TREE_NODE_LIBRARY & AddLib(wxString const &aName, wxString const &aDesc)
Construct an empty library node, add it to the root, and return it.
void UpdateScore(const std::vector< std::unique_ptr< EDA_COMBINED_MATCHER > > &aMatchers, std::function< bool(LIB_TREE_NODE &aNode)> *aFilter) override
Update the score for this part.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
void SortNodes(bool aUseScores)
Sort child nodes quickly and recursively (IntrinsicRanks must have been set).
PTR_VECTOR m_Children
void AssignIntrinsicRanks(const std::vector< wxString > &aShownColumns, bool presorted=false)
Store intrinsic ranks on all children of this node.
@ SYMBOL_LIB
Definition project.h:191
Stands in for LIB_SYMBOL, reproducing LIB_SYMBOL::cacheSearchTerms() term-for-term so the test exerci...
wxString GetName() const override
LIB_ID GetLIB_ID() const override
TEST_LIB_TREE_ITEM(const wxString &aLibNickname, const wxString &aName, const wxString &aKeywords, const wxString &aDesc)
wxString GetLibNickname() const override
std::vector< SEARCH_TERM > & GetSearchTerms() override
std::vector< SEARCH_TERM > m_searchTerms
Minimal concrete adapter exposing the loaded column widths.
PROJECT::LIB_TYPE_T getLibType() override
TEST_LIB_TREE_MODEL_ADAPTER(APP_SETTINGS_BASE::LIB_TREE &aSettings)
int LoadedWidth(const wxString &aColumn) const
Abstract pattern-matching tool and implementations.
@ CTX_LIBITEM
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition ptree.cpp:194
std::map< wxString, int > column_widths
Column widths, keyed by header name.
A structure for storing weighted search terms.
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(CorruptPersistedColumnWidthIgnoredOnLoad)
A persisted column width can become corrupt after a mixed-DPI monitor change (issue 24702 reported wi...
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_CHECK_EQUAL(result, "25.4")