Skip to content

Commit

Permalink
Implement assert_lte and assert_gte (#625)
Browse files Browse the repository at this point in the history
* Implement `assert_lte` and `assert_gte`
* fix troubleshooting change
* Parameterize `gt`, `gte`, `lt`, `lte` assert tests
* add docs
* separate out tests again to individual classes and add float tests
  • Loading branch information
nycdotnet authored Jun 21, 2024
1 parent bf2c09f commit 874dddc
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 29 deletions.
22 changes: 22 additions & 0 deletions addons/gut/test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,17 @@ func assert_gt(got, expected, text=""):
else:
_fail(disp)

# ------------------------------------------------------------------------------
# Asserts got is greater than or equal to expected
# ------------------------------------------------------------------------------
func assert_gte(got, expected, text=""):
var disp = "[" + _str(got) + "] expected to be >= than [" + _str(expected) + "]: " + text
if(_do_datatypes_match__fail_if_not(got, expected, text)):
if(got >= expected):
_pass(disp)
else:
_fail(disp)

# ------------------------------------------------------------------------------
# Asserts got is less than expected
# ------------------------------------------------------------------------------
Expand All @@ -393,6 +404,17 @@ func assert_lt(got, expected, text=""):
else:
_fail(disp)

# ------------------------------------------------------------------------------
# Asserts got is less than or equal to expected
# ------------------------------------------------------------------------------
func assert_lte(got, expected, text=""):
var disp = "[" + _str(got) + "] expected to be <= than [" + _str(expected) + "]: " + text
if(_do_datatypes_match__fail_if_not(got, expected, text)):
if(got <= expected):
_pass(disp)
else:
_fail(disp)

# ------------------------------------------------------------------------------
# asserts that got is true
# ------------------------------------------------------------------------------
Expand Down
37 changes: 37 additions & 0 deletions documentation/docs/Asserts-and-Methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ assert_gt(1.0, 1) # FAIL
assert_gt(smaller, bigger) # FAIL
```

### assert_gte
`assert_gte(got, expected, text="")`<br>
assserts got >= expected
``` gdscript
var bigger = 5
var smaller = 0
gut.p('-- passing --')
assert_gte(bigger, smaller, 'Bigger should be greater than or equal to smaller') # PASS
assert_gte('b', 'a') # PASS
assert_gte('a', 'A') # PASS
assert_gte(1.1, 1) # PASS
assert_gte('a', 'a') # PASS
gut.p('-- failing --')
assert_gte(0.9, 1.0) # FAIL
assert_gte(smaller, bigger) # FAIL
```

### assert_lt
`assert_lt(got, expected, text="")`<br>
asserts got < expected
Expand All @@ -142,12 +161,30 @@ var smaller = 0
gut.p('-- passing --')
assert_lt(smaller, bigger, 'Smaller should be less than bigger') # PASS
assert_lt('a', 'b') # PASS
assert_lt(99, 100) # PASS
gut.p('-- failing --')
assert_lt('z', 'x') # FAIL
assert_lt(-5, -5) # FAIL
```

### assert_lte
`assert_lte(got, expected, text="")`<br>
asserts got <= expected
``` gdscript
var bigger = 5
var smaller = 0
gut.p('-- passing --')
assert_lte(smaller, bigger, 'Smaller should be less than or equal to bigger') # PASS
assert_lte('a', 'b') # PASS
assert_lte(1.0, 1.0) # PASS
assert_lte(-5, -5) # PASS
gut.p('-- failing --')
assert_lte('z', 'x') # FAIL
assert_lte(1.1, 1.0) # FAIL
```

### assert_true
`assert_true(got, text="")`<br>
asserts got == true.
Expand Down
122 changes: 93 additions & 29 deletions test/unit/test_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class BaseTestClass:
test_with_gut = null
}

const SHOULD_PASS = &"Should pass"
const SHOULD_FAIL = &"Should fail"

func print_fail_pass_text(t):
for i in range(t._fail_pass_text.size()):
gut.p('sub-test: ' + t._fail_pass_text[i], gut.LOG_LEVEL_FAIL_ONLY)
Expand Down Expand Up @@ -428,49 +431,110 @@ class TestAssertAlmostNe:
assert_fail(gr.test)
assert_fail_msg_contains(gr.test, '01230')



# ------------------------------------------------------------------------------
class TestAssertGt:
extends BaseTestClass

func test_passes_with_greater_integer():
gr.test.assert_gt(2, 1, "Should Pass")
assert_pass(gr.test, 1, '2 > 1')
# The assert_gt method asserts that `got` is greater than `expected`.
var test_data = ParameterFactory.named_parameters(
['got', 'expected', 'pass_fail_gt'],
[
[2, 1, SHOULD_PASS],
[2, 2, SHOULD_FAIL],
[2, 3, SHOULD_FAIL],
[2.2, 2.1, SHOULD_PASS],
[2.2, 2.2, SHOULD_FAIL],
[2.2, 2.3, SHOULD_FAIL],
['B', 'A', SHOULD_PASS],
['B', 'B', SHOULD_FAIL],
['B', 'C', SHOULD_FAIL]
])

