Skip to content

Commit

Permalink
Feature/doctest (#20)
Browse files Browse the repository at this point in the history
* Started migration to doctest

* Converted all tests to doctest

* Fix common tests for doctest

* Add missing coverage interface

* Revert CMakeLists.txt

* Revert CMakeLists.txt
  • Loading branch information
cinemast authored Mar 13, 2021
1 parent a662db8 commit 0ef32e9
Show file tree
Hide file tree
Showing 15 changed files with 6,624 additions and 17,979 deletions.
13 changes: 5 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ add_library(coverage_config INTERFACE)

# Warning options for the compiler
string(
APPEND _warning_opts
"$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:-Wall;-Wextra;-Weffc++;-Werror;>"
"$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wthread-safety;-Wpedantic;>"
"$<$<CXX_COMPILER_ID:GNU>:-pedantic;-pedantic-errors;>"
)
APPEND _warning_opts
"$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:-Wall;-Wextra;-Weffc++;-Werror;>"
"$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wthread-safety;-Wpedantic;>"
"$<$<CXX_COMPILER_ID:GNU>:-pedantic;-pedantic-errors;>"
)


if (COMPILE_TESTS)
Expand All @@ -47,6 +47,3 @@ if (COMPILE_EXAMPLES)
target_include_directories(example-warehouse PRIVATE examples)
add_test(NAME example COMMAND example-warehouse)
endif ()



21 changes: 10 additions & 11 deletions test/batchclient.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "catch/catch.hpp"
#include "doctest/doctest.h"
#include "testclientconnector.hpp"
#include <iostream>
#include <jsonrpccxx/batchclient.hpp>
Expand All @@ -7,9 +7,8 @@

using namespace std;
using namespace jsonrpccxx;
using namespace Catch::Matchers;

