test_method_bind.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*************************************************************************/
  2. /* test_method_bind.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include <inttypes.h>
  35. #include <stdio.h>
  36. #include <wchar.h>
  37. namespace TestMethodBind {
  38. class MethodBindTester : public Object {
  39. GDCLASS(MethodBindTester, Object);
  40. public:
  41. enum Test {
  42. TEST_METHOD,
  43. TEST_METHOD_ARGS,
  44. TEST_METHODC,
  45. TEST_METHODC_ARGS,
  46. TEST_METHODR,
  47. TEST_METHODR_ARGS,
  48. TEST_METHODRC,
  49. TEST_METHODRC_ARGS,
  50. TEST_METHOD_DEFARGS,
  51. TEST_MAX
  52. };
  53. int test_num = 0;
  54. bool test_valid[TEST_MAX];
  55. void test_method() {
  56. test_valid[TEST_METHOD] = true;
  57. }
  58. void test_method_args(int p_arg) {
  59. test_valid[TEST_METHOD_ARGS] = p_arg == test_num;
  60. }
  61. void test_methodc() {
  62. test_valid[TEST_METHODC] = true;
  63. }
  64. void test_methodc_args(int p_arg) {
  65. test_valid[TEST_METHODC_ARGS] = p_arg == test_num;
  66. }
  67. int test_methodr() {
  68. test_valid[TEST_METHODR] = true; //temporary
  69. return test_num;
  70. }
  71. int test_methodr_args(int p_arg) {
  72. test_valid[TEST_METHODR_ARGS] = true; //temporary
  73. return p_arg;
  74. }
  75. int test_methodrc() {
  76. test_valid[TEST_METHODRC] = true; //temporary
  77. return test_num;
  78. }
  79. int test_methodrc_args(int p_arg) {
  80. test_valid[TEST_METHODRC_ARGS] = true; //temporary
  81. return p_arg;
  82. }
  83. void test_method_default_args(int p_arg1, int p_arg2, int p_arg3, int p_arg4, int p_arg5) {
  84. test_valid[TEST_METHOD_DEFARGS] = p_arg1 == 1 && p_arg2 == 2 && p_arg3 == 3 && p_arg4 == 4 && p_arg5 == 5; //temporary
  85. }
  86. static void _bind_methods() {
  87. ClassDB::bind_method(D_METHOD("test_method"), &MethodBindTester::test_method);
  88. ClassDB::bind_method(D_METHOD("test_method_args"), &MethodBindTester::test_method_args);
  89. ClassDB::bind_method(D_METHOD("test_methodc"), &MethodBindTester::test_methodc);
  90. ClassDB::bind_method(D_METHOD("test_methodc_args"), &MethodBindTester::test_methodc_args);
  91. ClassDB::bind_method(D_METHOD("test_methodr"), &MethodBindTester::test_methodr);
  92. ClassDB::bind_method(D_METHOD("test_methodr_args"), &MethodBindTester::test_methodr_args);
  93. ClassDB::bind_method(D_METHOD("test_methodrc"), &MethodBindTester::test_methodrc);
  94. ClassDB::bind_method(D_METHOD("test_methodrc_args"), &MethodBindTester::test_methodrc_args);
  95. ClassDB::bind_method(D_METHOD("test_method_default_args"), &MethodBindTester::test_method_default_args, DEFVAL(9) /* wrong on purpose */, DEFVAL(4), DEFVAL(5));
  96. }
  97. virtual void run_tests() {
  98. for (int i = 0; i < TEST_MAX; i++) {
  99. test_valid[i] = false;
  100. }
  101. //regular
  102. test_num = Math::rand();
  103. call("test_method");
  104. test_num = Math::rand();
  105. call("test_method_args", test_num);
  106. test_num = Math::rand();
  107. call("test_methodc");
  108. test_num = Math::rand();
  109. call("test_methodc_args", test_num);
  110. //return
  111. test_num = Math::rand();
  112. test_valid[TEST_METHODR] = int(call("test_methodr")) == test_num && test_valid[TEST_METHODR];
  113. test_num = Math::rand();
  114. test_valid[TEST_METHODR_ARGS] = int(call("test_methodr_args", test_num)) == test_num && test_valid[TEST_METHODR_ARGS];
  115. test_num = Math::rand();
  116. test_valid[TEST_METHODRC] = int(call("test_methodrc")) == test_num && test_valid[TEST_METHODRC];
  117. test_num = Math::rand();
  118. test_valid[TEST_METHODRC_ARGS] = int(call("test_methodrc_args", test_num)) == test_num && test_valid[TEST_METHODRC_ARGS];
  119. call("test_method_default_args", 1, 2, 3, 4);
  120. }
  121. };
  122. TEST_CASE("[MethodBind] check all method binds") {
  123. MethodBindTester *mbt = memnew(MethodBindTester);
  124. print_line("testing method bind");
  125. mbt->run_tests();
  126. CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD]);
  127. CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_ARGS]);
  128. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODC]);
  129. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODC_ARGS]);
  130. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODR]);
  131. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODR_ARGS]);
  132. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODRC]);
  133. CHECK(mbt->test_valid[MethodBindTester::TEST_METHODRC_ARGS]);
  134. CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_DEFARGS]);
  135. memdelete(mbt);
  136. }
  137. } // namespace TestMethodBind
  138. #endif // TEST_METHOD_BIND_H