Skip to content

Commit

Permalink
Add try_equal assertion to std::test
Browse files Browse the repository at this point in the history
This assertion calls a closure which may throw, and compares its value
against an expected value. This is useful when testing the return value
of a throwing method.

Changelog: added
  • Loading branch information
yorickpeterse committed Oct 5, 2022
1 parent 6620c44 commit 2609bbc
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions libstd/src/std/test.inko
Expand Up @@ -165,6 +165,21 @@ class pub Test {
@failures.push(Failure.new(format(err), 'no value is thrown'))
}
}

# Asserts that an expression (which may throw) equals the given value.
fn pub mut try_equal[T: Equal + fmt::Format, E: fmt::Format](
block: fn !! E -> T,
expected: ref T
) {
let got = try block.call else (err) {
@failures.push(Failure.new(format(err), format(expected)))
return
}

if got == expected { return }

@failures.push(Failure.new(format(got), format(expected)))
}
}

# A type used for reporting test progress.
Expand Down

0 comments on commit 2609bbc

Please sign in to comment.