Skip to content

Commit

Permalink
Tests: Add basic unittests for CInifile
Browse files Browse the repository at this point in the history
  • Loading branch information
AMS21 committed Dec 8, 2023
1 parent 466fe12 commit 8e2dbb7
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/cibuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ jobs:
CXX: ${{ matrix.CXX }}
run: cmake --build build --config ${{ matrix.Configuration }} --parallel $(nproc || echo 4)

- name: Run CTest
working-directory: build
run: ctest --parallel $(nproc || echo 4)

- name: Make package
if: ${{ steps.cmake-build.outcome == 'success' }}
id: make-package
Expand Down Expand Up @@ -171,6 +175,10 @@ jobs:
shell: alpine.sh {0}
run: cmake --build build --config ${{ matrix.Configuration }} --parallel $(nproc || echo 4)

- name: Run CTest
shell: alpine.sh {0}
run: ctest --parallel $(nproc || echo 4)

build-macos:
name: macOS ${{ matrix.Configuration }} ${{ matrix.Platform }}
runs-on: macos-latest
Expand All @@ -197,3 +205,6 @@ jobs:

- name: Run CMake Build
run: cmake --build build --config ${{ matrix.Configuration }} --parallel $(sysctl -n hw.ncpu || echo 4)

- name: Run CTest
run: ctest --parallel $(sysctl -n hw.ncpu || echo 4)
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@
[submodule "Externals/xrLuaFix"]
path = Externals/xrLuaFix
url = https://github.com/OpenXRay/xrLuaFix.git
[submodule "Externals/doctest"]
path = Externals/doctest
url = https://github.com/doctest/doctest.git
branch = master
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ add_subdirectory(src)
add_subdirectory(res)
add_subdirectory(misc)

# Tests
option(BUILD_TESTS "Build tests" ON)
if (BUILD_TESTS)
include(CTest)
add_subdirectory(tests)
endif()

get_property(LIB64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)

if ("${LIB64}" STREQUAL "TRUE")
Expand Down
2 changes: 2 additions & 0 deletions Externals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ if (NOT TARGET xrLuabind)
"Read the build instructions: https://github.com/OpenXRay/xray-16/wiki"
)
endif()

add_subdirectory(doctest EXCLUDE_FROM_ALL)
1 change: 1 addition & 0 deletions Externals/doctest
Submodule doctest added at ae7a13
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(xrCore)
9 changes: 9 additions & 0 deletions tests/xrCore/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

add_executable(
xrCoreTests
main.cpp
xr_ini_test.cpp)

target_link_libraries(xrCoreTests xrCore doctest::doctest)
target_include_directories(xrCoreTests PRIVATE "${CMAKE_SOURCE_DIR}/src")
add_test(NAME xrCoreTests COMMAND xrCoreTests)
17 changes: 17 additions & 0 deletions tests/xrCore/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#define DOCTEST_CONFIG_IMPLEMENT
#include <doctest/doctest.h>

#include <Common/Platform.hpp>
#include <xrCore/xrCore.h>

int main(int argc, char** argv)
{
doctest::Context context;

context.applyCommandLine(argc, argv);

// Initialize memory
Memory._initialize();

return context.run();
}
88 changes: 88 additions & 0 deletions tests/xrCore/xr_ini_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <doctest/doctest.h>

#include <Common/Platform.hpp>
#include <xrCore/xrCore.h>
#include <xrCore/xr_types.h>

#include <xrCore/xr_ini.h>

CInifile
read_from_string(pcstr str,
CInifile::allow_include_func_t allow_include = nullptr) {
IReader reader = IReader(const_cast<pstr>(str), xr_strlen(str));
return CInifile(&reader, "test.ini", allow_include);
}

TEST_CASE("parse empty file") {
CInifile ini = read_from_string("");

CHECK_EQ(ini.section_count(), 0);
}

TEST_CASE("parse empty section") {
CInifile ini = read_from_string("[a]");

CHECK_EQ(ini.section_count(), 1);
CHECK_UNARY(ini.section_exist("a"));
}

TEST_CASE("parse simple section") {
CInifile ini = read_from_string(
R"ini(
[a]
key = value
)ini");

CHECK_UNARY(ini.section_exist("a"));
CHECK_UNARY(ini.line_exist("a", "key"));
CHECK_EQ(xr_strcmp(ini.read<pcstr>("a", "key"), "value"), 0);
}

TEST_CASE("parse integer value") {
CInifile ini = read_from_string(
R"ini(
[a]
key = 123
)ini");

CHECK_UNARY(ini.section_exist("a"));
CHECK_UNARY(ini.line_exist("a", "key"));
CHECK_EQ(ini.read<u32>("a", "key"), 123);
}

TEST_CASE("Parse float value") {
CInifile ini = read_from_string(
R"ini(
[a]
key = 123.456
)ini");

CHECK_UNARY(ini.section_exist("a"));
CHECK_UNARY(ini.line_exist("a", "key"));
CHECK_EQ(ini.read<f32>("a", "key"), 123.456f);
}

TEST_CASE("Parse quoted value") {
CInifile ini = read_from_string(
R"ini(
[a]
key = "value"
)ini");

CHECK_UNARY(ini.section_exist("a"));
CHECK_UNARY(ini.line_exist("a", "key"));
CHECK_EQ(xr_strcmp(ini.read<pcstr>("a", "key"), "\"value\""), 0);
}

TEST_CASE("Parse multiline value") {
CInifile ini = read_from_string(
R"ini(
[a]
key = "multiline
value"
)ini");

CHECK_UNARY(ini.section_exist("a"));
CHECK_UNARY(ini.line_exist("a", "key"));
CHECK_EQ(xr_strcmp(ini.read<pcstr>("a", "key"), "\"multiline\r\nvalue\""), 0);
}

0 comments on commit 8e2dbb7

Please sign in to comment.