Skip to content

Commit

Permalink
Fix auto_eq to work with &Matcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjacotg committed Jul 19, 2024
1 parent 4cd2cae commit 323a85f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#![doc(hidden)]

/// Auto-ref macro that wrap the expression with `eq(...)` if the expression is
/// Macro that wraps the expression with `eq(...)` if the expression is
/// not a matcher.
///
/// This is useful to let users pass expected value to macro matchers like
Expand All @@ -24,54 +24,57 @@
/// If you are interested in using it in your matcher, please file an issue to
/// stabilize this.
#[macro_export]
macro_rules! __auto_ref_eq {
macro_rules! __auto_eq {
($e:expr) => {{
#[allow(unused_imports)]
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::{
ExpectedKind, MatcherKind,
};
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::ExpectedKind;
match $e {
expected => (&expected).kind().matcher(expected),
expected => {
$crate::matcher_support::__internal_unstable_do_not_depend_on_these::Wrapper(
&expected,
)
.kind()
.matcher(expected)
}
}
}};
}

// This reimplements the pattern presented in
// https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md
// https://github.com/dtolnay/case-studies/issues/14
pub mod internal {
use crate::{
matcher::MatcherBase,
matchers::{eq, EqMatcher},
};

pub struct MatcherTag;
pub struct Wrapper<T>(pub T);

pub trait MatcherKind {
impl<'a, T: MatcherBase> Wrapper<&'a T> {
#[inline]
fn kind(&self) -> MatcherTag {
pub fn kind(&self) -> MatcherTag {
MatcherTag
}
}

impl<M: MatcherBase> MatcherKind for M {}

impl MatcherTag {
pub trait ExpectedKind {
#[inline]
pub fn matcher<M>(self, matcher: M) -> M {
matcher
fn kind(&self) -> ExpectedTag {
ExpectedTag
}
}

pub struct ExpectedTag;
impl<T> ExpectedKind for Wrapper<T> {}

pub trait ExpectedKind {
pub struct MatcherTag;

impl MatcherTag {
#[inline]
fn kind(&self) -> ExpectedTag {
ExpectedTag
pub fn matcher<M>(self, matcher: M) -> M {
matcher
}
}

impl<T> ExpectedKind for &T {}
pub struct ExpectedTag;

impl ExpectedTag {
#[inline]
Expand All @@ -87,11 +90,17 @@ mod tests {

#[test]
fn auto_ref_matcher() -> Result<()> {
verify_that!(123, __auto_ref_eq!(ge(9)))
verify_that!(123, __auto_eq!(ge(9)))
}

#[test]
fn auto_ref_expected() -> Result<()> {
verify_that!(123, __auto_ref_eq!(123))
verify_that!(123, __auto_eq!(123))
}

#[test]
fn auto_ref_on_ref_matcher() -> Result<()> {
let matcher = eq(123);
verify_that!(123, __auto_eq!(&matcher))
}
}
6 changes: 3 additions & 3 deletions googletest/src/matcher_support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
//! these facilities could be useful to downstream users writing custom
//! matchers.
mod auto_ref_eq;
mod auto_eq;
pub(crate) mod count_elements;
pub(crate) mod edit_distance;
pub(crate) mod summarize_diff;
pub(crate) mod zipped_iterator;

pub mod __internal_unstable_do_not_depend_on_these {
pub use super::auto_ref_eq::internal::{ExpectedKind, MatcherKind};
pub use crate::__auto_ref_eq as auto_ref_eq;
pub use super::auto_eq::internal::{ExpectedKind, Wrapper};
pub use crate::__auto_eq as auto_eq;
}
12 changes: 6 additions & 6 deletions googletest/src/matchers/field_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ macro_rules! __field {
macro_rules! field_internal {
(&$($t:ident)::+.$field:tt, ref $m:expr) => {{
use $crate::matchers::__internal_unstable_do_not_depend_on_these::field_matcher;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
field_matcher(
|o: &_| {
match o {
Expand All @@ -211,11 +211,11 @@ macro_rules! field_internal {
}
},
&stringify!($field),
auto_ref_eq!($m))
auto_eq!($m))
}};
(&$($t:ident)::+.$field:tt, $m:expr) => {{
use $crate::matchers::__internal_unstable_do_not_depend_on_these::field_matcher;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
field_matcher(
|o: &&_| {
match o {
Expand All @@ -228,11 +228,11 @@ macro_rules! field_internal {
}
},
&stringify!($field),
auto_ref_eq!($m))
auto_eq!($m))
}};
($($t:ident)::+.$field:tt, $m:expr) => {{
use $crate::matchers::__internal_unstable_do_not_depend_on_these::field_matcher;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
field_matcher(
|o: &_| {
match o {
Expand All @@ -245,7 +245,7 @@ macro_rules! field_internal {
}
},
&stringify!($field),
auto_ref_eq!($m))
auto_eq!($m))
}};
}

Expand Down
16 changes: 8 additions & 8 deletions googletest/src/matchers/property_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,35 +179,35 @@ macro_rules! property_internal {

(&$($t:ident)::+.$method:tt($($argument:tt),* $(,)?), ref $m:expr) => {{
use $crate::matchers::__internal_unstable_do_not_depend_on_these::property_ref_matcher;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
property_ref_matcher(
|o: &$($t)::+| $($t)::+::$method(o, $($argument),*),
&stringify!($method($($argument),*)),
auto_ref_eq!($m))
auto_eq!($m))
}};
($($t:ident)::+.$method:tt($($argument:tt),* $(,)?), ref $m:expr) => {{
use $crate::matchers::__internal_unstable_do_not_depend_on_these::property_ref_matcher;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
property_ref_matcher(
|o: $($t)::+| $($t)::+::$method(o, $($argument),*),
&stringify!($method($($argument),*)),
auto_ref_eq!($m))
auto_eq!($m))
}};
(& $($t:ident)::+.$method:tt($($argument:tt),* $(,)?), $m:expr) => {{
use $crate::matchers::__internal_unstable_do_not_depend_on_these::property_matcher;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
property_matcher(
|o: &&$($t)::+| o.$method($($argument),*),
&stringify!($method($($argument),*)),
auto_ref_eq!($m))
auto_eq!($m))
}};
($($t:ident)::+.$method:tt($($argument:tt),* $(,)?), $m:expr) => {{
use $crate::matchers::__internal_unstable_do_not_depend_on_these::property_matcher;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
property_matcher(
|o: &$($t)::+| o.$method($($argument),*),
&stringify!($method($($argument),*)),
auto_ref_eq!($m))
auto_eq!($m))
}};
}

Expand Down
17 changes: 15 additions & 2 deletions googletest/tests/field_matcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn matches_struct_ref_to_ref_binding_mode() -> Result<()> {
}

#[test]
fn matches_struct_with_auto_ref_eq() -> Result<()> {
fn matches_struct_with_auto_eq() -> Result<()> {
#[derive(Debug)]
struct Strukt {
a_field: String,
Expand All @@ -239,7 +239,7 @@ fn matches_struct_with_auto_ref_eq() -> Result<()> {
}

#[test]
fn matches_enum_with_auto_ref_eq() -> Result<()> {
fn matches_enum_with_auto_eq() -> Result<()> {
#[derive(Debug)]
enum Enum {
Str(String),
Expand All @@ -249,3 +249,16 @@ fn matches_enum_with_auto_ref_eq() -> Result<()> {

verify_that!(Enum::Str("32".into()), field!(Enum::Str.0, "32"))
}

#[test]
fn matches_enum_with_auto_eq_with_wrapper() -> Result<()> {
#[derive(Debug)]
struct Wrapper<I> {
wrapped: I,
}

verify_that!(
Wrapper { wrapped: Wrapper { wrapped: 23 } },
field!(Wrapper.wrapped, field!(Wrapper.wrapped, &23))
)
}
2 changes: 1 addition & 1 deletion googletest/tests/matches_pattern_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ fn matches_copy_struct_property_non_copy() -> Result<()> {
}

#[test]
fn matches_struct_auto_ref_eq() -> Result<()> {
fn matches_struct_auto_eq() -> Result<()> {
#[derive(Debug, Clone)]
struct AStruct {
int: i32,
Expand Down
2 changes: 1 addition & 1 deletion googletest/tests/property_matcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ fn matches_ref_to_ref_with_binding_mode() -> Result<()> {
}

#[test]
fn matches_property_auto_ref_eq() -> Result<()> {
fn matches_property_auto_eq() -> Result<()> {
#[derive(Debug)]
struct Struct;
impl Struct {
Expand Down

0 comments on commit 323a85f

Please sign in to comment.