Skip to content

Commit

Permalink
GD-546: Allow to test un-types values by assert_that
Browse files Browse the repository at this point in the history
# Why
see #546

# What
- do allow all Variant types for `is_equal` and `is_not_equal` when using the untyped `assert_that`
- changed the fixed typed  `is_equal` and `is_not_equal` to accept Variant type
  • Loading branch information
MikeSchulze committed Jul 22, 2024
1 parent fac168f commit 0168576
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 36 deletions.
8 changes: 4 additions & 4 deletions addons/gdUnit4/src/GdUnitFloatAssert.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ class_name GdUnitFloatAssert
extends GdUnitAssert


## Verifies that the current value is equal to expected one.
## Verifies that the current String is equal to the given one.
@warning_ignore("unused_parameter")
func is_equal(expected :float) -> GdUnitFloatAssert:
func is_equal(expected :Variant) -> GdUnitFloatAssert:
return self


## Verifies that the current value is not equal to expected one.
## Verifies that the current String is not equal to the given one.
@warning_ignore("unused_parameter")
func is_not_equal(expected :float) -> GdUnitFloatAssert:
func is_not_equal(expected :Variant) -> GdUnitFloatAssert:
return self


Expand Down
9 changes: 4 additions & 5 deletions addons/gdUnit4/src/GdUnitIntAssert.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
class_name GdUnitIntAssert
extends GdUnitAssert


## Verifies that the current value is equal to expected one.
## Verifies that the current String is equal to the given one.
@warning_ignore("unused_parameter")
func is_equal(expected :int) -> GdUnitIntAssert:
func is_equal(expected :Variant) -> GdUnitIntAssert:
return self


## Verifies that the current value is not equal to expected one.
## Verifies that the current String is not equal to the given one.
@warning_ignore("unused_parameter")
func is_not_equal(expected :int) -> GdUnitIntAssert:
func is_not_equal(expected :Variant) -> GdUnitIntAssert:
return self


Expand Down
12 changes: 6 additions & 6 deletions addons/gdUnit4/src/GdUnitTestSuite.gd
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,13 @@ func assert_that(current :Variant) -> GdUnitAssert:
TYPE_STRING:
return assert_str(current)
TYPE_VECTOR2, TYPE_VECTOR2I, TYPE_VECTOR3, TYPE_VECTOR3I, TYPE_VECTOR4, TYPE_VECTOR4I:
return assert_vector(current)
return assert_vector(current, false)
TYPE_DICTIONARY:
return assert_dict(current)
TYPE_ARRAY, TYPE_PACKED_BYTE_ARRAY, TYPE_PACKED_INT32_ARRAY, TYPE_PACKED_INT64_ARRAY,\
TYPE_PACKED_FLOAT32_ARRAY, TYPE_PACKED_FLOAT64_ARRAY, TYPE_PACKED_STRING_ARRAY,\
TYPE_PACKED_VECTOR2_ARRAY, TYPE_PACKED_VECTOR3_ARRAY, TYPE_PACKED_COLOR_ARRAY:
return assert_array(current)
return assert_array(current, false)
TYPE_OBJECT, TYPE_NIL:
return assert_object(current)
_:
Expand Down Expand Up @@ -531,13 +531,13 @@ func assert_float(current :Variant) -> GdUnitFloatAssert:
## [codeblock]
## assert_vector(Vector2(1.2, 1.000001)).is_equal(Vector2(1.2, 1.000001))
## [/codeblock]
func assert_vector(current :Variant) -> GdUnitVectorAssert:
return __lazy_load("res://addons/gdUnit4/src/asserts/GdUnitVectorAssertImpl.gd").new(current)
func assert_vector(current :Variant, type_check := true) -> GdUnitVectorAssert:
return __lazy_load("res://addons/gdUnit4/src/asserts/GdUnitVectorAssertImpl.gd").new(current, type_check)


## An assertion tool to verify arrays.
func assert_array(current :Variant) -> GdUnitArrayAssert:
return __lazy_load("res://addons/gdUnit4/src/asserts/GdUnitArrayAssertImpl.gd").new(current)
func assert_array(current :Variant, type_check := true) -> GdUnitArrayAssert:
return __lazy_load("res://addons/gdUnit4/src/asserts/GdUnitArrayAssertImpl.gd").new(current, type_check)


## An assertion tool to verify dictionaries.
Expand Down
5 changes: 4 additions & 1 deletion addons/gdUnit4/src/asserts/GdAssertMessages.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const SUB_COLOR := Color(1, 0, 0, .3)
const ADD_COLOR := Color(0, 1, 0, .3)


