|
1 | 1 | // Copyright (c) 2026 Edward Boggis-Rolfe All rights reserved. |
2 | 2 |
|
3 | | -#include <cstdio> |
4 | | -#include <cstring> |
| 3 | +#include <gtest/gtest.h> |
5 | 4 |
|
6 | 5 | #include "json_test_impl.h" |
7 | 6 |
|
8 | | -// --------------------------------------------------------------------------- |
9 | | -// Minimal test runner — no external test framework dependency. |
10 | | -// Returns 0 if all tests pass, 1 on the first failure. |
11 | | -// --------------------------------------------------------------------------- |
12 | | - |
13 | | -int main() |
| 7 | +namespace json_test |
14 | 8 | { |
15 | | - json_test::json_test_impl impl; |
16 | | - int result; |
17 | | - |
18 | | - // --- test_json_objects -------------------------------------------------- |
19 | 9 |
|
20 | | - result = impl.test_json_objects(); |
21 | | - if (result != 0) |
| 10 | + TEST(JsonObjects, AllChecks) |
22 | 11 | { |
23 | | - std::fprintf(stderr, "FAILED test_json_objects (returned %d)\n", result); |
24 | | - return 1; |
| 12 | + json_test_impl impl; |
| 13 | + ASSERT_EQ(0, impl.test_json_objects()); |
25 | 14 | } |
26 | | - std::printf("PASSED test_json_objects\n"); |
27 | | - |
28 | | - // --- test_json_serialisation -------------------------------------------- |
29 | 15 |
|
| 16 | + TEST(JsonSerialisation, NullRoundtrip) |
30 | 17 | { |
31 | | - // Null roundtrip |
| 18 | + json_test_impl impl; |
32 | 19 | json::v1::object in(nullptr); |
33 | 20 | json::v1::object out; |
34 | | - result = impl.test_json_serialisation(in, out); |
35 | | - if (result != 0 || out != in) |
36 | | - { |
37 | | - std::fprintf(stderr, "FAILED test_json_serialisation/null (returned %d)\n", result); |
38 | | - return 1; |
39 | | - } |
| 21 | + ASSERT_EQ(0, impl.test_json_serialisation(in, out)); |
| 22 | + ASSERT_EQ(in, out); |
| 23 | + } |
40 | 24 |
|
41 | | - // String roundtrip |
42 | | - in = json::v1::object(std::string("hello")); |
43 | | - out = json::v1::object(nullptr); |
44 | | - result = impl.test_json_serialisation(in, out); |
45 | | - if (result != 0 || out != in) |
46 | | - { |
47 | | - std::fprintf(stderr, "FAILED test_json_serialisation/string (returned %d)\n", result); |
48 | | - return 1; |
49 | | - } |
| 25 | + TEST(JsonSerialisation, StringRoundtrip) |
| 26 | + { |
| 27 | + json_test_impl impl; |
| 28 | + json::v1::object in(std::string("hello")); |
| 29 | + json::v1::object out; |
| 30 | + ASSERT_EQ(0, impl.test_json_serialisation(in, out)); |
| 31 | + ASSERT_EQ(in, out); |
| 32 | + } |
50 | 33 |
|
51 | | - // Number roundtrip |
52 | | - in = json::v1::object(42L); |
53 | | - out = json::v1::object(nullptr); |
54 | | - result = impl.test_json_serialisation(in, out); |
55 | | - if (result != 0 || out != in) |
56 | | - { |
57 | | - std::fprintf(stderr, "FAILED test_json_serialisation/number (returned %d)\n", result); |
58 | | - return 1; |
59 | | - } |
| 34 | + TEST(JsonSerialisation, NumberRoundtrip) |
| 35 | + { |
| 36 | + json_test_impl impl; |
| 37 | + json::v1::object in(42L); |
| 38 | + json::v1::object out; |
| 39 | + ASSERT_EQ(0, impl.test_json_serialisation(in, out)); |
| 40 | + ASSERT_EQ(in, out); |
| 41 | + } |
60 | 42 |
|
61 | | - // Map roundtrip |
| 43 | + TEST(JsonSerialisation, MapRoundtrip) |
| 44 | + { |
| 45 | + json_test_impl impl; |
62 | 46 | json::v1::map m; |
63 | 47 | m.emplace("key", json::v1::object("value")); |
64 | | - in = json::v1::object(m); |
65 | | - out = json::v1::object(nullptr); |
66 | | - result = impl.test_json_serialisation(in, out); |
67 | | - if (result != 0 || out != in) |
68 | | - { |
69 | | - std::fprintf(stderr, "FAILED test_json_serialisation/map (returned %d)\n", result); |
70 | | - return 1; |
71 | | - } |
| 48 | + json::v1::object in(m); |
| 49 | + json::v1::object out; |
| 50 | + ASSERT_EQ(0, impl.test_json_serialisation(in, out)); |
| 51 | + ASSERT_EQ(in, out); |
72 | 52 | } |
73 | | - std::printf("PASSED test_json_serialisation\n"); |
74 | | - |
75 | | - // --- simple_database_test ----------------------------------------------- |
76 | 53 |
|
77 | | - result = impl.simple_database_test("test_app"); |
78 | | - if (result != 0) |
| 54 | + TEST(JsonDatabase, SimpleDatabaseTest) |
79 | 55 | { |
80 | | - std::fprintf(stderr, "FAILED simple_database_test (returned %d)\n", result); |
81 | | - return 1; |
| 56 | + json_test_impl impl; |
| 57 | + ASSERT_EQ(0, impl.simple_database_test("test_app")); |
82 | 58 | } |
83 | | - std::printf("PASSED simple_database_test\n"); |
84 | 59 |
|
85 | | - std::printf("\nAll tests passed.\n"); |
86 | | - return 0; |
87 | | -} |
| 60 | +} // namespace json_test |
0 commit comments