must 0.2.0

assertion library for rust
Documentation

//! # Usage
//! Add ```must``` to your Cargo.toml file.
//!
//! ```ignore
//! [dev-dependencies]
//! must = "0.2.*"
//! ```
//!
//! And add
//!
//! ```ignore
//! #[cfg_attr(test, macro_use)]
//! #[cfg(test)]
//! extern crate must;
//! ```
//!
//! to your [root module][].
//!
//! Then add
//!
//! ```ignore
//! 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.
//!
//! ```rust,ignore
//! 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)
//!
//! ```rust
//! #[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][].
//!
//!
//!
//! [lazy module]:lazy/index.html
//! [root module]:https://doc.rust-lang.org/book/crates-and-modules.html

#[macro_use]
extern crate log;
extern crate mutator;
extern crate num_traits;

pub use mutator::Mutator;
pub use self::errors::{Error, ErrorKind};
pub use self::lazy::{LazyAssertion, LazyMatcher};
pub mod errors;
pub mod lazy;
pub mod marker;
pub mod prelude;
pub mod error_renderer;
pub mod matchers;


/// **fail!** macro is used to add source and custom message to result.
///
/// fail!() - Just capture source location.
///
/// fail!("") - Add message.
///
/// fail!("{:?}", var) - Add message.
///
#[macro_export]
macro_rules! fail {
    () => {{
        fn fail_with_src(err: &$crate::Error) {
            let src = $crate::error_renderer::Source {
                filename: file!(),
                line: line!(),
                column: column!(),
            };

            let renderer = &$crate::error_renderer::ErrRenderer {
			    err: err,
			    src: Some(src),
                msg: None,
		    };
            panic!("{}", renderer);
        }

        fail_with_src
    }};

     ($fmt:expr) => {{
        let f = |ref err: &$crate::Error| {
            let src = $crate::error_renderer::Source {
                filename: file!(),
                line: line!(),
                column: column!(),
            };

            let renderer = &$crate::error_renderer::ErrRenderer {
			    err: err,
			    src: Some(src),
                msg: Some(format!($fmt)),
		    };
            panic!("{}", renderer);
        };

        f
    }};

    ($fmt:expr, $($arg:tt)*) => {{
        let f = |ref err: &$crate::Error| {
            let src = $crate::error_renderer::Source {
                filename: file!(),
                line: line!(),
                column: column!(),
            };

            let renderer = &$crate::error_renderer::ErrRenderer {
			    err: err,
			    src: Some(src),
                msg: Some(format!($fmt, $($arg)* )),
		    };
            panic!("{}", renderer);
        };

        f
    }};
}