Skip to main content

Crate lontra_asserts

Crate lontra_asserts 

Source
Expand description

§lontra-asserts

This crate contains a number of helpers that are useful for writing test.

This crate is part of the lontra project.

§Functionality

§assert_matches!

Allows inline match-style checks where

  • deref! uses Deref-coercion of the left hand side expression.
  • eq! allows inline equality comparison with a named variable.
  • Literals are compared via == for better compatibility with compatible types that don’t support a native match, such as Path.
use thiserror::Error;

#[derive(Debug, Error)]
enum Error {
  #[error("bad int {0}")]
  Int(i32),
  #[error("bad string {0}")]
  String(String),
  #[error("something is bad")]
  Nested(#[source] Box<Error>),
  #[error("bad struct {a} {b}")]
  Struct {
    a: i32,
    b: String,
    #[source]
    c: Box<Error>,
  }
}

let result: Result<(), _> = Err(Error::Struct {
  a: 1,
  b: "hello".to_owned(),
  c: Box::new(Error::Int(10)),
});
let value = 1;

assert_matches!(result, Err(Error::Struct {
  a: eq!(value),
  b: "hello",
  c: deref!(Error::Int(10)),
}));

§assert_regex!

Compares an AsRef<str> with a regular expression. A full match is required: foo matches f.o but not fo.

assert_regex!("hello", "h.*o");
// (?s:) makes `.` match newlines.
assert_regex!("hello\nworld", r"(?s:h.*d)");

§TestResult

TestResult is a std::result::Result type that can be used as a return type in a test. TestResult makes ? used in a test body panic with an accurate location information by providing a #[track_caller] From implementation.

This is heavily inspired by the testresult crate, but with better panic messages for anyhow::Error.

For example, the code below will produce an exact line number of the failing result?;, the error message, and the error chain, if any.

#[derive(Debug, Error)]
#[error("my error - {0}")]
struct Error(String);

#[test]
fn my_test() -> TestResult {
    let result: Result<(), _> = Err(Error("an issue".into()));
    result?;
    Ok(())
}

§Scope

This crate provides helpers that should be used in test code only.

While the functionality provided in this crate can be used to check production code, currently this crate makes no guarantee on how expressions under test are evaluated. Because of that this crate is only recommended for tests.

For example the following potentially problematic behaviours are untested:

  • A code that looks like a pattern may be evaluated as an expression.
  • An expression evaluation may be skipped entirely for certain patterns.

§Status

This crate is under active development. It is recommended to depend on an exact version of this crate until the API is stabilised, e.g. lontra-asserts = "=0.1.8".

§Stability guarantee

This crate currently doesn’t commit to a stable API right now, but this is a goal.

Unless specified otherwise, panic messages will always be subject to change.

Re-exports§

pub use test_result::TestResult;
pub use lontra_asserts_derive as derive;

Modules§

assert_matches
assert_regex
test_result

Macros§

assert_matches
Checks whether $got matches $want.
assert_regex