Skip to content

Add null checks in assert_almost_* methods - #856

Open
CodeSmith32 wants to merge 3 commits into
bitwes:mainfrom
CodeSmith32:patch-1
Open

Add null checks in assert_almost_* methods#856
CodeSmith32 wants to merge 3 commits into
bitwes:mainfrom
CodeSmith32:patch-1

Conversation

@CodeSmith32

Copy link
Copy Markdown
  • pre-check inputs for nulls before running comparison: report nulls as failed tests vs crashing

This closes issue #855
Please feel free to edit this PR / reply here if I'm missing anything.

e.g., I'd assume that nulls in the inputs should just be reported as failed tests vs. crashing, but if not, that's fine. (Though, I'll at least keep these changes in my own project.)


Note here:

This PR causes passing null for got, expected, or error_interval to either assert_almost_eq or assert_almost_ne to fail the test. I'd assume both cases are correct, but, to be clear, this does mean that assert_almost_ne(null, null, 1) fails rather than passes. If that sounds wrong, let me know, or just edit it. Either way, maybe we add a note to the docs for these methods?

Thanks for this amazing tool!

- pre-check inputs for nulls before running comparison: report nulls as failed tests vs crashing
@bitwes

bitwes commented Aug 18, 2026

Copy link
Copy Markdown
Owner

Needs some tests. I got curious about a couple other conditions and came up with the following test that can be expanded later if needed. Please add the following to res://test/unit/test_test.gd under TestAssertAlmostEq (around line number 345)

	var datatype_data = [
		[1, 2, null],
		[1, null, 3],
		[null, 2, 3],
	]
	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)

- write tests for assert_almost_eq / assert_almost_ne when inputs are different types or null
@CodeSmith32

Copy link
Copy Markdown
Author

Thanks @bitwes!

I've added those tests in. I made it a little more thorough:

  • I added tests for all permutations of null in the parameters.
  • I also wrote a test for different parameter types (not precisely related, but I thought still deserved), and asserted that such tests fail with "Cannot compare..." and don't crash (to prevent regressions)
  • Lastly, I added the same tests for assert_almost_ne.

I can change/remove these if needed. Just let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants