galvanic_assert/matchers/
core.rs

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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* Copyright 2017 Christopher Bacher
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! The core module contains the basic matchers needed for writing assertions.
//!
//! The matchers in this module all operate on single values.

use std::fmt::Debug;
use super::super::*;

macro_rules! matchresult_from_comparison {
    (  $actual: ident $comparison: tt $expected: ident, $name: expr ) => {{
        let builder = MatchResultBuilder::for_($name);
        if $actual $comparison &$expected {
            builder.matched()
        } else {
            builder.failed_comparison($actual, &$expected)
        }
    }}
}

/// A matcher which always matches.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), assertion_always_succeeds());
/// # }
pub fn assertion_always_succeeds<'a,T:'a>() -> Box<Matcher<'a,T> + 'a> {
    Box::new(|_s: &T| MatchResultBuilder::for_("succeeds_always").matched())
}
/// A matcher which always matches.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), any_value());
/// # }
pub fn any_value<'a,T:'a>() -> Box<Matcher<'a,T> + 'a> {
    Box::new(|_s: &T| MatchResultBuilder::for_("any_value").matched())
}

/// A matcher which never matches.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(
///     assert_that!(&(1+1), assertion_always_fails()),
///     panics
/// );
/// # }
pub fn assertion_always_fails<'a,T:'a>() -> Box<Matcher<'a,T> + 'a> {
    Box::new(|_s: &T| {
        MatchResultBuilder::for_("fails_always").failed_because("This matcher fails always")
    })
}
/// A matcher which never matches.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(
///     assert_that!(&(1+1), no_value()),
///     panics
/// );
/// # }
pub fn no_value<'a,T:'a>() -> Box<Matcher<'a,T> + 'a> { assertion_always_fails() }

/// Accepts a matcher and returns it unmodified.
///
/// This is just syntactic sugar.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), is(eq(2)));
/// # }
pub fn is<'a, T:'a>(matcher: Box<Matcher<'a,T> + 'a>) -> Box<Matcher<'a,T> + 'a> {
    matcher
}

/// Accepts a matcher and returns it unmodified.
///
/// This is just syntactic sugar.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// let vs = vec![1, 2];
/// assert_that!(&vs.len(), has(lt(3)));
/// # }
pub fn has<'a, T:'a>(matcher: Box<Matcher<'a,T> + 'a>) -> Box<Matcher<'a,T> + 'a> {
    matcher
}

/// A matcher negating the result of the passed matcher.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), not(eq(3)));
/// # }
pub fn not<'a, T: 'a>(matcher: Box<Matcher<'a,T> + 'a>) -> Box<Matcher<'a,T> + 'a> {
    Box::new(move |actual: &'a T| {
        match matcher.check(actual) {
            MatchResult::Matched { name } =>
                MatchResultBuilder::for_(&format!("not({})", name))
                                   .failed_because(&format!("{} is satisfied", name)),
            MatchResult::Failed { name, .. } =>
                MatchResultBuilder::for_(&format!("not({})", name)).matched()
        }
    })
}

/// Matches if the asserted value is equal to the expected value.
///
/// This matcher should not be used when asserting floating point values.
/// Use [close_to] instead. This is the same as [eq].
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), equal_to(2));
/// # }
pub fn equal_to<'a, T>(expected: T) -> Box<Matcher<'a,T> + 'a>
where T: PartialEq + Debug + 'a {
    Box::new(move |actual: &T| matchresult_from_comparison!(actual == expected, "equal"))
}
/// Matches if the asserted value is equal to the expected value.
///
/// This matcher should not be used when asserting floating point values.
/// Use [close_to] instead. This is the same as [equal_to].
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), eq(2));
/// # }
pub fn eq<'a, T: PartialEq + Debug + 'a>(expected: T) -> Box<Matcher<'a,T> + 'a> { equal_to(expected) }

/// Matches if the asserted value is less than the expected value.
///
/// This is the same as [lt].
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), less_than(3));
/// # }
pub fn less_than<'a, T>(expected: T) -> Box<Matcher<'a,T> + 'a>
where T: PartialOrd + Debug + 'a {
    Box::new(move |actual: &T| matchresult_from_comparison!(actual < expected, "less_than"))
}
/// Matches if the asserted value is less than the expected value.
///
/// This is the same as [less_than].
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), less_than(3));
/// # }
pub fn lt<'a, T: PartialOrd + Debug + 'a>(expected: T) -> Box<Matcher<'a,T> + 'a> { less_than(expected) }

