Skip to content

Commit

Permalink
Merge pull request #195 from asomers/piotrmaks-arg-ptr
Browse files Browse the repository at this point in the history
Piotrmaks arg ptr
  • Loading branch information
asomers committed Sep 5, 2020
2 parents 941261e + c3a682b commit 99677a9
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mockall/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
120 changes: 120 additions & 0 deletions mockall/tests/mock_deref_args.rs
Original file line number Diff line number Diff line change
@@ -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<T: 'static>(&self, x: Vec<T>);
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::<i32>()
.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::<i32>()
.withf(|sl: &Vec<i32>| sl == &[1, 2, 3])
.return_const(());
mock.foo(vec![1, 2, 3]);
}

}
3 changes: 3 additions & 0 deletions mockall_derive/src/mock_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, )*)
}
Expand Down Expand Up @@ -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, )*)
}
Expand Down Expand Up @@ -1536,6 +1538,7 @@ impl<'a> ToTokens for Matcher<'a> {
_Phantom(Box<dyn Fn(#(#fn_params,)*) + Send>)
}
impl #ig Matcher #tg #wc {
#[allow(clippy::ptr_arg)]
fn matches #lg (&self, #( #argnames: &#predty, )*) -> bool {
match self {
Matcher::Always => true,
Expand Down

0 comments on commit 99677a9

Please sign in to comment.