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

Add basic unittests for CInifile #1558

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions .github/workflows/cibuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ jobs:
id: cmake-build
run: cmake --build build --config ${{ matrix.configuration }} --parallel ${{ matrix.platform.threads || 4 }}

- name: Run CTest
working-directory: build
run: ctest --config ${{ matrix.configuration }} --parallel ${{ matrix.platform.threads || 4 }} --output-on-failure --schedule-random --no-tests=error

- name: Make package
if: ${{ steps.cmake-build.outcome == 'success' }}
id: make-package
Expand Down Expand Up @@ -233,3 +237,16 @@ jobs:
shutdown_vm: false
sync_files: false
run: cmake --build build --config ${{ matrix.Configuration }} --parallel 4

- name: Run CTest
uses: cross-platform-actions/action@v0.23.0
with:
operating_system: ${{ matrix.platform.os }}
architecture: ${{ matrix.platform.arch }}
version: ${{ matrix.platform.os-version }}
cpu_count: 4
memory: 13G
environment_variables: CFLAGS CXXFLAGS
shutdown_vm: false
sync_files: false
run: ctest -C ${{ matrix.Configuration }} --parallel 4 --output-on-failure --schedule-random --no-tests=error
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,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
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,16 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT XRAY_USE_DEFAULT_CXX_LIB)

if (XRAY_CXX_LIB STREQUAL "libstdc++")
add_compile_options(-stdlib=libstdc++)
add_link_options(-stdlib=libstdc++)
elseif (XRAY_CXX_LIB STREQUAL "libc++")
add_compile_options(-stdlib=libc++)
add_link_options(-stdlib=libc++)
if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
add_compile_options(-lcxxrt)
add_link_options(-lcxxrt)
else()
add_compile_options(-lc++abi)
add_link_options(-lc++abi)
endif()
endif()
endif()
Expand Down Expand Up @@ -297,6 +301,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
1 change: 1 addition & 0 deletions Externals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_subdirectory(GameSpy)
add_subdirectory(OPCODE)
add_subdirectory(ode)
add_subdirectory(imgui-proj)
add_subdirectory(doctest EXCLUDE_FROM_ALL)

if (NOT TARGET xrLuabind)
message(FATAL_ERROR
Expand Down
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)
13 changes: 13 additions & 0 deletions tests/xrCore/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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)
# https://github.com/doctest/doctest/blob/master/doc/markdown/configuration.md
target_compile_definitions(xrCoreTests PRIVATE
DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
DOCTEST_CONFIG_SUPER_FAST_ASSERTS
)
15 changes: 15 additions & 0 deletions tests/xrCore/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#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);

Memory._initialize();

return context.run();
}
94 changes: 94 additions & 0 deletions tests/xrCore/xr_ini_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#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(ini.read<pcstr>("a", "key"), "value");
}

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(ini.read<pcstr>("a", "key"), "\"value\"");
}

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(ini.read<pcstr>("a", "key"), "\"multiline\r\nvalue\"");
}
Loading