KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sexpr_test_utils.h
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) 2019 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 2
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/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 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#ifndef TEST_SEXPR_TEST_UTILS__H
25#define TEST_SEXPR_TEST_UTILS__H
26
27#include <sexpr/sexpr.h>
28
30
31namespace KI_TEST
32{
33
34
38inline SEXPR::SEXPR_TYPE getType( const SEXPR::SEXPR& aSexpr )
39{
40 if( aSexpr.IsList() )
42 else if( aSexpr.IsSymbol() )
44 else if( aSexpr.IsString() )
46 else if( aSexpr.IsDouble() )
48 else if( aSexpr.IsInteger() )
50
51 throw( std::invalid_argument( "<Unknown S-Expression Type!>" ) );
52}
53
57inline std::string getDebugType( SEXPR::SEXPR_TYPE aType )
58{
59 switch( aType )
60 {
62 return "List";
64 return "Symbol";
66 return "String";
68 return "Double";
70 return "Integer";
71 }
72
73 return "<Unknown S-Expression Type!>";
74}
75
76inline std::string GetSexprDebugType( const SEXPR::SEXPR& aSexpr )
77{
78 return getDebugType( getType( aSexpr ) );
79}
80
81inline bool IsSexprOfType( const SEXPR::SEXPR& aSexpr, SEXPR::SEXPR_TYPE aType )
82{
83 if( getType( aSexpr ) != aType )
84 {
85 BOOST_TEST_MESSAGE( "Sexpr is not of type: " << getDebugType( aType ) );
86 return false;
87 }
88
89 return true;
90}
91
97template <typename VAL_T>
98bool IsSexprValueEqual( const VAL_T& aGot, const VAL_T& aExpected )
99{
100 if( aGot != aExpected )
101 {
102 BOOST_TEST_MESSAGE( "Sexpr value not equal: got " << aGot << ", expected " << aExpected );
103 return false;
104 }
105
106 return true;
107}
108
112inline bool SexprIsSymbol( const SEXPR::SEXPR& aSexpr )
113{
115}
116
120inline bool SexprIsSymbolWithValue( const SEXPR::SEXPR& aSexpr, const std::string& aVal )
121{
123 && IsSexprValueEqual( aSexpr.GetSymbol(), aVal );
124}
125
129inline bool SexprIsString( const SEXPR::SEXPR& aSexpr )
130{
132}
133
137inline bool SexprIsStringWithValue( const SEXPR::SEXPR& aSexpr, const std::string& aVal )
138{
140 && IsSexprValueEqual( aSexpr.GetString(), aVal );
141}
142
146inline bool SexprIsInteger( const SEXPR::SEXPR& aSexpr )
147{
149}
150
154inline bool SexprIsIntegerWithValue( const SEXPR::SEXPR& aSexpr, std::int64_t aVal )
155{
157 && IsSexprValueEqual( aSexpr.GetLongInteger(), aVal );
158}
159
163inline bool SexprIsDouble( const SEXPR::SEXPR& aSexpr )
164{
166}
167
171inline bool SexprIsDoubleWithValue( const SEXPR::SEXPR& aSexpr, double aVal )
172{
174 && IsSexprValueEqual( aSexpr.GetDouble(), aVal );
175}
176
180inline bool SexprIsList( const SEXPR::SEXPR& aSexpr )
181{
183}
184
188inline bool SexprIsListOfLength( const SEXPR::SEXPR& aSexpr, size_t aExpectedLength )
189{
191 return false;
192
193 if( aSexpr.GetNumberOfChildren() != aExpectedLength )
194 {
195 BOOST_TEST_MESSAGE( "List is wrong length: got " << aSexpr.GetNumberOfChildren()
196 << ", expected " << aExpectedLength );
197 return false;
198 }
199
200 return true;
201}
202
209inline bool SexprConvertsToString( const SEXPR::SEXPR& aSexpr, const std::string& aExpStr )
210{
211 const std::string converted = aSexpr.AsString();
212
213 bool ok = true;
214
215 if( converted != aExpStr )
216 {
217 BOOST_TEST_INFO( "Sexpr string conversion mismatch: got '" << converted << "', expected '"
218 << aExpStr << "'" );
219 ok = false;
220 }
221
222 return ok;
223}
224
225} // namespace KI_TEST
226
227
228namespace SEXPR
229{
233inline std::ostream& boost_test_print_type( std::ostream& os, const SEXPR& aSexpr )
234{
235 os << "SEXPR [ " << KI_TEST::GetSexprDebugType( aSexpr ) << " ]\n " << aSexpr.AsString();
236 return os;
237}
238
239} // namespace SEXPR
240
241#endif // TEST_SEXPR_TEST_UTILS__H
bool IsInteger() const
Definition: sexpr.h:53
std::string const & GetSymbol() const
Definition: sexpr.cpp:128
std::string AsString(size_t aLevel=0) const
Definition: sexpr.cpp:151
bool IsString() const
Definition: sexpr.h:51
size_t GetNumberOfChildren() const
Definition: sexpr.cpp:70
bool IsList() const
Definition: sexpr.h:49
int64_t GetLongInteger() const
Definition: sexpr.cpp:95
bool IsDouble() const
Definition: sexpr.h:52
bool IsSymbol() const
Definition: sexpr.h:50
std::string const & GetString() const
Definition: sexpr.cpp:80
double GetDouble() const
Definition: sexpr.cpp:105
bool SexprIsDouble(const SEXPR::SEXPR &aSexpr)
Test predicate: is the s-expression a double?
bool SexprIsList(const SEXPR::SEXPR &aSexpr)
Test predicate: is the s-expression a double?
bool SexprIsSymbolWithValue(const SEXPR::SEXPR &aSexpr, const std::string &aVal)
Test predicate: is the s-expression a symbol with the given value?
std::string getDebugType(SEXPR::SEXPR_TYPE aType)
Get a debug-friendly string for a given s-expr type.
bool IsSexprOfType(const SEXPR::SEXPR &aSexpr, SEXPR::SEXPR_TYPE aType)
bool SexprIsInteger(const SEXPR::SEXPR &aSexpr)
Test predicate: is the s-expression an integer?
bool SexprIsDoubleWithValue(const SEXPR::SEXPR &aSexpr, double aVal)
Test predicate: is the s-expression a double with the given value?
bool SexprIsListOfLength(const SEXPR::SEXPR &aSexpr, size_t aExpectedLength)
Test predicate: is the s-expression a list with the given length?
SEXPR::SEXPR_TYPE getType(const SEXPR::SEXPR &aSexpr)
Get the type of the s-expression.
std::string GetSexprDebugType(const SEXPR::SEXPR &aSexpr)
bool SexprIsIntegerWithValue(const SEXPR::SEXPR &aSexpr, std::int64_t aVal)
Test predicate: is the s-expression an integer with the given value?
bool SexprIsStringWithValue(const SEXPR::SEXPR &aSexpr, const std::string &aVal)
Test predicate: is the s-expression a string with the given value?
bool SexprIsSymbol(const SEXPR::SEXPR &aSexpr)
Test predicate: is the s-expression a symbol?
bool IsSexprValueEqual(const VAL_T &aGot, const VAL_T &aExpected)
Predicate to check two s-expr values (of the same type) are equal.
bool SexprConvertsToString(const SEXPR::SEXPR &aSexpr, const std::string &aExpStr)
Predicate to check an SEXPR object converts to the expected string.
bool SexprIsString(const SEXPR::SEXPR &aSexpr)
Test predicate: is the s-expression a string?
std::ostream & boost_test_print_type(std::ostream &os, const SEXPR &aSexpr)
Boost print helper for SEXPR objects.
SEXPR_TYPE
Definition: sexpr.h:35