test_jsonrpc.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**************************************************************************/
  2. /* test_jsonrpc.cpp */
  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. #include "test_jsonrpc.h"
  31. #include "core/io/json.h"
  32. namespace TestJSONRPC {
  33. void check_error_code(const Dictionary &p_dict, const JSONRPC::ErrorCode &p_code) {
  34. CHECK(p_dict["jsonrpc"] == "2.0");
  35. REQUIRE(p_dict.has("error"));
  36. const Dictionary &err_body = p_dict["error"];
  37. const int &code = err_body["code"];
  38. CHECK(code == p_code);
  39. }
  40. void check_invalid(const Dictionary &p_dict) {
  41. check_error_code(p_dict, JSONRPC::INVALID_REQUEST);
  42. }
  43. void check_invalid_string(const String &p_str) {
  44. JSON json;
  45. REQUIRE(json.parse(p_str) == OK);
  46. const Dictionary &dict = json.get_data();
  47. check_invalid(dict);
  48. }
  49. String TestClassJSONRPC::something(const String &p_in) {
  50. return p_in + ", please";
  51. }
  52. void TestClassJSONRPC::_bind_methods() {
  53. ClassDB::bind_method(D_METHOD("something", "in"), &TestClassJSONRPC::something);
  54. }
  55. void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements) {
  56. TestClassJSONRPC json_rpc = TestClassJSONRPC();
  57. const Variant &observed = json_rpc.process_action(p_in, p_process_array_elements);
  58. CHECK(observed == p_expected);
  59. }
  60. void test_process_string(const String &p_in, const String &p_expected) {
  61. TestClassJSONRPC json_rpc = TestClassJSONRPC();
  62. const String &out_str = json_rpc.process_string(p_in);
  63. CHECK(out_str == p_expected);
  64. }
  65. void check_error_no_method(const Dictionary &p_dict) {
  66. check_error_code(p_dict, JSONRPC::METHOD_NOT_FOUND);
  67. }
  68. void test_process_action_bad_method(const Dictionary &p_in) {
  69. TestClassJSONRPC json_rpc = TestClassJSONRPC();
  70. const Dictionary &out_dict = json_rpc.process_action(p_in);
  71. check_error_no_method(out_dict);
  72. }
  73. } // namespace TestJSONRPC