From aef63bfecb7731c0307cc65eab0b9a06b8a7363d Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 25 Mar 2023 11:58:16 +0100 Subject: feat: add argument matching --- examples/basic.rs | 20 +++++++++++--------- examples/generic_method.rs | 25 +++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/basic.rs b/examples/basic.rs index a157413..01688f5 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,8 +1,9 @@ use ridicule::mock; +use ridicule::predicate::{always, eq}; -trait Foo: Sized +trait Foo { - fn bar(&self, num: u128) -> &Self; + fn bar(&self, num: u128, text: &str); } mock! { @@ -10,7 +11,7 @@ mock! { impl Foo for MockFoo { - fn bar(&self, num: u128) -> &Self; + fn bar(&self, num: u128, text: &str); } } @@ -18,11 +19,12 @@ fn main() { let mut mock_foo = MockFoo::new(); - mock_foo.expect_bar().returning(|me, num| { - println!("bar was called with {num}"); + mock_foo + .expect_bar() + .with(eq(1234), always()) + .returning(|_, num, text| { + println!("bar was called with {num} and '{text}'"); + }); - me - }); - - mock_foo.bar(123); + mock_foo.bar(1234, "Hello"); } diff --git a/examples/generic_method.rs b/examples/generic_method.rs index 4262ee2..7283810 100644 --- a/examples/generic_method.rs +++ b/examples/generic_method.rs @@ -1,12 +1,14 @@ use std::fmt::Display; +use predicates::float::is_close; use ridicule::mock; +use ridicule::predicate::function; trait Foo { fn bar(&self, num: u128) -> Baz; - fn abc(&mut self, baz: Baz); + fn abc(&mut self, thing_a: ThingA, thing_b: ThingB); } mock! { @@ -16,7 +18,7 @@ mock! { { fn bar(&self, num: u128) -> Baz; - fn abc(&mut self, baz_baz: Baz); + fn abc(&mut self, thing_a: ThingA, thing_b: ThingB); } } @@ -39,6 +41,17 @@ fn main() 128u8 }); + mock_foo + .expect_abc::<&str, f64>() + .with( + function(|thing_a: &&str| thing_a.starts_with("Good morning")), + is_close(7.13081), + ) + .returning(|_me, _thing_a, _thing_b| { + println!("abc was called"); + }) + .times(1); + assert_eq!(mock_foo.bar::(123), "Hello".to_string()); assert_eq!(mock_foo.bar::(123), "Hello".to_string()); assert_eq!(mock_foo.bar::(123), "Hello".to_string()); @@ -47,4 +60,12 @@ fn main() // mock_foo.bar::(123); assert_eq!(mock_foo.bar::(456), 128); + + mock_foo.abc( + concat!( + "Good morning, and in case I don't see ya, good afternoon,", + " good evening, and good night!" + ), + 7.13081f64, + ); } -- cgit v1.2.3-18-g5258