KiCad PCB EDA Suite
Loading...
Searching...
No Matches
unit_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 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 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 UNIT_TEST_UTILS__H
25#define UNIT_TEST_UTILS__H
26
27#define BOOST_NO_AUTO_PTR
28
29#include <boost/test/unit_test.hpp>
30#include <turtle/mock.hpp>
31
33
34#include <functional>
35#include <optional>
36#include <set>
37
38#include <wx/gdicmn.h>
39#include <wx/string.h>
40
41
42
43template<class T>
45{
46 PRINTABLE_OPT( const std::optional<T>& aOpt ) : m_Opt( aOpt ){};
47 PRINTABLE_OPT( const T& aVal ) : m_Opt( aVal ){};
48
49 std::optional<T> m_Opt;
50};
51
52
56#define KI_CHECK_OPT_EQUAL( lhs, rhs ) \
57 BOOST_CHECK_EQUAL( PRINTABLE_OPT( lhs ), PRINTABLE_OPT( rhs ) )
58
59
60template <class T>
61inline std::ostream& operator<<( std::ostream& aOs, const PRINTABLE_OPT<T>& aOptional )
62{
63 if( aOptional.m_Opt.has_value() )
64 aOs << *aOptional.m_Opt;
65 else
66 aOs << "nullopt";
67
68 return aOs;
69}
70
71
72template <class L, class R>
73inline bool operator==( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& aRhs )
74{
75 if( !aLhs.m_Opt.has_value() && !aRhs.m_Opt.has_value() )
76 return true; // both nullopt
77
78 return aLhs.m_Opt.has_value() && aRhs.m_Opt.has_value() && *aLhs.m_Opt == *aRhs.m_Opt;
79}
80
81
82template <class L, class R>
83inline bool operator!=( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& aRhs )
84{
85 return !( aLhs == aRhs );
86}
87
88
89// boost_test_print_type has to be in the same namespace as the printed type
90namespace std
91{
92
96template <typename T>
97std::ostream& boost_test_print_type( std::ostream& os, std::vector<T> const& aVec )
98{
99 os << "std::vector size " << aVec.size() << " [";
100
101 for( const auto& i : aVec )
102 {
103 os << "\n " << i;
104 }
105
106 os << "]";
107 return os;
108}
109
113template <typename K, typename V>
114std::ostream& boost_test_print_type( std::ostream& os, std::map<K, V> const& aMap )
115{
116 os << "std::map size " << aMap.size() << " [";
117
118 for( const auto& [key, value] : aMap )
119 {
120 os << "\n " << key << " = " << value;
121 }
122
123 os << "]";
124 return os;
125}
126
130template <typename K, typename V>
131std::ostream& boost_test_print_type( std::ostream& os, std::pair<K, V> const& aPair )
132{
133 os << "[" << aPair.first << ", " << aPair.second << "]";
134 return os;
135}
136
137} // namespace std
138
139
140//-----------------------------------------------------------------------------+
141// Boost.Test printing helpers for wx types / wide string literals
142//-----------------------------------------------------------------------------+
143namespace boost { namespace test_tools { namespace tt_detail {
144
145template<>
146struct print_log_value<wxString>
147{
148 void operator()( std::ostream& os, wxString const& v )
149 {
150#if wxUSE_UNICODE
151 os << v.ToUTF8().data();
152#else
153 os << v;
154#endif
155 }
156};
157
158// Wide string literal arrays
159template<std::size_t N>
160struct print_log_value<wchar_t[ N ]>
161{
162 void operator()( std::ostream& os, const wchar_t (&ws)[ N ] )
163 {
164 wxString tmp( ws );
165#if wxUSE_UNICODE
166 os << tmp.ToUTF8().data();
167#else
168 os << tmp;
169#endif
170 }
171};
172
173}}} // namespace boost::test_tools::tt_detail
174
175
180std::ostream& boost_test_print_type( std::ostream& os, wxPoint const& aVec );
181
182namespace KI_TEST
183{
184
185template <typename EXP_CONT> using EXP_OBJ = typename EXP_CONT::value_type;
186template <typename FOUND_CONT> using FOUND_OBJ = typename FOUND_CONT::value_type;
187
204template <typename EXP_OBJ, typename FOUND_OBJ>
205using MATCH_PRED = std::function<bool( const EXP_OBJ&, const FOUND_OBJ& )>;
206
241template <typename EXP_CONT, typename FOUND_CONT, typename MATCH_PRED>
242void CheckUnorderedMatches( const EXP_CONT& aExpected, const FOUND_CONT& aFound,
243 MATCH_PRED aMatchPredicate )
244{
245 using EXP_OBJ = typename EXP_CONT::value_type;
246
247 // set of object we've already found
248 std::set<const EXP_OBJ*> matched;
249
250 // fill the set of object that match
251 for( const auto& found : aFound )
252 {
253 for( const auto& expected : aExpected )
254 {
255 if( aMatchPredicate( expected, found ) )
256 {
257 matched.insert( &expected );
258 break;
259 }
260 }
261 }
262
263 // first check every expected object was "found"
264 for( const EXP_OBJ& exp : aExpected )
265 {
266 BOOST_CHECK_MESSAGE( matched.count( &exp ) > 0, "Expected item was not found. Expected: \n"
267 << exp );
268 }
269
270 // check every "found" object was expected
271 for( const EXP_OBJ* found : matched )
272 {
273 const bool was_expected = std::find_if( aExpected.begin(), aExpected.end(),
274 [found]( const EXP_OBJ& aObj )
275 {
276 return &aObj == found;
277 } ) != aExpected.end();
278
279 BOOST_CHECK_MESSAGE( was_expected, "Found item was not expected. Found: \n" << *found );
280 }
281}
282
283
287template <typename T>
288bool CollectionHasNoDuplicates( const T& aCollection )
289{
290 T sorted = aCollection;
291 std::sort( sorted.begin(), sorted.end() );
292
293 return std::adjacent_find( sorted.begin(), sorted.end() ) == sorted.end();
294}
295
296
304{
305 std::string m_CaseName;
306
307 friend std::ostream& operator<<( std::ostream& os, const NAMED_CASE& aCase )
308 {
309 os << aCase.m_CaseName;
310 return os;
311 }
312};
313
314
321#ifdef DEBUG
322#define CHECK_WX_ASSERT( STATEMENT ) BOOST_CHECK_THROW( STATEMENT, KI_TEST::WX_ASSERT_ERROR );
323#else
324#define CHECK_WX_ASSERT( STATEMENT )
325#endif
326
335std::string GetEeschemaTestDataDir();
336
337std::string GetTestDataRootDir();
338
339void SetMockConfigDir();
340
341} // namespace KI_TEST
342
343#endif // UNIT_TEST_UTILS__H
std::string GetTestDataRootDir()
void CheckUnorderedMatches(const EXP_CONT &aExpected, const FOUND_CONT &aFound, MATCH_PRED aMatchPredicate)
Check that a container of "found" objects matches a container of "expected" objects.
typename FOUND_CONT::value_type FOUND_OBJ
std::function< bool(const EXP_OBJ &, const FOUND_OBJ &)> MATCH_PRED
A match predicate: check that a "found" object is equivalent to or represents an "expected" object,...
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
typename EXP_CONT::value_type EXP_OBJ
bool CollectionHasNoDuplicates(const T &aCollection)
Predicate to check a collection has no duplicate elements.
void SetMockConfigDir()
STL namespace.
std::ostream & boost_test_print_type(std::ostream &os, std::vector< T > const &aVec)
Boost print helper for generic vectors.
A named data-driven test case.
friend std::ostream & operator<<(std::ostream &os, const NAMED_CASE &aCase)
std::optional< T > m_Opt
PRINTABLE_OPT(const T &aVal)
PRINTABLE_OPT(const std::optional< T > &aOpt)
void operator()(std::ostream &os, const wchar_t(&ws)[N])
void operator()(std::ostream &os, wxString const &v)
VECTOR3I expected(15, 30, 45)
bool operator!=(const PRINTABLE_OPT< L > &aLhs, const PRINTABLE_OPT< R > &aRhs)
bool operator==(const PRINTABLE_OPT< L > &aLhs, const PRINTABLE_OPT< R > &aRhs)
std::ostream & boost_test_print_type(std::ostream &os, wxPoint const &aVec)
Boost print helper for wxPoint.
std::ostream & operator<<(std::ostream &aOs, const PRINTABLE_OPT< T > &aOptional)