Public Member Functions | Protected Member Functions
unittest Class Reference
Inheritance diagram for unittest:
bfelf_loader_ut bfm_ut bfunwind_ut crt_ut debug_ring_ut driver_entry_ut entry_ut exit_handler_intel_x64_ut intrinsics_ut memory_manager_ut misc_ut serial_ut vcpu_ut vmcs_ut vmxon_ut

Public Member Functions

template<typename F >
void expect_exception_with_args (F &&f, std::shared_ptr< const std::exception > expected, gsl::cstring_span<> func, int line, int path_id=-1)
 
template<typename F >
void expect_no_exception_with_args (F &&f, gsl::cstring_span<> func, int line, int path_id=-1)
 
void expect_true_with_args (bool condition, gsl::cstring_span<> condition_text, gsl::cstring_span<> func, int line)
 
void expect_false_with_args (bool condition, gsl::cstring_span<> condition_text, gsl::cstring_span<> func, int line)
 
 unittest ()
 
virtual ~unittest ()
 
decltype(auto) run ()
 
void expect_failed (const char *condition, const char *func, int line)
 
void assert_failed (const char *condition, const char *func, int line)
 

Protected Member Functions

virtual bool list ()
 
virtual bool init ()
 
virtual bool fini ()
 
template<typename T >
void run_unittest_with_mocks (MockRepository &mocks, T lamda, const char *func, int line)
 
void compare_exceptions (const struct exception_state &state, gsl::cstring_span<> func, int line, int path_id=-1)
 
void inc_pass ()
 
void inc_fail ()
 

Detailed Description

Unit Test

This class provides the scaffolding needed to perform a unit test. Note that this class also provides support for Hippomocks.

In general, if you want an example of how to use this class, start with one of the existing unit tests, as they are cookie-cutter in design. For example, take a look at the following:

debug_ring_ut

The unit test macros should be used when creating a unit test. expect_true
expect_false
expect_exception
expect_no_exception

And when a shared_ptr/unique_ptr must be created that is being mocked by Hippomocks, be sure to use:

bfn::mock_shared
bfn::mock_unique

Definition at line 355 of file unittest.h.

Constructor & Destructor Documentation

◆ unittest()

unittest::unittest ( )
inline

Definition at line 764 of file unittest.h.

◆ ~unittest()

virtual unittest::~unittest ( )
inlinevirtual

Definition at line 769 of file unittest.h.

Member Function Documentation

◆ list()

virtual bool unittest::list ( )
inlineprotectedvirtual

List Tests

Override this function to call each of your tests.

bool list() override
{
this->test1()
this->test2()
return true;
}
Returns
true if the tests passed, false otherwise

Reimplemented in vmcs_ut, bfelf_loader_ut, bfunwind_ut, driver_entry_ut, bfm_ut, crt_ut, debug_ring_ut, entry_ut, exit_handler_intel_x64_ut, intrinsics_ut, memory_manager_ut, misc_ut, serial_ut, vcpu_ut, and vmxon_ut.

Definition at line 383 of file unittest.h.

◆ init()

virtual bool unittest::init ( )
inlineprotectedvirtual

Init Tests

Override this function to initalize your tests. It's better to use this fucntion than to use your constructor because if the init fails the unit test will stop, and report failure.

bool init() override
{
<init here>
return true;
}
Returns
true if the init passed, false otherwise

Reimplemented in vmcs_ut, bfelf_loader_ut, bfunwind_ut, driver_entry_ut, bfm_ut, crt_ut, debug_ring_ut, entry_ut, exit_handler_intel_x64_ut, intrinsics_ut, memory_manager_ut, misc_ut, serial_ut, vcpu_ut, and vmxon_ut.

Definition at line 405 of file unittest.h.

◆ fini()

virtual bool unittest::fini ( )
inlineprotectedvirtual

Fini Tests

Override this function to finalize your tests. It's better to use this fucntion than to use your destructor because if the fini fails the unit test will stop, and report failure.

bool fini() override
{
<fini here>
return true;
}
Returns
true if the fini passed, false otherwise

Reimplemented in vmcs_ut, bfelf_loader_ut, bfunwind_ut, driver_entry_ut, bfm_ut, crt_ut, debug_ring_ut, entry_ut, exit_handler_intel_x64_ut, intrinsics_ut, memory_manager_ut, misc_ut, serial_ut, vcpu_ut, and vmxon_ut.

Definition at line 427 of file unittest.h.

◆ run_unittest_with_mocks()

