test_method_bind.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**************************************************************************/
  2. /* test_method_bind.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_METHOD_BIND_H
  31. #define TEST_METHOD_BIND_H
  32. #include "core/object/class_db.h"
  33. #include "tests/test_macros.h"
  34. namespace TestMethodBind {
  35. class MethodBindTester : public Object {
  36. GDCLASS(MethodBindTester, Object);
  37. public:
  38. enum Test {
  39. TEST_METHOD,
  40. TEST_METHOD_ARGS,
  41. TEST_METHODC,
  42. TEST_METHODC_ARGS,
  43. TEST_METHODR,
  44. TEST_METHODR_ARGS,
  45. TEST_METHODRC,
  46. TEST_METHODRC_ARGS,
  47. TEST_METHOD_DEFARGS,
  48. TEST_METHOD_OBJECT_CAST,
  49. TEST_MAX
  50. };
  51. class ObjectSubclass : public Object {
  52. public:
  53. int value = 1;
  54. };
  55. int test_num = 0;
  56. bool test_valid[TEST_MAX];
  57. void test_method() {
  58. test_valid[TEST_METHOD] = true;
  59. }
  60. void test_method_args(int p_arg) {
  61. test_valid[TEST_METHOD_ARGS] = p_arg == test_num;
  62. }
  63. void test_methodc() {
  64. test_valid[TEST_METHODC] = true;
  65. }
  66. void test_methodc_args(int p_arg) {
  67. test_valid[TEST_METHODC_ARGS] = p_arg == test_num;
  68. }
  69. int test_methodr() {
  70. test_valid[TEST_METHODR] = true; //temporary
  71. return test_num;
  72. }
  73. int test_methodr_args(int p_arg) {
  74. test_valid[TEST_METHODR_ARGS] = true; //temporary
  75. return p_arg;
  76. }
  77. int test_methodrc() {
  78. test_valid[TEST_METHODRC] = true; //temporary
  79. return test_num;
  80. }
  81. int test_methodrc_args(int p_arg) {
  82. test_valid[TEST_METHODRC_ARGS] = true; //temporary
  83. return p_arg;
  84. }
  85. void test_method_default_args(int p_arg1, int p_arg2, int p_arg3, int p_arg4, int p_arg5) {
  86. test_valid[TEST_METHOD_DEFARGS] = p_arg1 == 1 && p_arg2 == 2 && p_arg3 == 3 && p_arg4 == 4 && p_arg5 == 5; //temporary
  87. }
  88. void test_method_object_cast(ObjectSubclass *p_object) {
  89. test_valid[TEST_METHOD_OBJECT_CAST] = p_object->value == 1;
  90. }
  91. static void _bind_methods() {
  92. ClassDB::bind_method(D_METHOD("test_method"), &MethodBindTester::test_method);
  93. ClassDB::bind_method(D_METHOD("test_method_args"), &MethodBindTester::test_method_args);
  94. ClassDB::bind_method(D_METHOD("test_methodc"), &MethodBindTester::test_methodc);
  95. ClassDB::bind_method(D_METHOD("test_methodc_args"), &MethodBindTester::test_methodc_args);
  96. ClassDB::bind_method(D_METHOD("test_methodr"), &MethodBindTester::test_methodr);
  97. ClassDB::bind_method(D_METHOD("test_methodr_args"), &MethodBindTester::test_methodr_args);
  98. ClassDB::bind_method(D_METHOD("test_methodrc"), &MethodBindTester::test_methodrc);
  99. ClassDB::bind_method(D_METHOD("test_methodrc_args"), &MethodBindTester::test_methodrc_args);
  100. ClassDB::bind_method(D_METHOD("test_method_default_args"), &MethodBindTester::test_method_default_args, DEFVAL(9) /* wrong on purpose */, DEFVAL(4), DEFVAL(5));
  101. ClassDB::bind_method(D_METHOD("test_method_object_cast", "object"), &MethodBindTester::test_method_object_cast);
  102. }
  103. virtual void run_tests() {
  104. for (int i = 0; i < TEST_MAX; i++) {
  105. test_valid[i] = false;
  106. }
  107. //regular
  108. test_num = Math::rand();
  109. call("test_method");
  110. test_num = Math::rand();
  111. call("test_method_args", test_num);
  112. test_num = Math::rand();
  113. call("test_methodc");
  114. test_num = Math::rand();
  115. call("test_methodc_args", test_num);
  116. //return
  117. test_num = Math::rand();
  118. test_valid[TEST_METHODR] = int(call("test_methodr")) == test_num && test_valid[TEST_METHODR];
  119. test_num = Math::rand();
  120. test_valid[TEST_METHODR_ARGS] = int(call("test_methodr_args", test_num)) == test_num && test_valid[TEST_METHODR_ARGS];
  121. test_num = Math::rand();
  122. test_valid[TEST_METHODRC] = int(call("test_methodrc")) == test_num && test_valid[TEST_METHODRC];
  123. test_num = Math::rand();
  124. test_valid[TEST_METHODRC_ARGS] = int(call("test_methodrc_args", test_num)) == test_num && test_valid[TEST_METHODRC_ARGS];
  125. call("test_method_default_args", 1, 2, 3, 4);
  126. ObjectSubclass *obj = memnew(ObjectSubclass);
  127. call("test_method_object_cast", obj);
  128. memdelete(obj);
  129. }
  130. };
  131. TEST_CASE("[MethodBind] check all method binds") {
  132. MethodBindTester *mbt = memnew(MethodBindTester);
  133. mbt->run_tests();
  134. CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD]);
  135. CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_ARGS]);
  136. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODC]);
  137. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODC_ARGS]);
  138. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODR]);
  139. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODR_ARGS]);
  140. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODRC]);
  141. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODRC_ARGS]);
  142. CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_DEFARGS]);
  143. CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_OBJECT_CAST]);
  144. memdelete(mbt);
  145. }
  146. } // namespace TestMethodBind
  147. #endif // TEST_METHOD_BIND_H