fuchsia_bluetooth/expectation/
prelude.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::{format_err, Context, Error};
6
7use super::*;
8
9/// A shorthand for creating an expectation that `expected` and `actual` are equal and testing the
10/// predicate.
11pub fn expect_eq<'t, T: Debug + PartialEq + Send>(
12    expected: &'t T,
13    actual: &'t T,
14    file: &str,
15    line: u32,
16    expected_name: &str,
17    actual_name: &str,
18) -> Result<(), Error> {
19    let pred: Predicate<&'t T> = Predicate::Equal(
20        Arc::new(expected),
21        Arc::new(|a: &&T, b: &&T| a == b),
22        Arc::new(|t| show_debug(t)),
23    );
24    pred.assert_satisfied(&actual)
25        .map_err(|dstr| format_err!("{}\n{:?}", actual_name, dstr))
26        .context(format_err!(
27            "({}:{}) expect_eq! failed: {} != {}",
28            file,
29            line,
30            expected_name,
31            actual_name
32        ))
33}
34
35#[macro_export]
36macro_rules! expect_eq {
37    ($expected:expr, $actual:expr) => {
38        $crate::expectation::prelude::expect_eq(
39            &$expected,
40            &$actual,
41            file!(),
42            line!(),
43            stringify!($expected),
44            stringify!($actual),
45        )
46    };
47}
48
49#[test]
50fn expect_eq_works() {
51    assert!(expect_eq!("abc", "abc").is_ok());
52    assert!(expect_eq!("abc", "def").is_err());
53}