class::test::Function

class test::Function
  : public test::Test

The Function template class is designed to test any arbitrary C or C++ function (not member methods, just regular functions).

The following is an example that tests open() for various error conditions.

#include <sapi/test.hpp>

//because open() has variable arguments we wrap it to make it work with this class
int test_open(const char * path, int o_flags, int o_mode){
  return open(path, o_flags, o_mode);
}

bool is_test_enabled = true;

Test::initilize("test name", "0.1");

if( is_test_enabled ){
  //when test is constructed
  Function<int, const char *, int, int> open_function_test("open", test_open);

  open_function_test.execute("NO EXIST Read only", -1, ENOENT, "/home/non-existing-file.txt", O_RDONLY, 0);
  open_function_test.execute("NO EXIST Read Write", -1, ENOENT, "/home/non-existing-file.txt", O_RDWR, 0);
  open_function_test.execute("NO EXIST Read Write", -1, ENAMETOOLONG, "/home/non-existing-file-too-long-too-long-too-long-too-long-too-long-too-long-too-long-too-long-too-long-too-long.txt", O_RDWR, 0);

  //open_function_test will call the deconstructor here before Test::finalize()
}

Test::finalize();

Summary

[Function](#classtest_1_1_function_1a1e0182083c4e3f29220cfbf8f47b25a5)(  

const char * test_name,
return_type(*)(args…) function,
Test * parent
)

[~Function](#classtest_1_1_function_1a4d9eed15ec59ee33ffa365012535e746)()

return_type execute_case_with_expected_return(

const char * case_name,
return_type expected_value,
int expected_errno,
args… arguments
)

return_type execute_case_with_less_than_zero_on_error(

const char * case_name,
int expected_errno,
args… arguments
)

Members

Function(

const char * test_name,
return_type(*)(args…) function,
Test * parent
)

Constructs a new function test object.

Parameters

  • test_name The name of the test

  • function A function pointer to the function that will be tested

  • parent The parent test or zero if there is no parent


~Function()

return_type execute_case_with_expected_return(

const char * case_name,
return_type expected_value,
int expected_errno,
args… arguments
)

Executes a test case.

Parameters

  • case_name The name of the case

  • expected_value The expected return value

  • expected_errno The expected error number

  • arguments The arguments to pass to the test function

Returns

The value that the tested function returns


return_type execute_case_with_less_than_zero_on_error(

const char * case_name,
int expected_errno,
args… arguments
)


X

Thanks for Coming!

Subscribe to news and updates