main.gd 12 KB

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