|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use crate::matcher::{MatchExplanation, Matcher, MatcherResult}; |
| 16 | +use crate::matchers::eq_matcher::create_diff; |
| 17 | +use std::{fmt::Debug, marker::PhantomData, ops::Deref}; |
| 18 | + |
| 19 | +/// Matches a value equal (in the sense of `==`) to the dereferenced value of |
| 20 | +/// `expected`. |
| 21 | +/// |
| 22 | +/// This is similar to [`eq`][crate::matchers::eq] but takes a reference or |
| 23 | +/// smart pointer to the expected value rather than consuming it. This is useful |
| 24 | +/// when: |
| 25 | +/// |
| 26 | +/// * one has only a reference to the expected value, and |
| 27 | +/// * the expected value cannot or should not be copied or cloned to create an |
| 28 | +/// owned value from it. |
| 29 | +/// |
| 30 | +/// ``` |
| 31 | +/// # use googletest::{matchers::eq_deref_of, verify_that}; |
| 32 | +/// #[derive(Debug, PartialEq)] |
| 33 | +/// struct NonCloneableStruct(i32); |
| 34 | +/// let expected = NonCloneableStruct(123); |
| 35 | +/// verify_that!(NonCloneableStruct(123), eq_deref_of(&expected)) |
| 36 | +/// # .unwrap() |
| 37 | +/// ``` |
| 38 | +/// |
| 39 | +/// **Note**: while one can use `eq_deref_of` with the configuration methods of |
| 40 | +/// [`StrMatcherConfigurator`][crate::matchers::str_matcher::StrMatcherConfigurator] |
| 41 | +/// to configure string equality, it is not possible to do so when the input is |
| 42 | +/// a smart pointer to a string. |
| 43 | +/// |
| 44 | +/// ```compile_fail |
| 45 | +/// # use googletest::{matchers::{eq_deref_of, str_matcher::StrMatcherConfigurator}, verify_that}; |
| 46 | +/// verify_that!("A string", eq_deref_of(Box::new("A STRING")).ignoring_ascii_case()) // Does not compile |
| 47 | +/// # .unwrap() |
| 48 | +/// ``` |
| 49 | +/// |
| 50 | +/// Otherwise, this has the same behaviour as [`eq`][crate::matchers::eq]. |
| 51 | +pub fn eq_deref_of<ActualT: ?Sized, ExpectedRefT>( |
| 52 | + expected: ExpectedRefT, |
| 53 | +) -> EqDerefOfMatcher<ActualT, ExpectedRefT> { |
| 54 | + EqDerefOfMatcher { expected, phantom: Default::default() } |
| 55 | +} |
| 56 | + |
| 57 | +/// A matcher which matches a value equal to the derefenced value of `expected`. |
| 58 | +/// |
| 59 | +/// See [`eq_deref_of`]. |
| 60 | +pub struct EqDerefOfMatcher<ActualT: ?Sized, ExpectedRefT> { |
| 61 | + pub(crate) expected: ExpectedRefT, |
| 62 | + phantom: PhantomData<ActualT>, |
| 63 | +} |
| 64 | + |
| 65 | +impl<ActualT, ExpectedRefT, ExpectedT> Matcher for EqDerefOfMatcher<ActualT, ExpectedRefT> |
| 66 | +where |
| 67 | + ActualT: Debug + ?Sized, |
| 68 | + ExpectedRefT: Deref<Target = ExpectedT> + Debug, |
| 69 | + ExpectedT: PartialEq<ActualT> + Debug, |
| 70 | +{ |
| 71 | + type ActualT = ActualT; |
| 72 | + |
| 73 | + fn matches(&self, actual: &ActualT) -> MatcherResult { |
| 74 | + (self.expected.deref() == actual).into() |
| 75 | + } |
| 76 | + |
| 77 | + fn describe(&self, matcher_result: MatcherResult) -> String { |
| 78 | + match matcher_result { |
| 79 | + MatcherResult::Matches => format!("is equal to {:?}", self.expected), |
| 80 | + MatcherResult::DoesNotMatch => format!("isn't equal to {:?}", self.expected), |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + fn explain_match(&self, actual: &ActualT) -> MatchExplanation { |
| 85 | + create_diff( |
| 86 | + &format!("{:#?}", self.expected.deref()), |
| 87 | + &format!("{:#?}", actual), |
| 88 | + &self.describe(self.matches(actual)), |
| 89 | + ) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[cfg(test)] |
| 94 | +mod tests { |
| 95 | + use super::eq_deref_of; |
| 96 | + use crate::prelude::*; |
| 97 | + use indoc::indoc; |
| 98 | + |
| 99 | + #[derive(Debug, PartialEq)] |
| 100 | + struct NonCloneNonCopyStruct(i32); |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn matches_value_with_ref_to_equal_value() -> Result<()> { |
| 104 | + verify_that!(NonCloneNonCopyStruct(123), eq_deref_of(&NonCloneNonCopyStruct(123))) |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn matches_value_with_box_of_equal_value() -> Result<()> { |
| 109 | + verify_that!(NonCloneNonCopyStruct(123), eq_deref_of(Box::new(NonCloneNonCopyStruct(123)))) |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn does_not_match_value_with_non_equal_value() -> Result<()> { |
| 114 | + verify_that!(NonCloneNonCopyStruct(123), not(eq_deref_of(&NonCloneNonCopyStruct(234)))) |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn shows_structured_diff() -> Result<()> { |
| 119 | + #[derive(Debug, PartialEq)] |
| 120 | + struct Strukt { |
| 121 | + int: i32, |
| 122 | + string: String, |
| 123 | + } |
| 124 | + |
| 125 | + let result = verify_that!( |
| 126 | + Strukt { int: 123, string: "something".into() }, |
| 127 | + eq_deref_of(Box::new(Strukt { int: 321, string: "someone".into() })) |
| 128 | + ); |
| 129 | + verify_that!( |
| 130 | + result, |
| 131 | + err(displays_as(contains_substring(indoc! { |
| 132 | + r#" |
| 133 | + Value of: Strukt { int: 123, string: "something".into() } |
| 134 | + Expected: is equal to Strukt { int: 321, string: "someone" } |
| 135 | + Actual: Strukt { |
| 136 | + int: 123, |
| 137 | + string: "something", |
| 138 | + }, which isn't equal to Strukt { int: 321, string: "someone" } |
| 139 | + Debug diff: |
| 140 | + Strukt { |
| 141 | + + int: 123, |
| 142 | + - int: 321, |
| 143 | + + string: "something", |
| 144 | + - string: "someone", |
| 145 | + } |
| 146 | + "#}))) |
| 147 | + ) |
| 148 | + } |
| 149 | +} |
0 commit comments