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
140template <typename T>
141std::ostream& boost_test_print_type( std::ostream& os, std::set<T> const& aSet )
142{
143 os << "set size " << aSet.size() << " [";
144 for( const auto& i : aSet )
145 {
146 os << "\n " << i;
147 }
148
149 os << "]";
150 return os;
151}
152
153} // namespace std
154
155
156//-----------------------------------------------------------------------------+
157// Boost.Test printing helpers for wx types / wide string literals
158//-----------------------------------------------------------------------------+
159
160// C++20 removed operator<<(ostream&, const wchar_t*) (P1423R3), which breaks wxString streaming
161// via implicit wchar_t* conversion in Boost.Test's lazy_ostream (used by BOOST_TEST_CONTEXT,
162// BOOST_TEST_MESSAGE, BOOST_CHECK_MESSAGE, and BOOST_FAIL).
163inline std::ostream& boost_test_print_type( std::ostream& os, const wxString& v )
164{
165#if wxUSE_UNICODE
166 os << v.ToUTF8().data();
167#else
168 os << v.c_str();
169#endif
170 return os;
171}
172
173
174// Wide string literal arrays
175template <std::size_t N>
176std::ostream& boost_test_print_type( std::ostream& os, const wchar_t ( &ws )[N] )
177{
178 wxString tmp( ws );
179#if wxUSE_UNICODE
180 os << tmp.ToUTF8().data();
181#else
182 os << tmp;
183#endif
184 return os;
185}
186
187
188namespace boost { namespace unit_test
189{
196template<> struct is_forward_iterable<wxString> : public mpl::false_ {};
197
198}} // namespace boost::unit_test
199
200
201namespace boost { namespace test_tools { namespace tt_detail {
202
203template<std::size_t N>
204struct print_log_value<wchar_t[ N ]>
205{
206 void operator()( std::ostream& os, const wchar_t (&ws)[ N ] )
207 {
208 wxString tmp( ws );
209#if wxUSE_UNICODE
210 os << tmp.ToUTF8().data();
211#else
212 os << tmp;
213#endif
214 }
215};
216
217}}} // namespace boost::test_tools::tt_detail
218
219
224std::ostream& boost_test_print_type( std::ostream& os, wxPoint const& aVec );
225
226namespace KI_TEST
227{
228
229template <typename EXP_CONT> using EXP_OBJ = typename EXP_CONT::value_type;
230template <typename FOUND_CONT> using FOUND_OBJ = typename FOUND_CONT::value_type;
231
248template <typename EXP_OBJ, typename FOUND_OBJ>
249using MATCH_PRED = std::function<bool( const EXP_OBJ&, const FOUND_OBJ& )>;
250
284template <typename EXP_CONT, typename FOUND_CONT, typename MATCH_PRED>
285void CheckUnorderedMatches( const EXP_CONT& aExpected, const FOUND_CONT& aFound,
286 MATCH_PRED aMatchPredicate )
287{
288 using EXP_OBJ = typename EXP_CONT::value_type;
289
290 // set of object we've already found
291 std::set<const EXP_OBJ*> matched;
292
293 // fill the set of object that match
294 for( const auto& found : aFound )
295 {
296 for( const auto& expected : aExpected )
297 {
298 if( aMatchPredicate( expected, found ) )
299 {
300 matched.insert( &expected );
301 break;
302 }
303 }
304 }
305
306 // first check every expected object was "found"
307 for( const EXP_OBJ& exp : aExpected )
308 {
309 BOOST_CHECK_MESSAGE( matched.count( &exp ) > 0, "Expected item was not found. Expected: \n"
310 << exp );
311 }
312
313 // check every "found" object was expected
314 for( const EXP_OBJ* found : matched )
315 {
316 const bool was_expected = std::find_if( aExpected.begin(), aExpected.end(),
317 [found]( const EXP_OBJ& aObj )
318 {
319 return &aObj == found;
320 } ) != aExpected.end();
321
322 BOOST_CHECK_MESSAGE( was_expected, "Found item was not expected. Found: \n" << *found );
323 }
324}
325
326
330template <typename T>
331bool CollectionHasNoDuplicates( const T& aCollection )
332{
333 T sorted = aCollection;
334 std::sort( sorted.begin(), sorted.end() );
335
336 return std::adjacent_find( sorted.begin(), sorted.end() ) == sorted.end();
337}
338
339
347{
348 std::string m_CaseName;
349
350 friend std::ostream& operator<<( std::ostream& os, const NAMED_CASE& aCase )
351 {
352 os << aCase.m_CaseName;
353 return os;
354 }
355};
356
357
366#if wxDEBUG_LEVEL > 0
367#define CHECK_WX_ASSERT( STATEMENT ) BOOST_CHECK_THROW( STATEMENT, KI_TEST::WX_ASSERT_ERROR );
368#else
369#define CHECK_WX_ASSERT( STATEMENT )
370#endif
371
372
377class COUNTING_WXLOG : public wxLog
378{
379public:
380 COUNTING_WXLOG( wxLogLevelValues aMaxLevel ) :
381 m_maxLevel( aMaxLevel )
382 {
383 }
384
385 unsigned GetCount() const { return m_count; }
386
387protected:
388 void DoLogRecord( wxLogLevel aLevel, const wxString&, const wxLogRecordInfo& ) override
389 {
390 if( aLevel <= m_maxLevel )
391 m_count++;
392 }
393
394private:
395 wxLogLevelValues m_maxLevel;
396 unsigned m_count = 0;
397};
398
399
407{
408public:
416 SCOPED_COUNTING_WXLOG( unsigned* aLogCount, wxLogLevelValues aMaxLevel = wxLOG_Error ) :
418 m_logger( aMaxLevel ),
419 m_logCountRef( aLogCount )
420 {
421 }
422
424 {
425 // Update the caller's log count reference with the number of logs recorded
426 if( m_logCountRef )
427 *m_logCountRef = m_logger.GetCount();
428 }
429
430 /*
431 * Gets the current count of matching log messages.
432 */
433 unsigned GetCount() const { return m_logger.GetCount(); }
434
435private:
437 unsigned* m_logCountRef;
438};
439
440
449std::string GetEeschemaTestDataDir();
450
451std::string GetTestDataRootDir();
452
461std::vector<uint8_t> LoadBinaryData( const std::string& aFilePath, std::optional<size_t> aLoadBytes = std::nullopt );
462
476std::string LoadStringData( const wxString& aPath );
477
478void SetMockConfigDir();
479
480
488bool CanDoDisplayTests();
489
490} // namespace KI_TEST
491
492#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)
Test utilities for timestamps.
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)