Skip to content

Commit 1399d0e

Browse files
committed
feat: introduce gtest unit testing
1 parent 61bb98b commit 1399d0e

File tree

13 files changed

+764
-111
lines changed

13 files changed

+764
-111
lines changed

CMakeLists.txt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
cmake_minimum_required(VERSION 3.12)
22

3-
project(gifscript CXX)
3+
project(gifscript CXX C)
44

55
set(CMAKE_CXX_STANDARD 23)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})
88

9-
# Using CMAKE_CXX_STANDARD doesn't work for GCC...
10-
# This also doesn't work for Clang 14...
11-
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
12-
set(CPP_23_ARG "-std=c++2b")
13-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
14-
set(CPP_23_ARG "-std=c++23")
15-
endif()
16-
179
find_package(RAGEL REQUIRED)
1810

1911
set(CORE_DIR ${CMAKE_SOURCE_DIR}/core)
@@ -45,9 +37,11 @@ RAGEL_TARGET(gifscript
4537
COMPILE_FLAGS -G2
4638
)
4739

40+
set_source_files_properties(parser.cpp PROPERTIES COMPILE_FLAGS "-Wno-unused-variable")
41+
4842
set(GENERATED_SOURCES
4943
${CMAKE_CURRENT_BINARY_DIR}/parser.h
50-
${CMAKE_CURRENT_BINARY_DIR}/parser.c
44+
${CMAKE_CURRENT_BINARY_DIR}/parser.cpp
5145
${RAGEL_gifscript_OUTPUTS}
5246
)
5347

@@ -64,6 +58,7 @@ set(CORE_SOURCES
6458
${CORE_INCLUDE}/logger.hpp
6559
${CORE_INCLUDE}/machine.hpp
6660
${CORE_INCLUDE}/registers.hpp
61+
${CORE_SRC}/logger.cpp
6762
${CORE_SRC}/machine.cpp
6863
${CORE_SRC}/registers.cpp
6964
)
@@ -93,16 +88,22 @@ execute_process(
9388

9489
string(CONCAT GIT_VERSION "\"" ${GIT_VERSION} "\"")
9590
message("git version: ${GIT_VERSION}")
96-
target_compile_options(gifscript_core PRIVATE -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})
97-
target_compile_options(gifscript PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})
98-
target_compile_options(tpircsfig PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})
91+
target_compile_options(gifscript_core PRIVATE -Wall -Werror -Wno-unused-const-variable)
92+
target_compile_options(gifscript PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable)
93+
target_compile_options(tpircsfig PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable)
9994

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

10398
add_custom_command(
104-
OUTPUT parser.c parser.h
105-
COMMAND lemon -q ${CORE_SRC}/parser.y -d${CMAKE_CURRENT_BINARY_DIR} ${LEMON_PARSER_TEMPLATE}
99+
OUTPUT parser.cpp parser.h
100+
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
106101
DEPENDS ${CORE_SRC}/parser.y
107102
USES_TERMINAL
108103
)
104+
105+
if(NOT DISABLE_TESTS)
106+
include(CTest)
107+
enable_testing()
108+
add_subdirectory(tests)
109+
endif()

backends/include/backend.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,31 @@ class Backend
1515

1616
virtual void emit(GIFBlock& block) = 0;
1717
};
18+
19+
class DummyBackend : public Backend
20+
{
21+
public:
22+
DummyBackend() = default;
23+
~DummyBackend() override = default;
24+
25+
bool arg_parse(int argc, char** argv) override
26+
{
27+
return true;
28+
}
29+
30+
void set_output(const std::string_view& output) override
31+
{
32+
(void)output;
33+
}
34+
35+
void print_help() const override
36+
{
37+
}
38+
39+
void emit(GIFBlock& block) override
40+
{
41+
(void)block;
42+
}
43+
};
44+
45+
static DummyBackend dummy_backend;

core/include/logger.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
namespace logger
88
{
9+
// Primarily used to disable logging in tests
10+
extern bool g_log_enabled;
11+
#define CHECK_LOGGING_ENABLED() \
12+
if(!g_log_enabled) \
13+
return;
914
struct fmt_location
1015
{
1116
const char* fmt;
@@ -23,6 +28,7 @@ namespace logger
2328
template <typename... Args>
2429
void log(fmt_location format, fmt::color fgcol, Args&&... args)
2530
{
31+
CHECK_LOGGING_ENABLED();
2632
int size_s = std::snprintf(nullptr, 0, format.fmt, std::forward<Args>(args)...);
2733
char* buf = new char[size_s + 1];
2834
std::snprintf(buf, size_s + 1, format.fmt, std::forward<Args>(args)...);
@@ -34,28 +40,34 @@ namespace logger
3440
template <typename... Args>
3541
void info(fmt_location format, Args&&... args)
3642
{
43+
CHECK_LOGGING_ENABLED();
3744
fmt::print(fg(fmt::color::blue), "[INFO ] ");
3845
log(format, fmt::color::blue, std::forward<Args>(args)...);
3946
}
4047

4148
template <typename... Args>
4249
void warn(fmt_location format, Args&&... args)
4350
{
51+
CHECK_LOGGING_ENABLED();
4452
fmt::print(fg(fmt::color::yellow), "[WARN ] ");
4553
log(format, fmt::color::yellow, std::forward<Args>(args)...);
4654
}
4755

4856
template <typename... Args>
4957
void error(fmt_location format, Args&&... args)
5058
{
59+
CHECK_LOGGING_ENABLED();
5160
fmt::print(fg(fmt::color::red), "[ERROR] ");
5261
log(format, fmt::color::red, std::forward<Args>(args)...);
5362
}
5463

5564
template <typename... Args>
5665
void debug(fmt_location format, Args&&... args)
5766
{
67+
CHECK_LOGGING_ENABLED();
5868
fmt::print(fg(fmt::color::green), "[DEBUG] ");
5969
log(format, fmt::color::green, std::forward<Args>(args)...);
6070
}
6171
}; // namespace logger
72+
73+
#undef CHECK_LOGGING_ENABLED

core/include/machine.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class Machine
1313
{
14-
Backend* backend = nullptr;
14+
Backend* backend = &dummy_backend;
1515

1616
std::list<GIFBlock> blocks;
1717
std::map<std::string, GIFBlock> macros;

0 commit comments

Comments
 (0)