test_callable.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************/
  2. /* test_callable.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. #ifndef TEST_CALLABLE_H
  31. #define TEST_CALLABLE_H
  32. #include "core/object/class_db.h"
  33. #include "core/object/object.h"
  34. #include "tests/test_macros.h"
  35. namespace TestCallable {
  36. class TestClass : public Object {
  37. GDCLASS(TestClass, Object);
  38. protected:
  39. static void _bind_methods() {
  40. ClassDB::bind_method(D_METHOD("test_func_1", "foo", "bar"), &TestClass::test_func_1);
  41. ClassDB::bind_method(D_METHOD("test_func_2", "foo", "bar", "baz"), &TestClass::test_func_2);
  42. ClassDB::bind_static_method("TestClass", D_METHOD("test_func_5", "foo", "bar"), &TestClass::test_func_5);
  43. ClassDB::bind_static_method("TestClass", D_METHOD("test_func_6", "foo", "bar", "baz"), &TestClass::test_func_6);
  44. {
  45. MethodInfo mi;
  46. mi.name = "test_func_7";
  47. mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));
  48. mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));
  49. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_7", &TestClass::test_func_7, mi, varray(), false);
  50. }
  51. {
  52. MethodInfo mi;
  53. mi.name = "test_func_8";
  54. mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));
  55. mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));
  56. mi.arguments.push_back(PropertyInfo(Variant::INT, "baz"));
  57. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_8", &TestClass::test_func_8, mi, varray(), false);
  58. }
  59. }
  60. public:
  61. void test_func_1(int p_foo, int p_bar) {}
  62. void test_func_2(int p_foo, int p_bar, int p_baz) {}
  63. int test_func_3(int p_foo, int p_bar) const { return 0; }
  64. int test_func_4(int p_foo, int p_bar, int p_baz) const { return 0; }
  65. static void test_func_5(int p_foo, int p_bar) {}
  66. static void test_func_6(int p_foo, int p_bar, int p_baz) {}
  67. void test_func_7(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}
  68. void test_func_8(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}
  69. };
  70. TEST_CASE("[Callable] Argument count") {
  71. TestClass *my_test = memnew(TestClass);
  72. // Bound methods tests.
  73. // Test simple methods.
  74. Callable callable_1 = Callable(my_test, "test_func_1");
  75. CHECK_EQ(callable_1.get_argument_count(), 2);
  76. Callable callable_2 = Callable(my_test, "test_func_2");
  77. CHECK_EQ(callable_2.get_argument_count(), 3);
  78. Callable callable_3 = Callable(my_test, "test_func_5");
  79. CHECK_EQ(callable_3.get_argument_count(), 2);
  80. Callable callable_4 = Callable(my_test, "test_func_6");
  81. CHECK_EQ(callable_4.get_argument_count(), 3);
  82. // Test vararg methods.
  83. Callable callable_vararg_1 = Callable(my_test, "test_func_7");
  84. CHECK_MESSAGE(callable_vararg_1.get_argument_count() == 2, "vararg Callable should return the number of declared arguments");
  85. Callable callable_vararg_2 = Callable(my_test, "test_func_8");
  86. CHECK_MESSAGE(callable_vararg_2.get_argument_count() == 3, "vararg Callable should return the number of declared arguments");
  87. // Callable MP tests.
  88. // Test simple methods.
  89. Callable callable_mp_1 = callable_mp(my_test, &TestClass::test_func_1);
  90. CHECK_EQ(callable_mp_1.get_argument_count(), 2);
  91. Callable callable_mp_2 = callable_mp(my_test, &TestClass::test_func_2);
  92. CHECK_EQ(callable_mp_2.get_argument_count(), 3);
  93. Callable callable_mp_3 = callable_mp(my_test, &TestClass::test_func_3);
  94. CHECK_EQ(callable_mp_3.get_argument_count(), 2);
  95. Callable callable_mp_4 = callable_mp(my_test, &TestClass::test_func_4);
  96. CHECK_EQ(callable_mp_4.get_argument_count(), 3);
  97. // Test static methods.
  98. Callable callable_mp_static_1 = callable_mp_static(&TestClass::test_func_5);
  99. CHECK_EQ(callable_mp_static_1.get_argument_count(), 2);
  100. Callable callable_mp_static_2 = callable_mp_static(&TestClass::test_func_6);
  101. CHECK_EQ(callable_mp_static_2.get_argument_count(), 3);
  102. // Test bind.
  103. Callable callable_mp_bind_1 = callable_mp_2.bind(1);
  104. CHECK_MESSAGE(callable_mp_bind_1.get_argument_count() == 2, "bind should subtract from the argument count");
  105. Callable callable_mp_bind_2 = callable_mp_2.bind(1, 2);
  106. CHECK_MESSAGE(callable_mp_bind_2.get_argument_count() == 1, "bind should subtract from the argument count");
  107. // Test unbind.
  108. Callable callable_mp_unbind_1 = callable_mp_2.unbind(1);
  109. CHECK_MESSAGE(callable_mp_unbind_1.get_argument_count() == 4, "unbind should add to the argument count");
  110. Callable callable_mp_unbind_2 = callable_mp_2.unbind(2);
  111. CHECK_MESSAGE(callable_mp_unbind_2.get_argument_count() == 5, "unbind should add to the argument count");
  112. memdelete(my_test);
  113. }
  114. class TestBoundUnboundArgumentCount : public Object {
  115. GDCLASS(TestBoundUnboundArgumentCount, Object);
  116. protected:
  117. static void _bind_methods() {
  118. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func", &TestBoundUnboundArgumentCount::test_func, MethodInfo("test_func"));
  119. }
  120. public:
  121. Variant test_func(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  122. Array result;
  123. result.resize(p_argcount);
  124. for (int i = 0; i < p_argcount; i++) {
  125. result[i] = *p_args[i];
  126. }
  127. return result;
  128. }
  129. static String get_output(const Callable &p_callable) {
  130. Array effective_args;
  131. effective_args.push_back(7);
  132. effective_args.push_back(8);
  133. effective_args.push_back(9);
  134. effective_args.resize(3 - p_callable.get_unbound_arguments_count());
  135. effective_args.append_array(p_callable.get_bound_arguments());
  136. return vformat(
  137. "%d %d %s %s %s",
  138. p_callable.get_unbound_arguments_count(),
  139. p_callable.get_bound_arguments_count(),
  140. p_callable.get_bound_arguments(),
  141. p_callable.call(7, 8, 9),
  142. effective_args);
  143. }
  144. };
  145. TEST_CASE("[Callable] Bound and unbound argument count") {
  146. String (*get_output)(const Callable &) = TestBoundUnboundArgumentCount::get_output;
  147. TestBoundUnboundArgumentCount *test_instance = memnew(TestBoundUnboundArgumentCount);
  148. Callable test_func = Callable(test_instance, "test_func");
  149. CHECK(get_output(test_func) == "0 0 [] [7, 8, 9] [7, 8, 9]");
  150. CHECK(get_output(test_func.bind(1, 2)) == "0 2 [1, 2] [7, 8, 9, 1, 2] [7, 8, 9, 1, 2]");
  151. CHECK(get_output(test_func.bind(1, 2).unbind(1)) == "1 2 [1, 2] [7, 8, 1, 2] [7, 8, 1, 2]");
  152. CHECK(get_output(test_func.bind(1, 2).unbind(1).bind(3, 4)) == "0 3 [3, 1, 2] [7, 8, 9, 3, 1, 2] [7, 8, 9, 3, 1, 2]");
  153. CHECK(get_output(test_func.bind(1, 2).unbind(1).bind(3, 4).unbind(1)) == "1 3 [3, 1, 2] [7, 8, 3, 1, 2] [7, 8, 3, 1, 2]");
  154. CHECK(get_output(test_func.bind(1).bind(2).bind(3).unbind(1)) == "1 3 [3, 2, 1] [7, 8, 3, 2, 1] [7, 8, 3, 2, 1]");
  155. CHECK(get_output(test_func.bind(1).bind(2).unbind(1).bind(3)) == "0 2 [2, 1] [7, 8, 9, 2, 1] [7, 8, 9, 2, 1]");
  156. CHECK(get_output(test_func.bind(1).unbind(1).bind(2).bind(3)) == "0 2 [3, 1] [7, 8, 9, 3, 1] [7, 8, 9, 3, 1]");
  157. CHECK(get_output(test_func.unbind(1).bind(1).bind(2).bind(3)) == "0 2 [3, 2] [7, 8, 9, 3, 2] [7, 8, 9, 3, 2]");
  158. CHECK(get_output(test_func.unbind(1).unbind(1).unbind(1).bind(1, 2, 3)) == "0 0 [] [7, 8, 9] [7, 8, 9]");
  159. CHECK(get_output(test_func.unbind(1).unbind(1).bind(1, 2, 3).unbind(1)) == "1 1 [1] [7, 8, 1] [7, 8, 1]");
  160. CHECK(get_output(test_func.unbind(1).bind(1, 2, 3).unbind(1).unbind(1)) == "2 2 [1, 2] [7, 1, 2] [7, 1, 2]");
  161. CHECK(get_output(test_func.bind(1, 2, 3).unbind(1).unbind(1).unbind(1)) == "3 3 [1, 2, 3] [1, 2, 3] [1, 2, 3]");
  162. memdelete(test_instance);
  163. }
  164. } // namespace TestCallable
  165. #endif // TEST_CALLABLE_H