KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_markup_parser.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 along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
25
27
28// Code under test
29#include <markup_parser.h>
30#include <font/font.h>
31#include <font/stroke_font.h>
32
36BOOST_AUTO_TEST_SUITE( MarkupParser )
37
38void nodeToString( std::unique_ptr<MARKUP::NODE>& aNode, std::string& aStringToPopulate )
39{
40 aStringToPopulate += " {";
41
42 if( aNode->isOverbar() )
43 aStringToPopulate += "OVER";
44 if( aNode->isSubscript() )
45 aStringToPopulate += "SUB";
46 if( aNode->isSuperscript() )
47 aStringToPopulate += "SUP";
48
49 if( aNode->has_content() )
50 aStringToPopulate += "'" + aNode->string() + "'";
51
52 for( auto& c : aNode->children )
53 nodeToString( c, aStringToPopulate );
54
55 aStringToPopulate += "} ";
56}
57
59{
60 std::string Input;
61 std::string ExpectedResult;
62};
63
68{
69
70 std::vector<PARSE_CASE> cases =
71 {
72 {
73 "A normal string",
74 " { {'A normal string'} } "
75 },
76 {
77 "_{A subscript String}",
78 " { {SUB {'A subscript String'} } } "
79 },
80 {
81 "^{A superscript String}",
82 " { {SUP {'A superscript String'} } } "
83 },
84 {
85 "~{An overbar String}",
86 " { {OVER {'An overbar String'} } } "
87 },
88 {
89 "~{An incomplete markup",
90 " { {'~{An incomplete markup'} } "
91 },
92 {
93 "A string ~{overbar}",
94 " { {'A string '} {OVER {'overbar'} } } "
95 },
96 {
97 "A string ~{incomplete markup",
98 " { {'A string ~{incomplete markup'} } "
99 },
100 {
101 "A string ~{overbar} ~{incomplete markup",
102 " { {'A string '} {OVER {'overbar'} } {' ~{incomplete markup'} } "
103 },
104 { "A string ~{incomplete markup ~{overbar}",
105 " { {'A string ~{incomplete markup '} {OVER {'overbar'} } } "
106 }
107 };
108
109 for( auto& c : cases )
110 {
111 BOOST_TEST_INFO_SCOPE( c.Input );
112 MARKUP::MARKUP_PARSER parser( c.Input );
113
114 std::unique_ptr<MARKUP::NODE> rootNode = parser.Parse();
115 BOOST_REQUIRE( rootNode );
116
117 std::string result;
118 nodeToString( rootNode, result );
119
120 BOOST_CHECK_EQUAL( result, c.ExpectedResult );
121
122 // Uncomment for testing / generating test cases:
123 // printf( "%s\n", result.c_str() );
124 }
125
126}
127
128
135BOOST_AUTO_TEST_CASE( OverbarMultipleSpacesWidth )
136{
137 KIFONT::FONT* font = KIFONT::STROKE_FONT::LoadFont( wxEmptyString );
138
139 BOOST_REQUIRE( font );
140
141 VECTOR2I glyphSize( 1000, 1000 );
142
143 // Measure the width of a single space for reference
144 int spaceWidth = font->GetTextAsGlyphs( nullptr, nullptr, wxS( " " ), glyphSize,
145 VECTOR2I(), ANGLE_0, false, VECTOR2I(), 0 ).x;
146
147 BOOST_REQUIRE( spaceWidth > 0 );
148
149 // Measure the width of 5 spaces
150 int fiveSpaceWidth = font->GetTextAsGlyphs( nullptr, nullptr, wxS( " " ), glyphSize,
151 VECTOR2I(), ANGLE_0, false, VECTOR2I(), 0 ).x;
152
153 BOOST_CHECK_GT( fiveSpaceWidth, spaceWidth );
154
155 // Verify that LinebreakText preserves overbar with multiple spaces
156 struct LINEBREAK_CASE
157 {
158 wxString Input;
159 wxString Expected;
160 };
161
162 int wideColumn = 100000;
163
164 std::vector<LINEBREAK_CASE> cases =
165 {
166 { wxS( "~{ }" ), wxS( "~{ }" ) },
167 { wxS( "A ~{ }" ), wxS( "A ~{ }" ) },
168 { wxS( "A ~{ B }" ), wxS( "A ~{ B }" ) },
169 { wxS( "~{ } end" ), wxS( "~{ } end" ) },
170 { wxS( "/~{ }" ), wxS( "/~{ }" ) },
171 { wxS( "_{ }" ), wxS( "_{ }" ) },
172 { wxS( "^{ }" ), wxS( "^{ }" ) },
173 };
174
175 for( auto& c : cases )
176 {
177 BOOST_TEST_INFO_SCOPE( c.Input );
178
179 wxString text = c.Input;
180 font->LinebreakText( text, wideColumn, glyphSize, 0, false, false );
181 BOOST_CHECK_EQUAL( text, c.Expected );
182 }
183}
184
185
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:98
virtual VECTOR2I GetTextAsGlyphs(BOX2I *aBBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aSize, const VECTOR2I &aPosition, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const =0
Convert text string to an array of GLYPHs.
void LinebreakText(wxString &aText, int aColumnWidth, const VECTOR2I &aGlyphSize, int aThickness, bool aBold, bool aItalic) const
Insert characters into text to ensure that no lines are wider than aColumnWidth.
Definition font.cpp:609
static STROKE_FONT * LoadFont(const wxString &aFontName)
Load a stroke font.
std::unique_ptr< NODE > Parse()
STL class.
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
STL namespace.
std::string Input
std::string ExpectedResult
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
void nodeToString(std::unique_ptr< MARKUP::NODE > &aNode, std::string &aStringToPopulate)
Declare the test suite.
BOOST_AUTO_TEST_CASE(Parse)
Test the #Parse method.
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695