Skip to content

Commit 703ac4c

Browse files
committed
Add unit tests
1 parent f7f692f commit 703ac4c

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ CMakeUserPresets.json
4646

4747
# CMake build output
4848
/out
49+
/TestResults

Test/CMakeLists.txt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_executable(cppjson-Test)
1+
add_executable(cppjson-Test)
22

33
target_link_libraries(cppjson-Test PRIVATE
44
cppjson::cppjson
@@ -8,8 +8,23 @@ target_sources(cppjson-Test PRIVATE
88
Test.cpp
99
)
1010

11-
add_test(
12-
NAME cppjson-Test
13-
COMMAND cppjson-Test
14-
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
11+
set(TEST_SOURCES
12+
TestEmptyObject.cpp
13+
TestPrimitives.cpp
14+
TestNestedObjects.cpp
1515
)
16+
17+
if(MSVC)
18+
add_compile_options("/Zi")
19+
add_link_options("/PROFILE")
20+
endif()
21+
22+
23+
foreach(TEST_SRC IN LISTS TEST_SOURCES)
24+
message(Adding ${TEST_SRC})
25+
get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE)
26+
add_executable(${TEST_NAME} tests/${TEST_SRC})
27+
target_link_libraries(${TEST_NAME} PRIVATE cppjson::cppjson)
28+
29+
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
30+
endforeach()

Test/tests/TestEmptyObject.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <cppjson/cppjson.hpp>
2+
#include <print>
3+
4+
int main()
5+
{
6+
cppjson::Object object{};
7+
if (!object.IsEmpty())
8+
{
9+
std::println("TestEmptyObject failed");
10+
return 1;
11+
}
12+
return 0;
13+
}

Test/tests/TestNestedObjects.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <cppjson/cppjson.hpp>
2+
#include <print>
3+
4+
int main()
5+
{
6+
cppjson::Object object{};
7+
object["sub"]["veryNested"] = 6.0;
8+
9+
if ((double)object["sub"]["veryNested"] != 6.0)
10+
{
11+
std::println("TestNestedObjects failed");
12+
return 1;
13+
}
14+
return 0;
15+
}

Test/tests/TestPrimitives.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <cppjson/cppjson.hpp>
2+
#include <print>
3+
4+
int main()
5+
{
6+
cppjson::Object object{};
7+
object["test1"] = "Hello World";
8+
object["test2"] = 123.0;
9+
10+
if ((std::string)object["test1"] != "Hello World" || (double)object["test2"] != 123.0)
11+
{
12+
std::println("TestPrimitives failed");
13+
return 1;
14+
}
15+
return 0;
16+
}

0 commit comments

Comments
 (0)