Example of more powerful matcher being useful
This commit is contained in:
parent
5f0870ef34
commit
c7a64b8846
@ -28,10 +28,12 @@ SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "catch.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
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<json>& 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")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user