hamcrest/
core.rs

1// Copyright 2014 Carl Lerche, Steve Klabnik, Alex Crichton
2// Copyright 2015 Carl Lerche
3// Copyright 2016 Urban Hafner
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11use std::fmt;
12
13pub type MatchResult = Result<(), String>;
14
15pub fn success() -> MatchResult {
16    Ok(())
17}
18
19pub fn expect(predicate: bool, msg: String) -> MatchResult {
20    if predicate { success() } else { Err(msg) }
21}
22
23#[deprecated(since = "0.1.2", note = "Use the assert_that! macro instead")]
24pub fn assert_that<T, U: Matcher<T>>(actual: T, matcher: U) {
25    match matcher.matches(actual) {
26        Ok(_) => return,
27        Err(mismatch) => {
28            panic!("\nExpected: {}\n    but: {}", matcher, mismatch);
29        }
30    }
31}
32
33pub trait Matcher<T>: fmt::Display {
34    fn matches(&self, actual: T) -> MatchResult;
35}