1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
//! ## Usage
//!
//! ``` rust
//!
//!use easy_assert::{expected, expected_vec};
//!use easy_assert::option_assertions::OptionAssert;
//!
//!#[test]
//!pub fn none_is_none() {
//! OptionAssert::<&str>::assert_that(None).is_none();
//!}
//!
//!#[test]
//!#[should_panic]
//!pub fn some_is_not_none() {
//! OptionAssert::assert_that(Some("a")).is_none();
//!}
//!
//!#[test]
//!#[should_panic]
//!pub fn does_not_contains_value() {
//! OptionAssert::assert_that(Some(1))
//! .contains()
//! .value()
//! .matches_by(|a,b| a==b)
//! .to(expected(2));
//!}
//!
//!
//!#[test]
//!pub fn contains_vec() {
//! OptionAssert::assert_that(Some(vec!['a','b','c']))
//! .contains()
//! .list()
//! .with_element_matcher(|a,b| a==b)
//! .is_equal_to(expected_vec(vec!['a','b','c']))
//! .in_order();
//!}
//!
//!#[test]
//!#[should_panic]
//!pub fn does_not_contains_vec() {
//! OptionAssert::assert_that(Some(vec!['a','b','c']))
//! .contains()
//! .list()
//! .with_element_matcher(|a,b| a==b)
//! .is_equal_to(expected_vec(vec!['a','b','d']))
//! .in_order();
//!}
//!
//!#[test]
//!#[should_panic]
//!pub fn contains_for_none_will_raise_error() {
//! OptionAssert::<i32>::assert_that(None)
//! .contains()
//! .value()
//! .matches_by(|a,b| a==b)
//! .to(expected(2));
//!}
//!
//! ```
use crate::assertions::OptionCheck;
use crate::custom_assertions::CustomAssert;
use crate::list_assertions::ListAssert;
use crate::{actual, actual_vec};
use core::fmt::Display;
pub struct OptionAssert<T> {
actual: Option<T>,
}
impl<T: 'static> OptionAssert<T> {
pub fn assert_that(actual: Option<T>) -> OptionAssert<T> {
OptionAssert { actual }
}
pub fn is_none(&self) {
OptionCheck::is_none(self)
}
pub fn is_some(&self) {
OptionCheck::is_some(self)
}
pub fn contains(self) -> ElementMatcher<T> {
ElementMatcher {
actual: self.actual.expect("\n Actual: is None, but should be Some"),
}
}
}
impl<T: 'static> OptionCheck for OptionAssert<T> {
fn is_none(&self) {
if self.actual.is_some() {
panic!("\n Actual is Some, but should be None");
}
}
fn is_some(&self) {
if self.actual.is_none() {
panic!("\n Actual: is None, but should be Some");
}
}
}
pub struct ElementMatcher<T> {
actual: T,
}
impl<T> ElementMatcher<Vec<T>>
where
T: Display + 'static,
{
pub fn list(self) -> ListAssert<T> {
ListAssert::assert_that(actual_vec(self.actual))
}
}
impl<T> ElementMatcher<T>
where
T: Display + 'static,
{
pub fn value(self) -> CustomAssert<T> {
CustomAssert::assert_that(actual(self.actual))
}
}