Data Structures | Namespaces | Macros | Functions
unittest.h File Reference

Go to the source code of this file.

Data Structures

struct  exception_state
 
class  unittest
 

Namespaces

 bfn
 

Macros

#define NO_HIPPOMOCKS_NAMESPACE
 
#define expect_true(c)   expect_true_with_args(c, gsl::cstring_span<>(#c), gsl::cstring_span<>(__PRETTY_FUNCTION__), __LINE__)
 
#define expect_false(c)   expect_false_with_args(c, gsl::cstring_span<>(#c), gsl::cstring_span<>(__PRETTY_FUNCTION__), __LINE__)
 
#define expect_exception(f, e)   expect_exception_with_args(f, e, __PRETTY_FUNCTION__, __LINE__)
 
#define expect_no_exception(f)   expect_no_exception_with_args(f, __PRETTY_FUNCTION__, __LINE__)
 
#define RUN_UNITTEST_WITH_MOCKS(a, b)   this->run_unittest_with_mocks(a,b, static_cast<const char *>(__PRETTY_FUNCTION__), __LINE__);
 
#define RUN_ALL_TESTS(ut)   [&]() -> decltype(auto) { (void) argc; (void) argv; ut _ut; return _ut.run(); }()
 

Functions

auto operator"" _ut_ree (const char *str, std::size_t len)
 
auto operator"" _ut_lee (const char *str, std::size_t len)
 
auto operator"" _ut_iae (const char *str, std::size_t len)
 
auto operator"" _ut_ore (const char *str, std::size_t len)
 
auto operator"" _ut_dme (const char *str, std::size_t len)
 
auto operator"" _ut_ffe (const char *str, std::size_t len)
 
auto operator"" _ut_bae (const char *str, std::size_t len)
 
template<class T , class = typename std::enable_if<std::is_integral<T>::value>::type>
auto make_ptr (const T ptr)
 
template<class P , class T , class = typename std::enable_if<std::is_integral<T>::value>::type>
auto make_ptr (const T ptr)
 
template<class T , class = typename std::enable_if<std::is_pointer<T>::value>::type>
auto make_uintptr (const T ptr)
 
template<class T >
void no_delete (T *)
 
template<class T >
auto bfn::mock_no_delete (MockRepository &mocks)
 
template<class T >
auto bfn::mock_shared (MockRepository &mocks)
 
template<class T >
auto bfn::mock_unique (MockRepository &mocks)
 

Macro Definition Documentation

◆ NO_HIPPOMOCKS_NAMESPACE

#define NO_HIPPOMOCKS_NAMESPACE

Definition at line 40 of file unittest.h.

◆ expect_true

#define expect_true (   c)    expect_true_with_args(c, gsl::cstring_span<>(#c), gsl::cstring_span<>(__PRETTY_FUNCTION__), __LINE__)

Expect True

This macro passes the boolean condition c along with the caller's name and line information to expect_true_with_args. This macro must be invoked on the unittest object using this.

this->expect_true(1 == 0) // unit test fails
this->expect_true(1 == 1) // unit test passes

Definition at line 100 of file unittest.h.

◆ expect_false

#define expect_false (   c)    expect_false_with_args(c, gsl::cstring_span<>(#c), gsl::cstring_span<>(__PRETTY_FUNCTION__), __LINE__)

Expect False

This macro passes the boolean condition c along with the caller's name and line information to expect_false_with_args. This macro must be invoked on the unittest object using this.

this->expect_false(1 == 0) // unit test passes
this->expect_false(1 == 1) // unit test fails

Definition at line 114 of file unittest.h.

◆ expect_exception

#define expect_exception (   f,
 
)    expect_exception_with_args(f, e, __PRETTY_FUNCTION__, __LINE__)

Expect Exception

This macro is used to pass the function name and line number to expect_exception_with_args. The argument f is a function that takes no arguments and returns void. The argument e is a std::shared_ptr<std::exception> whose dynamic type is the same as the exception in the throw statement in function-under-test.

If the caller wishes to pass a function that takes arguments they must first std::bind the arguments or create a lambda with the desired call, and pass the result to expect_exception.

void g(int, double) { throw std::logic_error("error"); };
class Foo {
public:
void bar(int a, int b)
{
b = b + a;
throw std::runtime_error("addition error");
}
};
int arg1 = 0;
double arg2 = 1.0;
Foo foo;
auto f1 = std::bind(&Foo::bar, &foo, arg1, arg1);
auto f2 = std::bind(g, arg1, arg2);
auto e1 = std::make_shared<std::runtime_error>("addition error"));
auto e2 = std::make_shared<std::logic_error>("error"));
// unit test succeeds since foo.bar(arg1, arg1) throws runtime_error
this->expect_exception(f1, e1);
// unit test fails since runtime_error != logic_error
this->expect_exception(f1, e2);
// unit test succeeds since g(arg1, arg2) throws logic error
this->expect_exception(f2, e2);

