Skip to content

Commit

Permalink
json_formatter: Add tests for formatter::write
Browse files Browse the repository at this point in the history
Adds some unit tests for the various `formatter::write` overloads.
  • Loading branch information
StephanDollberg committed Sep 17, 2024
1 parent 65d6d9c commit d7d3388
Showing 1 changed file with 54 additions and 10 deletions.
64 changes: 54 additions & 10 deletions tests/unit/json_formatter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,15 @@ SEASTAR_TEST_CASE(test_jsonable) {
return make_ready_future();
}

SEASTAR_THREAD_TEST_CASE(test_stream_range_as_array) {
template<typename F>
void formatter_check_expected(sstring expected, F f, bool close = true) {
auto vec = std::vector<net::packet>{};
auto out = output_stream<char>(data_sink(std::make_unique<vector_data_sink>(vec)), 8);

auto mapper = stream_range_as_array(std::vector<int>{1,2,3}, [] (auto i) {
object_json obj;
obj.subject = std::to_string(i);
obj.values.push(i);
return obj;
});

mapper(std::move(out)).get();
f(out);
if (close) {
out.close().get();
};

auto packets = net::packet{};
for (auto &p : vec) {
Expand All @@ -112,6 +109,53 @@ SEASTAR_THREAD_TEST_CASE(test_stream_range_as_array) {
auto buf = packets.release();

sstring result(buf.front().get(), buf.front().size());
sstring expected = "[{\"subject\":\"1\",\"values\":[1]}, {\"subject\":\"2\",\"values\":[2]}, {\"subject\":\"3\",\"values\":[3]}]";
BOOST_CHECK_EQUAL(expected, result);
}


SEASTAR_THREAD_TEST_CASE(test_stream_range_as_array) {
sstring expected = "[{\"subject\":\"1\",\"values\":[1]}, {\"subject\":\"2\",\"values\":[2]}, {\"subject\":\"3\",\"values\":[3]}]";
formatter_check_expected(expected, [] (auto& out) {
auto mapper = stream_range_as_array(std::vector<int>{1,2,3}, [] (auto i) {
object_json obj;
obj.subject = std::to_string(i);
obj.values.push(i);
return obj;
});

mapper(std::move(out)).get();
}, false);
}

SEASTAR_THREAD_TEST_CASE(formatter_write) {

formatter_check_expected("3", [](auto &out) {
json::formatter::write(out, 3).get();
});
formatter_check_expected("false", [](auto &out) {
json::formatter::write(out, false).get();
});
formatter_check_expected("\"foo\"", [](auto &out) {
json::formatter::write(out, "foo").get();
});

formatter_check_expected("{1:2,3:4}", [] (auto& out) {
json::formatter::write(out, std::map<int, int>({{1, 2}, {3, 4}})).get();
});
formatter_check_expected("{3:4,1:2}", [] (auto& out) {
json::formatter::write(out, std::unordered_map<int, int>({{1, 2}, {3, 4}})).get();
});
formatter_check_expected("[1,2,3,4]", [](auto &out) {
json::formatter::write(out, std::vector<int>({1, 2, 3, 4})).get();
});

formatter_check_expected("[{1:2},{3:4}]", [](auto &out) {
json::formatter::write(out, std::vector<std::pair<int, int>>({{1, 2}, {3, 4}})).get();
});
formatter_check_expected("[{1:2},{3:4}]", [](auto &out) {
json::formatter::write(out, std::vector<std::map<int, int>>({{{1, 2}}, {{3, 4}}})).get();
});
formatter_check_expected("[[1,2],[3,4]]", [](auto &out) {
json::formatter::write(out, std::vector<std::vector<int>>({{1, 2}, {3, 4}})).get();
});
}

0 comments on commit d7d3388

Please sign in to comment.