5 releases

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

#228 in Testing

Download history 10/week @ 2019-11-27 5/week @ 2019-12-04 96/week @ 2019-12-11 5/week @ 2019-12-25 1/week @ 2020-01-01 62/week @ 2020-01-08 30/week @ 2020-01-15 1/week @ 2020-02-05 5/week @ 2020-02-12 10/week @ 2020-02-19 17/week @ 2020-02-26 10/week @ 2020-03-04 5/week @ 2020-03-11

84 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::*;
# fn main() {
// 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

~295KB