-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup tests by separating them into multiple files
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
Showing
10 changed files
with
216 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.