Skip to content

Conversation

@stoutes
Copy link

@stoutes stoutes commented Jan 24, 2026

Resolves #28311

This feature adds the overloads for assert functions:

  1. assertTrue
  2. assertFalse
  3. assertEqual
  4. assertNotEqual
  5. assertGreaterThan
  6. assertLessThan

This will allow additional args on an assert failure to be printed.

Also added unit test files in UnitTest.

Comment on lines +262 to +272
var msg = "assertTrue failed. Given expression is False";

// Append additional arguments to error message
if n > 0 {
msg += " - ";
for param i in 0..<n {
msg += args(i): string;
if i < n-1 then msg += " ";
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking inspiration from the existing Errors.chpl assert code, this can be simplified to just use chpl_stringify_wrapper. This will make it more consistent with the behavior of assert and writeln

i.e.

var msg = "assertTrue failed. Given expression is False - " + chpl_stringify_wrapper((...args));
throw new AssertionError(msg);

*/
pragma "insert line file info"
pragma "always propagate line file info"
proc assertTrue(test: bool, args...?n) throws {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, note that n is always greater than 0, so no need to have the if statement

Comment on lines +644 to +654
/*
Assert that ``first != second``. If they are equal,
prints the two values along with any additional arguments provided.

:arg first: The first object to compare
:arg second: The second object to compare
:arg args: additional values to print on failure
*/
proc assertNotEqual(first, second, args...?n) throws {
if first.type == second.type {
if all(first == second) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementations of assertNotEqual, assertGreaterThan, and assertLessThan are not right, they should be using the same logic as the existing overloads.

Comment on lines +250 to +257
/*
Assert that ``test`` is `true`. If it is false, prints
'assert failed' along with any additional arguments provided.

:arg test: the boolean condition
:type test: `bool`
:arg args: additional values to print on failure
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is false, prints 'assert failed' along with any additional arguments provided.

This looks copied from the Errors module, it should be adjusted for this use case.

e..g, "If it is false, adds the args to the thrown errors message as if those args were printed using :proc:`~IO.write()`"

This should also have the :throws: tag

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested this with start_test test/library/packages/UnitTest/assertWithArgs.chpl? It doesn't look as if this good file will pass?

regardless, while these tests are fine and should be kept, I think you should also expand tests in test/library/packages/UnitTest/Assert*/*.chpl tests to include your new overloads, as those are more exhaustive in terms of types passed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a .good file

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.

[Feature Request]: Add UnitTest.assert* overloads with extra information

2 participants