Adding unit test and refactoring

Added unit test for errormanager and refactored to simplify use of error_check() method.
This commit is contained in:
Michael Tryby
2019-03-20 15:43:27 -04:00
parent 01eb164fdc
commit 7eadbc25ce
5 changed files with 141 additions and 36 deletions

18
tests/util/CMakeLists.txt Normal file
View File

@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.12)
enable_testing()
set (Boost_USE_STATIC_LIBS OFF)
find_package(Boost COMPONENTS unit_test_framework)
include_directories (${Boost_INCLUDE_DIRS} ../../src/)
set (test_source
./test_errormanager.cpp
../../src/util/errormanager.c
)
add_executable(test_errormanager ${test_source})
target_link_libraries(test_errormanager ${Boost_LIBRARIES})

View File

@@ -0,0 +1,81 @@
#define BOOST_TEST_MODULE errormanager
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "util/errormanager.h"
#define MESSAGE_STRING "This is unit testing!"
void mock_lookup(int errcode, char *errmsg, int len)
{
char *msg = NULL;
if (errcode == 100) {
msg = MESSAGE_STRING;
}
else {
msg = "";
}
strncpy(errmsg, msg, len);
}
boost::test_tools::predicate_result check_string(std::string test, std::string ref)
{
if (ref.compare(test) == 0)
return true;
else
return false;
}
BOOST_AUTO_TEST_SUITE(test_errormanager)
BOOST_AUTO_TEST_CASE (test_create_destroy)
{
error_handle_t *error_handle = NULL;
error_handle = error_new_manager(&mock_lookup);
error_dst_manager(error_handle);
}
struct Fixture{
Fixture() {
error_message = NULL;
error_handle = error_new_manager(&mock_lookup);
}
~Fixture() {
error_dst_manager(error_handle);
free(error_message);
}
int error;
error_handle_t *error_handle;
char *error_message;
};
BOOST_FIXTURE_TEST_CASE (test_set_clear, Fixture)
{
error = error_set(error_handle, 100);
BOOST_CHECK(error == 100);
error_clear(error_handle);
error = error_check(error_handle, &error_message);
BOOST_CHECK(error == 0);
BOOST_CHECK(error_message == NULL);
}
BOOST_FIXTURE_TEST_CASE(test_set_check, Fixture)
{
error = error_set(error_handle, 100);
BOOST_CHECK(error == 100);
error = error_check(error_handle, &error_message);
BOOST_CHECK(check_string(error_message, MESSAGE_STRING));
}
BOOST_AUTO_TEST_SUITE_END()