Skip to content

Commit

Permalink
feat: introduce gtest unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
F0bes committed Apr 6, 2024
1 parent 61bb98b commit 1399d0e
Show file tree
Hide file tree
Showing 13 changed files with 764 additions and 111 deletions.
31 changes: 16 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
cmake_minimum_required(VERSION 3.12)

project(gifscript CXX)
project(gifscript CXX C)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})

# Using CMAKE_CXX_STANDARD doesn't work for GCC...
# This also doesn't work for Clang 14...
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CPP_23_ARG "-std=c++2b")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CPP_23_ARG "-std=c++23")
endif()

find_package(RAGEL REQUIRED)

set(CORE_DIR ${CMAKE_SOURCE_DIR}/core)
Expand Down Expand Up @@ -45,9 +37,11 @@ RAGEL_TARGET(gifscript
COMPILE_FLAGS -G2
)

set_source_files_properties(parser.cpp PROPERTIES COMPILE_FLAGS "-Wno-unused-variable")

set(GENERATED_SOURCES
${CMAKE_CURRENT_BINARY_DIR}/parser.h
${CMAKE_CURRENT_BINARY_DIR}/parser.c
${CMAKE_CURRENT_BINARY_DIR}/parser.cpp
${RAGEL_gifscript_OUTPUTS}
)

Expand All @@ -64,6 +58,7 @@ set(CORE_SOURCES
${CORE_INCLUDE}/logger.hpp
${CORE_INCLUDE}/machine.hpp
${CORE_INCLUDE}/registers.hpp
${CORE_SRC}/logger.cpp
${CORE_SRC}/machine.cpp
${CORE_SRC}/registers.cpp
)
Expand Down Expand Up @@ -93,16 +88,22 @@ execute_process(

string(CONCAT GIT_VERSION "\"" ${GIT_VERSION} "\"")
message("git version: ${GIT_VERSION}")
target_compile_options(gifscript_core PRIVATE -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})
target_compile_options(gifscript PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})
target_compile_options(tpircsfig PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})
target_compile_options(gifscript_core PRIVATE -Wall -Werror -Wno-unused-const-variable)
target_compile_options(gifscript PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable)
target_compile_options(tpircsfig PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable)

target_include_directories(gifscript PUBLIC ${fmt_SOURCE_DIR}/include)
target_link_libraries(gifscript_core PUBLIC fmt::fmt)

add_custom_command(
OUTPUT parser.c parser.h
COMMAND lemon -q ${CORE_SRC}/parser.y -d${CMAKE_CURRENT_BINARY_DIR} ${LEMON_PARSER_TEMPLATE}
OUTPUT parser.cpp parser.h
COMMAND lemon -q ${CORE_SRC}/parser.y -d${CMAKE_CURRENT_BINARY_DIR} && mv parser.c parser.cpp && sed -i 's/parser.c/parser.cpp/g' parser.cpp
DEPENDS ${CORE_SRC}/parser.y
USES_TERMINAL
)

if(NOT DISABLE_TESTS)
include(CTest)
enable_testing()
add_subdirectory(tests)
endif()
28 changes: 28 additions & 0 deletions backends/include/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,31 @@ class Backend

virtual void emit(GIFBlock& block) = 0;
};

class DummyBackend : public Backend
{
public:
DummyBackend() = default;
~DummyBackend() override = default;

bool arg_parse(int argc, char** argv) override
{
return true;
}

void set_output(const std::string_view& output) override
{
(void)output;
}

void print_help() const override
{
}

void emit(GIFBlock& block) override
{
(void)block;
}
};

static DummyBackend dummy_backend;
12 changes: 12 additions & 0 deletions core/include/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

namespace logger
{
// Primarily used to disable logging in tests
extern bool g_log_enabled;
#define CHECK_LOGGING_ENABLED() \
if(!g_log_enabled) \
return;
struct fmt_location
{
const char* fmt;
Expand All @@ -23,6 +28,7 @@ namespace logger
template <typename... Args>
void log(fmt_location format, fmt::color fgcol, Args&&... args)
{
CHECK_LOGGING_ENABLED();
int size_s = std::snprintf(nullptr, 0, format.fmt, std::forward<Args>(args)...);
char* buf = new char[size_s + 1];
std::snprintf(buf, size_s + 1, format.fmt, std::forward<Args>(args)...);
Expand All @@ -34,28 +40,34 @@ namespace logger
template <typename... Args>
void info(fmt_location format, Args&&... args)
{
CHECK_LOGGING_ENABLED();
fmt::print(fg(fmt::color::blue), "[INFO ] ");
log(format, fmt::color::blue, std::forward<Args>(args)...);
}

template <typename... Args>
void warn(fmt_location format, Args&&... args)
{
CHECK_LOGGING_ENABLED();
fmt::print(fg(fmt::color::yellow), "[WARN ] ");
log(format, fmt::color::yellow, std::forward<Args>(args)...);
}

template <typename... Args>
void error(fmt_location format, Args&&... args)
{
CHECK_LOGGING_ENABLED();
fmt::print(fg(fmt::color::red), "[ERROR] ");
log(format, fmt::color::red, std::forward<Args>(args)...);
}

template <typename... Args>
void debug(fmt_location format, Args&&... args)
{
CHECK_LOGGING_ENABLED();
fmt::print(fg(fmt::color::green), "[DEBUG] ");
log(format, fmt::color::green, std::forward<Args>(args)...);
}
}; // namespace logger

#undef CHECK_LOGGING_ENABLED
2 changes: 1 addition & 1 deletion core/include/machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class Machine
{
Backend* backend = nullptr;
Backend* backend = &dummy_backend;

std::list<GIFBlock> blocks;
std::map<std::string, GIFBlock> macros;
Expand Down
Loading

0 comments on commit 1399d0e

Please sign in to comment.