static func format_dict(value :Dictionary) -> String:
static func format_dict(value :Variant) -> String:
if not value is Dictionary:
return str(value)

if value.is_empty():
return "{ }"
var as_rows := var_to_str(value).split("\n")
Expand Down
24 changes: 17 additions & 7 deletions addons/gdUnit4/src/asserts/GdUnitArrayAssertImpl.gd
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
extends GdUnitArrayAssert


var _base :GdUnitAssert
var _current_value_provider :ValueProvider
var _base: GdUnitAssert
var _current_value_provider: ValueProvider
var _type_check: bool


func _init(current :Variant) -> void:
func _init(current: Variant, type_check := true) -> void:
_type_check = type_check
_current_value_provider = DefaultValueProvider.new(current)
_base = ResourceLoader.load("res://addons/gdUnit4/src/asserts/GdUnitAssertImpl.gd", "GDScript",
ResourceLoader.CACHE_MODE_REUSE).new(current)
Expand Down Expand Up @@ -60,9 +62,17 @@ func max_length(left :Variant, right :Variant) -> int:
return rs if ls < rs else ls


func _array_equals_div(current :Array, expected :Array, case_sensitive :bool = false) -> Array:
func _toPackedStringArray(value :Variant) -> PackedStringArray:
if GdArrayTools.is_array_type(value):
return PackedStringArray(value as Array)
var v := PackedStringArray()
v.append(str(value))
return v


func _array_equals_div(current :Array, expected :Variant, case_sensitive :bool = false) -> Array:
var current_value := PackedStringArray(current)
var expected_value := PackedStringArray(expected)
var expected_value := _toPackedStringArray(expected)
var index_report := Array()
for index in current_value.size():
var c := current_value[index]
Expand Down Expand Up @@ -173,7 +183,7 @@ func is_not_null() -> GdUnitArrayAssert:

# Verifies that the current String is equal to the given one.
func is_equal(expected :Variant) -> GdUnitArrayAssert:
if not _validate_value_type(expected):
if _type_check and not _validate_value_type(expected):
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:
Expand All @@ -189,7 +199,7 @@ func is_equal(expected :Variant) -> GdUnitArrayAssert:

# Verifies that the current Array is equal to the given one, ignoring case considerations.
func is_equal_ignoring_case(expected :Variant) -> GdUnitArrayAssert:
if not _validate_value_type(expected):
if _type_check and not _validate_value_type(expected):
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:
Expand Down
2 changes: 0 additions & 2 deletions addons/gdUnit4/src/asserts/GdUnitAssertImpl.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ func _init(current :Variant) -> void:





func failure_message() -> String:
return _current_failure_message

Expand Down
4 changes: 2 additions & 2 deletions addons/gdUnit4/src/asserts/GdUnitFloatAssertImpl.gd
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func is_not_null() -> GdUnitFloatAssert:
return self


func is_equal(expected :float) -> GdUnitFloatAssert:
func is_equal(expected :Variant) -> GdUnitFloatAssert:
_base.is_equal(expected)
return self


func is_not_equal(expected :float) -> GdUnitFloatAssert:
func is_not_equal(expected :Variant) -> GdUnitFloatAssert:
_base.is_not_equal(expected)
return self

Expand Down
4 changes: 2 additions & 2 deletions addons/gdUnit4/src/asserts/GdUnitIntAssertImpl.gd
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func is_not_null() -> GdUnitIntAssert:
return self


func is_equal(expected :int) -> GdUnitIntAssert:
func is_equal(expected :Variant) -> GdUnitIntAssert:
_base.is_equal(expected)
return self


func is_not_equal(expected :int) -> GdUnitIntAssert:
func is_not_equal(expected :Variant) -> GdUnitIntAssert:
_base.is_not_equal(expected)
return self

Expand Down
15 changes: 8 additions & 7 deletions addons/gdUnit4/src/asserts/GdUnitVectorAssertImpl.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
extends GdUnitVectorAssert

var _base: GdUnitAssert
var _current_type :int
var _current_type: int
var _type_check: bool


func _init(current :Variant) -> void:
func _init(current: Variant, type_check := true) -> void:
_type_check = type_check
_base = ResourceLoader.load("res://addons/gdUnit4/src/asserts/GdUnitAssertImpl.gd", "GDScript",
ResourceLoader.CACHE_MODE_REUSE).new(current)
# save the actual assert instance on the current thread context
Expand Down Expand Up @@ -81,15 +82,15 @@ func is_not_null() -> GdUnitVectorAssert:
return self