Definition at line 162 of file unittest.h.

◆ expect_no_exception

#define expect_no_exception (   f)    expect_no_exception_with_args(f, __PRETTY_FUNCTION__, __LINE__)

Expect No Exception

This macro is used to pass the function name and line number to expect_no_exception_with_args. The argument f is a callable object that returns void and takes no arguments

If the caller wishes to pass a function that takes arguments they must first std::bind the arguments or create a lambda with the desired call, and pass the result to expect_no_exception.

void g(int, double);
class Foo {
public:
void bar(int, double);
};
int arg1 = 0;
double arg2 = 1.0;
Foo foo;
auto f1 = std::bind(&Foo::bar, &foo, arg1, arg2);
auto f2 = std::bind(g, arg1, arg2);
// unit test fails if foo.bar(arg1, arg2) throws
// unit test fails if g(arg1, arg2) throws

Definition at line 198 of file unittest.h.

◆ RUN_UNITTEST_WITH_MOCKS

#define RUN_UNITTEST_WITH_MOCKS (   a,
 
)    this->run_unittest_with_mocks(a,b, static_cast<const char *>(__PRETTY_FUNCTION__), __LINE__);

Run Unittests with Mocks

When using mocks, it's possible that hippomocks could throw an exception. For example, if you call a function on a mocked class that you have not setup an "OnCall" for. If this happens, a default function within hippomocks is called, that throws an exception.

To handle these types of issues, mocks should be used inside this function call using a lamda function. This way, if an exeption should occur, the unit test handles it properly. This call will also check to see if the mock's expectations are satisfied. If they are not, it will also fail the unit test.

MockRepository mocks;
Blah1 *blah1 = mocks.ClassMock<Blah1>();
Blah2 *blah2 = new Blah2;
mocks.OnCall(blah1, Blah1::a).Return(false);
{
this->expect_true(blah2->do_something(blah1) == true);
});

Definition at line 229 of file unittest.h.

◆ RUN_ALL_TESTS

#define RUN_ALL_TESTS (   ut)    [&]() -> decltype(auto) { (void) argc; (void) argv; ut _ut; return _ut.run(); }()

Run Unit Tests

This runs your unit test. Use the following to kick off your unit tests

int
main(int argc, char *argv[])
{
return RUN_ALL_TESTS(<name of unit test class>);
}

Definition at line 246 of file unittest.h.

Function Documentation

◆ operator"" _ut_ree()

auto operator"" _ut_ree ( const char *  str,
std::size_t  len 
)
inline

Definition at line 55 of file unittest.h.

◆ operator"" _ut_lee()

auto operator"" _ut_lee ( const char *  str,
std::size_t  len 
)
inline

Definition at line 58 of file unittest.h.

◆ operator"" _ut_iae()

auto operator"" _ut_iae ( const char *  str,
std::size_t  len 
)
inline

Definition at line 61 of file unittest.h.

◆ operator"" _ut_ore()

auto operator"" _ut_ore ( const char *  str,
std::size_t  len 
)
inline

Definition at line 64 of file unittest.h.

◆ operator"" _ut_dme()

auto operator"" _ut_dme ( const char *  str,
std::size_t  len 
)
inline

Definition at line 67 of file unittest.h.

◆ operator"" _ut_ffe()

auto operator"" _ut_ffe ( const char *  str,
std::size_t  len 
)
inline

Definition at line 70 of file unittest.h.

◆ operator"" _ut_bae()

auto operator"" _ut_bae ( const char *  str,
std::size_t  len 
)
inline

Definition at line 73 of file unittest.h.

◆ make_ptr() [1/2]

template<class T , class = typename std::enable_if<std::is_integral<T>::value>::type>
auto make_ptr ( const T  ptr)

Definition at line 77 of file unittest.h.

◆ make_ptr() [2/2]

template<class P , class T , class = typename std::enable_if<std::is_integral<T>::value>::type>
auto make_ptr ( const T  ptr)

Definition at line 81 of file unittest.h.

◆ make_uintptr()

template<class T , class = typename std::enable_if<std::is_pointer<T>::value>::type>
auto make_uintptr ( const T  ptr)

Definition at line 85 of file unittest.h.

◆ no_delete()

template<class T >
void no_delete ( T *  )

No Delete

This is used by mock_shared to prevent a shared pointer from performing the deletion of the variable. In this case, mock is doing this for us. Note that this should not be used directly.

Definition at line 255 of file unittest.h.