Skip to content

Commit 8956a3a

Browse files
committed
Tests: Add basic unittests for CInifile
1 parent 466fe12 commit 8956a3a

File tree

9 files changed

+153
-0
lines changed

9 files changed

+153
-0
lines changed

.github/workflows/cibuild.yml

+13
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ jobs:
113113
CXX: ${{ matrix.CXX }}
114114
run: cmake --build build --config ${{ matrix.Configuration }} --parallel $(nproc || echo 4)
115115

116+
- name: Run CTest
117+
working-directory: build
118+
run: ctest --parallel $(nproc || echo 4) --output-on-failure --schedule-random --no-tests=error
119+
116120
- name: Make package
117121
if: ${{ steps.cmake-build.outcome == 'success' }}
118122
id: make-package
@@ -171,6 +175,11 @@ jobs:
171175
shell: alpine.sh {0}
172176
run: cmake --build build --config ${{ matrix.Configuration }} --parallel $(nproc || echo 4)
173177

178+
- name: Run CTest
179+
shell: alpine.sh {0}
180+
working-directory: build
181+
run: ctest --parallel $(nproc || echo 4) --output-on-failure --schedule-random --no-tests=error
182+
174183
build-macos:
175184
name: macOS ${{ matrix.Configuration }} ${{ matrix.Platform }}
176185
runs-on: macos-latest
@@ -197,3 +206,7 @@ jobs:
197206

198207
- name: Run CMake Build
199208
run: cmake --build build --config ${{ matrix.Configuration }} --parallel $(sysctl -n hw.ncpu || echo 4)
209+
210+
- name: Run CTest
211+
working-directory: build
212+
run: ctest --parallel $(sysctl -n hw.ncpu || echo 4) --output-on-failure --schedule-random --no-tests=error

.gitmodules

+4
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@
4646
[submodule "Externals/xrLuaFix"]
4747
path = Externals/xrLuaFix
4848
url = https://github.com/OpenXRay/xrLuaFix.git
49+
[submodule "Externals/doctest"]
50+
path = Externals/doctest
51+
url = https://github.com/doctest/doctest.git
52+
branch = master

CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,16 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT XRAY_USE_DEFAULT_CXX_LIB)
133133

134134
if (XRAY_CXX_LIB STREQUAL "libstdc++")
135135
add_compile_options(-stdlib=libstdc++)
136+
add_link_options(-stdlib=libstdc++)
136137
elseif (XRAY_CXX_LIB STREQUAL "libc++")
137138
add_compile_options(-stdlib=libc++)
139+
add_link_options(-stdlib=libc++)
138140
if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
139141
add_compile_options(-lcxxrt)
142+
add_link_options(-lcxxrt)
140143
else()
141144
add_compile_options(-lc++abi)
145+
add_link_options(-lc++abi)
142146
endif()
143147
endif()
144148
endif()
@@ -298,6 +302,13 @@ add_subdirectory(src)
298302
add_subdirectory(res)
299303
add_subdirectory(misc)
300304

