main.gd 12 KB

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