/// Matches if the asserted value is greater than the expected value.
///
/// This is the same as [gt].
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), greater_than(1));
/// # }
pub fn greater_than<'a, T>(expected: T) -> Box<Matcher<'a,T> + 'a>
where T: PartialOrd + Debug + 'a {
    Box::new(move |actual: &T| matchresult_from_comparison!(actual > expected, "greater_than"))
}
/// Matches if the asserted value is greater than the expected value.
///
/// This is the same as [greater_than].
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), gt(1));
/// # }
pub fn gt<'a, T: PartialOrd + Debug + 'a>(expected: T) -> Box<Matcher<'a,T> + 'a> { greater_than(expected) }

/// Matches if the asserted value is less than or equal to the expected value.
///
/// This is the same as [leq].
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), less_than_or_equal(3));
/// assert_that!(&(1+1), less_than_or_equal(2));
/// # }
pub fn less_than_or_equal<'a, T>(expected: T) -> Box<Matcher<'a,T> + 'a>
where T: PartialOrd + Debug + 'a {
    Box::new(move |actual: &T| matchresult_from_comparison!(actual <= expected, "less_than_or_equal"))
}
/// Matches if the asserted value is less than or equal to the expected value.
///
/// This is the same as [less_than_or_equal].
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), leq(3));
/// assert_that!(&(1+1), leq(2));
/// # }
pub fn leq<'a, T: PartialOrd + Debug + 'a>(expected: T) -> Box<Matcher<'a,T> + 'a> { less_than_or_equal(expected) }

/// Matches if the asserted value is greater than or equal to the expected value.
///
/// This is the same as [geq].
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), greater_than_or_equal(1));
/// assert_that!(&(1+1), greater_than_or_equal(2));
/// # }
pub fn greater_than_or_equal<'a, T>(expected: T) -> Box<Matcher<'a,T> + 'a>
where T: PartialOrd + Debug + 'a {
    Box::new(move |actual: &T| matchresult_from_comparison!(actual >= expected, "greater_than_or_equal"))
}
/// Matches if the asserted value is greater than or equal to the expected value.
///
/// This is the same as [greater_than_or_equal].
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1+1), geq(1));
/// assert_that!(&(1+1), geq(2));
/// # }
pub fn geq<'a, T: PartialOrd + Debug + 'a>(expected: T) -> Box<Matcher<'a,T> + 'a> { greater_than_or_equal(expected) }

/// Matches if the asserted value is in an epsilon range around the expected value.
///
/// If floating point values are compared for equality this matcher should be used instead of [equal_to]
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// assert_that!(&(1.2 + 3.14), close_to(4.34, 0.00001));
/// # }
pub fn close_to<'a, T>(expected: T, eps: T) -> Box<Matcher<'a,T> + 'a>
where T: Copy + PartialOrd + std::ops::Add<Output=T> + std::ops::Sub<Output=T> + Debug + 'a {
    Box::new(move |actual: &T| {
        let builder = MatchResultBuilder::for_("close_to");
        if &(expected - eps) <= actual && actual <= &(expected + eps) {
            builder.matched()
        } else {
            builder.failed_because(&format!("{:?} should be between {:?} and {:?}",
                                            actual, expected - eps, expected + eps)
            )
        }
    })
}

/// Matches if asserted value and the expected value are truely the same object.
///
/// The two values are the same if the reside at the same memory address.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// #[derive(Debug)]
/// struct Foo;
/// let foo1 = Foo;
/// let foo2 = Foo;
///
/// assert_that!(&foo1, same_object(&foo1));
///
/// assert_that!(
///     assert_that!(&foo1, same_object(&foo2)),
///     panics
/// );
/// # }
pub fn same_object<'a, T>(expected: &'a T) -> Box<Matcher<'a,T> + 'a>
where T: Debug + 'a {
    Box::new(move |actual: &T| {
        let builder = MatchResultBuilder::for_("same_object");
        if (actual as *const _) == (expected as *const _) {
            builder.matched()
        } else {
            builder.failed_comparison(&actual, &expected)
        }
    })
}

