View Source Bond.Test (Bond v0.13.0)
ExUnit helpers for asserting that Bond contract violations are raised.
Each macro wraps the given expression in an ExUnit.Assertions.assert_raise/2 that expects
the matching Bond.PreconditionError / Bond.PostconditionError / Bond.CheckError, and
optionally checks fields on the raised exception against a keyword of expected values.
Field expectations may be exact values or Regex patterns. Regexes are matched against the
string form of the field (the exception's :expression field is a string; the :file field
is also a string). Any other expected value must be equal (via ==) to the field's value.
Usage
defmodule MyAppTest do
use ExUnit.Case
use Bond.Test
alias MyApp.Math
test "rejects negative input" do
assert_precondition_violation(Math.sqrt(-1), label: :non_negative_x)
end
test "rejects non-numeric input with a regex on the expression" do
assert_precondition_violation(Math.sqrt("NaN"),
label: :numeric_x,
expression: ~r/is_number/
)
end
test "postcondition violation when result is not a float" do
assert_postcondition_violation(Math.sqrt(2, fn _ -> 10 end),
module: MyApp.Math,
function: {:sqrt, 2}
)
end
endEach helper returns the raised exception struct so further assertions can be made on it:
error = assert_precondition_violation(Math.sqrt(-1))
assert error.binding[:x] == -1
Summary
Functions
Convenience: use Bond.Test is equivalent to import Bond.Test.
Asserts that the given call raises a Bond.CheckError.
Asserts that the given call raises a Bond.InvariantError.
Asserts that the given call raises a Bond.PostconditionError.
Asserts that the given call raises a Bond.PreconditionError.
Functions
Convenience: use Bond.Test is equivalent to import Bond.Test.
Asserts that the given call raises a Bond.CheckError.
See assert_precondition_violation/2 for details.
Asserts that the given call raises a Bond.InvariantError.
See assert_precondition_violation/2 for details.
Asserts that the given call raises a Bond.PostconditionError.
See assert_precondition_violation/2 for details.
Asserts that the given call raises a Bond.PreconditionError.
Returns the raised exception struct. Optional opts is a keyword of expected field
values (or Regex patterns) checked against the raised exception. See the module docs
for the supported fields and matching rules.