func test_assert_gt(p = use_parameters(test_data)):
gr.test.assert_gt(p.got, p.expected, p.pass_fail_gt)
if (p.pass_fail_gt == SHOULD_PASS):
assert_pass(gr.test, 1, str(p.got, ' > ', p.expected))
else:
assert_fail(gr.test, 1, str(p.got, ' > ', p.expected))

func test_fails_with_less_than_integer():
gr.test.assert_gt(1, 2, "Should fail")
assert_fail(gr.test, 1, '1 < 2')

func test_passes_with_greater_string():
gr.test.assert_gt("b", "a", "Should Pass")
assert_pass(gr.test)

func test_fails_with_less_than_string():
gr.test.assert_gt("a", "b", "Should Fail")
assert_fail(gr.test)
# ------------------------------------------------------------------------------
class TestAssertGte:
extends BaseTestClass

# The assert_gte method asserts that `got` is greater than or equal to `expected`.
var test_data = ParameterFactory.named_parameters(
['got', 'expected', 'pass_fail_gte'],
[
[2, 1, SHOULD_PASS],
[2, 2, SHOULD_PASS],
[2, 3, SHOULD_FAIL],
[2.2, 2.1, SHOULD_PASS],
[2.2, 2.2, SHOULD_PASS],
[2.2, 2.3, SHOULD_FAIL],
['B', 'A', SHOULD_PASS],
['B', 'B', SHOULD_PASS],
['B', 'C', SHOULD_FAIL]
])

func test_assert_gte(p = use_parameters(test_data)):
gr.test.assert_gte(p.got, p.expected, p.pass_fail_gte)
if (p.pass_fail_gte == SHOULD_PASS):
assert_pass(gr.test, 1, str(p.got, ' >= ', p.expected))
else:
assert_fail(gr.test, 1, str(p.got, ' >= ', p.expected))

# ------------------------------------------------------------------------------
class TestAssertLt:
extends BaseTestClass

func test_number_with_lt():
gr.test.assert_lt(1, 2, "Should Pass")
assert_pass(gr.test, 1, '1 < 2')

func test_number_with_gt():
gr.test.assert_lt(2, 1, "Should fail")
assert_fail(gr.test, 1, '2 > 1')

func test_string_with_lt():
gr.test.assert_lt("a", "b", "Should Pass")
assert_pass(gr.test)
# The assert_lt method asserts that `got` is less than `expected`.
var test_data = ParameterFactory.named_parameters(
['got', 'expected', 'pass_fail_lt'],
[
[2, 1, SHOULD_FAIL],
[2, 2, SHOULD_FAIL],
[2, 3, SHOULD_PASS],
[2.2, 2.1, SHOULD_FAIL],
[2.2, 2.2, SHOULD_FAIL],
[2.2, 2.3, SHOULD_PASS],
['B', 'A', SHOULD_FAIL],
['B', 'B', SHOULD_FAIL],
['B', 'C', SHOULD_PASS]
])

func test_assert_lt(p = use_parameters(test_data)):
gr.test.assert_lt(p.got, p.expected, p.pass_fail_lt)
if (p.pass_fail_lt == SHOULD_PASS):
assert_pass(gr.test, 1, str(p.got, ' < ', p.expected))
else:
assert_fail(gr.test, 1, str(p.got, ' < ', p.expected))

func test_string_with_gt():
gr.test.assert_lt("b", "a", "Should Fail")
assert_fail(gr.test)
# ------------------------------------------------------------------------------
class TestAssertLte:
extends BaseTestClass

# The assert_lte method asserts that `got` is less than or equal to `expected`.
var test_data = ParameterFactory.named_parameters(
['got', 'expected', 'pass_fail_lte'],
[
[2, 1, SHOULD_FAIL],
[2, 2, SHOULD_PASS],
[2, 3, SHOULD_PASS],
[2.2, 2.1, SHOULD_FAIL],
[2.2, 2.2, SHOULD_PASS],
[2.2, 2.3, SHOULD_PASS],
['B', 'A', SHOULD_FAIL],
['B', 'B', SHOULD_PASS],
['B', 'C', SHOULD_PASS]
])

func test_assert_lte(p = use_parameters(test_data)):
gr.test.assert_lte(p.got, p.expected, p.pass_fail_lte)
if (p.pass_fail_lte == SHOULD_PASS):
assert_pass(gr.test, 1, str(p.got, ' <= ', p.expected))
else:
assert_fail(gr.test, 1, str(p.got, ' <= ', p.expected))

# ------------------------------------------------------------------------------
# TODO rename tests since they are now in an inner class. See NOTE at top about naming.
Expand Down

0 comments on commit 874dddc

Please sign in to comment.