Skip to content

Commit

Permalink
Merge pull request #590 from asomers/auto_enum
Browse files Browse the repository at this point in the history
Add compatibility with auto_enums
  • Loading branch information
asomers authored Jul 6, 2024
2 parents f9edf41 + 57d16b5 commit 803873a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Add the ability to mock methods that use `#[auto_enum]`, from the
`auto_enums` crate. But only for methods that use RPIT; Mockall can't yet
handle syntax like `-> Result<(), impl T>`
([#590](https://github.com/asomers/mockall/pull/590))

- Add the ability to mock methods that use `#[inline]` or `#[cold]`, and
methods or traits that use `#[must_use]`.
([#555](https://github.com/asomers/mockall/pull/555))
Expand Down
1 change: 1 addition & 0 deletions mockall/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mockall_derive = { version = "=0.12.1", path = "../mockall_derive" }

[dev-dependencies]
async-trait = "0.1.38"
auto_enums = "0.8.5"
futures = "0.3.7"
mockall_double = { version = "^0.3.1", path = "../mockall_double" }
serde = "1.0.113"
Expand Down
35 changes: 35 additions & 0 deletions mockall/tests/auto_enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// vim: tw=80
//! A method that uses `#[auto_enum]`, from the auto_enums crate.
#![deny(warnings)]

use auto_enums::auto_enum;
use futures::{Future, FutureExt, future};
use mockall::*;

pub struct Foo{}

#[automock]
impl Foo {
#[auto_enum(Future)]
pub fn sign(&self, x: i32) -> impl Future<Output=i32>
{
if x > 0 {
future::ready(1)
} else {
future::ready(1).then(|_| future::ready(-1))
}
}
}

#[test]
fn rpit() {
let mut mock = MockFoo::new();
mock.expect_sign()
.returning(|_| {
Box::pin(future::ready(42))
});
let r = mock.sign(101)
.now_or_never()
.unwrap();
assert_eq!(42, r);
}
2 changes: 1 addition & 1 deletion mockall/tests/automock_impl_future.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// vim: tw=80
//! A trait with a constructor method that returns impl Future<...>.
//! A trait with a method that returns impl Future<...>.
//!
//! This needs special handling, because Box<dyn Future<...>> is pretty useless.
//! You need Pin<Box<dyn Future<...>>> instead.
Expand Down
4 changes: 4 additions & 0 deletions mockall_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,10 @@ impl<'a> AttrFormatter<'a> {
false
} else if *i.as_ref().unwrap() == "must_use" {
self.must_use
} else if *i.as_ref().unwrap() == "auto_enum" {
// Ignore auto_enum, because we transform the return value
// into a trait object.
false
} else {
true
}
Expand Down

0 comments on commit 803873a

Please sign in to comment.