/// Write patterns of structs/enums which use `Matcher`s instead of field values.
///
/// When providing matchers for multiple fields, not that *all* matchers will be evaluated.
/// Even if one of them returns `MatchResult::Failed`.
///
/// For struct-like structs/enum-variants not all fields need to be listed in the pattern.
/// Unwanted fields can safely be ommitted. For tuple-like structs/enum-variants all fields
/// need to be listed in the correct order. Although you can use `any_value()`
/// to effectively ignore the field.
///
/// Note that the correct brace/bracket style for tuple-like structs/enums is `Variant[any_value(), any_value()]`
/// not `Variant(any_value(), any_value())`. This discrepancy is due to macro parsing reasons.
///
/// #Examples
/// ```rust
/// // Matching a struct-like ...
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::*;
/// # fn main() {
/// struct Foo { x: i32, y: f64 }
/// let foo = Foo { x: 12, y: 23.4 };
/// assert_that!(&foo, has_structure!(Foo {
///     x: eq(12),
///     y: lt(25.0)
/// }));
/// assert_that!(&foo, has_structure!(Foo {
///     y: lt(25.0) // not all fields need to be given for struct-likes
/// }));
///
/// // Matching a tuple-like ...
/// struct Bar(i32, f64);
/// let bar = Bar(12, 23.4);
/// assert_that!(&bar, has_structure!(Bar[ eq(12), lt(25.0) ]));
///
/// // Matching enum variants ...
/// enum Baz {
///     Var1 { x: i32, y: f64 },
///     Var2(i32, f64)
/// }
/// let var1 = Baz::Var1 { x: 12, y: 23.4 };
/// assert_that!(&var1, has_structure!(Baz::Var1 {
///     x: eq(12),
///     y: lt(25.0)
/// }));
///
/// let var2 = Baz::Var2(12, 23.4);
/// assert_that!(&var2, has_structure!(Baz::Var2 [eq(12), lt(25.0)] ));
/// # }
#[macro_export]
macro_rules! has_structure {
    ( $variant:path { $( $field:ident : $matcher:expr ),* $(,)* } ) => { structure!($variant { $($field : $matcher),* }) };
    ( $variant:path [ $( $matchers:expr ),* ] ) => { structure!($variant [ $($matchers),* ]) }
}
/// Shorter name for `has_structure!`.
#[macro_export]
macro_rules! structure {
    ( $variant:path { $( $field:ident : $matcher:expr ),* $(,)* } ) => {
        Box::new(|actual: &_| {
            use galvanic_assert::{MatchResultBuilder, MatchResult};
            let builder = MatchResultBuilder::for_("has_structure");
            #[allow(unreachable_patterns)]
            match actual {
                &$variant { $( ref $field, )* ..} => {
                    let mut failed_msgs = Vec::new();
                    $(
                        if let MatchResult::Failed{ name, reason } = $matcher.check($field) {
                            failed_msgs.push(
                                format!("Matcher '{}' for field '{}' at {}:{} failed:\n\t{}",
                                        name, stringify!($field), file!().to_string(), line!(), reason)
                            );
                        }
                    )*
                    if failed_msgs.is_empty() { builder.matched() }
                    else { builder.failed_because(&failed_msgs.join("\n")) }
                },
                _ => builder.failed_because(
                        &format!("passed variant does not match '{}'", stringify!($variant))
                )
            }
        })
    };

    (@expand ( $variant:path ; $field:ident ; $m:expr ; $($wildcard:tt),* ) -> ($($body:tt)*) ) => {
        structure!(@generate ($field ; $($body)* ($m ; &$variant($($wildcard,)* ref $field))) )
    };
    (@expand ( $variant:path ; $field:ident ; $m:expr , $($matchers:expr),* ; $($wildcard:tt),* ) -> ($($body:tt)*) ) => {
        structure!(@expand ( $variant ; $field ; $($matchers),* ; $($wildcard,)* _ ) -> ($($body)* ($m ; &$variant($($wildcard,)* ref $field, ..)),) )
    };
    (@generate ($field:ident ; $(($matcher:expr ; $pattern:pat)),*) ) => {
        Box::new(|actual: &_| {
            use galvanic_assert::{MatchResultBuilder, MatchResult};
            let builder = MatchResultBuilder::for_("has_structure");

            let mut failed_msgs = Vec::new();
            $(
                #[allow(unreachable_patterns)]
                match actual {
                    $pattern => if let MatchResult::Failed{ name, reason } = $matcher.check($field) {
                        failed_msgs.push(
                            format!("Matcher '{}' for field '{}' at {}:{} failed:\n\t{}",
                                    name, stringify!($field), file!().to_string(), line!(), reason)
                        );
                    },
                    _ => return builder.failed_because(
                            &format!("passed variant does not match '{}'", stringify!($variant))
                    )
                }
            )*

            if failed_msgs.is_empty() { builder.matched() }
            else { builder.failed_because(&failed_msgs.join("\n")) }
        })
    };
    ( $variant:path [ $( $matchers:expr ),* ] ) => {
        structure![ @expand ( $variant ; x ; $($matchers),* ; ) -> () ]
    };
}