added test of json node

pull/87/head
Falk Werner 4 years ago
parent 679d7763dd
commit 9fa6241b6b

@ -37,7 +37,7 @@ wf_impl_json_doc_dispose(
}
struct wf_json const *
wf_impl_jsoc_doc_root(
wf_impl_json_doc_root(
struct wf_json_doc * doc)
{
return &doc->root;

@ -25,7 +25,7 @@ wf_impl_json_doc_dispose(
struct wf_json_doc * doc);
extern struct wf_json const *
wf_impl_jsoc_doc_root(
wf_impl_json_doc_root(
struct wf_json_doc * doc);

@ -129,6 +129,7 @@ wf_impl_json_cleanup(
struct wf_json * actual = &(json->value.a.items[i]);
wf_impl_json_cleanup(actual);
}
free(json->value.a.items);
}
break;
case WF_JSON_TYPE_OBJECT:
@ -139,6 +140,7 @@ wf_impl_json_cleanup(
struct wf_json * actual = &(json->value.o.items[i].json);
wf_impl_json_cleanup(actual);
}
free(json->value.o.items);
}
break;
default:

@ -210,7 +210,7 @@ wf_impl_json_parse_array(
}
} while ((result) && (',' == c));
if ((result) && (']' == c))
if ((result) && (']' != c))
{
result = false;
}

