easy_assert/
string_assertions.rs

1//! ## Usage
2//!
3//! ``` rust
4//! use easy_assert::{actual, expected};
5//! use easy_assert::string_assertions::StringAssert;
6//!
7//!  StringAssert::assert_that(actual(String::from("a")))
8//!  .is_equal()
9//!  .to(expected(String::from("a")));
10//!
11//!  StringAssert::assert_that(actual(String::from("a")))
12//!  .is_not_equal()
13//!  .to(expected(String::from("b")));
14//!
15//!  StringAssert::assert_that(actual(String::from("abcde")))
16//!  .contains(expected(String::from("bcd")));
17//!
18//!  StringAssert::assert_that(actual(String::from("abcde")))
19//!  .length()
20//!  .is(expected(5));
21//! ```
22
23use crate::assertions::{Contains, Equals, Length, NotEquals};
24use crate::{test_failed, Actual, Expected};
25
26pub struct StringAssert {
27    actual: Actual<String>,
28}
29
30impl StringAssert {
31    pub fn assert_that(actual: Actual<String>) -> StringAssert {
32        StringAssert { actual }
33    }
34
35    pub fn is_equal(self) -> Box<dyn Equals<String>> {
36        Box::new(self)
37    }
38
39    pub fn length(self) -> Box<dyn Length> {
40        Box::new(self)
41    }
42
43    pub fn is_not_equal(self) -> Box<dyn NotEquals<String>> {
44        Box::new(self)
45    }
46
47    pub fn contains(self, expected: Expected<String>) {
48        Contains::<String>::contains(&self, expected);
49    }
50}
51
52impl Equals<String> for StringAssert {
53    fn to(&self, expected: Expected<String>) {
54        if self.actual.ne(&expected) {
55            let error_message = format!(
56                "\n Actual: {} \n not equal to \n  Expected: {} \n",
57                self.actual, expected
58            );
59            test_failed(&error_message);
60        }
61    }
62}
63
64impl NotEquals<String> for StringAssert {
65    fn to(&self, expected: Expected<String>) {
66        if self.actual.eq(&expected) {
67            let error_message = format!(
68                "\n Actual: {} \n not equal to \n  Expected: {} \n",
69                self.actual, expected
70            );
71            test_failed(&error_message);
72        }
73    }
74}
75
76impl Length for StringAssert {
77    fn is(&self, expected: Expected<usize>) {
78        if self.actual.value.len() != expected.value {
79            let error_message = format!(
80                "\n Actual: {} \n length not equal to expected \n {} \n",
81                self.actual, expected
82            );
83            test_failed(&error_message);
84        }
85    }
86}
87
88impl Contains<String> for StringAssert {
89    fn contains(&self, expected: Expected<String>) {
90        if !self.actual.value.contains(&expected.value) {
91            let error_message = format!(
92                "\n Actual: {} \n does not contains \n  Expected: {} \n",
93                self.actual, expected
94            );
95            test_failed(&error_message);
96        }
97    }
98}