main.gd 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. assert_equal(mp_callable.get_argument_count(), 3)
  86. mp_callable.call(example, "void", 36)
  87. assert_equal(custom_signal_emitted, ["unbound_method1: Example - void", 36])
  88. # Check that it works with is_connected().
  89. assert_equal(example.renamed.is_connected(mp_callable), false)
  90. example.renamed.connect(mp_callable)
  91. assert_equal(example.renamed.is_connected(mp_callable), true)
  92. # Make sure a new object is still treated as equivalent.
  93. assert_equal(example.renamed.is_connected(example.test_callable_mp()), true)
  94. assert_equal(mp_callable.hash(), example.test_callable_mp().hash())
  95. example.renamed.disconnect(mp_callable)
  96. assert_equal(example.renamed.is_connected(mp_callable), false)
  97. # mp_callable() with return value.
  98. var mp_callable_ret: Callable = example.test_callable_mp_ret()
  99. assert_equal(mp_callable_ret.get_argument_count(), 3)
  100. assert_equal(mp_callable_ret.call(example, "test", 77), "unbound_method2: Example - test - 77")
  101. # mp_callable() with const method and return value.
  102. var mp_callable_retc: Callable = example.test_callable_mp_retc()
  103. assert_equal(mp_callable_retc.get_argument_count(), 3)
  104. assert_equal(mp_callable_retc.call(example, "const", 101), "unbound_method3: Example - const - 101")
  105. # mp_callable_static() with void method.
  106. var mp_callable_static: Callable = example.test_callable_mp_static()
  107. assert_equal(mp_callable_static.get_argument_count(), 3)
  108. mp_callable_static.call(example, "static", 83)
  109. assert_equal(custom_signal_emitted, ["unbound_static_method1: Example - static", 83])
  110. # Check that it works with is_connected().
  111. assert_equal(example.renamed.is_connected(mp_callable_static), false)
  112. example.renamed.connect(mp_callable_static)
  113. assert_equal(example.renamed.is_connected(mp_callable_static), true)
  114. # Make sure a new object is still treated as equivalent.
  115. assert_equal(example.renamed.is_connected(example.test_callable_mp_static()), true)
  116. assert_equal(mp_callable_static.hash(), example.test_callable_mp_static().hash())
  117. example.renamed.disconnect(mp_callable_static)
  118. assert_equal(example.renamed.is_connected(mp_callable_static), false)
  119. # mp_callable_static() with return value.
  120. var mp_callable_static_ret: Callable = example.test_callable_mp_static_ret()
  121. assert_equal(mp_callable_static_ret.get_argument_count(), 3)
  122. assert_equal(mp_callable_static_ret.call(example, "static-ret", 84), "unbound_static_method2: Example - static-ret - 84")
  123. # CallableCustom.
  124. var custom_callable: Callable = example.test_custom_callable();
  125. assert_equal(custom_callable.is_custom(), true);
  126. assert_equal(custom_callable.is_valid(), true);
  127. assert_equal(custom_callable.call(), "Hi")
  128. assert_equal(custom_callable.hash(), 27);
  129. assert_equal(custom_callable.get_object(), null);
  130. assert_equal(custom_callable.get_method(), "");
  131. assert_equal(custom_callable.get_argument_count(), 2)
  132. assert_equal(str(custom_callable), "<MyCallableCustom>");
  133. # PackedArray iterators
  134. assert_equal(example.test_vector_ops(), 105)
  135. assert_equal(example.test_vector_init_list(), 105)
  136. # Properties.
  137. assert_equal(example.group_subgroup_custom_position, Vector2(0, 0))
  138. example.group_subgroup_custom_position = Vector2(50, 50)
  139. assert_equal(example.group_subgroup_custom_position, Vector2(50, 50))
  140. # Test Object::cast_to<>() and that correct wrappers are being used.
  141. var control = Control.new()
  142. var sprite = Sprite2D.new()
  143. var example_ref = ExampleRef.new()
  144. assert_equal(example.test_object_cast_to_node(control), true)
  145. assert_equal(example.test_object_cast_to_control(control), true)
  146. assert_equal(example.test_object_cast_to_example(control), false)
  147. assert_equal(example.test_object_cast_to_node(example), true)
  148. assert_equal(example.test_object_cast_to_control(example), true)
  149. assert_equal(example.test_object_cast_to_example(example), true)
  150. assert_equal(example.test_object_cast_to_node(sprite), true)
  151. assert_equal(example.test_object_cast_to_control(sprite), false)
  152. assert_equal(example.test_object_cast_to_example(sprite), false)
  153. assert_equal(example.test_object_cast_to_node(example_ref), false)
  154. assert_equal(example.test_object_cast_to_control(example_ref), false)
  155. assert_equal(example.test_object_cast_to_example(example_ref), false)
  156. control.queue_free()
  157. sprite.queue_free()
  158. # Test conversions to and from Variant.
  159. assert_equal(example.test_variant_vector2i_conversion(Vector2i(1, 1)), Vector2i(1, 1))
  160. assert_equal(example.test_variant_vector2i_conversion(Vector2(1.0, 1.0)), Vector2i(1, 1))
  161. assert_equal(example.test_variant_int_conversion(10), 10)
  162. assert_equal(example.test_variant_int_conversion(10.0), 10)
  163. assert_equal(example.test_variant_float_conversion(10.0), 10.0)
  164. assert_equal(example.test_variant_float_conversion(10), 10.0)
  165. # Test that ptrcalls from GDExtension to the engine are correctly encoding Object and RefCounted.
  166. var new_node = Node.new()
  167. example.test_add_child(new_node)
  168. assert_equal(new_node.get_parent(), example)
  169. var new_tileset = TileSet.new()
  170. var new_tilemap = TileMap.new()
  171. example.test_set_tileset(new_tilemap, new_tileset)
  172. assert_equal(new_tilemap.tile_set, new_tileset)
  173. new_tilemap.queue_free()
  174. # Test variant call.
  175. var test_obj = TestClass.new()
  176. assert_equal(example.test_variant_call(test_obj), "hello world")
  177. # Constants.
  178. assert_equal(Example.FIRST, 0)
  179. assert_equal(Example.ANSWER_TO_EVERYTHING, 42)
  180. assert_equal(Example.CONSTANT_WITHOUT_ENUM, 314)
  181. # BitFields.
  182. assert_equal(Example.FLAG_ONE, 1)
  183. assert_equal(Example.FLAG_TWO, 2)
  184. assert_equal(example.test_bitfield(0), 0)
  185. assert_equal(example.test_bitfield(Example.FLAG_ONE | Example.FLAG_TWO), 3)
  186. # Test variant iterator.
  187. assert_equal(example.test_variant_iterator([10, 20, 30]), [15, 25, 35])
  188. assert_equal(example.test_variant_iterator(null), "iter_init: not valid")
  189. # RPCs.
  190. assert_equal(example.return_last_rpc_arg(), 0)
  191. example.test_rpc(42)
  192. assert_equal(example.return_last_rpc_arg(), 42)
  193. example.test_send_rpc(100)
  194. assert_equal(example.return_last_rpc_arg(), 100)
  195. # Virtual method.
  196. var event = InputEventKey.new()
  197. event.key_label = KEY_H
  198. event.unicode = 72
  199. get_viewport().push_input(event)
  200. assert_equal(custom_signal_emitted, ["_input: H", 72])
  201. # Check NOTIFICATION_POST_INITIALIZED, both when created from GDScript and godot-cpp.
  202. var new_example_ref = ExampleRef.new()
  203. assert_equal(new_example_ref.was_post_initialized(), true)
  204. assert_equal(example.test_post_initialize(), true)
  205. # Test a virtual method defined in GDExtension and implemented in script.
  206. assert_equal(example.test_virtual_implemented_in_script("Virtual", 939), "Implemented")
  207. assert_equal(custom_signal_emitted, ["Virtual", 939])
  208. # Test that notifications happen on both parent and child classes.
  209. var example_child = $ExampleChild
  210. assert_equal(example_child.get_value1(), 11)
  211. assert_equal(example_child.get_value2(), 33)
  212. example_child.notification(NOTIFICATION_ENTER_TREE, true)
  213. assert_equal(example_child.get_value1(), 11)
  214. assert_equal(example_child.get_value2(), 22)
  215. exit_with_status()
  216. func _on_Example_custom_signal(signal_name, value):
  217. custom_signal_emitted = [signal_name, value]