garanti/expect

Assertion matchers for garanti tests.

Most functions takes an actual and an expected value and returns a garanti.AssertionResult with the result. On failure, the result also contains a list of garanti.Expectations with structured detail (e.g. Actual/Expected, or Missing/Extra for collections) for reporters to display together with the summary message.

Ordering matchers (to_be_greater, to_be_less, and their _or_equal variants) only report the Actual value, since the comparison itself is already captured in the failure summary.

Values

pub fn all(
  results: List(garanti.AssertionResult),
) -> garanti.AssertionResult

Combine multiple asserts into one result.

Useful for combining multiple related asserts. All asserts are executed.

  • If all passed results are passing, a passing result is returned.
  • If any passed results are failing, only the failing results are returned.
  • If the list only contain one result, that result is returned.

Examples

expect.all([expect.to_be_equal(1, 1), expect.to_be_equal(2, 2)])
// -> Pass

expect.all([
 expect.to_be_equal(1, 1),
 expect.to_be_equal(1, 2),
])
// -> Fail("A combined test failed:", [
//  NestedTestFailure("Expected 1 to equal 2", [...])
// ])

a pass is retured. I

pub fn to_be_empty(actual: List(a)) -> garanti.AssertionResult

Asserts that a list has no elemenets.

Examples

expect.to_be_empty([])
// -> Pass

expect.to_be_empty([1, 2, 3])
// -> Fail("Expected list to be empty:")
pub fn to_be_equal(
  actual: a,
  expected: a,
) -> garanti.AssertionResult

Asserts that two values of the same type are equal.

Examples

expect.to_be_equal(1, 1)
// -> Pass

expect.to_be_equal("hello", "world")
// -> Fail("Expected \"hello\" to equal \"world\".")
pub fn to_be_equivalent(
  actual: List(a),
  expected: List(a),
) -> garanti.AssertionResult

Asserts that two lists contain the same elements, regardless of order.

Each element is matched by equality, and duplicates are considered. I.e. [1, 1, 2] is not equivalent to [1, 2].

Note: this assertion compares every element in both lists, so avoid it for very large lists where performance matters.

Examples

expect.to_be_equivalent([1, 2, 3], [3, 1, 2])
// -> Pass

expect.to_be_equivalent([1, 2], [1, 2, 3])
// -> Fail("Expect to be equivalent:")

expect.to_be_equivalent([1, 2, 3], [1, 2])
// -> Fail("Expect to be equivalent:")
pub fn to_be_error(
  actual: Result(a, b),
) -> garanti.AssertionResult

Asserts that the actual value is anError.

This is a blunt test as the value of Error is not asserted. Using to_be_error_then will provide more robust test.

Examples

expect.to_be_error(Error(Nil))
// -> Pass

expect.to_be_error(Ok(42))
// -> Fail("Expected actual to be Error but it was an Ok of 42.")
pub fn to_be_error_then(
  actual: Result(a, b),
  t: fn(b) -> garanti.AssertionResult,
) -> garanti.AssertionResult

Asserts that the actual value is an Error, then runs further assertions on the inner value via a callback.

Use this when you want to both verify a Result is Error and make additional assertions on the unwrapped value in a single expression.

Examples

expect.to_be_error_then(Error(404), fn(n) { expect.to_be_equal(n, 404) })
// -> Pass

// Or alternative
use value <- expect.to_be_error_then(Error(404))
expect.to_be_equal(value, 404)
// -> Pass

expect.to_be_error_then(Error(418), fn(n) { expect.to_be_equal(n, 404) })
// -> Fail("Expected 418 to equal 404.")
pub fn to_be_greater(
  actual: a,
  expected: a,
  compare: fn(a, a) -> order.Order,
) -> garanti.AssertionResult

Assert that the actual value is greater than the expected value.

Examples

expect.to_be_greater(3, 2, int.compare)
// -> Pass

expect.to_be_greater(2, 3, int.compare)
// -> Fail("Expected 2 to be greater than 3")
pub fn to_be_greater_or_equal(
  actual: a,
  expected: a,
  compare: fn(a, a) -> order.Order,
) -> garanti.AssertionResult

Assert that the actual value is greater than or equal to the expected value.

Examples

expect.to_be_greater_or_equal(2, 2)
// -> Pass

expect.to_be_greater_or_equal(2, 3)
// -> Fail("Expected 2 to be greater than or equal to 3")
pub fn to_be_less(
  actual: a,
  expected: a,
  compare: fn(a, a) -> order.Order,
) -> garanti.AssertionResult

Assert that the actual value is less than the expected value.

Examples

expect.to_be_less(2, 3)
// -> Pass

expect.to_be_less(3, 2)
// -> Fail("Expected 3 to be less than 2")
pub fn to_be_less_or_equal(
  actual: a,
  expected: a,
  compare: fn(a, a) -> order.Order,
) -> garanti.AssertionResult

Assert that the actual value is less than or equal to the expected value.

Examples

expect.to_be_less_or_equal(2, 2)
// -> Pass

expect.to_be_less_or_equal(2, 3)
// -> Fail("Expected 2 to be less than or equal to 3")
pub fn to_be_none(
  actual: option.Option(a),
) -> garanti.AssertionResult

Assert that the given option is None.

Examples

expect.to_be_none(option.None)
// -> Pass

expect.to_be_none(option.Some("hello"))
// -> Fail("Expected \"hello\" to be None.")
pub fn to_be_ok(actual: Result(a, b)) -> garanti.AssertionResult

Asserts that the actual value is Ok.

This is a blunt test as the value of Ok is not asserted. Using to_be_ok_then will provide more robust test.

Examples

expect.to_be_ok(Ok(42))
// -> Pass

expect.to_be_ok(Error(Nil))
// -> Fail("Expected actual to be Ok but it was an Error of Nil.")
pub fn to_be_ok_then(
  actual: Result(a, b),
  t: fn(a) -> garanti.AssertionResult,
) -> garanti.AssertionResult

Asserts that the actual value is Ok, then runs further assertions on the inner value via a callback.

Use this when you want to both verify a Result is Ok and make additional assertions on the unwrapped value in a single expression.

Examples

expect.to_be_ok_then(Ok(42), fn(n) { expect.to_be_equal(n, 42) })
// -> Pass

// Or alternative
use value <- expect.to_be_ok_then(Ok(42))
expect.to_be_equal(value, 42)
// -> Pass

expect.to_be_ok_then(Ok(42), fn(n) { expect.to_be_equal(n, 0) })
// -> Fail("Expected 42 to equal 0.")
pub fn to_be_some(
  actual: option.Option(a),
  expected: a,
) -> garanti.AssertionResult

Asserts that the actual value is a Some of an expected value.

Examples

expect.to_be_some(option.Some(1), 1)
// -> Pass

expect.to_be_some(option.None, "world")
// -> Fail("Expected None to be \"world\".")
pub fn to_contain(
  actual: List(a),
  expected: a,
) -> garanti.AssertionResult

Assert that a list contains a given element.

Examples

expect.to_contain([1, 2, 3], 2)
// -> Pass

expect.to_contain([1, 2, 3], 4)
// -> Fail("Expected list to contain 4 but contained [1, 2, 3]")
pub fn to_not_be_equal(
  actual: a,
  expected: a,
) -> garanti.AssertionResult

Asserts that two values of the same type are NOT equal.

Examples

expect.to_not_be_equal(1, 2)
// -> Pass

expect.to_not_be_equal("hello", "hello")
// -> Fail("Expected \"hello\" to NOT equal \"hello\".")
Search Document