fluent_asserter/
string_asserter.rs1use super::*;
2
3pub trait StrAssertions<T>
7where
8 T: Into<String> + Clone,
9{
10 fn contains(&self, expected: &str);
11 fn starts_with(&self, expected_start: &str);
12 fn ends_with(&self, expected_end: &str);
13 fn is_empty(&self);
14 fn is_not_empty(&self);
15 fn has_length(&self, expected_length: usize);
16 fn contains_all(&self, args: &[&str]); fn contains_any(&self, args: &[&str]); }
19
20impl<T> StrAssertions<T> for Asserter<T>
21where
22 T: Into<String> + Clone,
23{
24 fn contains(&self, expected: &str) {
25 let string = self.value.clone().into();
26 let is_present = string.contains(expected);
27
28 if !is_present {
29 panic!(
30 "Expected {} to contain '{}', but it does not.",
31 self.name, expected
32 );
33 }
34 }
35 fn starts_with(&self, expected_start: &str) {
36 let string = self.value.clone().into();
37 let starts_with = string.starts_with(expected_start);
38
39 if !starts_with {
40 panic!(
41 "Expected {} to start with '{}', but it does not.",
42 self.name, expected_start
43 )
44 }
45 }
46
47 fn ends_with(&self, expected_end: &str) {
48 let string = self.value.clone().into();
49 let ends_with = string.ends_with(expected_end);
50
51 if !ends_with {
52 panic!(
53 "Expected {} to end with '{}', but it does not.",
54 self.name, expected_end
55 )
56 }
57 }
58
59 fn is_empty(&self) {
60 let string = self.value.clone().into();
61
62 if !string.is_empty() {
63 panic!("Expected {} to be empty, but it is not.", self.name)
64 }
65 }
66
67 fn is_not_empty(&self) {
68 let string = self.value.clone().into();
69
70 if string.is_empty() {
71 panic!("Expected {} to not be empty, but it is.", self.name)
72 }
73 }
74
75 fn has_length(&self, expected_length: usize) {
76 let string = self.value.clone().into();
77 let len = string.len();
78
79 if len != expected_length {
80 panic!(
81 "Expected {} to have length {}, but it has {}",
82 self.name, expected_length, len
83 );
84 }
85 }
86
87 fn contains_all(&self, args: &[&str]) {
88 let string = self.value.clone().into();
90 let contains_all = args.iter().all(|&w| string.contains(&w));
91
92 if !contains_all {
94 panic!(
95 "Expected {} '{}' to contain the strings {:?}, but it does not.",
96 self.name, string, args
97 );
98 }
99 }
100
101 fn contains_any(&self, args: &[&str]) {
102 let string = self.value.clone().into();
103 let contains_any = args.iter().any(|&w| string.contains(&w));
104
105 if !contains_any {
106 panic!(
107 "Expected {} '{}' to contain at least one of the strings {:?}, but it does not.",
108 self.name, string, args
109 );
110 }
111 }
112}