|
| 1 | +/**************************************************************************/ |
| 2 | +/* test_jsonrpc.h */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +#ifndef TEST_JSONRPC_H |
| 32 | +#define TEST_JSONRPC_H |
| 33 | + |
| 34 | +#include "tests/test_macros.h" |
| 35 | +#include "tests/test_utils.h" |
| 36 | + |
| 37 | +#include "../jsonrpc.h" |
| 38 | + |
| 39 | +namespace TestJSONRPC { |
| 40 | + |
| 41 | +void check_invalid(const Dictionary &p_dict); |
| 42 | + |
| 43 | +TEST_CASE("[JSONRPC] process_action invalid") { |
| 44 | + JSONRPC json_rpc = JSONRPC(); |
| 45 | + |
| 46 | + check_invalid(json_rpc.process_action("String is invalid")); |
| 47 | + check_invalid(json_rpc.process_action(1234)); |
| 48 | + check_invalid(json_rpc.process_action(false)); |
| 49 | + check_invalid(json_rpc.process_action(3.14159)); |
| 50 | +} |
| 51 | + |
| 52 | +void check_invalid_string(const String &p_str); |
| 53 | + |
| 54 | +TEST_CASE("[JSONRPC] process_string invalid") { |
| 55 | + JSONRPC json_rpc = JSONRPC(); |
| 56 | + |
| 57 | + check_invalid_string(json_rpc.process_string("\"String is invalid\"")); |
| 58 | + check_invalid_string(json_rpc.process_string("1234")); |
| 59 | + check_invalid_string(json_rpc.process_string("false")); |
| 60 | + check_invalid_string(json_rpc.process_string("3.14159")); |
| 61 | +} |
| 62 | + |
| 63 | +class TestClassJSONRPC : public JSONRPC { |
| 64 | + GDCLASS(TestClassJSONRPC, JSONRPC) |
| 65 | + |
| 66 | +public: |
| 67 | + String something(const String &p_in); |
| 68 | + |
| 69 | +protected: |
| 70 | + static void _bind_methods(); |
| 71 | +}; |
| 72 | + |
| 73 | +void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements = false); |
| 74 | + |
| 75 | +TEST_CASE("[JSONRPC] process_action Dictionary") { |
| 76 | + ClassDB::register_class<TestClassJSONRPC>(); |
| 77 | + |
| 78 | + Dictionary in_dict = Dictionary(); |
| 79 | + in_dict["method"] = "something"; |
| 80 | + in_dict["id"] = "ID"; |
| 81 | + in_dict["params"] = "yes"; |
| 82 | + |
| 83 | + Dictionary expected_dict = Dictionary(); |
| 84 | + expected_dict["jsonrpc"] = "2.0"; |
| 85 | + expected_dict["id"] = "ID"; |
| 86 | + expected_dict["result"] = "yes, please"; |
| 87 | + |
| 88 | + test_process_action(in_dict, expected_dict); |
| 89 | +} |
| 90 | + |
| 91 | +TEST_CASE("[JSONRPC] process_action Array") { |
| 92 | + Array in; |
| 93 | + Dictionary in_1; |
| 94 | + in_1["method"] = "something"; |
| 95 | + in_1["id"] = 1; |
| 96 | + in_1["params"] = "more"; |
| 97 | + in.push_back(in_1); |
| 98 | + Dictionary in_2; |
| 99 | + in_2["method"] = "something"; |
| 100 | + in_2["id"] = 2; |
| 101 | + in_2["params"] = "yes"; |
| 102 | + in.push_back(in_2); |
| 103 | + |
| 104 | + Array expected; |
| 105 | + Dictionary expected_1; |
| 106 | + expected_1["jsonrpc"] = "2.0"; |
| 107 | + expected_1["id"] = 1; |
| 108 | + expected_1["result"] = "more, please"; |
| 109 | + expected.push_back(expected_1); |
| 110 | + Dictionary expected_2; |
| 111 | + expected_2["jsonrpc"] = "2.0"; |
| 112 | + expected_2["id"] = 2; |
| 113 | + expected_2["result"] = "yes, please"; |
| 114 | + expected.push_back(expected_2); |
| 115 | + |
| 116 | + test_process_action(in, expected, true); |
| 117 | +} |
| 118 | + |
| 119 | +void test_process_string(const String &p_in, const String &p_expected); |
| 120 | + |
| 121 | +TEST_CASE("[JSONRPC] process_string Dictionary") { |
| 122 | + const String in = "{\"method\":\"something\",\"id\":\"ID\",\"params\":\"yes\"}"; |
| 123 | + const String expected = "{\"id\":\"ID\",\"jsonrpc\":\"2.0\",\"result\":\"yes, please\"}"; |
| 124 | + |
| 125 | + test_process_string(in, expected); |
| 126 | +} |
| 127 | + |
| 128 | +void test_process_action_bad_method(const Dictionary &p_in); |
| 129 | + |
| 130 | +TEST_CASE("[JSONRPC] process_action bad method") { |
| 131 | + Dictionary in_dict; |
| 132 | + in_dict["method"] = "nothing"; |
| 133 | + |
| 134 | + test_process_action_bad_method(in_dict); |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace TestJSONRPC |
| 138 | + |
| 139 | +#endif // TEST_JSONRPC_H |
0 commit comments