Skip to content

Commit

Permalink
Object cases in modules (for ease of review)
Browse files Browse the repository at this point in the history
  • Loading branch information
AThousandShips committed Jan 24, 2025
1 parent b737434 commit cdb16c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
23 changes: 14 additions & 9 deletions modules/jsonrpc/tests/test_jsonrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ void check_invalid(const Dictionary &p_dict) {
}

void check_invalid_string(const String &p_str) {
JSON json;
REQUIRE(json.parse(p_str) == OK);
const Dictionary &dict = json.get_data();
Ref<JSON> json;
json.instantiate();

REQUIRE(json->parse(p_str) == OK);
const Dictionary &dict = json->get_data();
check_invalid(dict);
}

Expand All @@ -62,25 +64,28 @@ void TestClassJSONRPC::_bind_methods() {
}

void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements) {
TestClassJSONRPC json_rpc = TestClassJSONRPC();
const Variant &observed = json_rpc.process_action(p_in, p_process_array_elements);
TestClassJSONRPC *json_rpc = memnew(TestClassJSONRPC);
const Variant &observed = json_rpc->process_action(p_in, p_process_array_elements);
CHECK(observed == p_expected);
memdelete(json_rpc);
}

void test_process_string(const String &p_in, const String &p_expected) {
TestClassJSONRPC json_rpc = TestClassJSONRPC();
const String &out_str = json_rpc.process_string(p_in);
TestClassJSONRPC *json_rpc = memnew(TestClassJSONRPC);
const String &out_str = json_rpc->process_string(p_in);
CHECK(out_str == p_expected);
memdelete(json_rpc);
}

void check_error_no_method(const Dictionary &p_dict) {
check_error_code(p_dict, JSONRPC::METHOD_NOT_FOUND);
}

void test_process_action_bad_method(const Dictionary &p_in) {
TestClassJSONRPC json_rpc = TestClassJSONRPC();
const Dictionary &out_dict = json_rpc.process_action(p_in);
TestClassJSONRPC *json_rpc = memnew(TestClassJSONRPC);
const Dictionary &out_dict = json_rpc->process_action(p_in);
check_error_no_method(out_dict);
memdelete(json_rpc);
}

} // namespace TestJSONRPC
24 changes: 14 additions & 10 deletions modules/jsonrpc/tests/test_jsonrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,27 @@ namespace TestJSONRPC {
void check_invalid(const Dictionary &p_dict);

TEST_CASE("[JSONRPC] process_action invalid") {
JSONRPC json_rpc = JSONRPC();
JSONRPC *json_rpc = memnew(JSONRPC);

check_invalid(json_rpc.process_action("String is invalid"));
check_invalid(json_rpc.process_action(1234));
check_invalid(json_rpc.process_action(false));
check_invalid(json_rpc.process_action(3.14159));
check_invalid(json_rpc->process_action("String is invalid"));
check_invalid(json_rpc->process_action(1234));
check_invalid(json_rpc->process_action(false));
check_invalid(json_rpc->process_action(3.14159));

memdelete(json_rpc);
}

void check_invalid_string(const String &p_str);

TEST_CASE("[JSONRPC] process_string invalid") {
JSONRPC json_rpc = JSONRPC();
JSONRPC *json_rpc = memnew(JSONRPC);

check_invalid_string(json_rpc->process_string("\"String is invalid\""));
check_invalid_string(json_rpc->process_string("1234"));
check_invalid_string(json_rpc->process_string("false"));
check_invalid_string(json_rpc->process_string("3.14159"));

check_invalid_string(json_rpc.process_string("\"String is invalid\""));
check_invalid_string(json_rpc.process_string("1234"));
check_invalid_string(json_rpc.process_string("false"));
check_invalid_string(json_rpc.process_string("3.14159"));
memdelete(json_rpc);
}

class TestClassJSONRPC : public JSONRPC {
Expand Down

0 comments on commit cdb16c5

Please sign in to comment.