Skip to content

Commit

Permalink
Cleanup tests by separating them into multiple files
Browse files Browse the repository at this point in the history
Separate the test suite into multiple files and make the `test ctx` variable a global variable inside `test_ctx.hpp` with the test entry point in `test_main.cpp`.

* **Add `test_ctx.hpp`**: Define `TestCtx` struct and `g_testctx` global variable.
* **Add `test_main.cpp`**: Include necessary headers, define `main` function to initialize `TestCtx` and run all tests.
* **Add `test_parse.cpp`**: Include necessary headers, define `Parse` test cases using `g_testctx`.
* **Add `test_add.cpp`**: Include necessary headers, define `Add` test cases using `g_testctx`.
* **Add `test_remove.cpp`**: Include necessary headers, define `Remove` test cases using `g_testctx`.
* **Add `test_has.cpp`**: Include necessary headers, define `Has` test cases using `g_testctx`.
* **Add `test_edit.cpp`**: Include necessary headers, define `Edit` test cases using `g_testctx`.
* **Add `test_conversion.cpp`**: Include necessary headers, define `Conversion` test cases using `g_testctx`.
* **Modify `CMakeLists.txt`**: Remove `test.cpp` from `add_executable`, add new test files to `add_executable`.
* **Delete `test.cpp`**: Remove the original single test file.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/X-rays5/inireader/tree/main?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
X-rays5 committed Oct 21, 2024
1 parent e68f665 commit e06bc04
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 214 deletions.
4 changes: 2 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ set(CMAKE_CXX_STANDARD 17)
add_subdirectory(vendor/googletest)
enable_testing()

add_executable(${PROJECT_NAME} test.cpp)
add_executable(${PROJECT_NAME} test_main.cpp test_parse.cpp test_add.cpp test_remove.cpp test_has.cpp test_edit.cpp test_conversion.cpp)

target_include_directories(${PROJECT_NAME} PRIVATE ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} gtest gtest_main)

add_test(inireader ${PROJECT_NAME})
add_test(inireader ${PROJECT_NAME})
212 changes: 0 additions & 212 deletions test/test.cpp

This file was deleted.

12 changes: 12 additions & 0 deletions test/test_add.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <gtest/gtest.h>
#include "test_ctx.hpp"

TEST(Add, Default) {
g_testctx->ini_file.GetRootSection().Add("testv", "hi");
EXPECT_STREQ(g_testctx->ini_file.GetRootSection()["testv"].as<const char*>(), "hi");
}

TEST(Add, Kv) {
g_testctx->ini_file.AddSection("addedsection").Add("testv", "value");
EXPECT_STREQ(g_testctx->ini_file["addedsection"]["testv"].as<const char*>(), "value");
}
22 changes: 22 additions & 0 deletions test/test_conversion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <gtest/gtest.h>
#include "test_ctx.hpp"

TEST(Conversion, UTF8) {
std::string hello_world{ u8"hello, 世界" };

std::u16string hello_world_u16{ini::conversion::utility::DecodeUTF(hello_world)};
EXPECT_EQ(hello_world, ini::conversion::utility::EncodeUTF(hello_world_u16));
#ifdef _WIN32
EXPECT_EQ(hello_world_u16.length(), 13); // 13 bytes, 9 characters
#else
EXPECT_EQ(hello_world_u16.length(), 9); // 9 bytes, 9 characters
#endif

std::u32string hello_world_u32{ini::conversion::utility::DecodeUTF<char32_t>(hello_world)};
EXPECT_EQ(hello_world, ini::conversion::utility::EncodeUTF<char32_t>(hello_world_u32));
#ifdef _WIN32
EXPECT_EQ(hello_world_u32.length(), 13); // 13 bytes, 9 characters
#else
EXPECT_EQ(hello_world_u32.length(), 9); // 9 bytes, 9 characters
#endif
}
12 changes: 12 additions & 0 deletions test/test_ctx.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef TEST_CTX_HPP
#define TEST_CTX_HPP

#include "../include/inireader/inireader.hpp"

struct TestCtx {
ini::Parser ini_file;
};

inline TestCtx* g_testctx = nullptr;

#endif // TEST_CTX_HPP
33 changes: 33 additions & 0 deletions test/test_edit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <gtest/gtest.h>
#include "test_ctx.hpp"

