-
Notifications
You must be signed in to change notification settings - Fork 446
Add overload for assert functions for unit test #28341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
dd2d5ae
24b1f8f
0a2fd8d
2fc9f1a
d59031b
880e074
42772e4
400dcbb
b30bd55
3e088a0
9374018
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -247,6 +247,33 @@ module UnitTest { | |
| throw new owned AssertionError("assertTrue failed. Given expression is False"); | ||
| } | ||
|
|
||
| /* | ||
| 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 | ||
| */ | ||
| pragma "insert line file info" | ||
| pragma "always propagate line file info" | ||
| proc assertTrue(test: bool, args...?n) throws { | ||
|
jabraham17 marked this conversation as resolved.
|
||
| if !test { | ||
| 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 += " "; | ||
| } | ||
| } | ||
|
|
||
|
jabraham17 marked this conversation as resolved.
Outdated
|
||
| throw new owned AssertionError(msg); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| Assert that ``test`` is `false`. | ||
|
|
||
|
|
@@ -260,6 +287,32 @@ module UnitTest { | |
| throw new owned AssertionError("assertFalse failed. Given expression is True"); | ||
| } | ||
|
|
||
| /* | ||
| Assert that ``test`` is `false`. If it is true, 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 | ||
| */ | ||
| 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"; | ||
|
|
||
| if n > 0 { | ||
| msg += " - "; | ||
| for param i in 0..<n { | ||
| msg += args(i): string; | ||
| if i < n-1 then msg += " "; | ||
| } | ||
| } | ||
|
|
||
| throw new owned AssertionError(msg); | ||
| } | ||
| } | ||
|
|
||
| pragma "insert line file info" | ||
| pragma "always propagate line file info" | ||
| @chpldoc.nodoc | ||
|
|
@@ -474,6 +527,32 @@ module UnitTest { | |
| checkAssertEquality(first, second); | ||
| } | ||
|
|
||
| /* | ||
| Assert that ``first == second``. If they are not 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 assertEqual(first, second, args...?n) throws { | ||
| try { | ||
| checkAssertEquality(first, second); | ||
| } catch e: AssertionError { | ||
| var msg = e.message(); | ||
|
|
||
| if n > 0 { | ||
| msg += " - "; | ||
| for param i in 0..<n { | ||
| msg += args(i): string; | ||
| if i < n-1 then msg += " "; | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is still not indented right
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noting this is not resolved, the indentation is still wrong |
||
|
|
||
| throw new owned AssertionError(msg); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| Assert that x matches the regular expression pattern. | ||
|
|
||
|
|
@@ -562,6 +641,36 @@ module UnitTest { | |
| } | ||
| } | ||
|
|
||
| /* | ||
| 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) { | ||
|
jabraham17 marked this conversation as resolved.
Outdated
|
||
| // Build the base error message | ||
| var tmpString = "assert failed - \n" + first:string + | ||
| "\nequals \n" + second:string; | ||
|
|
||
| // Append additional arguments | ||
| if n > 0 { | ||
| tmpString += " - "; | ||
| for param i in 0..<n { | ||
| tmpString += args(i): string; | ||
| if i < n-1 then tmpString += " "; | ||
| } | ||
| } | ||
|
|
||
| throw new owned AssertionError(tmpString); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| Assert that ``first > second``. | ||
|
|
||
|
|
@@ -581,6 +690,34 @@ module UnitTest { | |
| } | ||
| } | ||
|
|
||
| /* | ||
| Assert that ``first > second``. If ``first <= second``, | ||
| 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 assertGreaterThan(first, second, args...?n) throws { | ||
| if first.type == second.type { | ||
| if first <= second { | ||
| var tmpString = "assert failed - " + first:string + | ||
| " <= " + second:string; | ||
|
|
||
| if n > 0 { | ||
| tmpString += " - "; | ||
| for param i in 0..<n { | ||
| tmpString += args(i): string; | ||
| if i < n-1 then tmpString += " "; | ||
| } | ||
| } | ||
|
|
||
| throw new owned AssertionError(tmpString); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| pragma "insert line file info" | ||
| pragma "always propagate line file info" | ||
| @chpldoc.nodoc | ||
|
|
@@ -801,6 +938,33 @@ module UnitTest { | |
| } | ||
| } | ||
|
|
||
| /* | ||
| Assert that ``first < second``. If ``first >= second``, | ||
| 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 assertLessThan(first, second, args...?n) throws { | ||
| if first.type == second.type { | ||
| if first >= second { | ||
| var tmpString = "assert failed - " + first:string + | ||
| " >= " + second:string; | ||
|
|
||
| if n > 0 { | ||
| tmpString += " - "; | ||
| for param i in 0..<n { | ||
| tmpString += args(i): string; | ||
| if i < n-1 then tmpString += " "; | ||
| } | ||
| } | ||
|
|
||
| throw new owned AssertionError(tmpString); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pragma "insert line file info" | ||
| pragma "always propagate line file info" | ||
| @chpldoc.nodoc | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tested this with regardless, while these tests are fine and should be kept, I think you should also expand tests in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I re-did the good file, compiled and ran the test, and it showed a pass. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| testTrueWithArgs() | ||
| Flavour: OK | ||
| ====================================================================== | ||
| ---------------------------------------------------------------------- | ||
| testFalseWithArgs() | ||
| Flavour: OK | ||
| ====================================================================== | ||
| ---------------------------------------------------------------------- | ||
| testEqualWithArgs() | ||
| Flavour: OK | ||
| ====================================================================== | ||
| ---------------------------------------------------------------------- | ||
| testNotEqualWithArgs() | ||
| Flavour: OK | ||
| ====================================================================== | ||
| ---------------------------------------------------------------------- | ||
| testGreaterThanWithArgs() | ||
| Flavour: OK | ||
| ====================================================================== | ||
| ---------------------------------------------------------------------- | ||
| testLessThanWithArgs() | ||
| Flavour: OK | ||
| ====================================================================== | ||
| ---------------------------------------------------------------------- | ||
| EOF |
|
stoutes marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); |
Uh oh!
There was an error while loading. Please reload this page.