main.gd 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. extends "res://test_base.gd"
  2. var custom_signal_emitted = null
  3. class TestClass:
  4. func test(p_msg: String) -> String:
  5. return p_msg + " world"
  6. func _ready():
  7. var example: Example = $Example
  8. # Signal.
  9. example.emit_custom_signal("Button", 42)
  10. assert_equal(custom_signal_emitted, ["Button", 42])
  11. # To string.
  12. assert_equal(example.to_string(),'Example:[ GDExtension::Example <--> Instance ID:%s ]' % example.get_instance_id())
  13. # It appears there's a bug with instance ids :-(
  14. #assert_equal($Example/ExampleMin.to_string(), 'ExampleMin:[Wrapped:%s]' % $Example/ExampleMin.get_instance_id())
  15. # Call static methods.
  16. assert_equal(Example.test_static(9, 100), 109);
  17. # It's void and static, so all we know is that it didn't crash.
  18. Example.test_static2()
  19. # Property list.
  20. example.property_from_list = Vector3(100, 200, 300)
  21. assert_equal(example.property_from_list, Vector3(100, 200, 300))
  22. var prop_list = example.get_property_list()
  23. for prop_info in prop_list:
  24. if prop_info['name'] == 'mouse_filter':
  25. assert_equal(prop_info['usage'], PROPERTY_USAGE_NO_EDITOR)
  26. # Call simple methods.
  27. example.simple_func()
  28. assert_equal(custom_signal_emitted, ['simple_func', 3])
  29. example.simple_const_func()
  30. assert_equal(custom_signal_emitted, ['simple_const_func', 4])
  31. # Pass custom reference.
  32. assert_equal(example.custom_ref_func(null), -1)
  33. var ref1 = ExampleRef.new()
  34. ref1.id = 27
  35. assert_equal(example.custom_ref_func(ref1), 27)
  36. ref1.id += 1;
  37. assert_equal(example.custom_const_ref_func(ref1), 28)
  38. # Pass core reference.
  39. assert_equal(example.image_ref_func(null), "invalid")
  40. assert_equal(example.image_const_ref_func(null), "invalid")
  41. var image = Image.new()
  42. assert_equal(example.image_ref_func(image), "valid")
  43. assert_equal(example.image_const_ref_func(image), "valid")
  44. # Return values.
  45. assert_equal(example.return_something("some string"), "some string42")
  46. assert_equal(example.return_something_const(), get_viewport())
  47. var null_ref = example.return_empty_ref()
  48. assert_equal(null_ref, null)
  49. var ret_ref = example.return_extended_ref()
  50. assert_not_equal(ret_ref.get_instance_id(), 0)
  51. assert_equal(ret_ref.get_id(), 0)
  52. assert_equal(example.get_v4(), Vector4(1.2, 3.4, 5.6, 7.8))
  53. assert_equal(example.test_node_argument(example), example)
  54. # VarArg method calls.
  55. var var_ref = ExampleRef.new()
  56. assert_not_equal(example.extended_ref_checks(var_ref).get_instance_id(), var_ref.get_instance_id())
  57. assert_equal(example.varargs_func("some", "arguments", "to", "test"), 4)
  58. assert_equal(example.varargs_func_nv("some", "arguments", "to", "test"), 46)
  59. example.varargs_func_void("some", "arguments", "to", "test")
  60. assert_equal(custom_signal_emitted, ["varargs_func_void", 5])
  61. # Method calls with default values.
  62. assert_equal(example.def_args(), 300)
  63. assert_equal(example.def_args(50), 250)
  64. assert_equal(example.def_args(50, 100), 150)
  65. # Array and Dictionary
  66. assert_equal(example.test_array(), [1, 2])
  67. assert_equal(example.test_tarray(), [ Vector2(1, 2), Vector2(2, 3) ])
  68. assert_equal(example.test_dictionary(), {"hello": "world", "foo": "bar"})
  69. var array: Array[int] = [1, 2, 3]
  70. assert_equal(example.test_tarray_arg(array), 6)
  71. example.callable_bind()
  72. assert_equal(custom_signal_emitted, ["bound", 11])
  73. # String += operator
  74. assert_equal(example.test_string_ops(), "ABCĎE")
  75. # UtilityFunctions::str()
  76. assert_equal(example.test_str_utility(), "Hello, World! The answer is 42")
  77. # Test converting string to char* and doing comparison.
  78. assert_equal(example.test_string_is_fourty_two("blah"), false)
  79. assert_equal(example.test_string_is_fourty_two("fourty two"), true)
  80. # String::resize().
  81. assert_equal(example.test_string_resize("What"), "What!?")
  82. # mp_callable() with void method.
  83. var mp_callable: Callable = example.test_callable_mp()
  84. assert_equal(mp_callable.is_valid(), true)
  85. mp_callable.call(example, "void", 36)
  86. assert_equal(custom_signal_emitted, ["unbound_method1: Example - void", 36])
  87. # Check that it works with is_connected().
  88. assert_equal(example.renamed.is_connected(mp_callable), false)
  89. example.renamed.connect(mp_callable)
  90. assert_equal(example.renamed.is_connected(mp_callable), true)
  91. # Make sure a new object is still treated as equivalent.
  92. assert_equal(example.renamed.is_connected(example.test_callable_mp()), true)
  93. assert_equal(mp_callable.hash(), example.test_callable_mp().hash())
  94. example.renamed.disconnect(mp_callable)
  95. assert_equal(example.renamed.is_connected(mp_callable), false)
  96. # mp_callable() with return value.
  97. var mp_callable_ret: Callable = example.test_callable_mp_ret()
  98. assert_equal(mp_callable_ret.call(example, "test", 77), "unbound_method2: Example - test - 77")
  99. # mp_callable() with const method and return value.
  100. var mp_callable_retc: Callable = example.test_callable_mp_retc()
  101. assert_equal(mp_callable_retc.call(example, "const", 101), "unbound_method3: Example - const - 101")
  102. # mp_callable_static() with void method.
  103. var mp_callable_static: Callable = example.test_callable_mp_static()
  104. mp_callable_static.call(example, "static", 83)
  105. assert_equal(custom_signal_emitted, ["unbound_static_method1: Example - static", 83])
  106. # Check that it works with is_connected().
  107. assert_equal(example.renamed.is_connected(mp_callable_static), false)
  108. example.renamed.connect(mp_callable_static)
  109. assert_equal(example.renamed.is_connected(mp_callable_static), true)
  110. # Make sure a new object is still treated as equivalent.
  111. assert_equal(example.renamed.is_connected(example.test_callable_mp_static()), true)
  112. assert_equal(mp_callable_static.hash(), example.test_callable_mp_static().hash())
  113. example.renamed.disconnect(mp_callable_static)
  114. assert_equal(example.renamed.is_connected(mp_callable_static), false)
  115. # mp_callable_static() with return value.
  116. var mp_callable_static_ret: Callable = example.test_callable_mp_static_ret()
  117. assert_equal(mp_callable_static_ret.call(example, "static-ret", 84), "unbound_static_method2: Example - static-ret - 84")
  118. # CallableCustom.
  119. var custom_callable: Callable = example.test_custom_callable();
  120. assert_equal(custom_callable.is_custom(), true);
  121. assert_equal(custom_callable.is_valid(), true);
  122. assert_equal(custom_callable.call(), "Hi")
  123. assert_equal(custom_callable.hash(), 27);
  124. assert_equal(custom_callable.get_object(), null);
  125. assert_equal(custom_callable.get_method(), "");
  126. assert_equal(str(custom_callable), "<MyCallableCustom>");
  127. # PackedArray iterators
  128. assert_equal(example.test_vector_ops(), 105)
  129. # Properties.
  130. assert_equal(example.group_subgroup_custom_position, Vector2(0, 0))
  131. example.group_subgroup_custom_position = Vector2(50, 50)
  132. assert_equal(example.group_subgroup_custom_position, Vector2(50, 50))
  133. # Test Object::cast_to<>() and that correct wrappers are being used.
  134. var control = Control.new()
  135. var sprite = Sprite2D.new()
  136. var example_ref = ExampleRef.new()
  137. assert_equal(example.test_object_cast_to_node(control), true)
  138. assert_equal(example.test_object_cast_to_control(control), true)
  139. assert_equal(example.test_object_cast_to_example(control), false)
  140. assert_equal(example.test_object_cast_to_node(example), true)
  141. assert_equal(example.test_object_cast_to_control(example), true)
  142. assert_equal(example.test_object_cast_to_example(example), true)
  143. assert_equal(example.test_object_cast_to_node(sprite), true)
  144. assert_equal(example.test_object_cast_to_control(sprite), false)
  145. assert_equal(example.test_object_cast_to_example(sprite), false)
  146. assert_equal(example.test_object_cast_to_node(example_ref), false)
  147. assert_equal(example.test_object_cast_to_control(example_ref), false)
  148. assert_equal(example.test_object_cast_to_example(example_ref), false)
  149. control.queue_free()
  150. sprite.queue_free()
  151. # Test conversions to and from Variant.
  152. assert_equal(example.test_variant_vector2i_conversion(Vector2i(1, 1)), Vector2i(1, 1))
  153. assert_equal(example.test_variant_vector2i_conversion(Vector2(1.0, 1.0)), Vector2i(1, 1))
  154. assert_equal(example.test_variant_int_conversion(10), 10)
  155. assert_equal(example.test_variant_int_conversion(10.0), 10)
  156. assert_equal(example.test_variant_float_conversion(10.0), 10.0)
  157. assert_equal(example.test_variant_float_conversion(10), 10.0)
  158. # Test that ptrcalls from GDExtension to the engine are correctly encoding Object and RefCounted.
  159. var new_node = Node.new()
  160. example.test_add_child(new_node)
  161. assert_equal(new_node.get_parent(), example)
  162. var new_tileset = TileSet.new()
  163. var new_tilemap = TileMap.new()
  164. example.test_set_tileset(new_tilemap, new_tileset)
  165. assert_equal(new_tilemap.tile_set, new_tileset)
  166. new_tilemap.queue_free()
  167. # Test variant call.
  168. var test_obj = TestClass.new()
  169. assert_equal(example.test_variant_call(test_obj), "hello world")
  170. # Constants.
  171. assert_equal(Example.FIRST, 0)
  172. assert_equal(Example.ANSWER_TO_EVERYTHING, 42)
  173. assert_equal(Example.CONSTANT_WITHOUT_ENUM, 314)
  174. # BitFields.
  175. assert_equal(Example.FLAG_ONE, 1)
  176. assert_equal(Example.FLAG_TWO, 2)
  177. assert_equal(example.test_bitfield(0), 0)
  178. assert_equal(example.test_bitfield(Example.FLAG_ONE | Example.FLAG_TWO), 3)
  179. # Test variant iterator.
  180. assert_equal(example.test_variant_iterator([10, 20, 30]), [15, 25, 35])
  181. assert_equal(example.test_variant_iterator(null), "iter_init: not valid")
  182. # RPCs.
  183. assert_equal(example.return_last_rpc_arg(), 0)
  184. example.test_rpc(42)
  185. assert_equal(example.return_last_rpc_arg(), 42)
  186. example.test_send_rpc(100)
  187. assert_equal(example.return_last_rpc_arg(), 100)
  188. # Virtual method.
  189. var event = InputEventKey.new()
  190. event.key_label = KEY_H
  191. event.unicode = 72
  192. get_viewport().push_input(event)
  193. assert_equal(custom_signal_emitted, ["_input: H", 72])
  194. # Check NOTIFICATION_POST_INITIALIZED, both when created from GDScript and godot-cpp.
  195. var new_example_ref = ExampleRef.new()
  196. assert_equal(new_example_ref.was_post_initialized(), true)
  197. assert_equal(example.test_post_initialize(), true)
  198. exit_with_status()
  199. func _on_Example_custom_signal(signal_name, value):
  200. custom_signal_emitted = [signal_name, value]