From ba865b581fbc1d0589b402b028f2bce70332891b Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 1 Apr 2023 15:00:06 +0200 Subject: feat: allow for usage of associated types of generics --- examples/generic_method.rs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'examples/generic_method.rs') diff --git a/examples/generic_method.rs b/examples/generic_method.rs index 7283810..c4e3214 100644 --- a/examples/generic_method.rs +++ b/examples/generic_method.rs @@ -1,12 +1,15 @@ -use std::fmt::Display; - use predicates::float::is_close; use ridicule::mock; use ridicule::predicate::function; +trait Haha +{ + type Hello; +} + trait Foo { - fn bar(&self, num: u128) -> Baz; + fn bar(&self, num: u128) -> Baz::Hello; fn abc(&mut self, thing_a: ThingA, thing_b: ThingB); } @@ -16,18 +19,28 @@ mock! { impl Foo for MockFoo { - fn bar(&self, num: u128) -> Baz; + fn bar(&self, num: u128) -> Baz::Hello; fn abc(&mut self, thing_a: ThingA, thing_b: ThingB); } } +impl Haha for u16 +{ + type Hello = String; +} + +impl Haha for &str +{ + type Hello = u8; +} + fn main() { let mut mock_foo = MockFoo::new(); mock_foo - .expect_bar() + .expect_bar::() .returning(|_me, num| { println!("bar was called with {num}"); @@ -35,7 +48,7 @@ fn main() }) .times(3); - mock_foo.expect_bar().returning(|_me, num| { + mock_foo.expect_bar::<&str>().returning(|_me, num| { println!("bar was called with {num}"); 128u8 @@ -52,14 +65,14 @@ fn main() }) .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()); + 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()); // Would panic // mock_foo.bar::(123); - assert_eq!(mock_foo.bar::(456), 128); + assert_eq!(mock_foo.bar::<&str>(456), 128); mock_foo.abc( concat!( -- cgit v1.2.3-18-g5258