Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GD-579: Part5: Minimize unsafe_cast warnings #586

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion addons/gdUnit4/bin/GdUnitCmdTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class CLIRunner:
_report_dir = GdUnitFileAccess.current_dir() + "reports"
_executor = GdUnitTestSuiteExecutor.new()
# stop checked first test failure to fail fast
@warning_ignore("unsafe_cast")
(_executor as GdUnitTestSuiteExecutor).fail_fast(true)
if GdUnit4CSharpApiLoader.is_mono_supported():
prints("GdUnit4Net version '%s' loaded." % GdUnit4CSharpApiLoader.version())
Expand Down Expand Up @@ -137,7 +138,7 @@ class CLIRunner:
else:
set_process(false)
# process next test suite
var test_suite := _test_suites_to_process.pop_front() as Node
var test_suite: Node = _test_suites_to_process.pop_front()

if _cs_executor != null and _cs_executor.IsExecutable(test_suite):
_cs_executor.Execute(test_suite)
Expand Down Expand Up @@ -307,6 +308,7 @@ class CLIRunner:
return
# build runner config by given commands
var commands :Array[CmdCommand] = []
@warning_ignore("unsafe_cast")
commands.append_array(result.value() as Array)
result = (
CmdCommandHandler.new(_cmd_options)
Expand Down
2 changes: 1 addition & 1 deletion addons/gdUnit4/bin/GdUnitCopyLog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func _process(_delta: float) -> bool:
if result.is_error():
write_report(result.error_message(), godot_log_file)
return true
write_report(result.value() as String, godot_log_file)
write_report(result.value_as_string(), godot_log_file)
return true


Expand Down
2 changes: 1 addition & 1 deletion addons/gdUnit4/src/GdUnitTestSuite.gd
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func resource_as_string(resource_path :String) -> String:

## Reads a resource by given path <resource_path> and return Variand translated by str_to_var
func resource_as_var(resource_path :String) -> Variant:
@warning_ignore("unsafe_method_access")
@warning_ignore("unsafe_method_access", "unsafe_cast")
return str_to_var(__gdunit_file_access().resource_as_string(resource_path) as String)


Expand Down
56 changes: 35 additions & 21 deletions addons/gdUnit4/src/asserts/GdAssertMessages.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ static func format_dict(value :Variant) -> String:
if not value is Dictionary:
return str(value)

if (value as Dictionary).is_empty():
var dict_value: Dictionary = value
if dict_value.is_empty():
return "{ }"
var as_rows := var_to_str(value).split("\n")
for index in range( 1, as_rows.size()-1):
Expand Down Expand Up @@ -119,10 +120,12 @@ static func _colored_value(value :Variant) -> String:
if value == null:
return "'[color=%s]<null>[/color]'" % [VALUE_COLOR]
if value is InputEvent:
return "[color=%s]<%s>[/color]" % [VALUE_COLOR, input_event_as_text(value as InputEvent)]
if (value as Object).has_method("_to_string"):
var ie: InputEvent = value
return "[color=%s]<%s>[/color]" % [VALUE_COLOR, input_event_as_text(ie)]
var obj_value: Object = value
if obj_value.has_method("_to_string"):
return "[color=%s]<%s>[/color]" % [VALUE_COLOR, str(value)]
return "[color=%s]<%s>[/color]" % [VALUE_COLOR, (value as Object).get_class()]
return "[color=%s]<%s>[/color]" % [VALUE_COLOR, obj_value.get_class()]
TYPE_DICTIONARY:
return "'[color=%s]%s[/color]'" % [VALUE_COLOR, format_dict(value)]
_:
Expand Down Expand Up @@ -379,10 +382,10 @@ static func error_arr_contains(current: Variant, expected: Variant, not_expect:
var failure_message := "Expecting contains SAME elements:" if by_reference else "Expecting contains elements:"
var error := "%s\n %s\n do contains (in any order)\n %s" % [
_error(failure_message), _colored_value(current), _colored_value(expected)]
if not (not_expect as Array).is_empty():
if not is_empty(not_expect):
error += "\nbut some elements where not expected:\n %s" % _colored_value(not_expect)
if not (not_found as Array).is_empty():
var prefix := "but" if (not_expect as Array).is_empty() else "and"
if not is_empty(not_found):
var prefix := "but" if is_empty(not_expect) else "and"
error += "\n%s could not find elements:\n %s" % [prefix, _colored_value(not_found)]
return error

Expand All @@ -396,17 +399,19 @@ static func error_arr_contains_exactly(
"Expecting contains exactly elements:" if compare_mode == GdObjects.COMPARE_MODE.PARAMETER_DEEP_TEST
else "Expecting contains SAME exactly elements:"
)
if (not_expect as Array).is_empty() and (not_found as Array).is_empty():
var diff := _find_first_diff(current as Array, expected as Array)
if is_empty(not_expect) and is_empty(not_found):
var arr_current: Array = current
var arr_expected: Array = expected
var diff := _find_first_diff(arr_current, arr_expected)
return "%s\n %s\n do contains (in same order)\n %s\n but has different order %s" % [
_error(failure_message), _colored_value(current), _colored_value(expected), diff]

var error := "%s\n %s\n do contains (in same order)\n %s" % [
_error(failure_message), _colored_value(current), _colored_value(expected)]
if not (not_expect as Array).is_empty():
if not is_empty(not_expect):
error += "\nbut some elements where not expected:\n %s" % _colored_value(not_expect)
if not (not_found as Array).is_empty():
var prefix := "but" if (not_expect as Array).is_empty() else "and"
if not is_empty(not_found):
var prefix := "but" if is_empty(not_expect) else "and"
error += "\n%s could not find elements:\n %s" % [prefix, _colored_value(not_found)]
return error

Expand All @@ -424,10 +429,10 @@ static func error_arr_contains_exactly_in_any_order(
)
var error := "%s\n %s\n do contains exactly (in any order)\n %s" % [
_error(failure_message), _colored_value(current), _colored_value(expected)]
if not (not_expect as Array).is_empty():
if not is_empty(not_expect):
error += "\nbut some elements where not expected:\n %s" % _colored_value(not_expect)
if not (not_found as Array).is_empty():
var prefix := "but" if (not_expect as Array).is_empty() else "and"
if not is_empty(not_found):
var prefix := "but" if is_empty(not_expect) else "and"
error += "\n%s could not find elements:\n %s" % [prefix, _colored_value(not_found)]
return error

Expand All @@ -436,7 +441,7 @@ static func error_arr_not_contains(current: Variant, expected: Variant, found: V
var failure_message := "Expecting:" if compare_mode == GdObjects.COMPARE_MODE.PARAMETER_DEEP_TEST else "Expecting SAME:"
var error := "%s\n %s\n do not contains\n %s" % [
_error(failure_message), _colored_value(current), _colored_value(expected)]
if not (found as Array).is_empty():
if not is_empty(found):
error += "\n but found elements:\n %s" % _colored_value(found)
return error

Expand Down Expand Up @@ -564,14 +569,17 @@ static func error_no_more_interactions(summary :Dictionary) -> String:


static func error_validate_interactions(current_interactions: Dictionary, expected_interactions: Dictionary) -> String:
var interactions := PackedStringArray()
var collected_interactions := PackedStringArray()
for args: Array in current_interactions.keys():
var times: int = current_interactions[args]
@warning_ignore("return_value_discarded")
interactions.append(_format_arguments(args, times))
var expected_interaction := _format_arguments(expected_interactions.keys()[0] as Array, expected_interactions.values()[0] as int)
collected_interactions.append(_format_arguments(args, times))

var arguments: Array = expected_interactions.keys()[0]
var interactions: int = expected_interactions.values()[0]
var expected_interaction := _format_arguments(arguments, interactions)
return "%s\n%s\n%s\n%s" % [
_error("Expecting interaction on:"), expected_interaction, _error("But found interactions on:"), "\n".join(interactions)]
_error("Expecting interaction on:"), expected_interaction, _error("But found interactions on:"), "\n".join(collected_interactions)]


static func _format_arguments(args :Array, times :int) -> String:
Expand All @@ -592,7 +600,8 @@ static func _to_typed_args(args :Array) -> PackedStringArray:

static func _format_arg(arg :Variant) -> String:
if arg is InputEvent:
return input_event_as_text(arg as InputEvent)
var ie: InputEvent = arg
return input_event_as_text(ie)
return str(arg)


Expand Down Expand Up @@ -641,3 +650,8 @@ static func build_failure_message(failure :String, additional_failure_message: S
%s
[color=LIME_GREEN][b]Additional info:[/b][/color]
%s""".dedent().trim_prefix("\n") % [message, additional_failure_message]


static func is_empty(value: Variant) -> bool:
var arry_value: Array = value
return arry_value != null and arry_value.is_empty()
30 changes: 24 additions & 6 deletions addons/gdUnit4/src/asserts/GdUnitArrayAssertImpl.gd
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func max_length(left: Variant, right: Variant) -> int:
# gdlint: disable=function-name
func _toPackedStringArray(value: Variant) -> PackedStringArray:
if GdArrayTools.is_array_type(value):
@warning_ignore("unsafe_cast")
return PackedStringArray(value as Array)
return PackedStringArray([str(value)])

Expand Down Expand Up @@ -115,9 +116,10 @@ func _contains(expected: Variant, compare_mode: GdObjects.COMPARE_MODE) -> GdUni
var current_value: Variant = get_current_value()
if current_value == null:
return report_error(GdAssertMessages.error_arr_contains(current_value, expected, [], expected, by_reference))
@warning_ignore("unsafe_cast")
var diffs := _array_div(compare_mode, current_value as Array[Variant], expected as Array[Variant])
#var not_expect := diffs[0] as Array
var not_found := diffs[1] as Array
var not_found: Array = diffs[1]
if not not_found.is_empty():
return report_error(GdAssertMessages.error_arr_contains(current_value, expected, [], not_found, by_reference))
return report_success()
Expand All @@ -136,12 +138,13 @@ func _contains_exactly(expected: Variant, compare_mode: GdObjects.COMPARE_MODE)
if _is_equals_sorted(current_value, expected, false, compare_mode):
return report_error(GdAssertMessages.error_arr_contains_exactly(current_value, expected, [], [], compare_mode))
# find the difference
@warning_ignore("unsafe_cast")
var diffs := _array_div(compare_mode,
current_value as Array[Variant],
expected as Array[Variant],
GdObjects.COMPARE_MODE.PARAMETER_DEEP_TEST)
var not_expect := diffs[0] as Array[Variant]
var not_found := diffs[1] as Array[Variant]
var not_expect: Array[Variant] = diffs[0]
var not_found: Array[Variant] = diffs[1]
return report_error(GdAssertMessages.error_arr_contains_exactly(current_value, expected, not_expect, not_found, compare_mode))


Expand All @@ -152,9 +155,10 @@ func _contains_exactly_in_any_order(expected: Variant, compare_mode: GdObjects.C
if current_value == null:
return report_error(GdAssertMessages.error_arr_contains_exactly_in_any_order(current_value, expected, [], expected, compare_mode))
# find the difference
@warning_ignore("unsafe_cast")
var diffs := _array_div(compare_mode, current_value as Array[Variant], expected as Array[Variant], false)
var not_expect := diffs[0] as Array
var not_found := diffs[1] as Array
var not_expect: Array[Variant] = diffs[0]
var not_found: Array[Variant] = diffs[1]
if not_expect.is_empty() and not_found.is_empty():
return report_success()
return report_error(GdAssertMessages.error_arr_contains_exactly_in_any_order(current_value, expected, not_expect, not_found, compare_mode))
Expand All @@ -166,10 +170,13 @@ func _not_contains(expected: Variant, compare_mode: GdObjects.COMPARE_MODE) -> G
var current_value: Variant = get_current_value()
if current_value == null:
return report_error(GdAssertMessages.error_arr_contains_exactly_in_any_order(current_value, expected, [], expected, compare_mode))
@warning_ignore("unsafe_cast")
var diffs := _array_div(compare_mode, current_value as Array[Variant], expected as Array[Variant])
var found := diffs[0] as Array
var found: Array[Variant] = diffs[0]
@warning_ignore("unsafe_cast")
if found.size() == (current_value as Array).size():
return report_success()
@warning_ignore("unsafe_cast")
var diffs2 := _array_div(compare_mode, expected as Array[Variant], diffs[1] as Array[Variant])
return report_error(GdAssertMessages.error_arr_not_contains(current_value, expected, diffs2[0], compare_mode))

Expand Down Expand Up @@ -208,8 +215,10 @@ func is_equal_ignoring_case(expected: Variant) -> GdUnitArrayAssert:
return report_error("ERROR: expected value: <%s>\n is not a Array Type!" % GdObjects.typeof_as_string(expected))
var current_value: Variant = get_current_value()
if current_value == null and expected != null:
@warning_ignore("unsafe_cast")
return report_error(GdAssertMessages.error_equal(null, GdArrayTools.as_string(expected as Array)))
if not _is_equal(current_value, expected, true):
@warning_ignore("unsafe_cast")
var diff := _array_equals_div(current_value as Array[Variant], expected as Array[Variant], true)
var expected_as_list := GdArrayTools.as_string(diff[0])
var current_as_list := GdArrayTools.as_string(diff[1])
Expand All @@ -232,21 +241,25 @@ func is_not_equal_ignoring_case(expected: Variant) -> GdUnitArrayAssert:
return report_error("ERROR: expected value: <%s>\n is not a Array Type!" % GdObjects.typeof_as_string(expected))
var current_value: Variant = get_current_value()
if _is_equal(current_value, expected, true):
@warning_ignore("unsafe_cast")
var c := GdArrayTools.as_string(current_value as Array)
@warning_ignore("unsafe_cast")
var e := GdArrayTools.as_string(expected as Array)
return report_error(GdAssertMessages.error_not_equal_case_insensetiv(c, e))
return report_success()


func is_empty() -> GdUnitArrayAssert:
var current_value: Variant = get_current_value()
@warning_ignore("unsafe_cast")
if current_value == null or (current_value as Array).size() > 0:
return report_error(GdAssertMessages.error_is_empty(current_value))
return report_success()


func is_not_empty() -> GdUnitArrayAssert:
var current_value: Variant = get_current_value()
@warning_ignore("unsafe_cast")
if current_value != null and (current_value as Array).size() == 0:
return report_error(GdAssertMessages.error_is_not_empty())
return report_success()
Expand Down Expand Up @@ -275,6 +288,7 @@ func is_not_same(expected: Variant) -> GdUnitArrayAssert:

func has_size(expected: int) -> GdUnitArrayAssert:
var current_value: Variant = get_current_value()
@warning_ignore("unsafe_cast")
if current_value == null or (current_value as Array).size() != expected:
return report_error(GdAssertMessages.error_has_size(current_value, expected))
return report_success()
Expand Down Expand Up @@ -362,9 +376,11 @@ func extractv(
GdUnitTuple.NO_ARG,
GdUnitTuple.NO_ARG
]
@warning_ignore("unsafe_cast")
for index: int in (extractors as Array).size():
var extractor: GdUnitValueExtractor = extractors[index]
ev[index] = extractor.extract_value(element)
@warning_ignore("unsafe_cast")
if (extractors as Array).size() > 1:
extracted_elements.append(GdUnitTuple.new(ev[0], ev[1], ev[2], ev[3], ev[4], ev[5], ev[6], ev[7], ev[8], ev[9]))
else:
Expand All @@ -380,6 +396,7 @@ func _is_equal(
case_sensitive := false,
compare_mode := GdObjects.COMPARE_MODE.PARAMETER_DEEP_TEST) -> bool:

@warning_ignore("unsafe_cast")
return GdObjects.equals(
(left as Array) if GdArrayTools.is_array_type(left) else left,
(right as Array) if GdArrayTools.is_array_type(right) else right,
Expand All @@ -394,6 +411,7 @@ func _is_equals_sorted(
case_sensitive := false,
compare_mode := GdObjects.COMPARE_MODE.PARAMETER_DEEP_TEST) -> bool:

@warning_ignore("unsafe_cast")
return GdObjects.equals_sorted(
left as Array,
right as Array,
Expand Down
5 changes: 5 additions & 0 deletions addons/gdUnit4/src/asserts/GdUnitDictionaryAssertImpl.gd
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ func is_not_same(expected :Variant) -> GdUnitDictionaryAssert:

func is_empty() -> GdUnitDictionaryAssert:
var current :Variant = current_value()
@warning_ignore("unsafe_cast")
if current == null or not (current as Dictionary).is_empty():
return report_error(GdAssertMessages.error_is_empty(current))
return report_success()


func is_not_empty() -> GdUnitDictionaryAssert:
var current :Variant = current_value()
@warning_ignore("unsafe_cast")
if current == null or (current as Dictionary).is_empty():
return report_error(GdAssertMessages.error_is_not_empty())
return report_success()
Expand All @@ -121,6 +123,7 @@ func has_size(expected: int) -> GdUnitDictionaryAssert:
var current :Variant = current_value()
if current == null:
return report_error(GdAssertMessages.error_is_not_null())
@warning_ignore("unsafe_cast")
if (current as Dictionary).size() != expected:
return report_error(GdAssertMessages.error_has_size(current, expected))
return report_success()
Expand All @@ -131,8 +134,10 @@ func _contains_keys(expected :Array, compare_mode :GdObjects.COMPARE_MODE) -> Gd
if current == null:
return report_error(GdAssertMessages.error_is_not_null())
# find expected keys
@warning_ignore("unsafe_cast")
var keys_not_found :Array = expected.filter(_filter_by_key.bind((current as Dictionary).keys(), compare_mode))
if not keys_not_found.is_empty():
@warning_ignore("unsafe_cast")
return report_error(GdAssertMessages.error_contains_keys((current as Dictionary).keys() as Array, expected, keys_not_found, compare_mode))
return report_success()

Expand Down
2 changes: 1 addition & 1 deletion addons/gdUnit4/src/asserts/GdUnitFileAssertImpl.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func _notification(event :int) -> void:


func current_value() -> String:
return _base.current_value() as String
return _base.current_value()


func report_success() -> GdUnitFileAssert:
Expand Down
2 changes: 2 additions & 0 deletions addons/gdUnit4/src/asserts/GdUnitFloatAssertImpl.gd
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ func is_not_negative() -> GdUnitFloatAssert:

func is_zero() -> GdUnitFloatAssert:
var current :Variant = current_value()
@warning_ignore("unsafe_cast")
if current == null or not is_equal_approx(0.00000000, current as float):
return report_error(GdAssertMessages.error_is_zero(current))
return report_success()


func is_not_zero() -> GdUnitFloatAssert:
var current :Variant = current_value()
@warning_ignore("unsafe_cast")
if current == null or is_equal_approx(0.00000000, current as float):
return report_error(GdAssertMessages.error_is_not_zero())
return report_success()
Expand Down
2 changes: 1 addition & 1 deletion addons/gdUnit4/src/asserts/GdUnitResultAssertImpl.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func validate_value_type(value :Variant) -> bool:


func current_value() -> GdUnitResult:
return _base.current_value() as GdUnitResult
return _base.current_value()


func report_success() -> GdUnitResultAssert:
Expand Down
Loading