KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_eda_text.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 (C) 2023 Wayne Stambaugh <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <boost/test/unit_test.hpp>
22#include <algorithm>
23#include <base_units.h>
24#include <eda_text.h>
25#include <text_var_dependency.h>
26
27
28BOOST_AUTO_TEST_SUITE( EdaText )
29
30
32{
33 std::hash<EDA_TEXT> hasher;
36
37 BOOST_CHECK_EQUAL( a, b );
38 BOOST_CHECK_EQUAL( hasher( a ), hasher( b ) );
39
40 a.SetText( wxS( "A" ) );
41 BOOST_CHECK_GT( a, b );
42 BOOST_CHECK_NE( hasher( a ), hasher( b ) );
43
44 b.SetText( wxS( "B" ) );
45 BOOST_CHECK_LT( a, b );
46 BOOST_CHECK_NE( hasher( a ), hasher( b ) );
47
48 a.SetText( wxS( "B" ) );
49 a.SetTextPos( VECTOR2I( 1, 0 ) );
50 BOOST_CHECK_GT( a, b );
51
52 a.SetTextPos( VECTOR2I( -1, 0 ) );
53 BOOST_CHECK_LT( a, b );
54
55 a.SetTextPos( VECTOR2I( 0, 0 ) );
56 b.SetTextPos( VECTOR2I( 0, 1 ) );
57 BOOST_CHECK_LT( a, b );
58
59 b.SetTextPos( VECTOR2I( 0, -1 ) );
60 BOOST_CHECK_GT( a, b );
61
62 // Text attributes are tested in the TEXT_ATTRIBUTES unit tests.
63}
64
65
66BOOST_AUTO_TEST_CASE( TextVarReferences_EmptyWhenNoVars )
67{
69 t.SetText( wxS( "plain text" ) );
70 BOOST_CHECK( t.GetTextVarReferences().empty() );
71 BOOST_CHECK( !t.HasTextVars() );
72}
73
74
75BOOST_AUTO_TEST_CASE( TextVarReferences_CapturedAfterSetText )
76{
78 t.SetText( wxS( "${VALUE} - ${U1:MPN}" ) );
79
80 BOOST_CHECK( t.HasTextVars() );
81
82 const auto& refs = t.GetTextVarReferences();
83 BOOST_REQUIRE_EQUAL( refs.size(), 2u );
84
85 auto hasKey = [&]( TEXT_VAR_REF_KEY::KIND k, const wxString& p, const wxString& s )
86 {
87 return std::any_of( refs.begin(), refs.end(),
88 [&]( const TEXT_VAR_REF_KEY& ref )
89 { return ref.kind == k && ref.primary == p && ref.secondary == s; } );
90 };
91
92 BOOST_CHECK( hasKey( TEXT_VAR_REF_KEY::KIND::LOCAL, wxS( "VALUE" ), wxS( "" ) ) );
93 BOOST_CHECK( hasKey( TEXT_VAR_REF_KEY::KIND::CROSS_REF, wxS( "U1" ), wxS( "MPN" ) ) );
94}
95
96
97BOOST_AUTO_TEST_CASE( TextVarReferences_InvalidatedOnRetext )
98{
100 t.SetText( wxS( "${VALUE}" ) );
101 BOOST_REQUIRE_EQUAL( t.GetTextVarReferences().size(), 1u );
102
103 t.SetText( wxS( "${REFERENCE}" ) );
104 const auto& refs = t.GetTextVarReferences();
105 BOOST_REQUIRE_EQUAL( refs.size(), 1u );
106 BOOST_CHECK( refs[0].primary == wxS( "REFERENCE" ) );
107}
108
109
110BOOST_AUTO_TEST_CASE( TextVarReferences_StableReferenceBetweenReads )
111{
112 // Refs are populated eagerly in cacheShownText() and stored as a plain
113 // member; two successive const reads must return the same storage address
114 // so listeners can hold the reference without copying.
115 EDA_TEXT t( unityScale );
116 t.SetText( wxS( "${X} and ${Y}" ) );
117
118 const auto& first = t.GetTextVarReferences();
119 const auto& second = t.GetTextVarReferences();
120
121 BOOST_REQUIRE_EQUAL( first.size(), 2u );
122 BOOST_CHECK( &first == &second );
123}
124
125
126BOOST_AUTO_TEST_CASE( TextVarReferences_EscapedLiteralNotEdge )
127{
128 // Finding 6 from codex review: extraction must run on raw m_text, not
129 // m_shown_text (post-UnescapeString). A backslash-escaped `\${VAR}` is a
130 // user literal and must NOT produce a dependency edge.
131 EDA_TEXT t( unityScale );
132 t.SetText( wxS( "literal: \\${VAR}" ) );
133
134 BOOST_CHECK( t.GetTextVarReferences().empty() );
135}
136
137
constexpr EDA_IU_SCALE unityScale
Definition base_units.h:128
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:93
const std::vector< TEXT_VAR_REF_KEY > & GetTextVarReferences() const
Return the set of ${...} references extracted from the source text.
Definition eda_text.cpp:649
void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:576
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition eda_text.h:133
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:269
Identifies a single resolvable source that a text item's ${...} reference depends on.
KIND
Categorizes a reference by the source that will produce its value.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(Compare)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687