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 mocking functions that use raw identifiers for their names. #534

Merged
merged 1 commit into from
Dec 21, 2023
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 @@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
trait bounds, yet are still object safe.
([#531](https://github.com/asomers/mockall/pull/531))

- Fixed mocking methods that use raw identifiers for their names. This was a
regression in 0.12.0.
([#534](https://github.com/asomers/mockall/pull/534))

## [ 0.12.0 ] - 2023-12-10

### Added
Expand Down
72 changes: 72 additions & 0 deletions mockall/tests/raw_identifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// vim: tw=80
//! It should be possible to mock things that use raw identifiers
#![deny(warnings)]
#![allow(non_camel_case_types)]

use mockall::*;

#[automock]
trait r#while {
fn r#match(&self);
fn r#loop();
}

#[automock]
pub mod r#break {
pub fn r#if() {unimplemented!() }
}

mock! {
r#do {}
impl r#while for r#do {
fn r#match(&self);
fn r#loop();
}
}

struct r#else {}
#[automock]
impl r#while for r#else {
fn r#match(&self) {unimplemented!()}
fn r#loop() {unimplemented!()}
}

#[test]
fn by_ref() {
let mut foo = Mockwhile::new();
foo.expect_match()
.return_const(());
foo.r#match();
}

#[test]
fn static_method() {
let ctx = Mockwhile::loop_context();
ctx.expect()
.returning(|| ());
Mockwhile::r#loop();
}

#[test]
fn manual_mock() {
let mut foo = Mockdo::new();
foo.expect_match()
.return_const(());
foo.r#match();
}

#[test]
fn module() {
let ctx = mock_break::if_context();
ctx.expect()
.returning(|| ());
mock_break::r#if();
}

#[test]
fn trait_impl() {
let mut mock = Mockelse::new();
mock.expect_match()
.returning(|| ());
mock.r#match();
}
4 changes: 2 additions & 2 deletions mockall_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,14 +970,14 @@ fn gen_keyid(g: &Generics) -> impl ToTokens {

/// Generate a mock identifier from the regular one: eg "Foo" => "MockFoo"
fn gen_mock_ident(ident: &Ident) -> Ident {
format_ident!("Mock{ident}")
format_ident!("Mock{}", ident)
}

/// Generate an identifier for the mock struct's private module: eg "Foo" =>
/// "__mock_Foo"
fn gen_mod_ident(struct_: &Ident, trait_: Option<&Ident>) -> Ident {
if let Some(t) = trait_ {
format_ident!("__mock_{struct_}_{t}")
format_ident!("__mock_{struct_}_{}", t)
} else {
format_ident!("__mock_{struct_}")
}
Expand Down
2 changes: 1 addition & 1 deletion mockall_derive/src/mock_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ impl MockFunction {
.doc(false)
.format();
let name = self.name();
let expect_ident = format_ident!("expect_{name}");
let expect_ident = format_ident!("expect_{}", name);
let expectation_obj = self.expectation_obj(self_args);
let funcname = &self.sig.ident;
let (_, tg, _) = if self.is_method_generic() {
Expand Down