diff options
| author | Dmitrii Maganov <vonagam@gmail.com> | 2022-11-27 09:56:53 +0200 | 
|---|---|---|
| committer | Dmitrii Maganov <vonagam@gmail.com> | 2023-01-31 11:54:41 +0200 | 
| commit | 5909f9f07547895de24fb6965d44c859b69a54a2 (patch) | |
| tree | 127f149411e46727b62f921b6951e4d8cd267c6b /modules/gdscript/tests | |
| parent | e9de988020f3d46c3e7b4fd5a8a80724996035e0 (diff) | |
GDScript: Fix issues with typed arrays
Diffstat (limited to 'modules/gdscript/tests')
23 files changed, 285 insertions, 4 deletions
diff --git a/modules/gdscript/tests/scripts/analyzer/errors/invalid_array_index.out b/modules/gdscript/tests/scripts/analyzer/errors/invalid_array_index.out index 6f7f0783f0..015ad756f8 100644 --- a/modules/gdscript/tests/scripts/analyzer/errors/invalid_array_index.out +++ b/modules/gdscript/tests/scripts/analyzer/errors/invalid_array_index.out @@ -1,2 +1,2 @@  GDTEST_ANALYZER_ERROR -Cannot get index "true" from "[0, 1]". +Invalid index type "bool" for a base of type "Array". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assign_differently_typed.gd b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assign_differently_typed.gd new file mode 100644 index 0000000000..ce50cccb3c --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assign_differently_typed.gd @@ -0,0 +1,4 @@ +func test(): +	var differently: Array[float] = [1.0] +	var typed: Array[int] = differently +	print('not ok') diff --git a/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assign_differently_typed.out b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assign_differently_typed.out new file mode 100644 index 0000000000..c6d39781ee --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assign_differently_typed.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Cannot assign a value of type Array[float] to variable "typed" with specified type Array[int]. diff --git a/modules/gdscript/tests/scripts/analyzer/typed_array_assignment.gd b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assignment.gd index 9f86d0531c..9f86d0531c 100644 --- a/modules/gdscript/tests/scripts/analyzer/typed_array_assignment.gd +++ b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assignment.gd diff --git a/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assignment.out b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assignment.out new file mode 100644 index 0000000000..8530783673 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_assignment.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Cannot include a value of type "String" as "int". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/typed_array_init_with_unconvertable_in_literal.gd b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_init_with_unconvertable_in_literal.gd new file mode 100644 index 0000000000..25cde1d40b --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_init_with_unconvertable_in_literal.gd @@ -0,0 +1,4 @@ +func test(): +	var unconvertable := 1 +	var typed: Array[Object] = [unconvertable] +	print('not ok') diff --git a/modules/gdscript/tests/scripts/analyzer/errors/typed_array_init_with_unconvertable_in_literal.out b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_init_with_unconvertable_in_literal.out new file mode 100644 index 0000000000..dfe3443761 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_init_with_unconvertable_in_literal.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Cannot have an element of type "int" in an array of type "Array[Object]". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/typed_array_pass_differently_to_typed.gd b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_pass_differently_to_typed.gd new file mode 100644 index 0000000000..1a90bd121e --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_pass_differently_to_typed.gd @@ -0,0 +1,7 @@ +func expect_typed(typed: Array[int]): +	print(typed.size()) + +func test(): +	var differently: Array[float] = [1.0] +	expect_typed(differently) +	print('not ok') diff --git a/modules/gdscript/tests/scripts/analyzer/errors/typed_array_pass_differently_to_typed.out b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_pass_differently_to_typed.out new file mode 100644 index 0000000000..297e1283e8 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/typed_array_pass_differently_to_typed.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Invalid argument for "expect_typed()" function: argument 1 should be "Array[int]" but is "Array[float]". diff --git a/modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.out b/modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.out index 082e3ade19..2729c5b6c7 100644 --- a/modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.out +++ b/modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.out @@ -2,7 +2,7 @@ GDTEST_OK  [0]  0  [1] -2 +0  [2]  2  ok diff --git a/modules/gdscript/tests/scripts/analyzer/features/typed_array_usage.gd b/modules/gdscript/tests/scripts/analyzer/features/typed_array_usage.gd new file mode 100644 index 0000000000..e1e6134fd4 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/typed_array_usage.gd @@ -0,0 +1,204 @@ +class A: pass +class B extends A: pass + +enum E { E0 = 391 } + +func floats_identity(floats: Array[float]): return floats + +class Members: +	var one: Array[int] = [104] +	var two: Array[int] = one + +	func check_passing() -> bool: +		assert(str(one) == '[104]') +		assert(str(two) == '[104]') +		two.push_back(582) +		assert(str(one) == '[104, 582]') +		assert(str(two) == '[104, 582]') +		two = [486] +		assert(str(one) == '[104, 582]') +		assert(str(two) == '[486]') +		return true + + +@warning_ignore("unsafe_method_access") +@warning_ignore("assert_always_true") +@warning_ignore("return_value_discarded") +func test(): +	var untyped_basic = [459] +	assert(str(untyped_basic) == '[459]') +	assert(untyped_basic.get_typed_builtin() == TYPE_NIL) + +	var inferred_basic := [366] +	assert(str(inferred_basic) == '[366]') +	assert(inferred_basic.get_typed_builtin() == TYPE_NIL) + +	var typed_basic: Array = [521] +	assert(str(typed_basic) == '[521]') +	assert(typed_basic.get_typed_builtin() == TYPE_NIL) + + +	var empty_floats: Array[float] = [] +	assert(str(empty_floats) == '[]') +	assert(empty_floats.get_typed_builtin() == TYPE_FLOAT) + +	untyped_basic = empty_floats +	assert(untyped_basic.get_typed_builtin() == TYPE_FLOAT) + +	inferred_basic = empty_floats +	assert(inferred_basic.get_typed_builtin() == TYPE_FLOAT) + +	typed_basic = empty_floats +	assert(typed_basic.get_typed_builtin() == TYPE_FLOAT) + +	empty_floats.push_back(705.0) +	untyped_basic.push_back(430.0) +	inferred_basic.push_back(263.0) +	typed_basic.push_back(518.0) +	assert(str(empty_floats) == '[705, 430, 263, 518]') +	assert(str(untyped_basic) == '[705, 430, 263, 518]') +	assert(str(inferred_basic) == '[705, 430, 263, 518]') +	assert(str(typed_basic) == '[705, 430, 263, 518]') + + +	const constant_float := 950.0 +	const constant_int := 170 +	var typed_float := 954.0 +	var filled_floats: Array[float] = [constant_float, constant_int, typed_float, empty_floats[1] + empty_floats[2]] +	assert(str(filled_floats) == '[950, 170, 954, 693]') +	assert(filled_floats.get_typed_builtin() == TYPE_FLOAT) + +	var casted_floats := [empty_floats[2] * 2] as Array[float] +	assert(str(casted_floats) == '[526]') +	assert(casted_floats.get_typed_builtin() == TYPE_FLOAT) + +	var returned_floats = (func () -> Array[float]: return [554]).call() +	assert(str(returned_floats) == '[554]') +	assert(returned_floats.get_typed_builtin() == TYPE_FLOAT) + +	var passed_floats = floats_identity([663.0 if randf() > 0.5 else 663.0]) +	assert(str(passed_floats) == '[663]') +	assert(passed_floats.get_typed_builtin() == TYPE_FLOAT) + +	var default_floats = (func (floats: Array[float] = [364.0]): return floats).call() +	assert(str(default_floats) == '[364]') +	assert(default_floats.get_typed_builtin() == TYPE_FLOAT) + +	var typed_int := 556 +	var converted_floats: Array[float] = [typed_int] +	assert(str(converted_floats) == '[556]') +	assert(converted_floats.get_typed_builtin() == TYPE_FLOAT) + + +	const constant_basic = [228] +	assert(str(constant_basic) == '[228]') +	assert(constant_basic.get_typed_builtin() == TYPE_NIL) + +	const constant_floats: Array[float] = [constant_float - constant_basic[0] - constant_int] +	assert(str(constant_floats) == '[552]') +	assert(constant_floats.get_typed_builtin() == TYPE_FLOAT) + + +	var source_floats: Array[float] = [999.74] +	untyped_basic = source_floats +	var destination_floats: Array[float] = untyped_basic +	destination_floats[0] -= 0.74 +	assert(str(source_floats) == '[999]') +	assert(str(untyped_basic) == '[999]') +	assert(str(destination_floats) == '[999]') +	assert(destination_floats.get_typed_builtin() == TYPE_FLOAT) + + +	var duplicated_floats := empty_floats.duplicate().slice(2, 3) +	duplicated_floats[0] *= 3 +	assert(str(duplicated_floats) == '[789]') +	assert(duplicated_floats.get_typed_builtin() == TYPE_FLOAT) + + +	var b_objects: Array[B] = [B.new(), null] +	assert(b_objects.size() == 2) +	assert(b_objects.get_typed_builtin() == TYPE_OBJECT) +	assert(b_objects.get_typed_script() == B) + +	var a_objects: Array[A] = [A.new(), B.new(), null, b_objects[0]] +	assert(a_objects.size() == 4) +	assert(a_objects.get_typed_builtin() == TYPE_OBJECT) +	assert(a_objects.get_typed_script() == A) + +	var a_passed = (func check_a_passing(a_objects: Array[A]): return a_objects.size()).call(a_objects) +	assert(a_passed == 4) + +	var b_passed = (func check_b_passing(basic: Array): return basic[0] != null).call(b_objects) +	assert(b_passed == true) + + +	var empty_strings: Array[String] = [] +	var empty_bools: Array[bool] = [] +	var empty_basic_one := [] +	var empty_basic_two := [] +	assert(empty_strings == empty_bools) +	assert(empty_basic_one == empty_basic_two) +	assert(empty_strings.hash() == empty_bools.hash()) +	assert(empty_basic_one.hash() == empty_basic_two.hash()) + + +	var assign_source: Array[int] = [527] +	var assign_target: Array[int] = [] +	assign_target.assign(assign_source) +	assert(str(assign_source) == '[527]') +	assert(str(assign_target) == '[527]') +	assign_source.push_back(657) +	assert(str(assign_source) == '[527, 657]') +	assert(str(assign_target) == '[527]') + + +	var defaults_passed = (func check_defaults_passing(one: Array[int] = [], two := one): +		one.push_back(887) +		two.push_back(198) +		assert(str(one) == '[887, 198]') +		assert(str(two) == '[887, 198]') +		two = [130] +		assert(str(one) == '[887, 198]') +		assert(str(two) == '[130]') +		return true +	).call() +	assert(defaults_passed == true) + + +	var members := Members.new() +	var members_passed := members.check_passing() +	assert(members_passed == true) + + +	var resized_basic: Array = [] +	resized_basic.resize(1) +	assert(typeof(resized_basic[0]) == TYPE_NIL) +	assert(resized_basic[0] == null) + +	var resized_ints: Array[int] = [] +	resized_ints.resize(1) +	assert(typeof(resized_ints[0]) == TYPE_INT) +	assert(resized_ints[0] == 0) + +	var resized_arrays: Array[Array] = [] +	resized_arrays.resize(1) +	assert(typeof(resized_arrays[0]) == TYPE_ARRAY) +	resized_arrays[0].resize(1) +	resized_arrays[0][0] = 523 +	assert(str(resized_arrays) == '[[523]]') + +	var resized_objects: Array[Object] = [] +	resized_objects.resize(1) +	assert(typeof(resized_objects[0]) == TYPE_NIL) +	assert(resized_objects[0] == null) + + +	var typed_enums: Array[E] = [] +	typed_enums.resize(1) +	assert(str(typed_enums) == '[0]') +	typed_enums[0] = E.E0 +	assert(str(typed_enums) == '[391]') +	assert(typed_enums.get_typed_builtin() == TYPE_INT) + + +	print('ok') diff --git a/modules/gdscript/tests/scripts/analyzer/features/typed_array_usage.out b/modules/gdscript/tests/scripts/analyzer/features/typed_array_usage.out new file mode 100644 index 0000000000..1b47ed10dc --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/typed_array_usage.out @@ -0,0 +1,2 @@ +GDTEST_OK +ok diff --git a/modules/gdscript/tests/scripts/analyzer/typed_array_assignment.out b/modules/gdscript/tests/scripts/analyzer/typed_array_assignment.out deleted file mode 100644 index ad2e6558d7..0000000000 --- a/modules/gdscript/tests/scripts/analyzer/typed_array_assignment.out +++ /dev/null @@ -1,2 +0,0 @@ -GDTEST_ANALYZER_ERROR -Cannot assign a value of type Array[String] to constant "arr" with specified type Array[int]. diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_basic_to_typed.gd b/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_basic_to_typed.gd new file mode 100644 index 0000000000..e9dbc1b640 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_basic_to_typed.gd @@ -0,0 +1,4 @@ +func test(): +	var basic := [1] +	var typed: Array[int] = basic +	print('not ok') diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_basic_to_typed.out b/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_basic_to_typed.out new file mode 100644 index 0000000000..bca700b4ec --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_basic_to_typed.out @@ -0,0 +1,6 @@ +GDTEST_RUNTIME_ERROR +>> SCRIPT ERROR +>> on function: test() +>> runtime/errors/typed_array_assign_basic_to_typed.gd +>> 3 +>> Trying to assign an array of type "Array" to a variable of type "Array[int]". diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_differently_typed.gd b/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_differently_typed.gd new file mode 100644 index 0000000000..920352a6ea --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_differently_typed.gd @@ -0,0 +1,4 @@ +func test(): +	var differently: Variant = [1.0] as Array[float] +	var typed: Array[int] = differently +	print('not ok') diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_differently_typed.out b/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_differently_typed.out new file mode 100644 index 0000000000..402ab38fb3 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/typed_array_assign_differently_typed.out @@ -0,0 +1,6 @@ +GDTEST_RUNTIME_ERROR +>> SCRIPT ERROR +>> on function: test() +>> runtime/errors/typed_array_assign_differently_typed.gd +>> 3 +>> Trying to assign an array of type "Array[float]" to a variable of type "Array[int]". diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_basic_to_typed.gd b/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_basic_to_typed.gd new file mode 100644 index 0000000000..e1fd0f7168 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_basic_to_typed.gd @@ -0,0 +1,7 @@ +func expect_typed(typed: Array[int]): +	print(typed.size()) + +func test(): +	var basic := [1] +	expect_typed(basic) +	print('not ok') diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_basic_to_typed.out b/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_basic_to_typed.out new file mode 100644 index 0000000000..6f210e944e --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_basic_to_typed.out @@ -0,0 +1,6 @@ +GDTEST_RUNTIME_ERROR +>> SCRIPT ERROR +>> on function: test() +>> runtime/errors/typed_array_pass_basic_to_typed.gd +>> 6 +>> Invalid type in function 'expect_typed' in base 'RefCounted ()'. The array of argument 1 (Array) does not have the same element type as the expected typed array argument. diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_differently_to_typed.gd b/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_differently_to_typed.gd new file mode 100644 index 0000000000..e2d2721e8c --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_differently_to_typed.gd @@ -0,0 +1,7 @@ +func expect_typed(typed: Array[int]): +	print(typed.size()) + +func test(): +	var differently: Variant = [1.0] as Array[float] +	expect_typed(differently) +	print('not ok') diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_differently_to_typed.out b/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_differently_to_typed.out new file mode 100644 index 0000000000..3cd4e25bd8 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/typed_array_pass_differently_to_typed.out @@ -0,0 +1,6 @@ +GDTEST_RUNTIME_ERROR +>> SCRIPT ERROR +>> on function: test() +>> runtime/errors/typed_array_pass_differently_to_typed.gd +>> 6 +>> Invalid type in function 'expect_typed' in base 'RefCounted ()'. The array of argument 1 (Array[float]) does not have the same element type as the expected typed array argument. diff --git a/modules/gdscript/tests/scripts/runtime/features/typed_array_init_with_untyped_in_literal.gd b/modules/gdscript/tests/scripts/runtime/features/typed_array_init_with_untyped_in_literal.gd new file mode 100644 index 0000000000..ec444b4ffa --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/typed_array_init_with_untyped_in_literal.gd @@ -0,0 +1,6 @@ +func test(): +	var untyped: Variant = 32 +	var typed: Array[int] = [untyped] +	assert(typed.get_typed_builtin() == TYPE_INT) +	assert(str(typed) == '[32]') +	print('ok') diff --git a/modules/gdscript/tests/scripts/runtime/features/typed_array_init_with_untyped_in_literal.out b/modules/gdscript/tests/scripts/runtime/features/typed_array_init_with_untyped_in_literal.out new file mode 100644 index 0000000000..1b47ed10dc --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/typed_array_init_with_untyped_in_literal.out @@ -0,0 +1,2 @@ +GDTEST_OK +ok  |