main.gd 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. # Call simple methods.
  23. example.simple_func()
  24. assert_equal(custom_signal_emitted, ['simple_func', 3])
  25. example.simple_const_func()
  26. assert_equal(custom_signal_emitted, ['simple_const_func', 4])
  27. # Pass custom reference.
  28. assert_equal(example.custom_ref_func(null), -1)
  29. var ref1 = ExampleRef.new()
  30. ref1.id = 27
  31. assert_equal(example.custom_ref_func(ref1), 27)
  32. ref1.id += 1;
  33. assert_equal(example.custom_const_ref_func(ref1), 28)
  34. # Pass core reference.
  35. assert_equal(example.image_ref_func(null), "invalid")
  36. assert_equal(example.image_const_ref_func(null), "invalid")
  37. var image = Image.new()
  38. assert_equal(example.image_ref_func(image), "valid")
  39. assert_equal(example.image_const_ref_func(image), "valid")
  40. # Return values.
  41. assert_equal(example.return_something("some string"), "some string42")
  42. assert_equal(example.return_something_const(), get_viewport())
  43. var null_ref = example.return_empty_ref()
  44. assert_equal(null_ref, null)
  45. var ret_ref = example.return_extended_ref()
  46. assert_not_equal(ret_ref.get_instance_id(), 0)
  47. assert_equal(ret_ref.get_id(), 0)
  48. assert_equal(example.get_v4(), Vector4(1.2, 3.4, 5.6, 7.8))
  49. assert_equal(example.test_node_argument(example), example)
  50. # VarArg method calls.
  51. var var_ref = ExampleRef.new()
  52. assert_not_equal(example.extended_ref_checks(var_ref).get_instance_id(), var_ref.get_instance_id())
  53. assert_equal(example.varargs_func("some", "arguments", "to", "test"), 4)
  54. assert_equal(example.varargs_func_nv("some", "arguments", "to", "test"), 46)
  55. example.varargs_func_void("some", "arguments", "to", "test")
  56. assert_equal(custom_signal_emitted, ["varargs_func_void", 5])
  57. # Method calls with default values.
  58. assert_equal(example.def_args(), 300)
  59. assert_equal(example.def_args(50), 250)
  60. assert_equal(example.def_args(50, 100), 150)
  61. # Array and Dictionary
  62. assert_equal(example.test_array(), [1, 2])
  63. assert_equal(example.test_tarray(), [ Vector2(1, 2), Vector2(2, 3) ])
  64. assert_equal(example.test_dictionary(), {"hello": "world", "foo": "bar"})
  65. var array: Array[int] = [1, 2, 3]
  66. assert_equal(example.test_tarray_arg(array), 6)
  67. # String += operator
  68. assert_equal(example.test_string_ops(), "ABCĎE")
  69. # UtilityFunctions::str()
  70. assert_equal(example.test_str_utility(), "Hello, World! The answer is 42")
  71. # Test converting string to char* and doing comparison.
  72. assert_equal(example.test_string_is_fourty_two("blah"), false)
  73. assert_equal(example.test_string_is_fourty_two("fourty two"), true)
  74. # PackedArray iterators
  75. assert_equal(example.test_vector_ops(), 105)
  76. # Properties.
  77. assert_equal(example.group_subgroup_custom_position, Vector2(0, 0))
  78. example.group_subgroup_custom_position = Vector2(50, 50)
  79. assert_equal(example.group_subgroup_custom_position, Vector2(50, 50))
  80. # Test Object::cast_to<>() and that correct wrappers are being used.
  81. var control = Control.new()
  82. var sprite = Sprite2D.new()
  83. var example_ref = ExampleRef.new()
  84. assert_equal(example.test_object_cast_to_node(control), true)
  85. assert_equal(example.test_object_cast_to_control(control), true)
  86. assert_equal(example.test_object_cast_to_example(control), false)
  87. assert_equal(example.test_object_cast_to_node(example), true)
  88. assert_equal(example.test_object_cast_to_control(example), true)
  89. assert_equal(example.test_object_cast_to_example(example), true)
  90. assert_equal(example.test_object_cast_to_node(sprite), true)
  91. assert_equal(example.test_object_cast_to_control(sprite), false)
  92. assert_equal(example.test_object_cast_to_example(sprite), false)
  93. assert_equal(example.test_object_cast_to_node(example_ref), false)
  94. assert_equal(example.test_object_cast_to_control(example_ref), false)
  95. assert_equal(example.test_object_cast_to_example(example_ref), false)
  96. control.queue_free()
  97. sprite.queue_free()
  98. # Test conversions to and from Variant.
  99. assert_equal(example.test_variant_vector2i_conversion(Vector2i(1, 1)), Vector2i(1, 1))
  100. assert_equal(example.test_variant_vector2i_conversion(Vector2(1.0, 1.0)), Vector2i(1, 1))
  101. assert_equal(example.test_variant_int_conversion(10), 10)
  102. assert_equal(example.test_variant_int_conversion(10.0), 10)
  103. assert_equal(example.test_variant_float_conversion(10.0), 10.0)
  104. assert_equal(example.test_variant_float_conversion(10), 10.0)
  105. # Test that ptrcalls from GDExtension to the engine are correctly encoding Object and RefCounted.
  106. var new_node = Node.new()
  107. example.test_add_child(new_node)
  108. assert_equal(new_node.get_parent(), example)
  109. var new_tileset = TileSet.new()
  110. var new_tilemap = TileMap.new()
  111. example.test_set_tileset(new_tilemap, new_tileset)
  112. assert_equal(new_tilemap.tile_set, new_tileset)
  113. new_tilemap.queue_free()
  114. # Test variant call.
  115. var test_obj = TestClass.new()
  116. assert_equal(example.test_variant_call(test_obj), "hello world")
  117. # Constants.
  118. assert_equal(Example.FIRST, 0)
  119. assert_equal(Example.ANSWER_TO_EVERYTHING, 42)
  120. assert_equal(Example.CONSTANT_WITHOUT_ENUM, 314)
  121. # BitFields.
  122. assert_equal(Example.FLAG_ONE, 1)
  123. assert_equal(Example.FLAG_TWO, 2)
  124. assert_equal(example.test_bitfield(0), 0)
  125. assert_equal(example.test_bitfield(Example.FLAG_ONE | Example.FLAG_TWO), 3)
  126. # RPCs.
  127. assert_equal(example.return_last_rpc_arg(), 0)
  128. example.test_rpc(42)
  129. assert_equal(example.return_last_rpc_arg(), 42)
  130. example.test_send_rpc(100)
  131. assert_equal(example.return_last_rpc_arg(), 100)
  132. # Virtual method.
  133. var event = InputEventKey.new()
  134. event.key_label = KEY_H
  135. event.unicode = 72
  136. get_viewport().push_input(event)
  137. assert_equal(custom_signal_emitted, ["_input: H", 72])
  138. exit_with_status()
  139. func _on_Example_custom_signal(signal_name, value):
  140. custom_signal_emitted = [signal_name, value]