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 return_const on mocked generic static methods #141

Merged
merged 1 commit into from
Jun 6, 2020
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 @@ -25,6 +25,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Fixed mocking methods with complicated types including a `super::` component.
([#137](https://github.com/asomers/mockall/pull/137))

- Mocked generic static methods can now use `return_const`. This capability
was omitted as an oversight from PR #47.
([#141](https://github.com/asomers/mockall/pull/141))

### Removed

## [0.7.1] - 3 May 2020
Expand Down
17 changes: 17 additions & 0 deletions mockall/tests/automock_generic_static_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
//! generic static methods with generic arguments

use mockall::*;
use std::sync::Mutex;

lazy_static! {
static ref A_MTX: Mutex<()> = Mutex::new(());
}

#[automock]
trait A {
Expand All @@ -10,8 +15,20 @@ trait A {

#[test]
fn returning() {
let _m = A_MTX.lock().unwrap();

let ctx = MockA::bar_context();
ctx.expect::<i16>()
.returning(|_| 42);
assert_eq!(42, MockA::bar(-1i16));
}

#[test]
fn return_const() {
let _m = A_MTX.lock().unwrap();

let ctx = MockA::bar_context();
ctx.expect::<i16>()
.return_const(42u32);
assert_eq!(42, MockA::bar(-1i16));
}
16 changes: 16 additions & 0 deletions mockall_derive/src/expectation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,22 @@ impl<'a> StaticExpectation<'a> {
.once()
}

/// Just like
/// [`Expectation::return_const`](struct.Expectation.html#method.return_const)
#v fn return_const<MockallOutput>
(&mut self, __mockall_c: MockallOutput)
-> &mut Expectation #tg
where MockallOutput: Clone + Into<#output> + Send + 'static
{
self.guard.store.get_mut(
&::mockall::Key::new::<(#(#argty, )*)>()
).unwrap()
.downcast_mut::<Expectations #tg>()
.unwrap()
.0[self.i]
.return_const(__mockall_c)
}

/// Just like
/// [`Expectation::returning`](struct.Expectation.html#method.returning)
#v fn returning<MockallF>(&mut self, __mockall_f: MockallF)
Expand Down