template<typename T >
void unittest::run_unittest_with_mocks ( MockRepository &  mocks,
lamda,
const char *  func,
int  line 
)
inlineprotected

Definition at line 430 of file unittest.h.

◆ compare_exceptions()

void unittest::compare_exceptions ( const struct exception_state state,
gsl::cstring_span<>  func,
int  line,
int  path_id = -1 
)
inlineprotected

Definition at line 477 of file unittest.h.

◆ expect_exception_with_args()

template<typename F >
void unittest::expect_exception_with_args ( F &&  f,
std::shared_ptr< const std::exception >  expected,
gsl::cstring_span<>  func,
int  line,
int  path_id = -1 
)
inline

Expect Exception With Args

This macro verifies a unit test does throw an exception. If the unit test does not throw an exception, the unit test reports a failue and continues testing.

The first argument is a function that takes no arguments and returns void. If the caller wishes to pass a function that takes arguments, they must first std::bind the arguments or create a lambda, and pass the result to expect_exception_with_args.

The second argument is a shared_ptr to an instance of a std::exception for which the dynamic type is the same as the type seen in the throw statement in the function-under-test. A shared_ptr is needed to for RTTI to work properly when the expected exception is compared to the caught exception.

The third and fourth arguments correspond to __PRETTY_FUNCTION__ and __LINE__ passed from the macro 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_with_args(f1, e1, func, 50);
// unit test fails since runtime_error != logic_error
this->expect_exception_with_args(f1, e2, func, 50);
// unit test succeeds since g(arg1, arg2) throws logic error
this->expect_exception_with_args(f2, e2, func, 4);

Definition at line 570 of file unittest.h.

◆ expect_no_exception_with_args()

template<typename F >
void unittest::expect_no_exception_with_args ( F &&  f,
gsl::cstring_span<>  func,
int  line,
int  path_id = -1 
)
inline

Expect No Exception With Args

This macro verifies a unit test does not throw an exception. If the unit test does throw an exception, it reports a failue and continues testing.

The first argument is a function that takes no arguments and returns void. If the caller wishes to pass a function that takes arguments, they must first std::bind the arguments or create a lambda, and pass the result expect_no_exception_with_args

The second and third arguments correspond to __PRETTY_FUNCTION__ and __LINE__ passed in from the expect_no_exception macro.

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
this->expect_no_exception_with_args(f1, func, 50);
// unit test fails if g(arg1, arg2) throws
this->expect_no_exception_with_args(f2, func, 4);

Definition at line 632 of file unittest.h.

◆ expect_true_with_args()

void unittest::expect_true_with_args ( bool  condition,
gsl::cstring_span<>  condition_text,
gsl::cstring_span<>  func,
int  line 
)
inline

Expect true with args

This function verifies a condition in a unit test is true. If the condition is not true, the unit test reports a failure and continues testing. The macro expect_true passes the name of the function-under-test and the line on which it is invoked to this function.

this->expect_true_with_args(1 == 1, "1 == 1", func, 100) // unit test of func passes at line 100
this->expect_true_with_args(1 == 0, "1 == 0", func, 100) // unit test of func fails at line 100

Definition at line 675 of file unittest.h.

◆ expect_false_with_args()

void unittest::expect_false_with_args ( bool  condition,
gsl::cstring_span<>  condition_text,
gsl::cstring_span<>  func,
int  line 
)
inline

Expect false with args

This function verifies a condition in a unit test to be false. If the condition is not false, the unit test reports a failue and continues testing. The macro expect_false passes the name of the function-under-test and the line on which it is invoked to

this->expect_false_with_args(1 == 1, "1 == 1", func, 10) // unit test of func fails at line 10
this->expect_false_with_args(1 == 0, "1 == 0", func, 10) // unit test of func passes at line 10

Definition at line 702 of file unittest.h.

◆ inc_pass()

void unittest::inc_pass ( )
inlineprotected

Definition at line 716 of file unittest.h.

◆ inc_fail()

void unittest::inc_fail ( )
inlineprotected

Definition at line 717 of file unittest.h.

◆ run()

decltype(auto) unittest::run ( )
inline

Definition at line 772 of file unittest.h.

◆ expect_failed()

void unittest::expect_failed ( const char *  condition,
const char *  func,
int  line 
)
inline

Definition at line 817 of file unittest.h.

◆ assert_failed()

void unittest::assert_failed ( const char *  condition,
const char *  func,
int  line 
)
inline

Definition at line 825 of file unittest.h.


The documentation for this class was generated from the following file: