From c7a64b8846ec92a0260f4550ec50c563d2824c55 Mon Sep 17 00:00:00 2001 From: Evan Driscoll Date: Sun, 3 Jun 2018 22:41:42 -0500 Subject: [PATCH] Example of more powerful matcher being useful --- test/src/unit-fancy-serialization.cpp | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/src/unit-fancy-serialization.cpp b/test/src/unit-fancy-serialization.cpp index e9e23c6c8..5b287f33a 100644 --- a/test/src/unit-fancy-serialization.cpp +++ b/test/src/unit-fancy-serialization.cpp @@ -28,10 +28,12 @@ SOFTWARE. */ #include "catch.hpp" +#include #include using nlohmann::json; +using nlohmann::json_pointer; using nlohmann::fancy_dump; using nlohmann::fancy_serializer_style; using nlohmann::fancy_serializer_stylizer; @@ -276,6 +278,52 @@ TEST_CASE("serialization") "one line": {"still one line":[1,2]} })")); } + + SECTION("example of more sophisticated matcher") + { + fancy_serializer_stylizer stylizer; + stylizer.get_default_style().set_old_multiline(); + + stylizer.register_style( + [] (const json_pointer& context) + { + // Matches if context[-2] is "each elem on one line" + return (context.cend() - context.cbegin() >= 2) + && (*(context.cend() - 2) == "each elem on one line"); + } + ).space_after_comma = true; + + auto str = fancy_to_string( + { + { + "each elem on one line", { + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5} + }, + }, + { + "fully multiline", { + {1, 2, 3}, + } + } + }, + stylizer); + + CHECK(str == dedent(R"( + { + "each elem on one line": [ + [1, 2, 3, 4, 5], + [1, 2, 3, 4, 5] + ], + "fully multiline": [ + [ + 1, + 2, + 3 + ] + ] + })")); + } } SECTION("Spaces after commas are controllable separately from multiline")