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