KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_connectivity_facts.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
24#include <locale_io.h>
25#include <project.h>
26#include <schematic.h>
27#include <sch_field.h>
28#include <sch_label.h>
29#include <sch_screen.h>
30#include <sch_sheet_path.h>
32#include <string_utils.h>
33
34#include <algorithm>
35
36using namespace SCH_CONNECTIVITY;
37
38BOOST_AUTO_TEST_SUITE( ConnectivityFacts )
39
40BOOST_AUTO_TEST_CASE( InputTextDoesNotReadThePreviousNet )
41{
42 LOCALE_IO locale;
43 SETTINGS_MANAGER settings;
44 std::unique_ptr<SCHEMATIC> schematic;
45 KI_TEST::LoadSchematic( settings, wxS( "issue18346" ), schematic );
46 const SCH_SHEET_PATH path = schematic->Hierarchy().front();
47 SCH_SCREEN& screen = *path.LastScreen();
48 auto* label = dynamic_cast<SCH_LABEL_BASE*>(
49 screen.GetConnectivityItem( KIID( "aca19205-a0bf-4ee3-bb76-dee6f12714b1" ) ) );
50 BOOST_REQUIRE( label );
51 label->Move( VECTOR2I( 100000000, 100000000 ) );
52
53 for( const wxString& previous : { wxString( "old_net" ), wxString( "another_net" ) } )
54 {
55 label->SetText( previous );
56 schematic->RebuildConnectivity();
57 const auto previousName = label->GetConnectionName( &path, false, true );
58 BOOST_REQUIRE( previousName );
59 BOOST_CHECK_EQUAL( *previousName, previous );
60
61 for( const wxString& token : { wxString( "NET_NAME" ), wxString( "SHORT_NET_NAME" ), wxString( "NET_CLASS" ) } )
62 {
63 const wxString expression = wxS( "${" ) + token + wxS( "}" );
64 schematic->Project().GetTextVars()[wxS( "S0_OUTPUT" )] = expression;
65
66 for( const wxString& input : { expression, wxString( "${S0_OUTPUT}" ) } )
67 {
68 label->SetText( wxS( "input_" ) + input );
69 const INSTANCE_FACTS instance = ExtractInstanceFacts( ExtractScreenFacts( screen ), screen, path );
70 auto text = std::find_if( instance.items.begin(), instance.items.end(),
71 [&]( const ITEM_TEXT_FACT& aText )
72 {
73 return aText.id == label->m_Uuid;
74 } );
75 BOOST_REQUIRE( text != instance.items.end() );
76 BOOST_CHECK_EQUAL( text->name, wxString( "input_" ) );
77 }
78 }
79
80 // Legacy is the reference for both engines; the hold keeps the engine row across the text bump
81 PUBLICATION_HOLD hold( schematic->Connectivity() );
82 label->SetText( wxS( "${SHORT_NET_NAME}" ) );
83 BOOST_CHECK_EQUAL( label->GetShownText( &path, FOR_NETNAME ), previous );
84 }
85}
86
87BOOST_AUTO_TEST_CASE( NestedLabelFieldsUseTheExplicitPath )
88{
89 LOCALE_IO locale;
90 SETTINGS_MANAGER settings;
91 std::unique_ptr<SCHEMATIC> schematic;
92 KI_TEST::LoadSchematic( settings, wxS( "issue23962/issue23962" ), schematic );
93 BOOST_REQUIRE_GT( schematic->Hierarchy().size(), 1 );
94 size_t checked = 0;
95
96 for( const SCH_SHEET_PATH& path : schematic->Hierarchy() )
97 {
98 SCH_SCREEN& screen = *path.LastScreen();
99
100 for( SCH_ITEM* item : screen.Items().OfType( SCH_DIRECTIVE_LABEL_T ) )
101 {
102 auto* label = static_cast<SCH_LABEL_BASE*>( item );
103
104 if( label->GetFields().empty() )
105 continue;
106
107 SCH_FIELD& field = label->GetFields().front();
108 field.SetText( wxS( "scope_${SHEETNAME}" ) );
109 label->SetText( wxS( "${" ) + field.GetName() + wxS( "}" ) );
110 const wxString expected = EscapeString( field.GetShownText( &path, FOR_NETNAME ), CTX_NETNAME );
111 const SCREEN_FACTS facts = ExtractScreenFacts( screen );
112
113 for( const SCH_SHEET_PATH& current : schematic->Hierarchy() )
114 {
115 schematic->SetCurrentSheet( current );
116 const INSTANCE_FACTS instance = ExtractInstanceFacts( facts, screen, path );
117 auto text = std::find_if( instance.items.begin(), instance.items.end(),
118 [&]( const ITEM_TEXT_FACT& aText )
119 {
120 return aText.id == label->m_Uuid;
121 } );
122 BOOST_REQUIRE( text != instance.items.end() );
124 ++checked;
125 }
126 }
127 }
128
129 BOOST_CHECK_GT( checked, 0 );
130}
131
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
Definition kiid.h:46
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
Keep the last publication readable while a batch stages edits before its single commit.
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString, int aDepth=0) const
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
void SetText(const wxString &aText) override
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
SCH_ITEM * GetConnectivityItem(const KIID &aId) const
Resolve a drawing item or a connectable child on this screen; ambiguous IDs return null.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
@ FOR_NETNAME
Definition common.h:90
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
Value keys and the key session of the schematic connectivity engine.
INSTANCE_FACTS ExtractInstanceFacts(const SCREEN_FACTS &aFacts, const SCH_SCREEN &aScreen, const SCH_SHEET_PATH &aPath)
SCREEN_FACTS ExtractScreenFacts(const SCH_SCREEN &aScreen, std::shared_ptr< const LIBRARY_SYMBOL_FACTS > aLibrarySymbols, const SCREEN_FACTS *aUnchangedNonLines)
Main-thread snapshots; no derived connectivity or model pointers survive extraction.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_NETNAME
The text and unit data that one sheet instance resolves for the items of its screen.
Definition conn_facts.h:348
std::vector< ITEM_TEXT_FACT > items
Definition conn_facts.h:355
The value copy of one screen.
Definition conn_facts.h:251
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(InputTextDoesNotReadThePreviousNet)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
VECTOR3I expected(15, 30, 45)
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:167
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683