From fa89ce1471e97f55016ade75543bb680d6e591d1 Mon Sep 17 00:00:00 2001 From: "marek.piotrowski" Date: Sun, 23 Jul 2023 23:20:49 +0200 Subject: [PATCH] Add more tests --- example/main.cpp | 11 ++--- .../nlohmann/detail/macro_scope_annotated.hpp | 30 ++++++------ tests/src/unit-annotations.cpp | 49 ++++++++++++++++--- 3 files changed, 63 insertions(+), 27 deletions(-) diff --git a/example/main.cpp b/example/main.cpp index c02d83906..aa8015230 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -32,24 +32,23 @@ private: public: AnotherExampleClass() = default; - NLOHMANN_DEFINE_TYPE_INTRUSIVE_ANNOTATED(AnotherExampleClass, property1, "comment两", - property2, "multiline\ncomment22", + NLOHMANN_DEFINE_TYPE_INTRUSIVE_ANNOTATED(AnotherExampleClass, property1, "comment11", + property2, "comment22", property3, "comment33", property4, "comment44", property5, "comment55"); }; int main() { - std::cout << "Hello, world!" << std::endl; ExampleClass ec; AnotherExampleClass aec; std::ofstream example_file; - example_file.open("/Users/marekpiotrowski/private/json/example.json"); + example_file.open("example_1.json"); nlohmann::json j = ec; example_file << j.dump_annotated(4, ' ', true) << std::endl; - // nlohmann::json j2 = aec; - // std::cout << j2.dump_annotated() << std::endl; + nlohmann::json j2 = aec; + std::cout << j2.dump_annotated() << std::endl; return 0; } \ No newline at end of file diff --git a/include/nlohmann/detail/macro_scope_annotated.hpp b/include/nlohmann/detail/macro_scope_annotated.hpp index e817d0f5e..6aca78392 100644 --- a/include/nlohmann/detail/macro_scope_annotated.hpp +++ b/include/nlohmann/detail/macro_scope_annotated.hpp @@ -59,21 +59,21 @@ #define GET_TERNARY_EXPAND_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,NAME,...) NAME #define TERNARY_EXPAND(...) GET_TERNARY_EXPAND_MACRO(__VA_ARGS__, \ - TERNARY_EXPAND15, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND14, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND13, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND12, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND11, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND10, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND9, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND8, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND7, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND6, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND5, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND4, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND3, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND2, TERNARY_NOT_ALLOWED, \ - TERNARY_EXPAND1, TERNARY_NOT_ALLOWED)(__VA_ARGS__) + TERNARY_EXPAND15, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND14, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND13, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND12, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND11, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND10, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND9, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND8, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND7, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND6, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND5, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND4, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND3, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND2, ANNOTATED_JSON_NOT_ALLOWED, \ + TERNARY_EXPAND1, ANNOTATED_JSON_NOT_ALLOWED)(__VA_ARGS__) #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_ANNOTATED(Type, ...) \ friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_ANNOTATED_EXPAND(NLOHMANN_JSON_ANNOTATED_PASTE(NLOHMANN_JSON_ANNOTATED_TO, __VA_ARGS__)) } \ diff --git a/tests/src/unit-annotations.cpp b/tests/src/unit-annotations.cpp index 0884e8824..df9b4f454 100644 --- a/tests/src/unit-annotations.cpp +++ b/tests/src/unit-annotations.cpp @@ -42,6 +42,17 @@ public: NLOHMANN_DEFINE_TYPE_INTRUSIVE_ANNOTATED(MultiLineAnnotationExampleClass, property1, "multiline\ncomment11", property2, "multiline\ncomment22"); }; + +class ExampleClassUTF8 { +private: + double property3{1.1}; + std::vector property5{1.5, 5.4, 3.2}; +public: + ExampleClassUTF8() = default; + + NLOHMANN_DEFINE_TYPE_INTRUSIVE_ANNOTATED(ExampleClassUTF8, property3, "comment三", + property5, "comment五"); +}; } TEST_CASE("annotation") @@ -50,7 +61,7 @@ TEST_CASE("annotation") { ExampleClass ex; nlohmann::json j = ex; - const auto ex_actual_json = j.dump_annotated(4, ' ', true); + const auto ex_actual_json = j.dump_annotated(4); const auto expected_json = "{\n" " /* comment1 */\n" " \"property1\": 1,\n" @@ -78,7 +89,7 @@ TEST_CASE("annotation") AnotherExampleClass another_ex; nlohmann::json j1 = ex; nlohmann::json j2 = another_ex; - const auto another_ex_actual_json = j2.dump_annotated(4, ' ', true); + const auto another_ex_actual_json = j2.dump_annotated(4); const auto expected_json = "{\n" " /* comment11 */\n" " \"property1\": 11,\n" @@ -91,7 +102,7 @@ TEST_CASE("annotation") { MultiLineAnnotationExampleClass ex; nlohmann::json j = ex; - const auto ex_actual_json = j.dump_annotated(4, ' ', true); + const auto ex_actual_json = j.dump_annotated(4); const auto expected_json = "{\n" " /* multiline */\n" " /* comment11 */\n" @@ -102,12 +113,38 @@ TEST_CASE("annotation") "}"; CHECK(ex_actual_json == expected_json); } - SECTION("utf8_comment_not_ascii") + SECTION("utf8_comment_ascii_not_ensured") { - // TODO + ExampleClassUTF8 ex; + nlohmann::json j = ex; + const auto ex_actual_json = j.dump_annotated(4); + const auto expected_json = "{\n" + " /* comment三 */\n" + " \"property3\": 1.1,\n" + " /* comment五 */\n" + " \"property5\": [\n" + " 1.5,\n" + " 5.4,\n" + " 3.2\n" + " ]\n" + "}"; + CHECK(ex_actual_json == expected_json); } SECTION("utf8_comment_ensure_ascii") { - // TODO + ExampleClassUTF8 ex; + nlohmann::json j = ex; + const auto ex_actual_json = j.dump_annotated(4, ' ', true); + const auto expected_json = "{\n" + " /* comment\\u4e09 */\n" + " \"property3\": 1.1,\n" + " /* comment\\u4e94 */\n" + " \"property5\": [\n" + " 1.5,\n" + " 5.4,\n" + " 3.2\n" + " ]\n" + "}"; + CHECK(ex_actual_json == expected_json); } } \ No newline at end of file