easy_assert/
lib.rs

1//! ## Description:
2//! easy-assert crate, which will help you to create more readable tests
3//! For now it can help you assert simple numeric values, strings, vecs
4//! and you can do your own custom assertions
5//! Everything divided by modules. Check each module for usage example.
6
7extern crate alloc;
8extern crate core;
9extern crate num_traits;
10
11use core::fmt::Display;
12
13pub mod bool_assertions;
14pub mod custom_assertions;
15pub mod list_assertions;
16pub mod num_assertions;
17pub mod option_assertions;
18pub mod string_assertions;
19
20mod assertions;
21mod values;
22
23/// Struct to wrap your value in order to have custom description if needed
24/// and never think about where to put actual or expected value.
25pub struct Actual<T> {
26    value: T,
27    description_func: fn(&T) -> String,
28}
29
30pub type Expected<T> = Actual<T>;
31
32/// Creates a new Actual wrapper. Value should implement Display trait.
33/// # Examples
34///
35/// Basic usage:
36///
37/// ``` rust
38/// use easy_assert::actual;
39/// let actual = actual(1);
40/// ```
41///
42/// Test usage:
43///
44/// ``` rust
45/// use easy_assert::{actual, expected};
46/// use easy_assert::num_assertions::NumericAssert;
47/// NumericAssert::assert_that(actual(1)).is_equal().to(expected(1));
48/// ```
49pub fn actual<T>(val: T) -> Actual<T>
50where
51    T: Display,
52{
53    Actual::create_for(val)
54}
55
56/// Creates a new Expected wrapper. Value should implement Display trait.
57/// # Examples
58///
59/// Basic usage:
60///
61/// ``` rust
62/// use easy_assert::expected;
63/// let expected = expected(1);
64/// ```
65///
66/// Test usage:
67///
68/// ``` rust
69/// use easy_assert::{actual, expected};
70/// use easy_assert::num_assertions::NumericAssert;
71/// NumericAssert::assert_that(actual(1)).is_equal().to(expected(1));
72/// ```
73pub fn expected<T>(val: T) -> Expected<T>
74where
75    T: Display,
76{
77    Expected::create_for(val)
78}
79
80/// Creates a new Actual wrapper. You should provide your own function to represent value as String.
81/// Can be used if you need custom description or value doesn't implement Display trait
82/// # Examples
83///
84/// Basic usage:
85///
86/// ``` rust
87/// use easy_assert::{actual_with};
88///
89/// fn my_description(value: &i32) -> String {
90///     format!("My own description for value {}", value)
91/// }
92///
93/// let actual = actual_with(1, my_description);
94/// ```
95///
96/// Test usage:
97///
98/// ``` rust
99/// use easy_assert::{actual_with, expected_with};
100/// use easy_assert::num_assertions::NumericAssert;
101///
102/// fn my_description(value: &i32) -> String {
103///     format!("My own description for value {}", value)
104/// }
105///
106/// NumericAssert::assert_that(actual_with(1, my_description))
107/// .is_equal().to(expected_with(1, my_description));
108/// ```
109pub fn actual_with<T>(val: T, description_func: fn(&T) -> String) -> Actual<T> {
110    Actual::new(val, description_func)
111}
112
113/// Creates a new Expected wrapper. You should provide your own function to represent value as String.
114/// Can be used if you need custom description or value doesn't implement Display trait
115/// # Examples
116///
117/// Basic usage:
118///
119/// ``` rust
120/// use easy_assert::{expected_with};
121/// fn my_description(value: &i32) -> String {
122///     format!("My own description for value {}", value)
123/// }
124///
125/// let expected = expected_with(1, my_description);
126/// ```
127///
128/// Test usage:
129///
130/// ``` rust
131/// use easy_assert::{actual_with, expected_with};
132/// use easy_assert::num_assertions::NumericAssert;
133///
134/// fn my_description(value: &i32) -> String {
135///     format!("My own description for value {}", value)
136/// }
137///
138/// NumericAssert::assert_that(actual_with(1, my_description))
139/// .is_equal().to(expected_with(1, my_description));
140/// ```
141pub fn expected_with<T>(val: T, description_func: fn(&T) -> String) -> Expected<T> {
142    Expected::new(val, description_func)
143}
144
145/// Convert vec with values into vec of Actual wrappers. You should provide your own function to represent value as String.
146/// # Examples
147///
148/// Basic usage:
149///
150/// ``` rust
151/// use easy_assert::actual_vec_with;
152/// fn my_description(value: &i32) -> String {
153///     format!("My own description for value {}", value)
154/// }
155///
156/// let actual = actual_vec_with(vec![1, 2, 3], my_description);
157/// ```
158///
159/// Test usage:
160///
161/// ``` rust
162///
163/// use easy_assert::{actual_vec_with, expected_vec_with};
164/// use easy_assert::list_assertions::ListAssert;
165///
166/// fn my_description(value: &i32) -> String {
167///     format!("My own description for value {}", value)
168/// }
169///
170/// ListAssert::assert_that(actual_vec_with(vec![1, 2, 3], my_description))
171///  .with_element_matcher(|a, b| a.eq(b))
172///  .is_equal_to(expected_vec_with(vec![3, 2, 1], my_description))
173///  .in_any_order()
174/// ```
175pub fn actual_vec_with<T>(values: Vec<T>, description_func: fn(&T) -> String) -> Vec<Actual<T>> {
176    let mut result: Vec<Actual<T>> = Vec::new();
177    for value in values {
178        result.push(Actual::new(value, description_func));
179    }
180
181    result
182}
183
184/// Convert vec with values into vec of Expected wrappers. You should provide your own function to represent value as String.
185/// # Examples
186///
187/// Basic usage:
188///
189/// ``` rust
190/// use easy_assert::expected_vec_with;
191/// fn my_description(value: &i32) -> String {
192///     format!("My own description for value {}", value)
193/// }
194///
195/// let expected = expected_vec_with(vec![1, 2, 3], my_description);
196/// ```
197///
198/// Test usage:
199///
200/// ``` rust
201///
202/// use easy_assert::{actual_vec_with, expected_vec_with};
203/// use easy_assert::list_assertions::ListAssert;
204///
205/// fn my_description(value: &i32) -> String {
206///     format!("My own description for value {}", value)
207/// }
208///
209/// ListAssert::assert_that(actual_vec_with(vec![1, 2, 3], my_description))
210///  .with_element_matcher(|a, b| a.eq(b))
211///  .is_equal_to(expected_vec_with(vec![3, 2, 1], my_description))
212///  .in_any_order()
213/// ```
214pub fn expected_vec_with<T>(
215    values: Vec<T>,
216    description_func: fn(&T) -> String,
217) -> Vec<Expected<T>> {
218    let mut result: Vec<Actual<T>> = Vec::new();
219    for value in values {
220        result.push(Expected::new(value, description_func));
221    }
222
223    result
224}
225
226/// Convert vec with values into vec of Actual wrappers. Value should implement Display trait.
227/// # Examples
228///
229/// Basic usage:
230///
231/// ``` rust
232/// use easy_assert::actual_vec;
233/// let actual = actual_vec(vec![1, 2, 3]);
234/// ```
235///
236/// Test usage:
237///
238/// ``` rust
239///
240/// use easy_assert::{actual_vec, expected_vec};
241/// use easy_assert::list_assertions::ListAssert;
242/// ListAssert::assert_that(actual_vec(vec![1, 2, 3]))
243///  .with_element_matcher(|a, b| a.eq(b))
244///  .is_equal_to(expected_vec(vec![3, 2, 1]))
245///  .in_any_order()
246/// ```
247pub fn actual_vec<T>(values: Vec<T>) -> Vec<Actual<T>>
248where
249    T: Display,
250{
251    let mut result: Vec<Actual<T>> = Vec::new();
252    for value in values {
253        result.push(Actual::create_for(value));
254    }
255
256    result
257}
258
259/// Convert vec with values into vec of Expected wrappers. Value should implement Display trait.
260/// # Examples
261///
262/// Basic usage:
263///
264/// ``` rust
265/// use easy_assert::expected_vec;
266/// let expected = expected_vec(vec![1, 2, 3]);
267/// ```
268///
269/// Test usage:
270///
271/// ``` rust
272///
273/// use easy_assert::{actual_vec, expected_vec};
274/// use easy_assert::list_assertions::ListAssert;
275/// ListAssert::assert_that(actual_vec(vec![1, 2, 3]))
276///  .with_element_matcher(|a, b| a.eq(b))
277///  .is_equal_to(expected_vec(vec![3, 2, 1]))
278///  .in_any_order()
279/// ```
280pub fn expected_vec<T>(values: Vec<T>) -> Vec<Expected<T>>
281where
282    T: Display,
283{
284    let mut result: Vec<Actual<T>> = Vec::new();
285    for value in values {
286        result.push(Expected::create_for(value));
287    }
288
289    result
290}
291
292fn test_failed(error_message: &str) {
293    let addition = "==================================================";
294    panic!("\n\n\n{}\n{}\n{}\n\n\n", addition, error_message, addition)
295}