Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: matching! mismatch debug for references when matching against a … #42

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Fixed
- Fix `matching!` against references to number literals ([#42](https://github.com/audunhalland/unimock/pull/42))

## [0.5.8]
### Fixed
- Mutation when generics are involved. ([#38](https://github.com/audunhalland/unimock/pull/38))
Expand Down
8 changes: 4 additions & 4 deletions tests/it/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ mod no_debug {

#[test]
fn can_match_a_non_debug_argument() {
match Unimock::new(VeryPrimitiveMock::primitive.stub(|each| {
let unimock = Unimock::new(VeryPrimitiveMock::primitive.stub(|each| {
each.call(matching!(PrimitiveEnum::Bar, _))
.answers(|_| PrimitiveEnum::Foo);
}))
.primitive(PrimitiveEnum::Bar, "")
{
}));

match unimock.primitive(PrimitiveEnum::Bar, "") {
PrimitiveEnum::Foo => {}
PrimitiveEnum::Bar => panic!(),
}
Expand Down
14 changes: 14 additions & 0 deletions tests/it/matching_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,17 @@ fn matching_str_or() {
);
u.f("a".to_string());
}

mod debug_matching_literal_autoref_debug {
use unimock::*;

#[unimock(api = TestMock)]
trait Test {
fn f(&self, arg: &i32);
}

#[test]
fn test() {
TestMock::f.next_call(matching!(42)).returns_default();
}
}
15 changes: 13 additions & 2 deletions unimock_macros/src/matching/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,23 @@ impl ArgMatcher {
let doc_lit =
syn::LitStr::new(doc_string.as_str(), proc_macro2::Span::call_site());

// If the matching! uses a literal, just format! the mismatch.
// This is done to prevent a problem with `.unimock_try_debug()` for e.g. `&i32`:
// The `&i32` as a mock argument will be `&&i32` in this context, and thus
// autoref specialization runs into a problem.
let mismatch_debug = match pat {
syn::Pat::Lit(_) => quote! {
Some(::unimock::private::lib::format!("{mismatch:?}"))
},
_ => quote! { mismatch.unimock_try_debug() },
};

Some(quote! {
match #arg_expr {
#pat => {}
mismatch => {
use ::unimock::private::{ProperDebug, NoDebug};
reporter.pat_fail(#index, mismatch.unimock_try_debug(), Some(#doc_lit));
reporter.pat_fail(#index, #mismatch_debug, Some(#doc_lit));
}
}
})
Expand Down Expand Up @@ -342,7 +353,7 @@ impl CompareMacro {
}
}

fn concat_args_parenthesized<F>(args: &Vec<Arg>, f: F) -> proc_macro2::TokenStream
fn concat_args_parenthesized<F>(args: &[Arg], f: F) -> proc_macro2::TokenStream
where
F: Fn(&Arg) -> proc_macro2::TokenStream,
{
Expand Down
Loading