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

Always drop static methods' Expectations during Drop #443

Merged
merged 1 commit into from
Dec 25, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Static methods' expectations will now be cleared during a panic.
([#443](https://github.com/asomers/mockall/pull/443))

- Methods with a `where Self: ...` clause will now be mocked like concrete
methods, not generic ones. Among other effects, this prevents "unused method
expect" warnings from the latest nightly compiler.
Expand Down
33 changes: 33 additions & 0 deletions mockall/tests/clear_expectations_on_panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// vim: tw=80
//! Static methods' expectations should be dropped during panic.
//!
//! https://github.com/asomers/mockall/issues/442
#![deny(warnings)]

use std::panic;

use mockall::*;

#[automock]
pub trait Foo {
fn foo() -> i32;
}

#[test]
fn drop_expectations_on_panic() {
panic::catch_unwind(|| {
let ctx = MockFoo::foo_context();
ctx.expect()
.times(1)
.return_const(42);
panic!("Panicking!");
}).unwrap_err();

// The previously set expectation should've been cleared during the panic,
// so we must set a new one.
let ctx = MockFoo::foo_context();
ctx.expect()
.times(1)
.return_const(42);
assert_eq!(42, MockFoo::foo());
}
4 changes: 1 addition & 3 deletions mockall_derive/src/mock_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1574,9 +1574,7 @@ impl<'a> ToTokens for Context<'a> {
}
impl #ty_ig Drop for Context #ty_tg #ty_wc {
fn drop(&mut self) {
if !std::thread::panicking() {
Self::do_checkpoint()
}
Self::do_checkpoint()
}
}
).to_tokens(tokens);
Expand Down