305+
# Tests
306+
option(BUILD_TESTS "Build tests" ON)
307+
if (BUILD_TESTS)
308+
include(CTest)
309+
add_subdirectory(tests)
310+
endif()
311+
301312
get_property(LIB64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
302313

303314
if ("${LIB64}" STREQUAL "TRUE")

Externals/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_subdirectory(OPCODE)
1616
add_subdirectory(ode)
1717
#add_subdirectory(NVTT)
1818
add_subdirectory(imgui-proj)
19+
add_subdirectory(doctest EXCLUDE_FROM_ALL)
1920

2021
if (NOT TARGET xrLuabind)
2122
message(FATAL_ERROR

Externals/doctest

Submodule doctest added at ae7a135

tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(xrCore)

tests/xrCore/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_executable(
2+
xrCoreTests
3+
main.cpp
4+
xr_ini_test.cpp)
5+
6+
target_link_libraries(xrCoreTests xrCore doctest::doctest)
7+
target_include_directories(xrCoreTests PRIVATE "${CMAKE_SOURCE_DIR}/src")
8+
add_test(NAME xrCoreTests COMMAND xrCoreTests)
9+
# https://github.com/doctest/doctest/blob/master/doc/markdown/configuration.md
10+
target_compile_definitions(xrCoreTests PRIVATE
11+
DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
12+
DOCTEST_CONFIG_SUPER_FAST_ASSERTS
13+
)

tests/xrCore/main.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#define DOCTEST_CONFIG_IMPLEMENT
2+
#include <doctest/doctest.h>
3+
4+
#include <Common/Platform.hpp>
5+
#include <xrCore/xrCore.h>
6+
7+
int main(int argc, char** argv)
8+
{
9+
doctest::Context context;
10+
context.applyCommandLine(argc, argv);
11+
12+
Memory._initialize();
13+
14+
return context.run();
15+
}

tests/xrCore/xr_ini_test.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <doctest/doctest.h>
2+
3+
#include <Common/Platform.hpp>
4+
#include <xrCore/xrCore.h>
5+
#include <xrCore/xr_types.h>
6+
7+
#include <xrCore/xr_ini.h>
8+
9+
CInifile read_from_string(pcstr str, CInifile::allow_include_func_t allow_include = nullptr)
10+
{
11+
IReader reader = IReader(const_cast<pstr>(str), xr_strlen(str));
12+
return CInifile(&reader, "test.ini", allow_include);
13+
}
14+
15+
TEST_CASE("parse empty file")
16+
{
17+
CInifile ini = read_from_string("");
18+
19+
CHECK_EQ(ini.section_count(), 0);
20+
}
21+
22+
TEST_CASE("parse empty section")
23+
{
24+
CInifile ini = read_from_string("[a]");
25+
26+
CHECK_EQ(ini.section_count(), 1);
27+
CHECK_UNARY(ini.section_exist("a"));
28+
}
29+
30+
TEST_CASE("parse simple section")
31+
{
32+
CInifile ini = read_from_string(
33+
R"ini(
34+
[a]
35+
key = value
36+
)ini");
37+
38+
CHECK_UNARY(ini.section_exist("a"));
39+
CHECK_UNARY(ini.line_exist("a", "key"));
40+
CHECK_EQ(ini.read<pcstr>("a", "key"), "value");
41+
}
42+
43+
TEST_CASE("parse integer value")
44+
{
45+
CInifile ini = read_from_string(
46+
R"ini(
47+
[a]
48+
key = 123
49+
)ini");
50+
51+
CHECK_UNARY(ini.section_exist("a"));
52+
CHECK_UNARY(ini.line_exist("a", "key"));
53+
CHECK_EQ(ini.read<u32>("a", "key"), 123);
54+
}
55+
56+
TEST_CASE("Parse float value")
57+
{
58+
CInifile ini = read_from_string(
59+
R"ini(
60+
[a]
61+
key = 123.456
62+
)ini");
63+
64+
CHECK_UNARY(ini.section_exist("a"));
65+
CHECK_UNARY(ini.line_exist("a", "key"));
66+
CHECK_EQ(ini.read<f32>("a", "key"), 123.456f);
67+
}
68+
69+
TEST_CASE("Parse quoted value")
70+
{
71+
CInifile ini = read_from_string(
72+
R"ini(
73+
[a]
74+
key = "value"
75+
)ini");
76+
77+
CHECK_UNARY(ini.section_exist("a"));
78+
CHECK_UNARY(ini.line_exist("a", "key"));
79+
CHECK_EQ(ini.read<pcstr>("a", "key"), "\"value\"");
80+
}
81+
82+
TEST_CASE("Parse multiline value")
83+
{
84+
CInifile ini = read_from_string(
85+
R"ini(
86+
[a]
87+
key = "multiline
88+
value"
89+
)ini");
90+
91+
CHECK_UNARY(ini.section_exist("a"));
92+
CHECK_UNARY(ini.line_exist("a", "key"));
93+
CHECK_EQ(ini.read<pcstr>("a", "key"), "\"multiline\r\nvalue\"");
94+
}

0 commit comments

Comments
 (0)