From b9ea7245c3758af5d4dd42684e480b94a10eed07 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sat, 20 Apr 2019 11:30:02 +0200 Subject: [PATCH] adds test implementation and some matchers --- test/json_matcher.hpp | 34 ++++++++++++++++++++++++++++++++++ test/mock_request.cc | 24 +++++++++++++++++++++--- test/mock_request.hpp | 25 +++++++++++++++++++++++++ test/test_static_filesystem.cc | 5 +++-- 4 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 test/json_matcher.hpp diff --git a/test/json_matcher.hpp b/test/json_matcher.hpp new file mode 100644 index 0000000..7822344 --- /dev/null +++ b/test/json_matcher.hpp @@ -0,0 +1,34 @@ +#ifndef WF_JSON_MATCHER_HPP +#define FW_JSON_MATCHER_HPP + +#include +#include + +namespace webfuse_test +{ + +MATCHER_P(JsonMatcher, expected_str, "") +{ + std::cout << "--- JsonMatcher ---" << std::endl; + bool matches = false; + json_t * expected = json_loads(expected_str, 0, nullptr); + if (nullptr != expected) + { + matches = (1 == json_equal(expected, arg)); + if (!matches) + { + char * actual = json_dumps(arg, 0); + std::cout << actual << std::endl; + *result_listener << "where arg is " << actual; + free(actual); + } + + json_decref(expected); + } + + return true; //matches; +} + +} + +#endif diff --git a/test/mock_request.cc b/test/mock_request.cc index 0792d59..efd8334 100644 --- a/test/mock_request.cc +++ b/test/mock_request.cc @@ -9,10 +9,26 @@ respond( json_t * response, void * user_data) { - (void) response; - webfuse_test::Request * request = reinterpret_cast(user_data); - request->respond_error(nullptr, 0); + + json_t * result = json_object_get(response, "result"); + json_t * error = json_object_get(response, "error"); + json_t * id_holder = json_object_get(response, "id"); + + int id = -1; + if (json_is_integer(id_holder)) + { + id = json_integer_value(id_holder); + } + + if (nullptr != result) + { + request->respond(result, id); + } + else + { + request->respond_error(error, id); + } } @@ -30,6 +46,8 @@ request_create( request->id = id; request->user_data = reinterpret_cast(req); request->respond = &respond; + + return request; } diff --git a/test/mock_request.hpp b/test/mock_request.hpp index e836304..4edacde 100644 --- a/test/mock_request.hpp +++ b/test/mock_request.hpp @@ -3,6 +3,7 @@ #include #include +#include #include "webfuse/provider/impl/request.h" @@ -29,6 +30,30 @@ request_create( Request * req, int id); +MATCHER_P3(GetAttrMatcher, inode, mode, file_type, "") +{ + json_t * inode_holder = json_object_get(arg, "inode"); + if ((!json_is_integer(inode_holder)) || (inode != json_integer_value(inode_holder))) + { + return false; + } + + json_t * mode_holder = json_object_get(arg, "mode"); + if ((!json_is_integer(mode_holder)) || (mode != json_integer_value(mode_holder))) + { + return false; + } + + json_t * type_holder = json_object_get(arg, "type"); + if ((!json_is_string(type_holder)) || (0 != strcmp(file_type, json_string_value(type_holder)))) + { + return false; + } + + return true; +} + + } diff --git a/test/test_static_filesystem.cc b/test/test_static_filesystem.cc index acea6a4..54af39d 100644 --- a/test/test_static_filesystem.cc +++ b/test/test_static_filesystem.cc @@ -8,6 +8,7 @@ using webfuse_test::request_create; using webfuse_test::MockRequest; +using webfuse_test::GetAttrMatcher; using testing::_; TEST(wfp_static_filesystem, init) @@ -18,7 +19,7 @@ TEST(wfp_static_filesystem, init) MockRequest mock; struct wfp_request * request = request_create(&mock, 42); - EXPECT_CALL(mock, respond_error(_,_)).Times(1); + EXPECT_CALL(mock, respond(GetAttrMatcher(1, 0555, "dir"), 42)).Times(1); config->provider.getattr(request, 1, config->user_data); @@ -26,4 +27,4 @@ TEST(wfp_static_filesystem, init) wfp_impl_static_filesystem_dispose(filesystem); wfp_client_config_dispose(config); -} \ No newline at end of file +}