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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::{format_err, Context, Error};

use super::*;

/// A shorthand for creating an expectation that `expected` and `actual` are equal and testing the
/// predicate.
pub fn expect_eq<'t, T: Debug + PartialEq + Send>(
    expected: &'t T,
    actual: &'t T,
    file: &str,
    line: u32,
    expected_name: &str,
    actual_name: &str,
) -> Result<(), Error> {
    let pred: Predicate<&'t T> = Predicate::Equal(
        Arc::new(expected),
        Arc::new(|a: &&T, b: &&T| a == b),
        Arc::new(|t| show_debug(t)),
    );
    pred.assert_satisfied(&actual)
        .map_err(|dstr| format_err!("{}\n{:?}", actual_name, dstr))
        .context(format_err!(
            "({}:{}) expect_eq! failed: {} != {}",
            file,
            line,
            expected_name,
            actual_name
        ))
}

#[macro_export]
macro_rules! expect_eq {
    ($expected:expr, $actual:expr) => {
        $crate::expectation::prelude::expect_eq(
            &$expected,
            &$actual,
            file!(),
            line!(),
            stringify!($expected),
            stringify!($actual),
        )
    };
}

#[test]
fn expect_eq_works() {
    assert!(expect_eq!("abc", "abc").is_ok());
    assert!(expect_eq!("abc", "def").is_err());
}