KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_doc_property_helpers.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 3
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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <boost/test/unit_test.hpp>
25
27
28
29using namespace KICAD_DIFF;
30
31
32BOOST_AUTO_TEST_SUITE( DocPropertyHelpers )
33
34
35// AppendPaperDeltas ---------------------------------------------------------
36
37BOOST_AUTO_TEST_CASE( AppendPaperDeltas_IdenticalProducesNoDeltas )
38{
39 PAGE_INFO before( PAGE_SIZE_TYPE::A4, /*portrait*/ false );
40 PAGE_INFO after ( PAGE_SIZE_TYPE::A4, /*portrait*/ false );
41
42 std::vector<PROPERTY_DELTA> deltas;
43 AppendPaperDeltas( deltas, before, after );
44
45 BOOST_CHECK( deltas.empty() );
46}
47
48
49BOOST_AUTO_TEST_CASE( AppendPaperDeltas_FormatChangeOnly )
50{
51 PAGE_INFO before( PAGE_SIZE_TYPE::A4, false );
52 PAGE_INFO after ( PAGE_SIZE_TYPE::A3, false );
53
54 std::vector<PROPERTY_DELTA> deltas;
55 AppendPaperDeltas( deltas, before, after );
56
57 BOOST_REQUIRE_EQUAL( deltas.size(), 1u );
58 BOOST_CHECK( deltas[0].name == DOC_PROP_PAGE_FORMAT );
59 // Format is encoded as ENUM with both the int (PAGE_SIZE_TYPE) and the
60 // label string (GetTypeAsString). Pin both halves — a regression that
61 // dropped the label would still pass an int-only check.
62 BOOST_CHECK( deltas[0].before.GetType() == DIFF_VALUE::T::ENUM );
63 BOOST_CHECK( deltas[0].after.GetType() == DIFF_VALUE::T::ENUM );
64 BOOST_CHECK_EQUAL( deltas[0].before.AsEnum().first,
65 static_cast<int>( PAGE_SIZE_TYPE::A4 ) );
66 BOOST_CHECK_EQUAL( deltas[0].after.AsEnum().first,
67 static_cast<int>( PAGE_SIZE_TYPE::A3 ) );
68 BOOST_CHECK_EQUAL( deltas[0].before.AsEnum().second,
69 before.GetTypeAsString().ToStdString() );
70 BOOST_CHECK_EQUAL( deltas[0].after.AsEnum().second,
71 after.GetTypeAsString().ToStdString() );
72}
73
74
75BOOST_AUTO_TEST_CASE( AppendPaperDeltas_OrientationChangeOnly )
76{
77 PAGE_INFO before( PAGE_SIZE_TYPE::A4, /*portrait*/ false );
78 PAGE_INFO after ( PAGE_SIZE_TYPE::A4, /*portrait*/ true );
79
80 std::vector<PROPERTY_DELTA> deltas;
81 AppendPaperDeltas( deltas, before, after );
82
83 BOOST_REQUIRE_EQUAL( deltas.size(), 1u );
84 BOOST_CHECK( deltas[0].name == DOC_PROP_PAGE_ORIENTATION );
85 // Orientation is encoded as BOOL (the IsPortrait flag).
86 BOOST_CHECK( deltas[0].before.GetType() == DIFF_VALUE::T::BOOL );
87 BOOST_CHECK( deltas[0].after.GetType() == DIFF_VALUE::T::BOOL );
88 BOOST_CHECK_EQUAL( deltas[0].before.AsBool(), false );
89 BOOST_CHECK_EQUAL( deltas[0].after.AsBool(), true );
90}
91
92
93BOOST_AUTO_TEST_CASE( AppendPaperDeltas_BothChange_EmitsTwoInDocumentedOrder )
94{
95 // Format delta MUST be appended before orientation delta — downstream
96 // marker rendering expects this stable order, so a refactor that flips
97 // them should trip the test.
98 PAGE_INFO before( PAGE_SIZE_TYPE::A4, false );
99 PAGE_INFO after ( PAGE_SIZE_TYPE::A3, true );
100
101 std::vector<PROPERTY_DELTA> deltas;
102 AppendPaperDeltas( deltas, before, after );
103
104 BOOST_REQUIRE_EQUAL( deltas.size(), 2u );
105 BOOST_CHECK( deltas[0].name == DOC_PROP_PAGE_FORMAT );
106 BOOST_CHECK( deltas[1].name == DOC_PROP_PAGE_ORIENTATION );
107}
108
109
110BOOST_AUTO_TEST_CASE( AppendPaperDeltas_AppendsToExistingVector )
111{
112 // The helper appends; it must not replace caller-seeded entries. The
113 // PCB and SCH callers stage additional document-level deltas (board
114 // thickness, drawing sheet, etc.) in the same vector before/after
115 // calling AppendPaperDeltas.
116 std::vector<PROPERTY_DELTA> deltas;
117 PROPERTY_DELTA preExisting;
118 preExisting.name = wxS( "Sentinel" );
119 deltas.push_back( preExisting );
120
121 PAGE_INFO before( PAGE_SIZE_TYPE::A4, false );
122 PAGE_INFO after ( PAGE_SIZE_TYPE::A3, false );
123 AppendPaperDeltas( deltas, before, after );
124
125 BOOST_REQUIRE_EQUAL( deltas.size(), 2u );
126 BOOST_CHECK( deltas[0].name == wxS( "Sentinel" ) );
127 BOOST_CHECK( deltas[1].name == DOC_PROP_PAGE_FORMAT );
128}
129
130
131// AppendTitleBlockDeltas / ApplyTitleBlockProperty --------------------------
132
133BOOST_AUTO_TEST_CASE( AppendTitleBlockDeltas_IdenticalProducesNoDeltas )
134{
135 TITLE_BLOCK before;
136 before.SetTitle( wxS( "Board" ) );
137 TITLE_BLOCK after = before;
138
139 std::vector<PROPERTY_DELTA> deltas;
140 AppendTitleBlockDeltas( deltas, before, after );
141
142 BOOST_CHECK( deltas.empty() );
143}
144
145
146BOOST_AUTO_TEST_CASE( AppendTitleBlockDeltas_ReportsEachChangedField )
147{
148 TITLE_BLOCK before;
149 before.SetTitle( wxS( "Board" ) );
150 before.SetComment( 2, wxS( "third" ) );
151
152 TITLE_BLOCK after;
153 after.SetTitle( wxS( "Board v2" ) );
154 after.SetRevision( wxS( "B" ) );
155 after.SetComment( 2, wxS( "third, edited" ) );
156
157 std::vector<PROPERTY_DELTA> deltas;
158 AppendTitleBlockDeltas( deltas, before, after );
159
160 BOOST_REQUIRE_EQUAL( deltas.size(), 3u );
161 BOOST_CHECK( deltas[0].name == DOC_PROP_TITLE );
162 BOOST_CHECK_EQUAL( deltas[0].before.AsString().ToStdString(), "Board" );
163 BOOST_CHECK_EQUAL( deltas[0].after.AsString().ToStdString(), "Board v2" );
164 BOOST_CHECK( deltas[1].name == DOC_PROP_REVISION );
165 BOOST_CHECK_EQUAL( deltas[1].before.AsString().ToStdString(), "" );
166 BOOST_CHECK_EQUAL( deltas[1].after.AsString().ToStdString(), "B" );
167 BOOST_CHECK( deltas[2].name == DocPropComment( 2 ) );
168 BOOST_CHECK_EQUAL( deltas[2].before.AsString().ToStdString(), "third" );
169 BOOST_CHECK_EQUAL( deltas[2].after.AsString().ToStdString(), "third, edited" );
170}
171
172
173BOOST_AUTO_TEST_CASE( ApplyTitleBlockProperty_CopiesOnlyTheNamedField )
174{
176 source.SetTitle( wxS( "Theirs" ) );
177 source.SetRevision( wxS( "C" ) );
178
179 TITLE_BLOCK target;
180 target.SetTitle( wxS( "Ours" ) );
181
182 BOOST_CHECK( ApplyTitleBlockProperty( target, DOC_PROP_REVISION, source ) );
183 BOOST_CHECK_EQUAL( target.GetRevision().ToStdString(), "C" );
184 BOOST_CHECK_EQUAL( target.GetTitle().ToStdString(), "Ours" );
185 BOOST_CHECK( !ApplyTitleBlockProperty( target, DOC_PROP_PAGE_FORMAT, source ) );
186}
187
188
const char * name
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
wxString GetTypeAsString() const
const PAGE_SIZE_TYPE & GetType() const
Definition page_info.h:97
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition title_block.h:38
void SetRevision(const wxString &aRevision)
Definition title_block.h:78
void SetComment(int aIdx, const wxString &aComment)
Definition title_block.h:98
const wxString & GetRevision() const
Definition title_block.h:83
void SetTitle(const wxString &aTitle)
Definition title_block.h:55
const wxString & GetTitle() const
Definition title_block.h:60
std::string source
wxString DocPropComment(int aIndex)
bool ApplyTitleBlockProperty(TITLE_BLOCK &aTarget, const wxString &aName, const TITLE_BLOCK &aSource)
Copy the title block field named by a document property from aSource into aTarget.
const wxString DOC_PROP_PAGE_FORMAT
Property-name keys for the synthetic document-level ITEM_CHANGE (empty KIID_PATH).
const wxString DOC_PROP_REVISION
void AppendTitleBlockDeltas(std::vector< PROPERTY_DELTA > &aDeltas, const TITLE_BLOCK &aBefore, const TITLE_BLOCK &aAfter)
Append a delta for every title block field that differs between the two documents,...
const wxString DOC_PROP_PAGE_ORIENTATION
void AppendPaperDeltas(std::vector< PROPERTY_DELTA > &aDeltas, const PAGE_INFO &aBefore, const PAGE_INFO &aAfter)
Append DOC_PROP_PAGE_FORMAT and/or DOC_PROP_PAGE_ORIENTATION deltas to aDeltas when the two PAGE_INFO...
const wxString DOC_PROP_TITLE
Title block fields. The comments are numbered from one, as the page settings dialog shows them.
Single (name, before, after) triple for one mutated property on an item.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(AppendPaperDeltas_IdenticalProducesNoDeltas)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")