From 6dd75f3797ef3280d022c5e34f8c237c575ed831 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Thu, 16 Jul 2020 17:38:35 +0200 Subject: [PATCH] add tests of json parser --- meson.build | 1 + test/webfuse/json/test_parser.cc | 103 +++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 test/webfuse/json/test_parser.cc diff --git a/meson.build b/meson.build index 85fcc56..d358db0 100644 --- a/meson.build +++ b/meson.build @@ -125,6 +125,7 @@ alltests = executable('alltests', 'test/webfuse/json/test_doc.cc', 'test/webfuse/json/test_node.cc', 'test/webfuse/json/test_reader.cc', + 'test/webfuse/json/test_parser.cc', 'test/webfuse/jsonrpc/mock_timer_callback.cc', 'test/webfuse/jsonrpc/mock_timer.cc', 'test/webfuse/jsonrpc/test_is_request.cc', diff --git a/test/webfuse/json/test_parser.cc b/test/webfuse/json/test_parser.cc new file mode 100644 index 0000000..06ab742 --- /dev/null +++ b/test/webfuse/json/test_parser.cc @@ -0,0 +1,103 @@ +#include "webfuse/impl/json/parser.h" +#include "webfuse/impl/json/reader.h" +#include "webfuse/impl/json/node_intern.h" + +#include +#include + +namespace +{ + +bool try_parse(std::string const & value) +{ + std::string contents = value; + struct wf_json_reader reader; + wf_impl_json_reader_init(&reader, const_cast(contents.data()), contents.size()); + wf_json json; + + bool const result = wf_impl_json_parse_value(&reader, &json); + if (result) + { + wf_impl_json_cleanup(&json); + } + + return result; +} + +} + + +TEST(json_parser, fail_no_contents) +{ + ASSERT_FALSE(try_parse("")); +} + +TEST(json_parser, fail_invalid_null) +{ + ASSERT_FALSE(try_parse("none")); +} + +TEST(json_parser, fail_invalid_true) +{ + ASSERT_FALSE(try_parse("tru")); +} + +TEST(json_parser, fail_invalid_false) +{ + ASSERT_FALSE(try_parse("fals")); +} + +TEST(json_parser, fail_invalid_int) +{ + ASSERT_FALSE(try_parse("-")); +} + +TEST(json_parser, fail_invalid_string) +{ + ASSERT_FALSE(try_parse("\"invalid")); +} + +TEST(json_parser, empty_array) +{ + ASSERT_TRUE(try_parse("[]")); +} + +TEST(json_parser, large_array) +{ + ASSERT_TRUE(try_parse("[1,2,3,4,5,6,7,8,9]")); +} + +TEST(json_parser, fail_unterminated_array) +{ + ASSERT_FALSE(try_parse("[1")); +} + +TEST(json_parser, empty_object) +{ + ASSERT_TRUE(try_parse("{}")); +} + +TEST(json_parser, large_object) +{ + ASSERT_TRUE(try_parse("{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}")); +} + +TEST(json_parser, fail_unterminated_object) +{ + ASSERT_FALSE(try_parse("{\"a\":1")); +} + +TEST(json_parser, fail_invalid_object_key) +{ + ASSERT_FALSE(try_parse("{a:1}")); +} + +TEST(json_parser, fail_missing_object_terminator) +{ + ASSERT_FALSE(try_parse("{\"a\"1}")); +} + +TEST(json_parser, fail_missing_object_value) +{ + ASSERT_FALSE(try_parse("{\"a\":}")); +}