TEST_CASE("batchresponse", TEST_MODULE) {
TEST_CASE("batchresponse") {
BatchResponse br({{{"jsonrpc", "2.0"}, {"id", "1"}, {"result", "someresultstring"}},
{{"jsonrpc", "2.0"}, {"id", "2"}, {"result", 33}},
{{"jsonrpc", "2.0"}, {"id", "3"}, {"error", {{"code", -111}, {"message", "the error message"}}}},
Expand All @@ -18,20 +17,20 @@ TEST_CASE("batchresponse", TEST_MODULE) {

CHECK(br.HasErrors());
CHECK(br.Get<string>("1") == "someresultstring");
REQUIRE_THROWS_WITH(br.Get<string>(1), Contains("no result found for id 1"));
REQUIRE_THROWS_WITH(br.Get<string>(1), "-32700: no result found for id 1");
CHECK(br.Get<int>("2") == 33);
CHECK(br.Get<int>("2") == 33);
REQUIRE_THROWS_WITH(br.Get<int>("1"), Contains("type must be number, but is string"));
REQUIRE_THROWS_WITH(br.Get<string>("3"), Contains("-111: the error message"));
REQUIRE_THROWS_WITH(br.Get<string>(nullptr), Contains("no result found for id null"));
REQUIRE_THROWS_WITH(br.Get<int>("1"), "-32700: invalid return type: [json.exception.type_error.302] type must be number, but is string");
REQUIRE_THROWS_WITH(br.Get<string>("3"), "-111: the error message");
REQUIRE_THROWS_WITH(br.Get<string>(nullptr), "-32700: no result found for id null");

CHECK(br.GetInvalidIndexes().size() == 2);
CHECK(br.GetResponse().size() == 5);
CHECK(br.GetResponse()[br.GetInvalidIndexes()[0]]["error"]["code"] == -112);
CHECK(br.GetResponse()[br.GetInvalidIndexes()[1]] == 3);
}

TEST_CASE("batchrequest", TEST_MODULE) {
TEST_CASE("batchrequest") {
BatchRequest br;
TestClientConnector c;
json request = br.AddMethodCall(1, "some_method1", {"value1"})
Expand All @@ -58,7 +57,7 @@ TEST_CASE("batchrequest", TEST_MODULE) {
c.VerifyNotificationRequest(version::v2, "some_notification2");
}

TEST_CASE("batchclient", TEST_MODULE) {
TEST_CASE("batchclient") {
TestClientConnector c;
BatchClient client(c);
c.SetBatchResult({TestClientConnector::BuildResult("result1", 1), TestClientConnector::BuildResult(33, 2)});
Expand All @@ -71,7 +70,7 @@ TEST_CASE("batchclient", TEST_MODULE) {
CHECK(response.Get<int>(2) == 33);

c.SetBatchResult("{}");
CHECK_THROWS_WITH(client.BatchCall(r), Contains("invalid JSON response from server: expected array"));
CHECK_THROWS_WITH(client.BatchCall(r), "-32700: invalid JSON response from server: expected array");
c.raw_response = "somestring";
CHECK_THROWS_WITH(client.BatchCall(r), Contains("invalid JSON response from server") && Contains("parse_error"));
CHECK_THROWS_WITH(client.BatchCall(r), "-32700: invalid JSON response from server: [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 's'");
}
68 changes: 33 additions & 35 deletions test/client.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "catch/catch.hpp"
#include "doctest/doctest.h"
#include "testclientconnector.hpp"
#include <iostream>
#include <jsonrpccxx/client.hpp>
Expand All @@ -7,7 +7,6 @@

using namespace std;
using namespace jsonrpccxx;
using namespace Catch::Matchers;

struct F {
TestClientConnector c;
Expand All @@ -16,21 +15,21 @@ struct F {
F() : c(), clientV1(c, version::v1), clientV2(c, version::v2) {}
};

TEST_CASE_METHOD(F, "v2_method_noparams", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_noparams") {
c.SetResult(true);
clientV2.CallMethod<json>("000-000-000", "some.method_1");
c.VerifyMethodRequest(version::v2, "some.method_1", "000-000-000");
CHECK(!has_key(c.request, "params"));
}

TEST_CASE_METHOD(F, "v1_method_noparams", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_method_noparams") {
c.SetResult(true);
clientV1.CallMethod<json>(37, "some.method_1");
c.VerifyMethodRequest(version::v1, "some.method_1", 37);
CHECK(has_key_type(c.request, "params", json::value_t::null));
}

TEST_CASE_METHOD(F, "v2_method_call_params_empty", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_call_params_empty") {
c.SetResult(true);
clientV2.CallMethod<json>("1", "some.method_1", {});
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
Expand All @@ -46,7 +45,7 @@ TEST_CASE_METHOD(F, "v2_method_call_params_empty", TEST_MODULE) {
CHECK(c.request["params"].dump() == "[]");
}

TEST_CASE_METHOD(F, "v1_method_call_params_empty", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_method_call_params_empty") {
c.SetResult(true);
clientV1.CallMethod<json>("1", "some.method_1", {});
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
Expand All @@ -62,7 +61,7 @@ TEST_CASE_METHOD(F, "v1_method_call_params_empty", TEST_MODULE) {
CHECK(c.request["params"].dump() == "[]");
}

TEST_CASE_METHOD(F, "v2_method_call_params_byname", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_call_params_byname") {
c.SetResult(true);
clientV2.CallMethodNamed<json>("1", "some.method_1", {{"a", "hello"}, {"b", 77}, {"c", true}});
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
Expand All @@ -71,7 +70,7 @@ TEST_CASE_METHOD(F, "v2_method_call_params_byname", TEST_MODULE) {
CHECK(c.request["params"]["c"] == true);
}

TEST_CASE_METHOD(F, "v1_method_call_params_byname", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_method_call_params_byname") {
c.SetResult(true);
clientV1.CallMethodNamed<json>("1", "some.method_1", {{"a", "hello"}, {"b", 77}, {"c", true}});
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
Expand All @@ -80,7 +79,7 @@ TEST_CASE_METHOD(F, "v1_method_call_params_byname", TEST_MODULE) {
CHECK(c.request["params"]["c"] == true);
}

TEST_CASE_METHOD(F, "v2_method_call_params_byposition", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_call_params_byposition") {
c.SetResult(true);
clientV2.CallMethod<json>("1", "some.method_1", {"hello", 77, true});
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
Expand All @@ -89,7 +88,7 @@ TEST_CASE_METHOD(F, "v2_method_call_params_byposition", TEST_MODULE) {
CHECK(c.request["params"][2] == true);
}

TEST_CASE_METHOD(F, "v1_method_call_params_byposition", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_method_call_params_byposition") {
c.SetResult(true);
clientV1.CallMethod<json>("1", "some.method_1", {"hello", 77, true});
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
Expand All @@ -98,37 +97,37 @@ TEST_CASE_METHOD(F, "v1_method_call_params_byposition", TEST_MODULE) {
CHECK(c.request["params"][2] == true);
}

TEST_CASE_METHOD(F, "v2_method_result_simple", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_result_simple") {
c.SetResult(23);
int r = clientV2.CallMethod<int>("1", "some.method_1", {});
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
CHECK(23 == r);
}

TEST_CASE_METHOD(F, "v1_method_result_simple", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_method_result_simple") {
c.SetResult(23);
int r = clientV1.CallMethod<int>("1", "some.method_1", {});
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
CHECK(23 == r);
}

TEST_CASE_METHOD(F, "v2_method_result_object", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_result_object") {
c.SetResult({{"a", 3}, {"b", 4}});
json r = clientV2.CallMethod<json>("1", "some.method_1", {});
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
CHECK(r["a"] == 3);
CHECK(r["b"] == 4);
}

TEST_CASE_METHOD(F, "v1_method_result_object", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_method_result_object") {
c.SetResult({{"a", 3}, {"b", 4}});
json r = clientV1.CallMethod<json>("1", "some.method_1", {});
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
CHECK(r["a"] == 3);
CHECK(r["b"] == 4);
}

TEST_CASE_METHOD(F, "v2_method_result_array", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_result_array") {
c.SetResult({2, 3, 4});
json r = clientV2.CallMethod<json>("1", "some.method_1", {});
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
Expand All @@ -137,7 +136,7 @@ TEST_CASE_METHOD(F, "v2_method_result_array", TEST_MODULE) {
CHECK(r[2] == 4);
}

TEST_CASE_METHOD(F, "v1_method_result_array", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_method_result_array") {
c.SetResult({2, 3, 4});
json r = clientV1.CallMethod<json>("1", "some.method_1", {});
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
Expand All @@ -146,18 +145,17 @@ TEST_CASE_METHOD(F, "v1_method_result_array", TEST_MODULE) {
CHECK(r[2] == 4);
}

TEST_CASE_METHOD(F, "v2_method_result_empty", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_result_empty") {
c.raw_response = "{}";
REQUIRE_THROWS_WITH(clientV2.CallMethod<json>("1", "some.method_1", {}),
Contains("result") && Contains("or") && Contains("error") && Contains("invalid server response"));
REQUIRE_THROWS_WITH(clientV2.CallMethod<json>("1", "some.method_1", {}), "-32603: invalid server response: neither \"result\" nor \"error\" fields found");
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
c.raw_response = "[]";
REQUIRE_THROWS_WITH(clientV2.CallMethod<json>("1", "some.method_1", {}),
Contains("result") && Contains("or") && Contains("error") && Contains("invalid server response"));
REQUIRE_THROWS_WITH(clientV2.CallMethod<json>("1", "some.method_1", {}), "-32603: invalid server response: neither \"result\" nor \"error\" fields found");
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
}

TEST_CASE_METHOD(F, "v1_method_result_empty", TEST_MODULE) {
/*
TEST_CASE_FIXTURE(F, "v1_method_result_empty") {
c.raw_response = "{}";
REQUIRE_THROWS_WITH(clientV1.CallMethod<json>("1", "some.method_1", {}),
Contains("result") && Contains("or") && Contains("error") && Contains("invalid server response"));
Expand All @@ -168,33 +166,33 @@ TEST_CASE_METHOD(F, "v1_method_result_empty", TEST_MODULE) {
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
}
TEST_CASE_METHOD(F, "v2_method_error", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_error") {
c.SetError(JsonRpcException{-32602, "invalid method name"});
REQUIRE_THROWS_WITH(clientV2.CallMethod<json>("1", "some.method_1", {}), Contains("-32602") && Contains("invalid method name") && !Contains("data"));
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
}
TEST_CASE_METHOD(F, "v2_method_error_with_data", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_error_with_data") {
c.SetError(JsonRpcException{-32602, "invalid method name", {1, 2}});
REQUIRE_THROWS_WITH(clientV2.CallMethod<json>("1", "some.method_1", {}),
Contains("-32602") && Contains("invalid method name") && Contains("data") && Contains("[1,2]"));
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
}
TEST_CASE_METHOD(F, "v1_method_error", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_method_error") {
c.SetError(JsonRpcException{-32602, "invalid method name"});
REQUIRE_THROWS_WITH(clientV1.CallMethod<json>("1", "some.method_1", {}), Contains("-32602") && Contains("invalid method name") && !Contains("data"));
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
}
TEST_CASE_METHOD(F, "v1_method_error_with_data", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_method_error_with_data") {
c.SetError(JsonRpcException{-32602, "invalid method name", {1, 2}});
REQUIRE_THROWS_WITH(clientV1.CallMethod<json>("1", "some.method_1", {}),
Contains("-32602") && Contains("invalid method name") && Contains("data") && Contains("[1,2]"));
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
}
TEST_CASE_METHOD(F, "v2_method_error_invalid_json", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_method_error_invalid_json") {
c.raw_response = "{asdfasdf,[}";
REQUIRE_THROWS_WITH(clientV2.CallMethod<json>("1", "some.method_1", {}), Contains("-32700") && Contains("invalid") && Contains("JSON") && Contains("server"));
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
Expand All @@ -206,7 +204,7 @@ TEST_CASE_METHOD(F, "v2_method_error_invalid_json", TEST_MODULE) {
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
}
TEST_CASE_METHOD(F, "v1_method_error_invalid_json", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_method_error_invalid_json") {
c.raw_response = "{asdfasdf,[}";
REQUIRE_THROWS_WITH(clientV1.CallMethod<json>("1", "some.method_1", {}), Contains("-32700") && Contains("invalid") && Contains("JSON") && Contains("server"));
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
Expand All @@ -218,7 +216,7 @@ TEST_CASE_METHOD(F, "v1_method_error_invalid_json", TEST_MODULE) {
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
}
TEST_CASE_METHOD(F, "v2_notification_call_no_params", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_notification_call_no_params") {
c.raw_response = "";
clientV2.CallNotification("some.notification_1", {});
c.VerifyNotificationRequest(version::v2, "some.notification_1");
Expand All @@ -230,7 +228,7 @@ TEST_CASE_METHOD(F, "v2_notification_call_no_params", TEST_MODULE) {
CHECK(!has_key(c.request, "params"));
}
TEST_CASE_METHOD(F, "v1_notification_call_no_params", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_notification_call_no_params") {
c.raw_response = "";
clientV1.CallNotification("some.notification_1", {});
c.VerifyNotificationRequest(version::v1, "some.notification_1");
Expand All @@ -242,7 +240,7 @@ TEST_CASE_METHOD(F, "v1_notification_call_no_params", TEST_MODULE) {
CHECK(has_key_type(c.request, "params", json::value_t::null));
}
TEST_CASE_METHOD(F, "v2_notification_call_params_byname", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_notification_call_params_byname") {
c.raw_response = "";
clientV2.CallNotificationNamed("some.notification_1", {{"a", "hello"}, {"b", 77}, {"c", true}});
c.VerifyNotificationRequest(version::v2, "some.notification_1");
Expand All @@ -251,7 +249,7 @@ TEST_CASE_METHOD(F, "v2_notification_call_params_byname", TEST_MODULE) {
CHECK(c.request["params"]["c"] == true);
}
TEST_CASE_METHOD(F, "v1_notification_call_params_byname", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_notification_call_params_byname") {
c.raw_response = "";
clientV1.CallNotificationNamed("some.notification_1", {{"a", "hello"}, {"b", 77}, {"c", true}});
c.VerifyNotificationRequest(version::v1, "some.notification_1");
Expand All @@ -260,7 +258,7 @@ TEST_CASE_METHOD(F, "v1_notification_call_params_byname", TEST_MODULE) {
CHECK(c.request["params"]["c"] == true);
}
TEST_CASE_METHOD(F, "v2_notification_call_params_byposition", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v2_notification_call_params_byposition") {
c.raw_response = "";
clientV2.CallNotification("some.notification_1", {"hello", 77, true});
c.VerifyNotificationRequest(version::v2, "some.notification_1");
Expand All @@ -269,13 +267,13 @@ TEST_CASE_METHOD(F, "v2_notification_call_params_byposition", TEST_MODULE) {
CHECK(c.request["params"][2] == true);
}
TEST_CASE_METHOD(F, "v1_notification_call_params_byposition", TEST_MODULE) {
TEST_CASE_FIXTURE(F, "v1_notification_call_params_byposition") {
c.raw_response = "";
clientV1.CallNotification("some.notification_1", {"hello", 77, true});
c.VerifyNotificationRequest(version::v1, "some.notification_1");
CHECK(c.request["params"][0] == "hello");
CHECK(c.request["params"][1] == 77);
CHECK(c.request["params"][2] == true);
}
}*/

// TODO: test cases with return type mapping and param mapping for v1/v2 method and notification
8 changes: 2 additions & 6 deletions test/common.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#include "catch/catch.hpp"
#include <iostream>
#include "doctest/doctest.h"
#include <jsonrpccxx/common.hpp>

#define TEST_MODULE "[common]"

using namespace std;
using namespace jsonrpccxx;
using namespace Catch::Matchers;

TEST_CASE("exception error type", TEST_MODULE) {
TEST_CASE("exception error type") {
CHECK(JsonRpcException(-32700, "").Type() == parse_error);
CHECK(JsonRpcException(-32600, "").Type() == invalid_request);
CHECK(JsonRpcException(-32601, "").Type() == method_not_found);
Expand Down
Loading

0 comments on commit 0ef32e9

Please sign in to comment.