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

Mock objects' checkpoint methods no longer check static expectations. #64

Merged
merged 1 commit into from
Nov 2, 2019
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] - ReleaseDate
### Added
### Changed

- Mock objects' checkpoint methods will no longer check static expectations.
This behavior is more useful where static and regular methods are both used
in the same test. The old behavior was a relic from release 0.3.0 and
before, when static methods did not yet have Context objects of their own.
([#64](https://github.com/asomers/mockall/pull/64))

### Fixed

- Fixed hygiene violations in some of mockall_derive's warnings.
Expand Down
24 changes: 16 additions & 8 deletions mockall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,21 +874,29 @@
//! ctx.checkpoint(); // Panics!
//! ```
//!
//! A mock object's checkpoint method works for static methods, too.
//! A mock object's checkpoint method does *not* checkpoint static methods.
//! This behavior is useful when using multiple mock objects at once. For
//! example:
//!
//! ```should_panic
//! ```
//! # use mockall::*;
//! #[automock]
//! pub trait A {
//! fn foo() -> u32;
//! fn build() -> Self;
//! fn bar(&self) -> i32;
//! }
//!
//! let mut mock = MockA::new();
//! let ctx = MockA::foo_context();
//! # fn main() {
//! let ctx = MockA::build_context();
//! ctx.expect()
//! .times(1)
//! .returning(|| 99);
//! mock.checkpoint(); // Also panics!
//! .times(2)
//! .returning(|| MockA::default());
//! let mut mock0 = MockA::build();
//! mock0.expect_bar().return_const(4);
//! mock0.bar();
//! mock0.checkpoint(); // Does not checkpoint the build method
//! let mock1 = MockA::build();
//! # }
//! ```
//!
//! One more thing: Mockall normally creates a zero-argument `new` method for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mock! {
fn foo<Q: 'static>(t: T, q: Q) -> u64;
// We must use a different method for every should_panic test, so the
// shared mutex doesn't get poisoned.
fn foo1<Q: 'static>(t: T, q: Q) -> u64;
fn foo2<Q: 'static>(t: T, q: Q) -> u64;
fn foo3<Q: 'static>(t: T, q: Q) -> u64;
}
Expand All @@ -19,18 +18,18 @@ lazy_static! {
static ref FOO_MTX: Mutex<()> = Mutex::new(());
}

// Checkpointing the mock object should check static methods
// Checkpointing the mock object should not checkpoint static methods too
#[test]
#[should_panic(expected =
"MockFoo::foo1: Expectation(<anything>) called fewer than 1 times")]
fn checkpoint() {
let _m = FOO_MTX.lock().unwrap();

let mut mock = MockFoo::<u32>::new();
let ctx = MockFoo::<u32>::foo1_context();
let ctx = MockFoo::<u32>::foo_context();
ctx.expect::<i16>()
.returning(|_, _| 0)
.times(1..3);
mock.checkpoint();
panic!("Shouldn't get here!");
MockFoo::foo(42u32, 69i16);
}

// It should also be possible to checkpoint just the context object
Expand Down
11 changes: 5 additions & 6 deletions mockall/tests/mock_struct_with_static_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mock!{
Foo {
fn bar(x: u32) -> u64;
// We must have a separate method for every should_panic test
fn bar1(x: u32) -> u64;
fn bar2(x: u32) -> u64;
fn bar3(x: u32) -> u64;
}
Expand All @@ -17,18 +16,18 @@ lazy_static! {
static ref BAR_MTX: Mutex<()> = Mutex::new(());
}

// Checkpointing the mock object should check static methods
// Checkpointing the mock object should not checkpoint static methods
#[test]
#[should_panic(expected =
"MockFoo::bar1: Expectation(<anything>) called fewer than 1 times")]
fn checkpoint() {
let _m = BAR_MTX.lock().unwrap();

let mut mock = MockFoo::new();
let ctx = MockFoo::bar1_context();
let ctx = MockFoo::bar_context();
ctx.expect()
.returning(|_| 32)
.times(1..3);
mock.checkpoint();
panic!("Shouldn't get here!");
MockFoo::bar(0);
}

// It should also be possible to checkpoint just the context object
Expand Down
8 changes: 3 additions & 5 deletions mockall_derive/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,9 @@ fn gen_mock_method(mock_struct_name: &syn::Ident,

// Finally this method's contribution to the checkpoint method
if meth_types.is_static {
quote!(#attrs {
let _timeses = #mod_ident::#ident::EXPECTATIONS.lock().unwrap()
.checkpoint()
.collect::<Vec<_>>();
})
// Don't checkpoint static methods. They get checkpointed by their
// context objects instead.
quote!()
} else {
quote!(#attrs { #expect_obj_name.checkpoint(); })
}.to_tokens(&mut cp_output);
Expand Down