From 71dc0bba07244070f83701209f557ac7dae12f44 Mon Sep 17 00:00:00 2001 From: Piotr Maks Date: Thu, 3 Sep 2020 10:45:23 +0200 Subject: [PATCH 1/4] Fix doc comment --- mockall/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mockall/src/lib.rs b/mockall/src/lib.rs index cfc7e440..edab48f7 100644 --- a/mockall/src/lib.rs +++ b/mockall/src/lib.rs @@ -1,8 +1,8 @@ // vim: tw=80 //! A powerful mock object library for Rust. //! -//! Mockall provides provides tools to create mock versions of almost any trait -//! or struct. They can be used in unit tests as a stand-in for the real +//! Mockall provides tools to create mock versions of almost any trait +//! or struct. They can be used in unit tests as a stand-in for the real //! object. //! //! # Usage From 56644b9685345b063562dd8222b52379c57d3305 Mon Sep 17 00:00:00 2001 From: Piotr Maks Date: Thu, 3 Sep 2020 12:59:58 +0200 Subject: [PATCH 2/4] Mock method with `String` or `Vec arguments --- mockall/tests/automock_string_arguments.rs | 31 +++++++++++++++++ mockall/tests/automock_vec_arguments.rs | 35 +++++++++++++++++++ mockall/tests/mock_string_arguments.rs | 36 +++++++++++++++++++ mockall/tests/mock_vec_arguments.rs | 40 ++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 mockall/tests/automock_string_arguments.rs create mode 100644 mockall/tests/automock_vec_arguments.rs create mode 100644 mockall/tests/mock_string_arguments.rs create mode 100644 mockall/tests/mock_vec_arguments.rs diff --git a/mockall/tests/automock_string_arguments.rs b/mockall/tests/automock_string_arguments.rs new file mode 100644 index 00000000..2bde84b2 --- /dev/null +++ b/mockall/tests/automock_string_arguments.rs @@ -0,0 +1,31 @@ +// vim: tw=80 +//! A method with `String` arguments +#![deny(warnings)] + +use mockall::*; + +#[automock] +trait Foo { + fn foo(&self, x: String); +} + +mod withf { + use super::*; + + #[test] + #[should_panic(expected = "MockFoo::foo: No matching expectation found")] + fn fail() { + let mut mock = MockFoo::new(); + mock.expect_foo().withf(|sl| sl == "abc").return_const(()); + let x = String::from("abcd"); + mock.foo(x); + } + + #[test] + fn ok() { + let mut mock = MockFoo::new(); + mock.expect_foo().withf(|sl| sl == "abc").return_const(()); + let x = String::from("abc"); + mock.foo(x); + } +} diff --git a/mockall/tests/automock_vec_arguments.rs b/mockall/tests/automock_vec_arguments.rs new file mode 100644 index 00000000..30659154 --- /dev/null +++ b/mockall/tests/automock_vec_arguments.rs @@ -0,0 +1,35 @@ +// vim: tw=80 +//! A method with `Vec` arguments +#![deny(warnings)] + +use mockall::*; + +#[automock] +trait Foo { + fn foo(&self, x: Vec); +} + +mod withf { + use super::*; + + #[test] + #[should_panic(expected = "MockFoo::foo: No matching expectation found")] + fn fail() { + let mut mock = MockFoo::new(); + mock.expect_foo::() + .withf(|sl| sl == &vec![1, 2, 3]) + .return_const(()); + let x = vec![1, 2, 3, 4]; + mock.foo(x); + } + + #[test] + fn ok() { + let mut mock = MockFoo::new(); + mock.expect_foo::() + .withf(|sl| sl == &vec![1, 2, 3]) + .return_const(()); + let x = vec![1, 2, 3]; + mock.foo(x); + } +} diff --git a/mockall/tests/mock_string_arguments.rs b/mockall/tests/mock_string_arguments.rs new file mode 100644 index 00000000..fa058ffd --- /dev/null +++ b/mockall/tests/mock_string_arguments.rs @@ -0,0 +1,36 @@ +// vim: tw=80 +//! A method with `String` arguments +#![deny(warnings)] + +use mockall::*; + +trait Foo { + fn foo(&self, x: String); +} + +mock! { + Foo { + fn foo(&self, x: String); + } +} + +mod withf { + use super::*; + + #[test] + #[should_panic(expected = "MockFoo::foo: No matching expectation found")] + fn fail() { + let mut mock = MockFoo::new(); + mock.expect_foo().withf(|sl| sl == "abc").return_const(()); + let x = String::from("abcd"); + mock.foo(x); + } + + #[test] + fn ok() { + let mut mock = MockFoo::new(); + mock.expect_foo().withf(|sl| sl == "abc").return_const(()); + let x = String::from("abc"); + mock.foo(x); + } +} diff --git a/mockall/tests/mock_vec_arguments.rs b/mockall/tests/mock_vec_arguments.rs new file mode 100644 index 00000000..666130c9 --- /dev/null +++ b/mockall/tests/mock_vec_arguments.rs @@ -0,0 +1,40 @@ +// vim: tw=80 +//! A method with `Vec` arguments +#![deny(warnings)] + +use mockall::*; + +trait Foo { + fn foo(&self, x: Vec); +} + +mock! { + Foo { + fn foo(&self, x: Vec); + } +} + +mod withf { + use super::*; + + #[test] + #[should_panic(expected = "MockFoo::foo: No matching expectation found")] + fn fail() { + let mut mock = MockFoo::new(); + mock.expect_foo::() + .withf(|sl| sl == &vec![1, 2, 3]) + .return_const(()); + let x = vec![1, 2, 3, 4]; + mock.foo(x); + } + + #[test] + fn ok() { + let mut mock = MockFoo::new(); + mock.expect_foo::() + .withf(|sl| sl == &vec![1, 2, 3]) + .return_const(()); + let x = vec![1, 2, 3]; + mock.foo(x); + } +} From 7ed1cab2024d166a1888d9f5bda27fdfec8b8ad2 Mon Sep 17 00:00:00 2001 From: Piotr Maks Date: Thu, 3 Sep 2020 14:03:55 +0200 Subject: [PATCH 3/4] Allow `clippy::ptr_arg` --- mockall_derive/src/mock_function.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mockall_derive/src/mock_function.rs b/mockall_derive/src/mock_function.rs index 04b2144a..2c96c971 100644 --- a/mockall_derive/src/mock_function.rs +++ b/mockall_derive/src/mock_function.rs @@ -821,6 +821,7 @@ impl<'a> ToTokens for Common<'a> { self.times.is_done() } + #[allow(clippy::ptr_arg)] fn matches #lg (&self, #( #argnames: &#predty, )*) -> bool { self.matcher.lock().unwrap().matches(#(#argnames, )*) } @@ -934,6 +935,7 @@ impl<'a> ToTokens for CommonExpectationMethods<'a> { } /// Validate this expectation's matcher. + #[allow(clippy::ptr_arg)] fn matches #lg (&self, #(#argnames: &#predty, )*) -> bool { self.common.matches(#(#argnames, )*) } @@ -1536,6 +1538,7 @@ impl<'a> ToTokens for Matcher<'a> { _Phantom(Box) } impl #ig Matcher #tg #wc { + #[allow(clippy::ptr_arg)] fn matches #lg (&self, #( #argnames: &#predty, )*) -> bool { match self { Matcher::Always => true, From c3a682b52e77f01518c43b5aeac90de6e881cce7 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sat, 5 Sep 2020 10:02:17 -0600 Subject: [PATCH 4/4] Improve mock_deref_args tests: * There is no need to duplicate the tests for both automock and mock!. Especially since version 0.8.0, there is less duplicated code, and most features don't need to be tested both ways. * Test with as well as withf * Add other Deref types to the test --- mockall/tests/automock_string_arguments.rs | 31 ------ mockall/tests/automock_vec_arguments.rs | 35 ------ mockall/tests/mock_deref_args.rs | 120 +++++++++++++++++++++ mockall/tests/mock_string_arguments.rs | 36 ------- mockall/tests/mock_vec_arguments.rs | 40 ------- 5 files changed, 120 insertions(+), 142 deletions(-) delete mode 100644 mockall/tests/automock_string_arguments.rs delete mode 100644 mockall/tests/automock_vec_arguments.rs create mode 100644 mockall/tests/mock_deref_args.rs delete mode 100644 mockall/tests/mock_string_arguments.rs delete mode 100644 mockall/tests/mock_vec_arguments.rs diff --git a/mockall/tests/automock_string_arguments.rs b/mockall/tests/automock_string_arguments.rs deleted file mode 100644 index 2bde84b2..00000000 --- a/mockall/tests/automock_string_arguments.rs +++ /dev/null @@ -1,31 +0,0 @@ -// vim: tw=80 -//! A method with `String` arguments -#![deny(warnings)] - -use mockall::*; - -#[automock] -trait Foo { - fn foo(&self, x: String); -} - -mod withf { - use super::*; - - #[test] - #[should_panic(expected = "MockFoo::foo: No matching expectation found")] - fn fail() { - let mut mock = MockFoo::new(); - mock.expect_foo().withf(|sl| sl == "abc").return_const(()); - let x = String::from("abcd"); - mock.foo(x); - } - - #[test] - fn ok() { - let mut mock = MockFoo::new(); - mock.expect_foo().withf(|sl| sl == "abc").return_const(()); - let x = String::from("abc"); - mock.foo(x); - } -} diff --git a/mockall/tests/automock_vec_arguments.rs b/mockall/tests/automock_vec_arguments.rs deleted file mode 100644 index 30659154..00000000 --- a/mockall/tests/automock_vec_arguments.rs +++ /dev/null @@ -1,35 +0,0 @@ -// vim: tw=80 -//! A method with `Vec` arguments -#![deny(warnings)] - -use mockall::*; - -#[automock] -trait Foo { - fn foo(&self, x: Vec); -} - -mod withf { - use super::*; - - #[test] - #[should_panic(expected = "MockFoo::foo: No matching expectation found")] - fn fail() { - let mut mock = MockFoo::new(); - mock.expect_foo::() - .withf(|sl| sl == &vec![1, 2, 3]) - .return_const(()); - let x = vec![1, 2, 3, 4]; - mock.foo(x); - } - - #[test] - fn ok() { - let mut mock = MockFoo::new(); - mock.expect_foo::() - .withf(|sl| sl == &vec![1, 2, 3]) - .return_const(()); - let x = vec![1, 2, 3]; - mock.foo(x); - } -} diff --git a/mockall/tests/mock_deref_args.rs b/mockall/tests/mock_deref_args.rs new file mode 100644 index 00000000..d0789d64 --- /dev/null +++ b/mockall/tests/mock_deref_args.rs @@ -0,0 +1,120 @@ +// vim: tw=80 +//! A method whose argument is a common `Deref` implementor +#![deny(warnings)] + +use mockall::*; +use std::{ + ffi::{CStr, CString, OsStr, OsString}, + path::{Path, PathBuf}, +}; + +mock! { + Foo { + fn foo(&self, x: Vec); + fn bar(&self, x: String); + fn baz(&self, x: CString); + fn bean(&self, x: OsString); + fn boom(&self, x: PathBuf); + } +} + +mod with { + use super::*; + + #[test] + fn cstr() { + let mut mock = MockFoo::new(); + mock.expect_baz() + .with(predicate::eq(CString::new("xxx").unwrap())) + .return_const(()); + mock.baz(CString::new("xxx").unwrap()); + } + + #[test] + fn osstr() { + let mut mock = MockFoo::new(); + mock.expect_bean() + .with(predicate::eq(OsStr::new("xxx").to_owned())) + .return_const(()); + mock.bean(OsString::from("xxx")); + } + + #[test] + fn path() { + let mut mock = MockFoo::new(); + mock.expect_boom() + .with(predicate::eq(Path::new("dir/file").to_owned())) + .return_const(()); + mock.boom(PathBuf::from("dir/file")); + } + + #[test] + fn string() { + let mut mock = MockFoo::new(); + mock.expect_bar() + .with(predicate::eq("xxx".to_owned())) + .return_const(()); + mock.bar(String::from("xxx")); + } + + #[test] + fn vec() { + let mut mock = MockFoo::new(); + mock.expect_foo::() + .with(predicate::eq(vec![1, 2, 3])) + .return_const(()); + mock.foo(vec![1, 2, 3]); + } +} + +mod withf { + use super::*; + + #[test] + fn cstr() { + let mut mock = MockFoo::new(); + const WANT: [u8; 4] = [120u8, 120, 120, 0]; + let want = CStr::from_bytes_with_nul(&WANT[..]).unwrap(); + mock.expect_baz() + .withf(move |s| s.as_c_str() == want) + .return_const(()); + mock.baz(CString::new("xxx").unwrap()); + } + + #[test] + fn osstr() { + let mut mock = MockFoo::new(); + mock.expect_bean() + .withf(move |s| s.as_os_str() == OsStr::new("xxx")) + .return_const(()); + mock.bean(OsString::from("xxx")); + } + + #[test] + fn path() { + let mut mock = MockFoo::new(); + mock.expect_boom() + .withf(move |s| s.as_path() == Path::new("dir/file")) + .return_const(()); + mock.boom(PathBuf::from("dir/file")); + } + + #[test] + fn string() { + let mut mock = MockFoo::new(); + mock.expect_bar() + .withf(|sl: &String| sl == "xxx") + .return_const(()); + mock.bar(String::from("xxx")); + } + + #[test] + fn vec() { + let mut mock = MockFoo::new(); + mock.expect_foo::() + .withf(|sl: &Vec| sl == &[1, 2, 3]) + .return_const(()); + mock.foo(vec![1, 2, 3]); + } + +} diff --git a/mockall/tests/mock_string_arguments.rs b/mockall/tests/mock_string_arguments.rs deleted file mode 100644 index fa058ffd..00000000 --- a/mockall/tests/mock_string_arguments.rs +++ /dev/null @@ -1,36 +0,0 @@ -// vim: tw=80 -//! A method with `String` arguments -#![deny(warnings)] - -use mockall::*; - -trait Foo { - fn foo(&self, x: String); -} - -mock! { - Foo { - fn foo(&self, x: String); - } -} - -mod withf { - use super::*; - - #[test] - #[should_panic(expected = "MockFoo::foo: No matching expectation found")] - fn fail() { - let mut mock = MockFoo::new(); - mock.expect_foo().withf(|sl| sl == "abc").return_const(()); - let x = String::from("abcd"); - mock.foo(x); - } - - #[test] - fn ok() { - let mut mock = MockFoo::new(); - mock.expect_foo().withf(|sl| sl == "abc").return_const(()); - let x = String::from("abc"); - mock.foo(x); - } -} diff --git a/mockall/tests/mock_vec_arguments.rs b/mockall/tests/mock_vec_arguments.rs deleted file mode 100644 index 666130c9..00000000 --- a/mockall/tests/mock_vec_arguments.rs +++ /dev/null @@ -1,40 +0,0 @@ -// vim: tw=80 -//! A method with `Vec` arguments -#![deny(warnings)] - -use mockall::*; - -trait Foo { - fn foo(&self, x: Vec); -} - -mock! { - Foo { - fn foo(&self, x: Vec); - } -} - -mod withf { - use super::*; - - #[test] - #[should_panic(expected = "MockFoo::foo: No matching expectation found")] - fn fail() { - let mut mock = MockFoo::new(); - mock.expect_foo::() - .withf(|sl| sl == &vec![1, 2, 3]) - .return_const(()); - let x = vec![1, 2, 3, 4]; - mock.foo(x); - } - - #[test] - fn ok() { - let mut mock = MockFoo::new(); - mock.expect_foo::() - .withf(|sl| sl == &vec![1, 2, 3]) - .return_const(()); - let x = vec![1, 2, 3]; - mock.foo(x); - } -}