Skip to content

Commit

Permalink
GD-465: Parameterized tests are skipped with const Array values (#466)
Browse files Browse the repository at this point in the history
# Why
The test was marked as skipped when the `test_parameters` contains
external const array values.
```
const _data1 := ["aa"]
const _data2 := ["bb"]

@warning_ignore("unused_parameter")
func test_with_extern_const_parameter_set(value :String, test_parameters := [_data1, _data2]) -> void:
```

# What
fix the parameterized argument parsing
  • Loading branch information
MikeSchulze authored May 21, 2024
1 parent f7953e2 commit 6d5cc8b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
37 changes: 23 additions & 14 deletions addons/gdUnit4/src/core/parse/GdFunctionArgument.gd
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,43 @@ func _parse_parameter_set(input :String) -> PackedStringArray:
return []

input = _cleanup_leading_spaces.sub(input, "", true)
input = input.trim_prefix("[").trim_suffix("]").replace("\n", "").trim_prefix(" ")
input = input.replace("\n", "").trim_prefix("[").trim_suffix("]").trim_prefix(" ")
var single_quote := false
var double_quote := false
var array_end := 0
var current_index := 0
var output :PackedStringArray = []
var start_index := 0
var buf := input.to_ascii_buffer()
var collected_characters: = PackedByteArray()
var matched :bool = false

for c in buf:
current_index += 1
matched = current_index == buf.size()
collected_characters.push_back(c)

match c:
# ignore spaces between array elements
32: if array_end == 0:
start_index += 1
continue
# step over array element seperator ','
# ' ': ignore spaces between array elements
32: if array_end == 0 and (not double_quote and not single_quote):
collected_characters.remove_at(collected_characters.size()-1)
# ',': step over array element seperator ','
44: if array_end == 0:
start_index += 1
continue
matched = true
collected_characters.remove_at(collected_characters.size()-1)
# '`':
39: single_quote = !single_quote
# '"':
34: if not single_quote: double_quote = !double_quote
# '['
91: if not double_quote and not single_quote: array_end +=1 # counts array open
# ']'
93: if not double_quote and not single_quote: array_end -=1 # counts array closed

# if array closed than collect the element
if array_end == 0 and current_index > start_index:
var parameters := input.substr(start_index, current_index-start_index)
parameters = _fix_comma_space.sub(parameters, ", ", true)
output.append(parameters)
start_index = current_index
if matched:
var parameters := _fix_comma_space.sub(collected_characters.get_string_from_ascii(), ", ", true)
if not parameters.is_empty():
output.append(parameters)
collected_characters.clear()
matched = false
return output
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func load_parameter_sets(test_case: _TestCase, do_validate := false) -> Array:
var script := GDScript.new()
script.source_code = source_code
# enable this lines only for debuging
#script.resource_path = GdUnitTools.create_temp_dir("parameter_extract") + "/%s__.gd" % fd.name()
#script.resource_path = GdUnitFileAccess.create_temp_dir("parameter_extract") + "/%s__.gd" % test_case.get_name()
#DirAccess.remove_absolute(script.resource_path)
#ResourceSaver.save(script, script.resource_path)
var result := script.reload()
Expand Down
14 changes: 14 additions & 0 deletions addons/gdUnit4/test/core/ParameterizedTestCaseTest.gd
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ var _expected_tests := {
["test_a"],
["test_b"],
["test_c"]
],
"test_with_extern_const_parameter_set" : [
["aa"],
["bb"]
]
}

Expand Down Expand Up @@ -294,3 +298,13 @@ func test_with_extern_parameter_set(value :String, test_parameters := _test_set)
assert_that(value).is_not_empty()
assert_that(test_parameters).is_empty()
collect_test_call("test_with_extern_parameter_set", [value])


const _data1 := ["aa"]
const _data2 := ["bb"]

@warning_ignore("unused_parameter")
func test_with_extern_const_parameter_set(value :String, test_parameters := [_data1, _data2]) -> void:
assert_that(value).is_not_empty()
assert_that(test_parameters).is_empty()
collect_test_call("test_with_extern_const_parameter_set", [value])
33 changes: 33 additions & 0 deletions addons/gdUnit4/test/core/parse/GdFunctionArgumentTest.gd
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,41 @@ func test__parse_argument_as_array_typ2() -> void:
)


func test__parse_argument_as_array_bad_formatted() -> void:
var test_parameters := """[
["test_a", null, "LOG", {}],
[
"test_b",
Node2D,
null,
{Node2D: "ER,ROR"}
],
[
"test_c",
Node2D,
"LOG",
{Node2D: "LOG 1"}
]
]"""
var fa := GdFunctionArgument.new(GdFunctionArgument.ARG_PARAMETERIZED_TEST, TYPE_STRING, test_parameters)
assert_array(fa.parameter_sets()).contains_exactly([
"""["test_a", null, "LOG", {}]""",
"""["test_b", Node2D, null, {Node2D: "ER,ROR"}]""",
"""["test_c", Node2D, "LOG", {Node2D: "LOG 1"}]"""
]
)


func test__parse_argument_as_reference() -> void:
var test_parameters := "_test_args()"

var fa := GdFunctionArgument.new(GdFunctionArgument.ARG_PARAMETERIZED_TEST, TYPE_STRING, test_parameters)
assert_array(fa.parameter_sets()).is_empty()


func test_parse_parameter_set_with_const_data_in_array() -> void:
var test_parameters := "[_data1, _data2]"

var fa := GdFunctionArgument.new(GdFunctionArgument.ARG_PARAMETERIZED_TEST, TYPE_STRING, test_parameters)
assert_array(fa.parameter_sets()).contains_exactly(["_data1", "_data2"])

0 comments on commit 6d5cc8b

Please sign in to comment.