Started adding unit tests

This commit is contained in:
Andreas Tsouchlos 2021-11-21 22:46:19 +01:00
parent ad737f1c73
commit 5a4b9e3298
9 changed files with 83 additions and 1 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "test/googletest"]
path = test/googletest
url = https://github.com/google/googletest.git

View File

@ -3,11 +3,14 @@ project(logger)
set(CMAKE_CXX_STANDARD 20)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
include_directories(inc)
add_executable(logger src/main.cpp)
@ -18,4 +21,9 @@ else()
endif()
include_directories(inc)
option(PACKAGE_TESTS "Build the tests" ON)
if(PACKAGE_TESTS)
enable_testing()
include(GoogleTest)
add_subdirectory(test)
endif()

View File

@ -18,6 +18,12 @@ struct fmt_node_t {
int length = 6;
int precision = 2;
FormatType type = FormatType::s;
constexpr bool operator==(const fmt_node_t& rhs) const {
return (rhs.has_zero_padding == has_zero_padding) &&
(rhs.length == length) && (rhs.precision == precision) &&
(rhs.type == type);
}
};
@ -26,6 +32,17 @@ public:
constexpr ast_node_t() {
}
constexpr ast_node_t(char c) : m_is_char(true), m_c(c) {
}
constexpr ast_node_t(fmt_node_t node) : m_is_char(false), m_node(node) {
}
constexpr bool operator==(const ast_node_t& rhs) const {
return ((rhs.m_is_char == m_is_char) && (rhs.m_c == m_c)) ||
((rhs.m_is_char != m_is_char) && (rhs.m_node == m_node));
}
constexpr void set_char(char c) {
m_c = c;
m_is_char = true;

View File

@ -2,6 +2,9 @@
#define LOGGER_UTILITY_H
#include <cstdlib>
namespace detail {

21
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,21 @@
add_subdirectory("googletest")
mark_as_advanced(
BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS
gmock_build_tests gtest_build_samples gtest_build_tests
gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols
)
macro(package_add_test TESTNAME)
add_executable(${TESTNAME} ${ARGN})
target_link_libraries(${TESTNAME} gtest gmock gtest_main)
gtest_discover_tests(${TESTNAME}
WORKING_DIRECTORY ${PROJECT_DIR}
PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}"
)
set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
endmacro()
package_add_test(utility_test src/utility.cpp)
package_add_test(parse_test src/parse.cpp)

1
test/googletest Submodule

@ -0,0 +1 @@
Subproject commit 3e0e32ba300ce8afe695ad3ba7e81b21b7cf237a

2
test/src/format.cpp Normal file
View File

@ -0,0 +1,2 @@
#include <format.h>
#include <gtest/gtest.h>

18
test/src/parse.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <gtest/gtest.h>
#include <parse.h>
TEST(Parse, ast_node) {
ast_node_t ast_node1{'a'};
ast_node_t ast_node2{'a'};
ast_node_t ast_node3{'b'};
EXPECT_EQ(ast_node1 == ast_node2, true);
EXPECT_EQ(ast_node1 == ast_node3, false);
}
TEST(Parse, chars_only) {
constexpr std::array<ast_node_t, 8> control = {'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h'};
EXPECT_EQ(detail::parse_string<"abcdefgh">().value, control);
}

9
test/src/utility.cpp Normal file
View File

@ -0,0 +1,9 @@
#include <utility.h>
#include <gtest/gtest.h>
TEST(Utility, const_pow) {
EXPECT_EQ(detail::const_pow(10, 0), 1);
EXPECT_EQ(detail::const_pow(0, 1), 0);
EXPECT_EQ(detail::const_pow(2, 8), 0b1'0000'0000);
}