use crate::backend::Assertion;
use crate::backend::assertions::sentence::AssertionSentence;
use std::fmt::Debug;
pub trait BooleanMatchers {
fn to_be_true(self) -> Self;
fn to_be_false(self) -> Self;
}
trait AsBoolean {
fn is_true(&self) -> bool;
fn is_false(&self) -> bool;
}
impl AsBoolean for bool {
fn is_true(&self) -> bool {
*self
}
fn is_false(&self) -> bool {
!*self
}
}
impl AsBoolean for &bool {
fn is_true(&self) -> bool {
**self
}
fn is_false(&self) -> bool {
!**self
}
}
impl<V> BooleanMatchers for Assertion<V>
where
V: AsBoolean + Debug + Clone,
{
fn to_be_true(self) -> Self {
let result = self.value.is_true();
let sentence = AssertionSentence::new("be", "true");
return self.add_step(sentence, result);
}
fn to_be_false(self) -> Self {
let result = self.value.is_false();
let sentence = AssertionSentence::new("be", "false");
return self.add_step(sentence, result);
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[test]
fn test_boolean_true() {
crate::Reporter::disable_deduplication();
expect!(true).to_be_true();
expect!(false).not().to_be_true();
}
#[test]
#[should_panic(expected = "not be true")]
fn test_not_true_fails() {
let _assertion = expect!(true).not().to_be_true();
std::hint::black_box(_assertion);
}
#[test]
#[should_panic(expected = "be true")]
fn test_false_to_be_true_fails() {
let _assertion = expect!(false).to_be_true();
std::hint::black_box(_assertion);
}
#[test]
fn test_boolean_false() {
crate::Reporter::disable_deduplication();
expect!(false).to_be_false();
expect!(true).not().to_be_false();
}
#[test]
#[should_panic(expected = "not be false")]
fn test_not_false_fails() {
let _assertion = expect!(false).not().to_be_false();
std::hint::black_box(_assertion);
}
#[test]
#[should_panic(expected = "be false")]
fn test_true_to_be_false_fails() {
let _assertion = expect!(true).to_be_false();
std::hint::black_box(_assertion);
}
}