Skip to content

Commit

Permalink
feat: let should_fail_with check that the failure reason contains t…
Browse files Browse the repository at this point in the history
…he expected message
  • Loading branch information
asterite committed Jun 24, 2024
1 parent f2f8ecc commit 160a549
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
19 changes: 18 additions & 1 deletion docs/docs/tooling/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,22 @@ fn test_king_arthur() {
fn test_bridgekeeper() {
main(32);
}

```

The string given to `should_fail_with` doesn't need to exactly match the failure reason, it just needs to be a substring of it:

```rust
fn main(african_swallow_avg_speed : Field) {
assert(african_swallow_avg_speed == 65, "What is the airspeed velocity of an unladen swallow");
}

#[test]
fn test_king_arthur() {
main(65);
}

#[test(should_fail_with = "airspeed velocity")]
fn test_bridgekeeper() {
main(32);
}
```
17 changes: 6 additions & 11 deletions test_programs/noir_test_failure/should_fail_mismatch/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
fn test_different_string() {
assert_eq(0, 1, "Different string");
}
// The assert message has a space
#[test(should_fail_with = "Not equal")]
fn test_with_extra_space() {
assert_eq(0, 1, "Not equal ");
}
// The assert message has a space
#[test(should_fail_with = "Not equal")]
fn test_runtime_mismatch() {
// We use a pedersen commitment here so that the assertion failure is only known at runtime.
assert_eq(std::hash::pedersen_commitment([27]).x, 0, "Not equal ");
}

// The failure reason is a substring of the expected message, but it should be the other way around
#[test(should_fail_with = "Definitely Not equal!")]
fn test_wrong_expectation() {
assert_eq(0, 1, "Not equal");
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ fn test_should_fail_with_match() {
assert_eq(0, 1, "Not equal");
}

#[test(should_fail_with = "Not equal")]
fn test_should_fail_with_match_partial_match() {
assert_eq(0, 1, "Definitely Not equal!");
}

#[test(should_fail)]
fn test_should_fail_without_match() {
assert_eq(0, 1);
Expand Down Expand Up @@ -48,6 +53,11 @@ unconstrained fn unconstrained_test_should_fail_with_match() {
assert_eq(0, 1, "Not equal");
}

#[test(should_fail_with = "Not equal")]
unconstrained fn unconstrained_test_should_fail_with_match_partial_match() {
assert_eq(0, 1, "Definitely Not equal!");
}

#[test(should_fail)]
unconstrained fn unconstrained_test_should_fail_without_match() {
assert_eq(0, 1);
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo/src/ops/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn check_expected_failure_message(
};

let expected_failure_message_matches =
matches!(&failed_assertion, Some(message) if message == expected_failure_message);
matches!(&failed_assertion, Some(message) if message.contains(expected_failure_message));
if expected_failure_message_matches {
return TestStatus::Pass;
}
Expand Down

0 comments on commit 160a549

Please sign in to comment.