TEST(Edit, Default) {
g_testctx->ini_file.GetRootSection().Add("edittest", 4);
EXPECT_EQ(g_testctx->ini_file.GetRootSection().HasValue("edittest"), true);
g_testctx->ini_file.GetRootSection()["edittest"] = 1234;
EXPECT_EQ(g_testctx->ini_file.GetRootSection()["edittest"].as<int>(), 1234);
EXPECT_EQ(g_testctx->ini_file.GetRootSection().Remove("edittest"), true);
}

TEST(Edit, Section) {
g_testctx->ini_file.AddSection("testsection").Add("edittest", "wow");
EXPECT_EQ(g_testctx->ini_file["testsection"].HasValue("edittest"), true);
g_testctx->ini_file["testsection"]["edittest"] = 1234;
EXPECT_EQ(g_testctx->ini_file["testsection"]["edittest"].as<int>(), 1234);
EXPECT_EQ(g_testctx->ini_file.RemoveSection("testsection"), true);
}

TEST(Edit, Reference) {
ini::Parser::IniSection& section = g_testctx->ini_file.AddSection("edit_ref");
section.Add("test_num", 1234);
section.Add("test_str", "hello");

EXPECT_TRUE(g_testctx->ini_file["edit_ref"]["test_num"].is<std::int32_t>());
EXPECT_TRUE(g_testctx->ini_file["edit_ref"]["test_str"].is<std::string>());
EXPECT_EQ(g_testctx->ini_file["edit_ref"]["test_num"].as<std::int32_t>(), 1234);
EXPECT_EQ(g_testctx->ini_file["edit_ref"]["test_str"].as<std::string>(), "hello");
EXPECT_EQ(g_testctx->ini_file["edit_ref"]["test_str"].as<std::string_view>(), "hello");

section.Remove("test_num");
EXPECT_FALSE(g_testctx->ini_file["edit_ref"].HasValue("test_num"));
}
16 changes: 16 additions & 0 deletions test/test_has.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <gtest/gtest.h>
#include "test_ctx.hpp"

TEST(Has, Default) {
g_testctx->ini_file.GetRootSection().Add("testv", "hi");
EXPECT_EQ(g_testctx->ini_file.GetRootSection().HasValue("testv"), true);
EXPECT_EQ(g_testctx->ini_file.GetRootSection().Remove("testv"), true);
}

TEST(Has, Section) {
EXPECT_EQ(g_testctx->ini_file.HasSection("Section 1"), true);
}

TEST(Has, Kv) {
EXPECT_EQ(g_testctx->ini_file["Section 1"].HasValue("Option 1"), true);
}
49 changes: 49 additions & 0 deletions test/test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <gtest/gtest.h>
#include "test_ctx.hpp"

int main(int argc, char** argv) {
constexpr const char* testfile = "default section value = test value ; section less\n"
"\n"
"[comment_val]\n"
"val1 = \"##hello\"\n"
"val2## = world\n"
"val3 = he##llo\n"
"[Section 1]\n"
"; comment\n"
"# comment2\n"
"test_line_break = test1\r"
"Option 1 = value 1 ; option 'Option 1' has value 'value 1'\n"
"Option 2 = value 2 # option 'Option 2' has value 'value 2'\n"
"oPtion 1 = value 2\\ \\ \\ ; option 'oPtion 1' has value ' value 2 ', 'oPtion 1' and 'Option 1' are different\n"
"Option 3= value 3 = not value 2\n"
"option 3 =value 3 = not value 2 = not value 1\\\n"
"\n"
"[Numbers]\n"
"num = -1285\n"
"num_bin = 0b01101001\n"
"num_hex = 0x12ae\n"
"num_oct = 01754\n"
"num_uint64 = 1122334400000000\n"
"\n"
"float1 = -124.45667356\n"
"float2 = 4.123456545\n"
"float3 = 412.3456545\n"
"float4 = -1.1245864\n"
"\n"
"[Other]\n"
"bool1 = 1\n"
"bool2 = on\n"
"bool3=off";
std::ofstream writer("test.ini");
writer << testfile;
writer.close();

TestCtx test_ctx;
g_testctx = &test_ctx;
test_ctx.ini_file.Parse(testfile, false);

::testing::InitGoogleTest(&argc, argv);
auto ret = RUN_ALL_TESTS();
std::filesystem::remove("test.ini");
return ret;
}
Loading

0 comments on commit e06bc04

Please sign in to comment.