unit_testing/objects/mod.rs
1use std::process::ExitStatus;
2use std::{collections::HashSet, io};
3
4///
5/// # Calculate the time of the test function
6///
7pub trait Take {
8 ///
9 /// # run assertion
10 ///
11 /// - `t` The test
12 ///
13 fn assert_that(&mut self, t: bool) -> bool;
14
15 ///
16 /// # Run assert and measure execution time
17 ///
18 /// - `t` The test
19 /// - `s` The success output message
20 /// - `e` The error output message
21 ///
22 fn take(&mut self, t: bool, s: &str, e: &str) -> &mut Self;
23
24 ///
25 ///
26 /// # Run an assert and measure the time
27 ///
28 /// - `t` The test
29 /// - `s` The success output message
30 /// - `e` The error output message
31 ///
32 fn check(&mut self, t: bool, s: &str, e: &str);
33}
34
35///
36/// # Add theory useful method
37///
38pub trait Theory {
39 ///
40 /// # A theory must be equal to false
41 ///
42 /// - `callback` The callback to execute
43 ///
44 fn chaos(&mut self, callback: &dyn Fn() -> bool) -> &mut Self;
45
46 ///
47 /// # Check if a theorem is true
48 ///
49 /// - `expected` The expected value
50 /// - `actual` The actual value
51 ///
52 fn theorem<T: PartialEq>(&mut self, expected: T, actual: &dyn Fn() -> T) -> &mut Self;
53
54 ///
55 /// # Test a theory
56 ///
57 /// - `expected` The expect callback result
58 /// - `callback` The callback to execute
59 ///
60 fn theory<T: PartialEq>(&mut self, expected: T, callback: &dyn Fn() -> T) -> &mut Self;
61}
62
63///
64/// # Assertion to expect a failure
65///
66pub trait Failure {
67 ///
68 /// # Check if a command exit status is a failure code
69 ///
70 /// - `callbacks` The callbacks to check
71 ///
72 fn command_fail(
73 &mut self,
74 callbacks: Vec<&dyn Fn() -> Result<ExitStatus, io::Error>>,
75 ) -> &mut Self;
76
77 ///
78 /// # Check if a callbacks return false
79 ///
80 /// - `callbacks` The callbacks to check
81 ///
82 fn fail(&mut self, callbacks: Vec<&dyn Fn() -> bool>) -> &mut Self;
83}
84
85///
86/// # Expectations to expect a success
87///
88pub trait Success {
89 ///
90 /// # Check if a command success
91 ///
92 /// - `callbacks` The callbacks to check
93 ///
94 fn run(&mut self, callbacks: Vec<&dyn Fn() -> Result<ExitStatus, io::Error>>) -> &mut Self;
95
96 ///
97 /// # Check if a callbacks return true
98 ///
99 /// - `callbacks` The callbacks to check
100 ///
101 fn success(&mut self, callbacks: Vec<&dyn Fn() -> bool>) -> &mut Self;
102}
103
104///
105/// # The method to implements for a new struct
106///
107pub trait Testable {
108 ///
109 /// - `sleep_time` The sleep time
110 ///
111 fn new(sleep_time: u64) -> Self;
112
113 ///
114 /// # Check if a pattern matches values
115 ///
116 /// - `pattern` The pattern to match
117 /// - `values` The values to check
118 ///
119 fn matches(&mut self, pattern: &str, values: Vec<String>) -> &mut Self;
120
121 ///
122 /// # check if a pattern the x index equals a value listing in values
123 ///
124 /// - `pattern` The pattern to match
125 /// - `x` The index to match
126 /// - `values` The values
127 ///
128 fn capture(&mut self, pattern: &str, x: &str, key: usize, values: Vec<String>) -> &mut Self;
129
130 ///
131 /// # Assert if callback return true
132 ///
133 /// - `f` The callback
134 ///
135 fn ok(&mut self, f: bool) -> &mut Self;
136
137 ///
138 /// # Assert if callback return false
139 ///
140 /// - `f` The callback
141 ///
142 fn ko(&mut self, f: bool) -> &mut Self;
143
144 ///
145 /// # Check if test pass
146 ///
147 /// - `test` The test assertion
148 ///
149 fn assert(&mut self, test: bool) -> bool;
150
151 ///
152 /// # Check if a and b are equals
153 ///
154 /// - `a` The first value
155 /// - `b` The second value
156 ///
157 fn eq<T: PartialEq>(&mut self, a: T, b: T) -> &mut Self;
158
159 ///
160 /// # Check if a and b are unequals
161 ///
162 /// - `a` The first value
163 /// - `b` The second value
164 ///
165 fn ne<T: PartialEq>(&mut self, a: T, b: T) -> &mut Self;
166
167 ///
168 /// # Check if a is superior to min
169 ///
170 /// - `a` The first value
171 /// - `min` The minimum value
172 ///
173 fn gt<T: PartialOrd>(&mut self, a: T, min: T) -> &mut Self;
174 ///
175 /// # Check if a is superior or equal to min
176 ///
177 /// - `a` The first value
178 /// - `min` The minimum value
179 ///
180 fn ge<T: PartialOrd>(&mut self, a: T, min: T) -> &mut Self;
181
182 ///
183 /// # Check if a is inferior to max
184 ///
185 /// - `a` The first value
186 /// - `max` The maximum value
187 ///
188 fn lt<T: PartialOrd>(&mut self, a: T, max: T) -> &mut Self;
189
190 fn le<T: PartialOrd>(&mut self, a: T, max: T) -> &mut Self;
191
192 ///
193 /// # Check if a is between min and max
194 ///
195 /// - `a` The first value
196 /// - `min` The minimum value
197 /// - `max` The maximum value
198 ///
199 fn between<T: PartialOrd>(&mut self, a: T, min: T, max: T) -> &mut Self;
200
201 ///
202 /// # Check if a vector contains a value
203 ///
204 /// - `a` The vector
205 /// - `b` The value to check
206 ///
207 fn vec_contains<T: PartialEq>(&mut self, a: Vec<T>, b: T) -> &mut Self;
208
209 ///
210 /// # Check if p is a program
211 ///
212 /// - `p` The program path
213 ///
214 fn exe(&mut self, p: &str) -> &mut Self;
215
216 ///
217 /// # Check if a vector not contains a value
218 ///
219 /// - `a` The vector
220 /// - `b` The value to check
221 ///
222 fn vec_no_contains<T: PartialEq>(&mut self, a: Vec<T>, b: T) -> &mut Self;
223
224 ///
225 /// # Check if an option contains a value
226 ///
227 /// - `a` The vector
228 /// - `b` The value to check
229 ///
230 fn option_contains<T: PartialEq>(&mut self, a: Option<T>, b: T) -> &mut Self;
231
232 ///
233 /// # Check if a hash contains a string
234 ///
235 /// - `a` The hash
236 /// - `b` The value to find
237 ///
238 fn hash_contains(&mut self, a: &mut HashSet<String>, b: String) -> &mut Self;
239
240 ///
241 /// # Check if a sting contains a substring
242 ///
243 /// - `a` The string
244 /// - `b` The substring
245 ///
246 fn str_contains(&mut self, a: &str, b: &str) -> &mut Self;
247
248 ///
249 /// # Check if a file contains a value
250 ///
251 /// - `f` The file
252 /// - `v` The value to check
253 ///
254 fn file_contains(&mut self, f: &str, v: &str) -> &mut Self;
255
256 ///
257 /// # Check if a path exists
258 ///
259 /// - `p` The path to test
260 ///
261 fn exists(&mut self, p: &str) -> &mut Self;
262
263 ///
264 /// # Check if a path not exists
265 ///
266 /// - `p` The path to check the no existence
267 ///
268 fn not_exists(&mut self, p: &str) -> &mut Self;
269
270 ///
271 /// # Check if a string begins with the expected value
272 ///
273 /// - `actual` The actual value
274 /// - `expected` The expected value
275 ///
276 fn start_with(&mut self, actual: &str, expected: &str) -> &mut Self;
277
278 ///
279 /// # Check if a string finnish with the expected value
280 ///
281 /// - `actual` The actual value
282 /// - `expected` The expected value
283 ///
284 fn end_with(&mut self, actual: &str, expected: &str) -> &mut Self;
285
286 ///
287 /// # Show assertions
288 ///
289 fn end(&mut self) -> bool;
290 fn it(
291 title: &str,
292 description: &str,
293 sleep_time: u64,
294 callbacks: Vec<&dyn Fn(&mut Self) -> &mut Self>,
295 );
296}