MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mttest Namespace Reference

MT Unit testing framework. More...

Classes

struct  CmpRes
 Result of comparing left and right (retains value references for debug printing) More...
 
class  IsStreamable
 Checks if a class can be streamd to ostream. More...
 
struct  TestCase
 Test case metadata. More...
 
struct  TestContext
 Context for unit tests. More...
 
struct  TestSuites
 Test suite metadata and runner. More...
 

Enumerations

enum  SuiteStatus { OK , FAIL , ERROR }
 Test suite status. More...
 

Functions

template<typename T>
std::string as_string (const T &val)
 Gets a value as a string (for debug printing)
 
std::string op_name (const std::string &op)
 Gets an operation name's output symbol.
 
bool check (bool l)
 Does a check (used by macros)
 
bool check (bool l, const std::string &)
 Does a check with a message (drops the message, used by macros)
 
std::string check_msg (bool, std::string message="")
 Gets the message from a check (used by macros)
 
template<typename L, typename R>
auto eq (const L &left, const R &right)
 Equality comparison (used by macros)
 
template<typename L, typename R>
auto ne (const L &left, const R &right)
 Inequality comparison (used by macros)
 
template<typename L, typename R>
auto lt (const L &left, const R &right)
 Less than comparison (used by macros)
 
template<typename L, typename R>
auto le (const L &left, const R &right)
 Less than or equal to comparison (used by macros)
 
template<typename L, typename R>
auto gt (const L &left, const R &right)
 Greater than comparison (used by macros)
 
template<typename L, typename R>
auto ge (const L &left, const R &right)
 Greater than or equal to comparison (used by macros)
 

Detailed Description

MT Unit testing framework.

Meant to be small and highly portable, rather than feature rich. Reason for creation comes from the needs of mtstd which has Cheerp support. Cheerp doesn't support variants or unions, so we needed a small test suite to just get things to run inside Cheerp. We built this in a day. We use mttest in both mtstd and mtcore and try to keep it in sync, but there may be some minor differences. We use this to help keep tests easy to port between the two systems (we have the same macros, test structures, etc.). Currently, our goal is to only grow these suites as needed. At some point we may focus on making it a fully mature test suite, but right now we only have it address our needs. We have not focused on making it general purpose.

Need to have exactly one cpp file that defines MTTEST_DEFINE_MAIN prior to including this file to generate main

Enumeration Type Documentation

◆ SuiteStatus

Test suite status.

Enumerator
OK 
FAIL 
ERROR 

Definition at line 164 of file mttest.hpp.

164 {
165 OK,
166 FAIL,
167 ERROR,
168};
@ ERROR
Definition mttest.hpp:167

Function Documentation

◆ as_string()

template<typename T>
std::string mttest::as_string ( const T & val)

Gets a value as a string (for debug printing)

Definition at line 74 of file mttest.hpp.

74 {
75 if constexpr (IsStreamable<T>::value) {
76 std::stringstream ss;
77 ss << val;
78 return ss.str();
79 } else {
80#if __cpp_constexpr >= 201907L
81 return std::string{typeid(T).name()} + "{?}";
82#else
83 return "{?}";
84#endif
85 }
86}
static const bool value
Definition mttest.hpp:70
Here is the caller graph for this function:

◆ check() [1/2]

bool mttest::check ( bool l)
inline

Does a check (used by macros)

Definition at line 294 of file mttest.hpp.

294{ return l; }

◆ check() [2/2]

bool mttest::check ( bool l,
const std::string &  )
inline

Does a check with a message (drops the message, used by macros)

Definition at line 296 of file mttest.hpp.

296{ return l; }

◆ check_msg()

std::string mttest::check_msg ( bool ,
std::string message = "" )
inline

Gets the message from a check (used by macros)

Definition at line 298 of file mttest.hpp.

298{ return message; }

◆ eq()

template<typename L, typename R>
auto mttest::eq ( const L & left,
const R & right )

Equality comparison (used by macros)

Definition at line 301 of file mttest.hpp.

301{ return CmpRes<L, R>{left, right, left == right}; }
Result of comparing left and right (retains value references for debug printing)
Definition mttest.hpp:89

◆ ge()

template<typename L, typename R>
auto mttest::ge ( const L & left,
const R & right )

Greater than or equal to comparison (used by macros)

Definition at line 316 of file mttest.hpp.

316{ return CmpRes<L, R>{left, right, left >= right}; }

◆ gt()

template<typename L, typename R>
auto mttest::gt ( const L & left,
const R & right )

Greater than comparison (used by macros)

Definition at line 313 of file mttest.hpp.

313{ return CmpRes<L, R>{left, right, left > right}; }

◆ le()

template<typename L, typename R>
auto mttest::le ( const L & left,
const R & right )

Less than or equal to comparison (used by macros)

Definition at line 310 of file mttest.hpp.

310{ return CmpRes<L, R>{left, right, left <= right}; }

◆ lt()

template<typename L, typename R>
auto mttest::lt ( const L & left,
const R & right )

Less than comparison (used by macros)

Definition at line 307 of file mttest.hpp.

307{ return CmpRes<L, R>{left, right, left < right}; }

◆ ne()

template<typename L, typename R>
auto mttest::ne ( const L & left,
const R & right )

Inequality comparison (used by macros)

Definition at line 304 of file mttest.hpp.

304{ return CmpRes<L, R>{left, right, left != right}; }

◆ op_name()

std::string mttest::op_name ( const std::string & op)
inline

Gets an operation name's output symbol.

Definition at line 96 of file mttest.hpp.

96 {
97 if (op == "eq") return " == ";
98 if (op == "ne") return " != ";
99 if (op == "lt") return " < ";
100 if (op == "le") return " <= ";
101 if (op == "gt") return " > ";
102 if (op == "ge") return " >= ";
103 if (op == "check") return "";
104 return " ??? ";
105}
Here is the caller graph for this function: