#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
use std::fmt::Debug;
pub mod assertions;
pub mod matchers;
#[cfg(test)]
mod tests;
pub enum MatchType {
ToBe,
ToHave,
}
impl MatchType {
fn description(&self) -> &str {
match self {
MatchType::ToBe => "to be",
MatchType::ToHave => "to have",
}
}
}
pub struct Expect<T, M> {
value: T,
_matcher: std::marker::PhantomData<M>,
}
impl<T, M> Expect<T, M>
where
T: Debug,
M: Matcher<T>,
{
pub fn new(value: T) -> Self {
Expect { value, _matcher: std::marker::PhantomData }
}
fn assert(&self, matcher: M, match_type: MatchType) {
if !matcher.matches(&self.value) {
panic!(
"Expected {:?} {} {}",
self.value,
match_type.description(),
matcher.description()
);
}
}
pub fn to_be(&self, matcher: M) {
self.assert(matcher, MatchType::ToBe);
}
pub fn to_have(&self, matcher: M) {
self.assert(matcher, MatchType::ToHave);
}
}
pub fn expect<T: Debug, M: Matcher<T>>(value: T) -> Expect<T, M> {
Expect::new(value)
}
pub trait Matcher<T> {
fn matches(&self, value: &T) -> bool;
fn description(&self) -> String;
}