@ -121,7 +121,7 @@ wf_impl_json_reader_read_string(
{
wf_impl_json_reader_skip_whitespace(reader);
char c = wf_impl_json_reader_get_char(reader);
if ('\"' == c) { return value; }
if ('\"' != c) { return false; }
size_t p = reader->pos;
*value = &(reader->contents[p]);

@ -123,6 +123,7 @@ test_certs_dep = declare_dependency(
alltests = executable('alltests',
'test/webfuse/json/test_writer.cc',
'test/webfuse/json/test_doc.cc',
'test/webfuse/json/test_node.cc',
'test/webfuse/jsonrpc/mock_timer_callback.cc',
'test/webfuse/jsonrpc/mock_timer.cc',
'test/webfuse/jsonrpc/test_is_request.cc',
@ -144,6 +145,7 @@ alltests = executable('alltests',
'test/webfuse/test_util/file.cc',
'test/webfuse/test_util/jansson_test_environment.cc',
'test/webfuse/test_util/lws_test_environment.cc',
'test/webfuse/test_util/json_doc.cc',
'test/webfuse/mocks/mock_authenticator.cc',
'test/webfuse/mocks/mock_fuse.cc',
'test/webfuse/mocks/mock_operation_context.cc',

@ -8,7 +8,7 @@ TEST(json_doc, loadb)
wf_json_doc * doc = wf_impl_json_doc_loadb(text, 4);
ASSERT_NE(nullptr, doc);
wf_json const * root = wf_impl_jsoc_doc_root(doc);
wf_json const * root = wf_impl_json_doc_root(doc);
ASSERT_EQ(WF_JSON_TYPE_BOOL, wf_impl_json_type(root));
ASSERT_TRUE(wf_impl_json_bool_get(root));

@ -0,0 +1,82 @@
#include "webfuse/impl/json/node.h"
#include "webfuse/test_util/json_doc.hpp"
#include <gtest/gtest.h>
using webfuse_test::JsonDoc;
TEST(json_node, null)
{
JsonDoc doc("null");
ASSERT_EQ(WF_JSON_TYPE_NULL, wf_impl_json_type(doc.root()));
}
TEST(json_node, true)
{
JsonDoc doc("true");
ASSERT_EQ(WF_JSON_TYPE_BOOL, wf_impl_json_type(doc.root()));
ASSERT_TRUE(wf_impl_json_bool_get(doc.root()));
}
TEST(json_node, false)
{
JsonDoc doc("false");
ASSERT_EQ(WF_JSON_TYPE_BOOL, wf_impl_json_type(doc.root()));
ASSERT_FALSE(wf_impl_json_bool_get(doc.root()));
}
TEST(json_node, int)
{
JsonDoc doc("42");
ASSERT_EQ(WF_JSON_TYPE_INT, wf_impl_json_type(doc.root()));
ASSERT_EQ(42, wf_impl_json_int_get(doc.root()));
}
TEST(json_node, string)
{
JsonDoc doc("\"brummni\"");
ASSERT_EQ(WF_JSON_TYPE_STRING, wf_impl_json_type(doc.root()));
ASSERT_STREQ("brummni", wf_impl_json_string_get(doc.root()));
}
TEST(json_node, array)
{
JsonDoc doc("[1,2,3]");
ASSERT_EQ(WF_JSON_TYPE_ARRAY, wf_impl_json_type(doc.root()));
ASSERT_EQ(3, wf_impl_json_array_size(doc.root()));
ASSERT_EQ(WF_JSON_TYPE_INT, wf_impl_json_type(wf_impl_json_array_get(doc.root(), 0)));
ASSERT_EQ(WF_JSON_TYPE_UNDEFINED, wf_impl_json_type(wf_impl_json_array_get(doc.root(), 4)));
}
TEST(json_node, object)
{
JsonDoc doc("{\"answer\": 42}");
ASSERT_EQ(WF_JSON_TYPE_OBJECT, wf_impl_json_type(doc.root()));
ASSERT_EQ(1, wf_impl_json_object_size(doc.root()));
ASSERT_EQ(WF_JSON_TYPE_INT, wf_impl_json_type(wf_impl_json_object_get(doc.root(), "answer")));
ASSERT_STREQ("answer", wf_impl_json_object_key(doc.root(), 0));
ASSERT_EQ(WF_JSON_TYPE_INT, wf_impl_json_type(wf_impl_json_object_value(doc.root(), 0)));
ASSERT_EQ(WF_JSON_TYPE_UNDEFINED, wf_impl_json_type(wf_impl_json_object_get(doc.root(), "unknown")));
ASSERT_STREQ("", wf_impl_json_object_key(doc.root(), 1));
ASSERT_EQ(WF_JSON_TYPE_UNDEFINED, wf_impl_json_type(wf_impl_json_object_value(doc.root(), 1)));
}
TEST(json_node, default_values)
{
JsonDoc doc("null");
ASSERT_EQ (false, wf_impl_json_bool_get(doc.root()));
ASSERT_EQ (0 , wf_impl_json_int_get(doc.root()));
ASSERT_STREQ("" , wf_impl_json_string_get(doc.root()));
ASSERT_EQ (0 , wf_impl_json_array_size(doc.root()));
ASSERT_EQ (WF_JSON_TYPE_UNDEFINED, wf_impl_json_type(wf_impl_json_array_get(doc.root(), 0)));
ASSERT_EQ (0 , wf_impl_json_object_size(doc.root()));
ASSERT_STREQ("" , wf_impl_json_object_key(doc.root(), 0));
ASSERT_EQ (WF_JSON_TYPE_UNDEFINED, wf_impl_json_type(wf_impl_json_object_get(doc.root(), 0)));
ASSERT_EQ (WF_JSON_TYPE_UNDEFINED, wf_impl_json_type(wf_impl_json_object_value(doc.root(), 0)));
}

@ -0,0 +1,22 @@
#include "webfuse/test_util/json_doc.hpp"
namespace webfuse_test
{
JsonDoc::JsonDoc(std::string const & text)
: contents(text)
{
doc = wf_impl_json_doc_loadb(const_cast<char*>(contents.data()), contents.size());
}
JsonDoc::~JsonDoc()
{
wf_impl_json_doc_dispose(doc);
}
wf_json const * JsonDoc::root()
{
return wf_impl_json_doc_root(doc);
}
}

@ -0,0 +1,23 @@
#ifndef WF_TEST_UTIL_JSON_DOC_HPP
#define WF_TEST_UTIL_JSON_DOC_HPP
#include "webfuse/impl/json/doc.h"
#include <string>
namespace webfuse_test
{
class JsonDoc
{
public:
JsonDoc(std::string const & text);
~JsonDoc();
wf_json const * root();
private:
std::string contents;
wf_json_doc * doc;
};
}
#endif
Loading…
Cancel
Save