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

Change mock!'s syntax for traits #205

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

- `mock!` supports a new syntax: "impl Trait for". It has two benefits:
* It can implement a generic trait for specific generic type(s).
* It allows mocking a non-local trait.
The syntax looks like this:
```rust
mock! {
Bar {}
impl Foo<i32> for Bar {
fn foo(&self, x: i32) -> i32;
}
}
```
([#205](https://github.com/asomers/mockall/pull/205))

- `#[automock]` now works on modules even without the `nightly` feature, and no
longer requires `#[feature(proc_macro_hygiene)]`.
([#198](https://github.com/asomers/mockall/pull/198))
Expand Down
22 changes: 11 additions & 11 deletions mockall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,11 @@
//! // Structure to mock
//! C {}
//! // First trait to implement on C
//! trait A {
//! impl A for C {
//! fn foo(&self);
//! }
//! // Second trait to implement on C
//! trait B: A {
//! impl B for C {
//! fn bar(&self);
//! }
//! }
Expand All @@ -770,7 +770,7 @@
//! # use mockall::*;
//! mock! {
//! MyStruct {} // Name of the mock struct, less the "Mock" prefix
//! trait Clone { // definition of the trait to mock
//! impl Clone for MyStruct { // specification of the trait to mock
//! fn clone(&self) -> Self;
//! }
//! }
Expand Down Expand Up @@ -1009,7 +1009,7 @@
//! mock! {
//! pub Bar {}
//! #[async_trait]
//! trait Foo {
//! impl Foo for Bar {
//! async fn foo(&self) -> u32;
//! }
//! }
Expand Down Expand Up @@ -1201,8 +1201,8 @@ pub use mockall_derive::automock;
/// * Real structure name and generics fields
/// * 0 or more methods of the structure, written without bodies, enclosed in a
/// {} block
/// * 0 or more traits to implement for the structure, written like normal
/// traits
/// * 0 or more impl blocks implementing traits on the structure, also without
/// bodies.
///
/// # Examples
///
Expand All @@ -1216,7 +1216,7 @@ pub use mockall_derive::automock;
/// pub MyStruct<T: Clone + 'static> {
/// fn bar(&self) -> u8;
/// }
/// trait Foo {
/// impl Foo for MyStruct {
/// fn foo(&self, x: u32);
/// }
/// }
Expand All @@ -1230,7 +1230,7 @@ pub use mockall_derive::automock;
/// # use mockall_derive::mock;
/// mock!{
/// pub Rc<T: 'static> {}
/// trait AsRef<T> {
/// impl<T: 'static> AsRef<T> for Rc<T> {
/// fn as_ref(&self) -> &T;
/// }
/// }
Expand All @@ -1241,7 +1241,7 @@ pub use mockall_derive::automock;
/// # use mockall_derive::mock;
/// mock!{
/// pub Rc<Q: 'static> {}
/// trait AsRef<T: 'static> {
/// impl<T: 'static> AsRef<T> for Rc<T> {
/// fn as_ref(&self) -> &T;
/// }
/// }
Expand All @@ -1255,7 +1255,7 @@ pub use mockall_derive::automock;
/// # use mockall_derive::mock;
/// mock!{
/// MyIter {}
/// trait Iterator {
/// impl Iterator for MyIter {
/// type Item=u32;
///
/// fn next(&mut self) -> Option<u32>;
Expand All @@ -1268,7 +1268,7 @@ pub use mockall_derive::automock;
/// # use mockall_derive::mock;
/// mock!{
/// MyIter {}
/// trait Iterator {
/// impl Iterator for MyIter {
/// type Item=u32;
///
/// fn next(&mut self) -> Option<<Self as Iterator>::Item>;
Expand Down
2 changes: 1 addition & 1 deletion mockall/tests/mock_associated_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mock! {
const Y: i32 = 69;
fn foo(&self);
}
trait Foo {
impl Foo for Foo {
const X: i32 = 42;
}
}
Expand Down
2 changes: 1 addition & 1 deletion mockall/tests/mock_associated_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use mockall::*;

mock! {
MyIter {}
trait Iterator {
impl Iterator for MyIter {
type Item=u32;

fn next(&mut self) -> Option<u32>;
Expand Down
2 changes: 1 addition & 1 deletion mockall/tests/mock_async_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait Foo {
mock! {
pub Bar { }
#[async_trait]
trait Foo {
impl Foo for Bar {
async fn foo(&self) -> u32;
}
}
Expand Down
2 changes: 1 addition & 1 deletion mockall/tests/mock_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use mockall::*;

mock! {
pub A {}
trait Clone {
impl Clone for A {
fn clone(&self) -> Self;
}
}
Expand Down
24 changes: 24 additions & 0 deletions mockall/tests/mock_concrete_struct_with_generic_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// vim: tw=80
//! A concrete struct that implements a generic trait
#![deny(warnings)]

use mockall::*;

trait Foo<T: 'static> {
fn foo(&self, x: T) -> T;
}
mock! {
Bar {}
impl Foo<i32> for Bar {
fn foo(&self, x: i32) -> i32;
}
}

#[test]
fn returning() {
let mut mock = MockBar::new();
mock.expect_foo()
.with(predicate::eq(42))
.returning(|x| x + 1);
assert_eq!(43, mock.foo(42));
}
2 changes: 1 addition & 1 deletion mockall/tests/mock_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mock!{
fn foo(&self);
}
/// Trait docs
trait Tr {
impl Tr for Foo {
/// Trait method docs
fn bar(&self);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait Foo {

mock!{
MyStruct {}
trait Foo {
impl Foo for MyStruct {
fn foo<T: 'static>(&self, t: T) -> &u32;
}
}
Expand Down
2 changes: 1 addition & 1 deletion mockall/tests/mock_generic_struct_with_generic_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait Foo<T: 'static> {
}
mock! {
Bar<T: 'static, Z: 'static> {}
trait Foo<T: 'static> {
impl<T: 'static, Z: 'static> Foo<T> for Bar<T, Z> {
fn foo(&self, x: T) -> T;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// vim: tw=80
//! A generic struct with a generic trait, using the <= 0.8.0 syntax
#![deny(warnings)]
#![allow(deprecated)]

use mockall::*;

trait Foo<T: 'static> {
fn foo(&self, x: T) -> T;
}
mock! {
Bar<T: 'static, Z: 'static> {}
trait Foo<T: 'static> {
fn foo(&self, x: T) -> T;
}
}

#[test]
fn returning() {
let mut mock = MockBar::<u32, u64>::new();
mock.expect_foo()
.returning(|x| x);
assert_eq!(5u32, mock.foo(5u32));
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait Foo<T> {
}
mock! {
Bar<T: 'static> {}
trait Foo<T> {
impl<T: 'static> Foo<T> for Bar {
fn foo(&self, x: T) -> T;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait Foo<T: 'static> {
}
mock! {
ExternalStruct<T: 'static> {}
trait Foo<T: 'static> {
impl<T: 'static> Foo<T> for ExternalStruct<T> {
fn foo(&self) -> T;
}
}
Expand Down
2 changes: 1 addition & 1 deletion mockall/tests/mock_generic_struct_with_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait Foo {

mock! {
Bar<T: Copy + 'static> {}
trait Foo {
impl<T: Copy + 'static> Foo for Bar<T> {
fn foo(&self, x: u32) -> u32;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use mockall::*;

mock! {
Foo<T: 'static> {}
trait Iterator {
impl<T: 'static> Iterator for Foo<T> {
type Item=T;
fn next(&mut self) -> Option<T>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mock! {
Foo<T: 'static> where T: Clone {
fn foo(&self, t: T) -> T;
}
trait Bar {
impl<T: 'static> Bar for Foo<T> where T: Clone {
fn bar(&self);
}
}
Expand Down
2 changes: 1 addition & 1 deletion mockall/tests/mock_generic_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait Foo {

mock! {
Bar<T: 'static> {}
trait Foo {
impl<T: 'static> Foo for Bar<T> {
fn foo(&self);
}
}
Expand Down
4 changes: 2 additions & 2 deletions mockall/tests/mock_inherited_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ trait B: A {

mock!{
B {}
trait A {
impl A for B {
fn foo(&self);
}
trait B {
impl B for B {
fn bar(&self);
}
}
Expand Down
4 changes: 2 additions & 2 deletions mockall/tests/mock_multiple_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ trait A {}
trait B {}
mock!{
MultiTrait {}
trait A {}
trait B {}
impl A for MultiTrait {}
impl B for MultiTrait {}
}

#[test]
Expand Down
28 changes: 28 additions & 0 deletions mockall/tests/mock_nonlocal_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// vim: tw=80
//! A trait that isn't imported directly into the local namespace
#![deny(warnings)]

use mockall::*;

mod my_module {
pub trait Foo {
fn foo(&self) -> i32;
}
}

mock! {
Bar {}
impl my_module::Foo for Bar {
fn foo(&self) -> i32;
}
}

#[test]
fn returning() {
use my_module::Foo;

let mut mock = MockBar::new();
mock.expect_foo()
.returning(|| 42);
assert_eq!(42, mock.foo());
}
2 changes: 1 addition & 1 deletion mockall/tests/mock_return_anonymous_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mock! {
Foo {
fn inherent_method(&self) -> X<'_>;
}
trait T {
impl T for Foo {
fn trait_method(&self) -> X<'_>;
}
}
Expand Down
2 changes: 1 addition & 1 deletion mockall/tests/mock_struct_with_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait Foo {

mock!{
pub Bar {}
trait Foo {
impl Foo for Bar {
fn foo(&self, x: u32) -> i64;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use mockall::*;

mock! {
pub MyIter {}
trait Iterator {
impl Iterator for MyIter {
type Item=u32;

fn next(&mut self) -> Option<u32>;
Expand Down
2 changes: 1 addition & 1 deletion mockall/tests/mock_trait_returning_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait Foo {

mock! {
pub Bar {}
trait Foo {
impl Foo for Bar {
fn foo(&self) -> &u32;
}
}
Expand Down
2 changes: 1 addition & 1 deletion mockall/tests/mock_trait_with_static_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait Bar {

mock!{
pub Foo {}
trait Bar {
impl Bar for Foo {
fn baz(x: u32) -> u64;
}
}
Expand Down
Loading