grillon/assertion/impls/
status.rs1use crate::assertion::traits::{Equality, RangeInclusive};
2use crate::assertion::{Assertion, Hand};
3use crate::dsl::{Part, Predicate};
4use crate::StatusCode;
5
6impl Equality<u16> for StatusCode {
7 type Assertion = Assertion<u16>;
8
9 fn is_eq(&self, rhs: &u16) -> Self::Assertion {
10 let lhs = self.as_u16();
11
12 Assertion {
13 predicate: Predicate::Is,
14 part: Part::StatusCode,
15 left: Hand::Left(lhs),
16 right: Hand::Right(*rhs),
17 result: (self == rhs).into(),
18 }
19 }
20
21 fn is_ne(&self, rhs: &u16) -> Self::Assertion {
22 let lhs = self.as_u16();
23
24 Assertion {
25 predicate: Predicate::IsNot,
26 part: Part::StatusCode,
27 left: Hand::Left(lhs),
28 right: Hand::Right(*rhs),
29 result: (self != rhs).into(),
30 }
31 }
32}
33
34impl Equality<StatusCode> for StatusCode {
35 type Assertion = Assertion<u16>;
36
37 fn is_eq(&self, rhs: &StatusCode) -> Self::Assertion {
38 Assertion {
39 predicate: Predicate::Is,
40 part: Part::StatusCode,
41 left: Hand::Left(self.as_u16()),
42 right: Hand::Right(rhs.as_u16()),
43 result: (self == rhs).into(),
44 }
45 }
46
47 fn is_ne(&self, rhs: &StatusCode) -> Self::Assertion {
48 Assertion {
49 predicate: Predicate::IsNot,
50 part: Part::StatusCode,
51 left: Hand::Left(self.as_u16()),
52 right: Hand::Right(rhs.as_u16()),
53 result: (self != rhs).into(),
54 }
55 }
56}
57
58impl RangeInclusive<StatusCode> for StatusCode {
59 type Assertion = Assertion<u16>;
60
61 fn in_range(&self, min: &StatusCode, max: &StatusCode) -> Self::Assertion {
62 let lhs = self.as_u16();
63 let (min, max) = (min.as_u16(), max.as_u16());
64 let result = lhs >= min && lhs <= max;
65
66 Assertion {
67 predicate: Predicate::Between,
68 part: Part::StatusCode,
69 left: Hand::Left(lhs),
70 right: Hand::Compound(min, max),
71 result: result.into(),
72 }
73 }
74}
75
76impl RangeInclusive<u16> for StatusCode {
77 type Assertion = Assertion<u16>;
78
79 fn in_range(&self, min: &u16, max: &u16) -> Self::Assertion {
80 let lhs = self.as_u16();
81 let result = &lhs >= min && &lhs <= max;
82
83 Assertion {
84 predicate: Predicate::Between,
85 part: Part::StatusCode,
86 left: Hand::Left(lhs),
87 right: Hand::Compound(*min, *max),
88 result: result.into(),
89 }
90 }
91}
92
93#[cfg(test)]
94pub mod tests {
95 use http::StatusCode;
96
97 use crate::assertion::traits::{Equality, RangeInclusive};
98
99 #[test]
100 fn impl_is_eq_status_code() {
101 let assertion = StatusCode::FORBIDDEN.is_eq(&StatusCode::FORBIDDEN);
102 assert!(assertion.passed(), "{}", assertion.log())
103 }
104
105 #[test]
106 fn impl_is_eq_u16() {
107 let assertion = StatusCode::FORBIDDEN.is_eq(&403);
108 assert!(assertion.passed(), "{}", assertion.log())
109 }
110
111 #[test]
112 fn impl_is_not_status_code() {
113 let assertion = StatusCode::FORBIDDEN.is_ne(&StatusCode::OK);
114 assert!(assertion.passed(), "{}", assertion.log())
115 }
116
117 #[test]
118 fn impl_is_not_u16() {
119 let assertion = StatusCode::FORBIDDEN.is_ne(&200);
120 assert!(assertion.passed(), "{}", assertion.log())
121 }
122
123 #[test]
124 fn impl_is_between_status_code() {
125 let assertion =
126 StatusCode::FORBIDDEN.in_range(&StatusCode::BAD_REQUEST, &StatusCode::NOT_FOUND);
127
128 assert!(assertion.passed(), "{}", assertion.log())
129 }
130
131 #[test]
132 fn impl_is_between_u16() {
133 assert!(StatusCode::FORBIDDEN.in_range(&400, &404).passed())
134 }
135
136 mod serialization {
137 use crate::assertion::Hand;
138
139 use super::*;
140 use serde_json::json;
141
142 #[test]
143 fn it_serializes_status_should_be() {
144 let status = StatusCode::UNAUTHORIZED;
145
146 let expected_json = json!({
147 "part": "status code",
148 "predicate": "should be",
149 "left": status.as_u16(),
150 "right": 401,
151 "result": "passed"
152 });
153
154 let assertion = status.is_eq(&401);
155
156 assert_eq!(
157 json!(assertion),
158 expected_json,
159 "Serialized assertion is not equals to the expected json",
160 );
161 }
162
163 #[test]
164 fn it_serializes_status_should_not_be() {
165 let status = StatusCode::UNAUTHORIZED;
166
167 let expected_json = json!({
168 "part": "status code",
169 "predicate": "should not be",
170 "left": status.as_u16(),
171 "right": 404,
172 "result": "passed"
173 });
174
175 let assertion = status.is_ne(&404);
176
177 assert_eq!(
178 json!(assertion),
179 expected_json,
180 "Serialized assertion is not equals to the expected json",
181 );
182 }
183
184 #[test]
185 fn it_serializes_status_is_between() {
186 let status = StatusCode::UNAUTHORIZED;
187
188 let expected_json = json!({
189 "part": "status code",
190 "predicate": "should be between",
191 "left": status.as_u16(),
192 "right": Hand::Compound(400, 404),
193 "result": "passed"
194 });
195
196 let assertion = status.in_range(&400, &404);
197
198 assert_eq!(
199 json!(assertion),
200 expected_json,
201 "Serialized assertion is not equals to the expected json",
202 );
203 }
204 }
205}