5 releases

Uses old Rust 2015

0.2.0 Dec 8, 2016
0.1.3 Dec 7, 2016
0.1.2 Dec 7, 2016
0.1.1 Dec 6, 2016
0.1.0 Dec 6, 2016

#7 in #macro-use

21 downloads per month

MIT/Apache

36KB
813 lines

must for rust rustdoc crates.io MIT/Apache-2.0

must is an assertion library for rust.


lib.rs:

Usage

Add must to your Cargo.toml file.

[dev-dependencies]
must = "0.2.*"

And add

#[cfg_attr(test, macro_use)]
#[cfg(test)]
extern crate must;

to your root module.

Then add

use must::prelude::*;

your test module.

Features

  • Lazy. You can use .with_msg() after assertion.
  • Fluent.
  • You can build your own composable test tool. See lazy module for full example.
let parser: fn(&str) -> Result<Lit, ParseError> = parse_lit;
parser.must_parse("false").as_ok(Lit::Bool(false)); // .or(fail!()) is optional
parser.must_parse("true").as_ok(Lit::Bool(true)).or(fail!());
parser.must_parse("352").as_ok(Lit::Num(352)).or(fail!());

How does it work?

  • If value is not explicitly taken by user, it panics on drop.
  • As it defers panic, with_msg() can be used almost anywhere.

Examples (Usage)

#[macro_use] extern crate must;
use must::prelude::*;
// fail!() is optional, and if not called, it will panic on drop.
// but as it's value is not used, it will be dropped right after one assertion chain.


Some(5u8).must_be_some_and(|val| {
    val.must_be(5) // closure must return assertion
}).or(fail!("your msg {}", "and args"));
  // fail! macro captures location, so you can know source of panic without backtrace.
  // fail! macro supports optional formatting.
  // As value is printed by must, you don't have to put it in format args.

fn double(x: usize) -> usize {
    x * 2
}

10.must_be_in_range(10..);
10.must_not_be_in_range(..10);
double(10).must_be_in_range(..);
double(10).must_not_be_in_range(..20);

Examples (Extending must with builder style)

See lazy module.

Dependencies

~405KB