func is_equal(expected :Variant) -> GdUnitVectorAssert:
if not _validate_is_vector_type(expected):
func is_equal(expected: Variant) -> GdUnitVectorAssert:
if _type_check and not _validate_is_vector_type(expected):
return self
_base.is_equal(expected)
return self


func is_not_equal(expected :Variant) -> GdUnitVectorAssert:
if not _validate_is_vector_type(expected):
func is_not_equal(expected: Variant) -> GdUnitVectorAssert:
if _type_check and not _validate_is_vector_type(expected):
return self
_base.is_not_equal(expected)
return self
Expand Down
116 changes: 116 additions & 0 deletions addons/gdUnit4/test/asserts/GdUnitVariantAssertThatTest.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
extends GdUnitTestSuite


func test_is_equal_success() -> void:
assert_that(3 as Variant).is_equal(3)
assert_that(3.14 as Variant).is_equal(3.14)
assert_that("3" as Variant).is_equal("3")
assert_that(true as Variant).is_equal(true)
assert_that(Vector2.ONE as Variant).is_equal(Vector2.ONE)
assert_that({a=1, b=2} as Variant).is_equal({a=1, b=2})
assert_that([1,2,3] as Variant).is_equal([1,2,3])
assert_that(RefCounted.new() as Variant).is_equal(RefCounted.new())


func test_is_equal_fail() -> void:
# bool vs int
assert_failure(func()->void:
assert_that(true as Variant).is_equal(1))\
.is_failed()

# bool vs string
assert_failure(func()->void:
assert_that(true as Variant).is_equal("true"))\
.is_failed()

# int vs string
assert_failure(func()->void:
assert_that(3 as Variant).is_equal("3"))\
.is_failed()

# float vs string
assert_failure(func()->void:
assert_that(3.14 as Variant).is_equal("3.14"))\
.is_failed()

# string vs int
assert_failure(func()->void:
assert_that("3" as Variant).is_equal(3))\
.is_failed()

# string vs float
assert_failure(func()->void:
assert_that("3.14" as Variant).is_equal(3.14))\
.is_failed()

# vector vs string
assert_failure(func()->void:
assert_that(Vector2.ONE as Variant).is_equal("ONE"))\
.is_failed()

# dictionary vs string
assert_failure(func()->void:
assert_that({a=1, b=2} as Variant).is_equal("FOO"))\
.is_failed()

# array vs string
assert_failure(func()->void:
assert_that([1,2,3] as Variant).is_equal("FOO"))\
.is_failed()

# object vs string
assert_failure(func()->void:
assert_that(RefCounted.new() as Variant).is_equal("FOO"))\
.is_failed()


func test_is_not_equal_success() -> void:
assert_that(3 as Variant).is_not_equal(4)
assert_that(3.14 as Variant).is_not_equal(3.15)
assert_that("3" as Variant).is_not_equal("33")
assert_that(true as Variant).is_not_equal(false)
assert_that(Vector2.ONE as Variant).is_not_equal(Vector2.UP)
assert_that({a=1, b=2} as Variant).is_not_equal({a=1, b=3})
assert_that([1,2,3] as Variant).is_not_equal([1,2,4])
assert_that(RefCounted.new() as Variant).is_not_equal(null)


func test_is_not_equal_fail() -> void:
# bool vs int
assert_failure(func()->void:
assert_that(true as Variant).is_not_equal(true)
)\
.is_failed()


assert_failure(func()->void:
assert_that(3 as Variant).is_not_equal(3))\
.is_failed()

assert_failure(func()->void:
assert_that(3.14 as Variant).is_not_equal(3.14))\
.is_failed()

assert_failure(func()->void:
assert_that("3" as Variant).is_not_equal("3"))\
.is_failed()

# vector vs string
assert_failure(func()->void:
assert_that(Vector2.ONE as Variant).is_not_equal(Vector2.ONE))\
.is_failed()

# dictionary vs string
assert_failure(func()->void:
assert_that({a=1, b=2} as Variant).is_not_equal({a=1, b=2}))\
.is_failed()

# array vs string
assert_failure(func()->void:
assert_that([1,2,3] as Variant).is_not_equal([1,2,3]))\
.is_failed()

# object vs string
assert_failure(func()->void:
assert_that(RefCounted.new() as Variant).is_not_equal(RefCounted.new()))\
.is_failed()

0 comments on commit 0168576

Please sign in to comment.