Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce gtest unit testing #5

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/branch-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ jobs:

- name: Build
run: cmake --build ${{github.workspace}}/build

- name: Test
run: cmake --build ${{github.workspace}}/build --target test
3 changes: 3 additions & 0 deletions .github/workflows/master-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
- name: Build
run: cmake --build ${{github.workspace}}/build

- name: Test
run: cmake --build ${{github.workspace}}/build --target test

- name: Create Release
uses: softprops/action-gh-release@v1
if: steps.tag_version.outputs.new_tag
Expand Down
35 changes: 18 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
cmake_minimum_required(VERSION 3.12)

project(gifscript CXX)
cmake_minimum_required(VERSION 3.22)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})
project(gifscript CXX C)

# 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()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})

find_package(RAGEL REQUIRED)

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
Loading