diff --git a/modules/packages/UnitTest.chpl b/modules/packages/UnitTest.chpl index 7241efafce9a..1a211af736da 100644 --- a/modules/packages/UnitTest.chpl +++ b/modules/packages/UnitTest.chpl @@ -247,6 +247,27 @@ module UnitTest { throw new owned AssertionError("assertTrue failed. Given expression is False"); } + /* + Assert that ``test`` is `true`. If it is false, adds the ``args`` to + the thrown error's ``message`` as if those args were printed using + :proc:`~IO.write()`. + + :arg test: the boolean condition + :type test: `bool` + :arg args: additional values to print on failure + :throws: AssertionError if the assertion fails + */ + pragma "insert line file info" + pragma "always propagate line file info" + pragma "insert line file info" + pragma "always propagate line file info" + proc assertTrue(test: bool, args...?n) throws { + if !test { + var msg = "assertTrue failed. Given expression is False - " + chpl_stringify_wrapper((...args)); + throw new owned AssertionError(msg); + } + } + /* Assert that ``test`` is `false`. @@ -260,6 +281,25 @@ module UnitTest { throw new owned AssertionError("assertFalse failed. Given expression is True"); } + /* + Assert that ``test`` is `false`. If it is true, adds the ``args`` to + the thrown error's ``message`` as if those args were printed using + :proc:`~IO.write()`. + + :arg test: the boolean condition + :type test: `bool` + :arg args: additional values to print on failure + :throws: AssertionError if the assertion fails + */ + pragma "insert line file info" + pragma "always propagate line file info" + proc assertFalse(test: bool, args...?n) throws { + if test { + var msg = "assertFalse failed. Given expression is True - " + chpl_stringify_wrapper((...args)); + throw new owned AssertionError(msg); + } + } + pragma "insert line file info" pragma "always propagate line file info" @chpldoc.nodoc @@ -474,6 +514,25 @@ module UnitTest { checkAssertEquality(first, second); } + /* + Assert that ``first == second``. If they are not equal, adds the ``args`` + to the thrown error's ``message`` as if those args were printed using + :proc:`~IO.write()`. + + :arg first: The first object to compare + :arg second: The second object to compare + :arg args: additional values to print on failure + :throws: AssertionError if the assertion fails + */ + proc assertEqual(first, second, args...?n) throws { + try { + checkAssertEquality(first, second); + } catch e: AssertionError { + var msg = e.message() + " - " + chpl_stringify_wrapper((...args)); + throw new owned AssertionError(msg); + } + } + /* Assert that x matches the regular expression pattern. @@ -562,6 +621,26 @@ module UnitTest { } } + /* + Assert that ``first != second``. If they are equal, adds the ``args`` + to the thrown error's ``message`` as if those args were printed using + :proc:`~IO.write()`. + + :arg first: The first object to compare + :arg second: The second object to compare + :arg args: additional values to print on failure + :throws: AssertionError if the assertion fails + */ + proc assertNotEqual(first, second, args...?n) throws { + if canResolve("!=", first, second) { + if !checkAssertInequality(first, second) { + throw new owned AssertionError("assert failed -\n'%?'\n==\n'%?' - ".format(first, second) + + chpl_stringify_wrapper((...args))); + } + } + } + + /* Assert that ``first > second``. @@ -581,6 +660,31 @@ module UnitTest { } } + /* + Assert that ``first > second``. If ``first <= second``, adds the ``args`` + to the thrown error's ``message`` as if those args were printed using + :proc:`~IO.write()`. + + :arg first: The first object to compare + :arg second: The second object to compare + :arg args: additional values to print on failure + :throws: AssertionError if the assertion fails + */ + proc assertGreaterThan(first, second, args...?n) throws { + if canResolve(">=", first, second) { + try { + checkGreater(first, second); + } catch e: AssertionError { + throw new owned AssertionError(e.message() + " - " + chpl_stringify_wrapper((...args))); + } + } + else { + throw new owned AssertionError("assert failed - First element is of type %? and Second is of type %? - ".format(first.type:string, second.type:string) + + chpl_stringify_wrapper((...args))); + } + } + + pragma "insert line file info" pragma "always propagate line file info" @chpldoc.nodoc @@ -801,6 +905,30 @@ module UnitTest { } } + /* + Assert that ``first < second``. If ``first >= second``, adds the ``args`` + to the thrown error's ``message`` as if those args were printed using + :proc:`~IO.write()`. + + :arg first: The first object to compare + :arg second: The second object to compare + :arg args: additional values to print on failure + :throws: AssertionError if the assertion fails + */ + proc assertLessThan(first, second, args...?n) throws { + if canResolve("<=", first, second) { + try { + checkLessThan(first, second); + } catch e: AssertionError { + throw new owned AssertionError(e.message() + " - " + chpl_stringify_wrapper((...args))); + } + } + else { + throw new owned AssertionError("assert failed - First element is of type %? and Second is of type %? - ".format(first.type:string, second.type:string) + + chpl_stringify_wrapper((...args))); + } + } + pragma "insert line file info" pragma "always propagate line file info" @chpldoc.nodoc diff --git a/test/library/packages/UnitTest/assertWithArgs.chpl b/test/library/packages/UnitTest/assertWithArgs.chpl new file mode 100644 index 000000000000..7bb411532d67 --- /dev/null +++ b/test/library/packages/UnitTest/assertWithArgs.chpl @@ -0,0 +1,99 @@ +// assertWithArgs.chpl +use UnitTest; + +// Test assertTrue with extra args +proc testTrueWithArgs(test: borrowed Test) throws { + var x = 5; + test.assertTrue(true, "This should pass with extra args:", x); + + // This should fail and show the message + try { + test.assertTrue(false, "Expected value:", x, "to be true"); + test.assertTrue(false, "Should not reach here"); + } catch e: AssertionError { + // Expected to fail - check message contains our args + test.assertTrue(e.message().find("Expected value:") >= 0); + } +} + +// Test assertFalse with extra args +proc testFalseWithArgs(test: borrowed Test) throws { + var y = 10; + test.assertFalse(false, "This should pass with extra args:", y); + + try { + test.assertFalse(true, "Expected false but got true, value:", y); + test.assertTrue(false, "Should not reach here"); + } catch e: AssertionError { + test.assertTrue(e.message().find("value:") >= 0); + } +} + +// Test assertEqual with extra args +proc testEqualWithArgs(test: borrowed Test) throws { + var a = 5, b = 5, c = 10; + test.assertEqual(a, b, "Values match:", a, "and", b); + + try { + test.assertEqual(a, c, "Expected a=", a, "to equal c=", c); + test.assertTrue(false, "Should not reach here"); + } catch e: AssertionError { + test.assertTrue(e.message().find("Expected a=") >= 0); + } +} + +// Test assertNotEqual with extra args +proc testNotEqualWithArgs(test: borrowed Test) throws { + var x = 5, y = 10; + test.assertNotEqual(x, y, "Values differ:", x, "and", y); + + try { + test.assertNotEqual(x, x, "Expected different values, got:", x); + test.assertTrue(false, "Should not reach here"); + } catch e: AssertionError { + test.assertTrue(e.message().find("Expected different") >= 0); + } +} + +// Test assertGreaterThan with extra args +proc testGreaterThanWithArgs(test: borrowed Test) throws { + var x = 10, y = 5; + test.assertGreaterThan(x, y, "x=", x, "is greater than y=", y); + + try { + test.assertGreaterThan(y, x, "Expected y=", y, "> x=", x); + test.assertTrue(false, "Should not reach here"); + } catch e: AssertionError { + test.assertTrue(e.message().find("Expected y=") >= 0); + } +} + +// Test assertLessThan with extra args +proc testLessThanWithArgs(test: borrowed Test) throws { + var x = 5, y = 10; + test.assertLessThan(x, y, "x=", x, "is less than y=", y); + + try { + test.assertLessThan(y, x, "Expected y=", y, "< x=", x); + test.assertTrue(false, "Should not reach here"); + } catch e: AssertionError { + test.assertTrue(e.message().find("Expected y=") >= 0); + } +} + +// Test with multiple types +proc testMixedTypes(test: borrowed Test) throws { + var i = 42; + var s = "hello"; + var r = 3.14; + + test.assertTrue(true, "Mixed types:", i, s, r); + + try { + test.assertEqual(i, 100, "int:", i, "string:", s, "real:", r); + } catch e: AssertionError { + test.assertTrue(e.message().find("string:") >= 0); + } +} + +UnitTest.main(); diff --git a/test/library/packages/UnitTest/assertWithArgs.good b/test/library/packages/UnitTest/assertWithArgs.good new file mode 100644 index 000000000000..140b2d31a688 --- /dev/null +++ b/test/library/packages/UnitTest/assertWithArgs.good @@ -0,0 +1,25 @@ +testTrueWithArgs() +Flavour: OK +====================================================================== +---------------------------------------------------------------------- +testFalseWithArgs() +Flavour: OK +====================================================================== +---------------------------------------------------------------------- +testEqualWithArgs() +Flavour: OK +====================================================================== +---------------------------------------------------------------------- +testNotEqualWithArgs() +Flavour: OK +====================================================================== +---------------------------------------------------------------------- +testGreaterThanWithArgs() +Flavour: OK +====================================================================== +---------------------------------------------------------------------- +testLessThanWithArgs() +Flavour: OK +====================================================================== +---------------------------------------------------------------------- +EOF \ No newline at end of file diff --git a/test/library/packages/UnitTest/assertWithArgsFailure.chpl b/test/library/packages/UnitTest/assertWithArgsFailure.chpl new file mode 100644 index 000000000000..a9f46aea0aa8 --- /dev/null +++ b/test/library/packages/UnitTest/assertWithArgsFailure.chpl @@ -0,0 +1,9 @@ +use UnitTest; + +proc testFailureMessage(test: borrowed Test) throws { + var x = 5, y = 10; + // This will fail and show our custom message + test.assertEqual(x, y, "Expected x=", x, "to equal y=", y); +} + +UnitTest.main(); \ No newline at end of file diff --git a/test/library/packages/UnitTest/assertWithArgsFailure.good b/test/library/packages/UnitTest/assertWithArgsFailure.good new file mode 100644 index 000000000000..182859d860c4 --- /dev/null +++ b/test/library/packages/UnitTest/assertWithArgsFailure.good @@ -0,0 +1,5 @@ +testFailureMessage() +Flavour: FAIL +====================================================================== +AssertionError: in assertWithArgsFailure.chpl:6 - in assertWithArgsFailure.chpl:6 - assert failed - '5' != '10' - Expected x=5to equal y=10 +----------------------------------------------------------------------