mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
added implementation of json parser
This commit is contained in:
133
test/webfuse_provider/json/test_json_parser.cc
Normal file
133
test/webfuse_provider/json/test_json_parser.cc
Normal file
@@ -0,0 +1,133 @@
|
||||
#include "webfuse_provider/impl/json/parser.h"
|
||||
#include "webfuse_provider/impl/json/node.h"
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstring>
|
||||
|
||||
namespace
|
||||
{
|
||||
wfp_json_doc * parse_json(char * text)
|
||||
{
|
||||
return wfp_impl_json_parse_buffer(text, strlen(text));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(json_parser, parse_true)
|
||||
{
|
||||
char text[] = "true";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_NE(nullptr, doc);
|
||||
wfp_json const * root = wfp_impl_json_root(doc);
|
||||
ASSERT_TRUE(wfp_impl_json_is_bool(root));
|
||||
ASSERT_TRUE(wfp_impl_json_get_bool(root));
|
||||
|
||||
wfp_impl_json_dispose(doc);
|
||||
}
|
||||
|
||||
TEST(json_parser, parse_false)
|
||||
{
|
||||
char text[] = "false";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_NE(nullptr, doc);
|
||||
wfp_json const * root = wfp_impl_json_root(doc);
|
||||
ASSERT_TRUE(wfp_impl_json_is_bool(root));
|
||||
ASSERT_FALSE(wfp_impl_json_get_bool(root));
|
||||
|
||||
wfp_impl_json_dispose(doc);
|
||||
}
|
||||
|
||||
TEST(json_parser, parse_int)
|
||||
{
|
||||
char text[] = "42";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_NE(nullptr, doc);
|
||||
wfp_json const * root = wfp_impl_json_root(doc);
|
||||
ASSERT_TRUE(wfp_impl_json_is_int(root));
|
||||
ASSERT_EQ(42, wfp_impl_json_get_int(root));
|
||||
|
||||
wfp_impl_json_dispose(doc);
|
||||
}
|
||||
|
||||
TEST(json_parser, parse_negative_int)
|
||||
{
|
||||
char text[] = "-1234";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_NE(nullptr, doc);
|
||||
wfp_json const * root = wfp_impl_json_root(doc);
|
||||
ASSERT_TRUE(wfp_impl_json_is_int(root));
|
||||
ASSERT_EQ(-1234, wfp_impl_json_get_int(root));
|
||||
|
||||
wfp_impl_json_dispose(doc);
|
||||
}
|
||||
|
||||
TEST(json_parser, parse_string)
|
||||
{
|
||||
char text[] = "\"brummni\"";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_NE(nullptr, doc);
|
||||
wfp_json const * root = wfp_impl_json_root(doc);
|
||||
ASSERT_TRUE(wfp_impl_json_is_string(root));
|
||||
ASSERT_STREQ("brummni", wfp_impl_json_get_string(root));
|
||||
|
||||
wfp_impl_json_dispose(doc);
|
||||
}
|
||||
|
||||
TEST(json_parser, parse_array)
|
||||
{
|
||||
char text[] = "[true,1,\"foo\",[42]]";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_NE(nullptr, doc);
|
||||
wfp_json const * root = wfp_impl_json_root(doc);
|
||||
ASSERT_TRUE(wfp_impl_json_is_array(root));
|
||||
ASSERT_EQ(4, wfp_impl_json_array_size(root));
|
||||
|
||||
ASSERT_TRUE(wfp_impl_json_is_bool(wfp_impl_json_array_get(root, 0)));
|
||||
ASSERT_TRUE(wfp_impl_json_is_int(wfp_impl_json_array_get(root, 1)));
|
||||
ASSERT_TRUE(wfp_impl_json_is_string(wfp_impl_json_array_get(root, 2)));
|
||||
ASSERT_TRUE(wfp_impl_json_is_array(wfp_impl_json_array_get(root, 3)));
|
||||
|
||||
wfp_impl_json_dispose(doc);
|
||||
}
|
||||
|
||||
TEST(json_parser, parse_object)
|
||||
{
|
||||
char text[] = "{\"method\":\"add\",\"params\":[1,2],\"id\":42}";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_NE(nullptr, doc);
|
||||
wfp_json const * root = wfp_impl_json_root(doc);
|
||||
ASSERT_TRUE(wfp_impl_json_is_object(root));
|
||||
ASSERT_EQ(3, wfp_impl_json_object_size(root));
|
||||
|
||||
ASSERT_STREQ("method", wfp_impl_json_object_key(root, 0));
|
||||
ASSERT_STREQ("params", wfp_impl_json_object_key(root, 1));
|
||||
ASSERT_STREQ("id", wfp_impl_json_object_key(root, 2));
|
||||
|
||||
wfp_impl_json_dispose(doc);
|
||||
}
|
||||
|
||||
TEST(json_parser, parse_fail_invalid_json)
|
||||
{
|
||||
{
|
||||
char text[] = "True";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_EQ(nullptr, doc);
|
||||
}
|
||||
|
||||
{
|
||||
char text[] = "flas";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_EQ(nullptr, doc);
|
||||
}
|
||||
|
||||
{
|
||||
char text[] = "[1,2,3}";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_EQ(nullptr, doc);
|
||||
}
|
||||
|
||||
{
|
||||
char text[] = "{\"method\":\"add\",\"params\":[1,2],\"id\":42";
|
||||
wfp_json_doc * doc = parse_json(text);
|
||||
ASSERT_EQ(nullptr, doc);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,7 +5,7 @@ TEST(jsonrpc_util, get_int)
|
||||
{
|
||||
json_t * object = json_object();
|
||||
json_object_set_new(object, "key", json_integer(23));
|
||||
int value = wfp_impl_json_get_int(object, "key", 42);
|
||||
int value = wfp_impl_json_get_integer(object, "key", 42);
|
||||
ASSERT_EQ(23, value);
|
||||
|
||||
json_decref(object);
|
||||
@@ -13,7 +13,7 @@ TEST(jsonrpc_util, get_int)
|
||||
|
||||
TEST(jsonrpc_util, failed_to_get_null_object)
|
||||
{
|
||||
int value = wfp_impl_json_get_int(nullptr, "key", 42);
|
||||
int value = wfp_impl_json_get_integer(nullptr, "key", 42);
|
||||
|
||||
ASSERT_EQ(42, value);
|
||||
}
|
||||
@@ -21,7 +21,7 @@ TEST(jsonrpc_util, failed_to_get_null_object)
|
||||
TEST(jsonrpc_util, failed_to_get_not_object)
|
||||
{
|
||||
json_t * object = json_array();
|
||||
int value = wfp_impl_json_get_int(nullptr, "key", 42);
|
||||
int value = wfp_impl_json_get_integer(nullptr, "key", 42);
|
||||
ASSERT_EQ(42, value);
|
||||
|
||||
json_decref(object);
|
||||
@@ -30,7 +30,7 @@ TEST(jsonrpc_util, failed_to_get_not_object)
|
||||
TEST(jsonrpc_util, failed_to_get_invalid_key)
|
||||
{
|
||||
json_t * object = json_object();
|
||||
int value = wfp_impl_json_get_int(object, "key", 42);
|
||||
int value = wfp_impl_json_get_integer(object, "key", 42);
|
||||
ASSERT_EQ(42, value);
|
||||
|
||||
json_decref(object);
|
||||
@@ -40,7 +40,7 @@ TEST(jsonrpc_util, failed_to_get_invalid_value_type)
|
||||
{
|
||||
json_t * object = json_object();
|
||||
json_object_set_new(object, "key", json_string("42"));
|
||||
int value = wfp_impl_json_get_int(object, "key", 42);
|
||||
int value = wfp_impl_json_get_integer(object, "key", 42);
|
||||
ASSERT_EQ(42, value);
|
||||
|
||||
json_decref(object);
|
||||
|
||||
Reference in New Issue
Block a user