Skip to content
Open
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
22 changes: 19 additions & 3 deletions addons/gut/test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ func _do_datatypes_match__fail_if_not(got, expected, text):
return did_pass


# Checks if the input is not null. If it is, a fail occurs. Otherwise, TRUE is
# returned.
func _fail_if_null(got, input_name, text):
if(got == null):
_fail(str(input_name, ' cannot be NULL.'))
return false

return true


# Create a string that lists all the methods that were called on an spied
# instance.
func _get_desc_of_calls_to_instance(inst):
Expand Down Expand Up @@ -928,7 +938,10 @@ func assert_ne(got, not_expected, text=""):
## [/codeblock]
func assert_almost_eq(got, expected, error_interval, text=''):
var disp = "[" + _str_precision(got, 20) + "] expected to equal [" + _str(expected) + "] +/- [" + str(error_interval) + "]: " + text
if(_do_datatypes_match__fail_if_not(got, expected, text) and _do_datatypes_match__fail_if_not(got, error_interval, text)):
if(
_fail_if_null(got, '[Got]', text) and _fail_if_null(expected, '[Expected]', text) and _fail_if_null(error_interval, '[Error Interval]', text) and
_do_datatypes_match__fail_if_not(got, expected, text) and _do_datatypes_match__fail_if_not(got, error_interval, text)
):
if not _is_almost_eq(got, expected, error_interval):
_fail(disp)
else:
Expand All @@ -939,7 +952,10 @@ func assert_almost_eq(got, expected, error_interval, text=''):
## outside the range of [param not_expected] +/- [param error_interval].
func assert_almost_ne(got, not_expected, error_interval, text=''):
var disp = "[" + _str_precision(got, 20) + "] expected to not equal [" + _str(not_expected) + "] +/- [" + str(error_interval) + "]: " + text
if(_do_datatypes_match__fail_if_not(got, not_expected, text) and _do_datatypes_match__fail_if_not(got, error_interval, text)):
if(
_fail_if_null(got, '[Got]', text) and _fail_if_null(not_expected, '[Not Expected]', text) and _fail_if_null(error_interval, '[Error Interval]', text) and
_do_datatypes_match__fail_if_not(got, not_expected, text) and _do_datatypes_match__fail_if_not(got, error_interval, text)
):
if _is_almost_eq(got, not_expected, error_interval):
_fail(disp)
else:
Expand All @@ -961,7 +977,7 @@ func _is_almost_eq(got, expected, error_interval) -> bool:

return(result)

## assserts got > expected
## asserts got > expected
## [codeblock]
## var bigger = 5
## var smaller = 0
Expand Down
53 changes: 53 additions & 0 deletions test/unit/test_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,32 @@ class TestAssertAlmostEq:
assert_fail_msg_contains(gr.test, '03450')
assert_fail_msg_contains(gr.test, '03210')

var datatype_data = [
[Vector3(1, 2, 3), 2.0, 3.0],
[1.0, Vector3(1, 2, 3), 3.0],
[1, 2, Vector3(1, 2, 3)],
[Vector3(1, 2, 3), Vector3(1, 2, 3), Vector2(1, 2)],
]
func test_datatype_checks(params = use_parameters(datatype_data)):
gr.test.assert_almost_eq(params[0], params[1], params[2])
assert_engine_error_count(0)
assert_fail(gr.test)
assert_fail_msg_contains(gr.test, "Cannot compare")

var null_data = [
[1, 2, null],
[1, null, 3],
[null, 2, 3],
[1, null, null],
[null, 2, null],
[null, null, 3],
[null, null, null],
]
func test_null_checks(params = use_parameters(null_data)):
gr.test.assert_almost_eq(params[0], params[1], params[2])
assert_engine_error_count(0)
assert_fail(gr.test)
assert_fail_msg_contains(gr.test, "cannot be NULL")

# ------------------------------------------------------------------------------
class TestAssertAlmostNe:
Expand Down Expand Up @@ -416,6 +442,33 @@ class TestAssertAlmostNe:
assert_fail(gr.test)
assert_fail_msg_contains(gr.test, '01230')

var datatype_data = [
[Vector3(1, 2, 3), 2.0, 3.0],
[1.0, Vector3(1, 2, 3), 3.0],
[1, 2, Vector3(1, 2, 3)],
[Vector3(1, 2, 3), Vector3(1, 2, 3), Vector2(1, 2)],
]
func test_datatype_checks(params = use_parameters(datatype_data)):
gr.test.assert_almost_ne(params[0], params[1], params[2])
assert_engine_error_count(0)
assert_fail(gr.test)
assert_fail_msg_contains(gr.test, "Cannot compare")

var null_data = [
[1, 2, null],
[1, null, 3],
[null, 2, 3],
[1, null, null],
[null, 2, null],
[null, null, 3],
[null, null, null],
]
func test_null_checks(params = use_parameters(null_data)):
gr.test.assert_almost_ne(params[0], params[1], params[2])
assert_engine_error_count(0)
assert_fail(gr.test)
assert_fail_msg_contains(gr.test, "cannot be NULL")

# ------------------------------------------------------------------------------
class TestAssertGt:
extends BaseTestClass
Expand Down