Skip to content

Commit 8bcd098

Browse files
committed
Tests: Add basic unittests for CInifile
1 parent 4a8b6fb commit 8bcd098

File tree

9 files changed

+157
-0
lines changed

9 files changed

+157
-0
lines changed

.github/workflows/cibuild.yml

+17
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ jobs:
152152
id: cmake-build
153153
run: cmake --build build --config ${{ matrix.configuration }} --parallel ${{ matrix.platform.threads || 4 }}
154154

155+
- name: Run CTest
156+
working-directory: build
157+
run: ctest --config ${{ matrix.configuration }} --parallel ${{ matrix.platform.threads || 4 }} --output-on-failure --schedule-random --no-tests=error
158+
155159
- name: Make package
156160
if: ${{ steps.cmake-build.outcome == 'success' }}
157161
id: make-package
@@ -233,3 +237,16 @@ jobs:
233237
shutdown_vm: false
234238
sync_files: false
235239
run: cmake --build build --config ${{ matrix.Configuration }} --parallel 4
240+
241+
- name: Run CTest
242+
uses: cross-platform-actions/[email protected]
243+
with:
244+
operating_system: ${{ matrix.platform.os }}
245+
architecture: ${{ matrix.platform.arch }}
246+
version: ${{ matrix.platform.os-version }}
247+
cpu_count: 4
248+
memory: 13G
249+
environment_variables: CFLAGS CXXFLAGS
250+
shutdown_vm: false
251+
sync_files: false
252+
run: ctest -C ${{ matrix.Configuration }} --parallel 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
@@ -139,12 +139,16 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT XRAY_USE_DEFAULT_CXX_LIB)
139139

140140
if (XRAY_CXX_LIB STREQUAL "libstdc++")
141141
add_compile_options(-stdlib=libstdc++)
142+
add_link_options(-stdlib=libstdc++)
142143
elseif (XRAY_CXX_LIB STREQUAL "libc++")
143144
add_compile_options(-stdlib=libc++)
145+
add_link_options(-stdlib=libc++)
144146
if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
145147
add_compile_options(-lcxxrt)
148+
add_link_options(-lcxxrt)
146149
else()
147150
add_compile_options(-lc++abi)
151+
add_link_options(-lc++abi)
148152
endif()
149153
endif()
150154
endif()
@@ -290,6 +294,13 @@ add_subdirectory(src)
290294
add_subdirectory(res)
291295
add_subdirectory(misc)
292296

297+
# Tests
298+
option(BUILD_TESTS "Build tests" ON)
299+
if (BUILD_TESTS)
300+
include(CTest)
301+
add_subdirectory(tests)
302+
endif()
303+
293304
get_property(LIB64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
294305

295306
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)