-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow mocking methods that return references to trait objects
Fixes #210
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// vim: tw=80 | ||
//! a method that returns a reference to a trait object | ||
#![deny(warnings)] | ||
|
||
use mockall::*; | ||
use std::fmt::Debug; | ||
|
||
trait T: Debug + Sync { | ||
fn mutate(&mut self) {} | ||
} | ||
|
||
impl T for u32 {} | ||
|
||
impl<Q> T for Q where Q: Debug + Sync + AsMut<dyn T> {} | ||
|
||
mock!{ | ||
Foo { | ||
fn foo(&self) -> &dyn Debug; | ||
fn bar(&self) -> &'static dyn T; | ||
fn baz(&mut self) -> &mut dyn T; | ||
} | ||
} | ||
|
||
#[test] | ||
fn return_const() { | ||
let mut mock = MockFoo::new(); | ||
mock.expect_foo() | ||
.return_const(Box::new(42u32) as Box<dyn Debug>); | ||
|
||
assert_eq!("42", format!("{:?}", mock.foo())); | ||
} | ||
|
||
#[test] | ||
fn static_ref() { | ||
let mut mock = MockFoo::new(); | ||
mock.expect_bar() | ||
.return_const(&42u32 as &dyn T); | ||
|
||
assert_eq!("42", format!("{:?}", mock.bar())); | ||
} | ||
|
||
#[test] | ||
fn return_var() { | ||
let mut mock = MockFoo::new(); | ||
mock.expect_baz() | ||
.return_var(Box::new(42u32) as Box<dyn T>); | ||
|
||
mock.baz().mutate(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters