Skip to content

Commit 2270cdb

Browse files
authored
Merge pull request #686 from Kranzes/automock-auto_impl-compat
Add compatibility with the `#[auto_impl]` macro
2 parents 055886e + d3b041d commit 2270cdb

4 files changed

Lines changed: 45 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## [ Unreleased ] - ReleaseDate
7+
8+
### Added
9+
10+
- Compatibility with the `#[auto_impl]` macro.
11+
([#686](https://github.com/asomers/mockall/pull/686))
12+
613
## [ 0.14.0 ] - 2025-11-22
714

815
### Added

mockall/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ mockall_derive = { version = "=0.14.0", path = "../mockall_derive" }
4949
trait-variant = "0.1.2"
5050
async-trait = "0.1.38"
5151
auto_enums = "0.8.5"
52+
auto_impl = "1.3.0"
5253
futures = "0.3.7"
5354
mockall_double = { version = "^0.3.1", path = "../mockall_double" }
5455
serde = "1.0.113"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// vim: tw=80
2+
//! A trait that uses both `#[automock]` and `#[auto_impl]`.
3+
//!
4+
//! `#[auto_impl]` only applies to trait definitions, so `#[automock]` must not
5+
//! copy it onto the generated mock.
6+
#![deny(warnings)]
7+
8+
use auto_impl::auto_impl;
9+
use mockall::*;
10+
11+
#[automock]
12+
#[auto_impl(&, Box)]
13+
pub trait Foo {
14+
fn foo(&self, x: u32) -> u32;
15+
}
16+
17+
#[test]
18+
fn returning() {
19+
let mut mock = MockFoo::new();
20+
mock.expect_foo()
21+
.returning(|x| x + 1);
22+
assert_eq!(5, mock.foo(4));
23+
}
24+
25+
// The mock is reachable through auto_impl's generated impl for Box.
26+
#[test]
27+
fn boxed() {
28+
let mut mock = MockFoo::new();
29+
mock.expect_foo()
30+
.returning(|x| x + 1);
31+
let boxed: Box<dyn Foo> = Box::new(mock);
32+
assert_eq!(5, boxed.foo(4));
33+
}

mockall_derive/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,10 @@ impl<'a> AttrFormatter<'a> {
878878
// Ignore auto_enum, because we transform the return value
879879
// into a trait object.
880880
false
881+
} else if *i.as_ref().unwrap() == "auto_impl" {
882+
// Ignore auto_impl, because it only applies to trait
883+
// definitions, not the generated mock struct or impl.
884+
false
881885
} else {
882886
true
883887
}

0 commit comments

Comments
 (0)