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, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef UNIT_TEST_UTILS__H
21#define UNIT_TEST_UTILS__H
22
23#define BOOST_NO_AUTO_PTR
24
25#include <boost/test/unit_test.hpp>
26#include <turtle/mock.hpp>
27
29
30#include <cstdint>
31#include <functional>
32#include <optional>
33#include <set>
34#include <vector>
35
36#include <wx/gdicmn.h>
37#include <wx/log.h>
38#include <wx/string.h>
39
40#include <wx_log_utils.h>
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//-----------------------------------------------------------------------------+
143
144// C++20 removed operator<<(ostream&, const wchar_t*) (P1423R3), which breaks wxString streaming
145// via implicit wchar_t* conversion in Boost.Test's lazy_ostream (used by BOOST_TEST_CONTEXT,
146// BOOST_TEST_MESSAGE, BOOST_CHECK_MESSAGE, and BOOST_FAIL).
147inline std::ostream& boost_test_print_type( std::ostream& os, const wxString& v )
148{
149#if wxUSE_UNICODE
150 os << v.ToUTF8().data();
151#else
152 os << v.c_str();
153#endif
154 return os;
155}
156
157
158// Wide string literal arrays
159template <std::size_t N>
160std::ostream& boost_test_print_type( std::ostream& os, const wchar_t ( &ws )[N] )
161{
162 wxString tmp( ws );
163#if wxUSE_UNICODE
164 os << tmp.ToUTF8().data();
165#else
166 os << tmp;
167#endif
168 return os;
169}
170
171
172namespace boost { namespace test_tools { namespace tt_detail {
173
174template<std::size_t N>
175struct print_log_value<wchar_t[ N ]>
176{
177 void operator()( std::ostream& os, const wchar_t (&ws)[ N ] )
178 {
179 wxString tmp( ws );
180#if wxUSE_UNICODE
181 os << tmp.ToUTF8().data();
182#else
183 os << tmp;
184#endif
185 }
186};
187
188}}} // namespace boost::test_tools::tt_detail
189
190
195std::ostream& boost_test_print_type( std::ostream& os, wxPoint const& aVec );
196
197namespace KI_TEST
198{
199
200template <typename EXP_CONT> using EXP_OBJ = typename EXP_CONT::value_type;
201template <typename FOUND_CONT> using FOUND_OBJ = typename FOUND_CONT::value_type;
202
219template <typename EXP_OBJ, typename FOUND_OBJ>
220using MATCH_PRED = std::function<bool( const EXP_OBJ&, const FOUND_OBJ& )>;
221
256template <typename EXP_CONT, typename FOUND_CONT, typename MATCH_PRED>
257void CheckUnorderedMatches( const EXP_CONT& aExpected, const FOUND_CONT& aFound,
258 MATCH_PRED aMatchPredicate )
259{
260 using EXP_OBJ = typename EXP_CONT::value_type;
261
262 // set of object we've already found
263 std::set<const EXP_OBJ*> matched;
264
265 // fill the set of object that match
266 for( const auto& found : aFound )
267 {
268 for( const auto& expected : aExpected )
269 {
270 if( aMatchPredicate( expected, found ) )
271 {
272 matched.insert( &expected );
273 break;
274 }
275 }
276 }
277
278 // first check every expected object was "found"
279 for( const EXP_OBJ& exp : aExpected )
280 {
281 BOOST_CHECK_MESSAGE( matched.count( &exp ) > 0, "Expected item was not found. Expected: \n"
282 << exp );
283 }
284
285 // check every "found" object was expected
286 for( const EXP_OBJ* found : matched )
287 {
288 const bool was_expected = std::find_if( aExpected.begin(), aExpected.end(),
289 [found]( const EXP_OBJ& aObj )
290 {
291 return &aObj == found;
292 } ) != aExpected.end();
293
294 BOOST_CHECK_MESSAGE( was_expected, "Found item was not expected. Found: \n" << *found );
295 }
296}
297
298
302template <typename T>
303bool CollectionHasNoDuplicates( const T& aCollection )
304{
305 T sorted = aCollection;
306 std::sort( sorted.begin(), sorted.end() );
307
308 return std::adjacent_find( sorted.begin(), sorted.end() ) == sorted.end();
309}
310
311
319{
320 std::string m_CaseName;
321
322 friend std::ostream& operator<<( std::ostream& os, const NAMED_CASE& aCase )
323 {
324 os << aCase.m_CaseName;
325 return os;
326 }
327};
328
329
338#if wxDEBUG_LEVEL > 0
339#define CHECK_WX_ASSERT( STATEMENT ) BOOST_CHECK_THROW( STATEMENT, KI_TEST::WX_ASSERT_ERROR );
340#else
341#define CHECK_WX_ASSERT( STATEMENT )
342#endif
343
344
349class COUNTING_WXLOG : public wxLog
350{
351public:
352 COUNTING_WXLOG( wxLogLevelValues aMaxLevel ) :
353 m_maxLevel( aMaxLevel )
354 {
355 }
356
357 unsigned GetCount() const { return m_count; }
358
359protected:
360 void DoLogRecord( wxLogLevel aLevel, const wxString&, const wxLogRecordInfo& ) override
361 {
362 if( aLevel <= m_maxLevel )
363 m_count++;
364 }
365
366private:
367 wxLogLevelValues m_maxLevel;
368 unsigned m_count = 0;
369};
370
371
379{
380public:
388 SCOPED_COUNTING_WXLOG( unsigned* aLogCount, wxLogLevelValues aMaxLevel = wxLOG_Error ) :
390 m_logger( aMaxLevel ),
391 m_logCountRef( aLogCount )
392 {
393 }
394
396 {
397 // Update the caller's log count reference with the number of logs recorded
398 if( m_logCountRef )
399 *m_logCountRef = m_logger.GetCount();
400 }
401
402 /*
403 * Gets the current count of matching log messages.
404 */
405 unsigned GetCount() const { return m_logger.GetCount(); }
406
407private:
409 unsigned* m_logCountRef;
410};
411
412
421std::string GetEeschemaTestDataDir();
422
423std::string GetTestDataRootDir();
424
433std::vector<uint8_t> LoadBinaryData( const std::string& aFilePath, std::optional<size_t> aLoadBytes = std::nullopt );
434
448std::string LoadStringData( const wxString& aPath );
449
450void SetMockConfigDir();
451
452
460bool CanDoDisplayTests();
461
462} // namespace KI_TEST
463
464#endif // UNIT_TEST_UTILS__H
Counts error-level records so a failed save can be checked for reports beyond the one it throws.
COUNTING_WXLOG(wxLogLevelValues aMaxLevel)
wxLogLevelValues m_maxLevel
unsigned GetCount() const
void DoLogRecord(wxLogLevel aLevel, const wxString &, const wxLogRecordInfo &) override
SCOPED_COUNTING_WXLOG(unsigned *aLogCount, wxLogLevelValues aMaxLevel=wxLOG_Error)
SCOPED_WXLOG_TARGET(wxLog *aTarget)
std::string GetTestDataRootDir()
std::vector< uint8_t > LoadBinaryData(const std::string &aFilePath, std::optional< size_t > aLoadBytes=std::nullopt)
Load the contents of a file into a vector of bytes.
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::string LoadStringData(const wxString &aPath)
Load the contents of a file into a string.
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.
bool CanDoDisplayTests()
Some tests on some platforms require a display connection to run.
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])
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 & operator<<(std::ostream &aOs, const PRINTABLE_OPT< T > &aOptional)
std::ostream & boost_test_print_type(std::ostream &os, const wxString &v)