diff options
| author | HampusM <hampus@hampusmat.com> | 2023-03-19 13:45:07 +0100 | 
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2023-03-19 13:45:07 +0100 | 
| commit | 657673f4a25a2a7299d3751d54d9597635bc529d (patch) | |
| tree | 0a1677d1b53958f19f392e5fc11f21d7f659485c | |
| parent | d03c747cfef277fda5823e08784c94c30c7f3964 (diff) | |
docs: add examples
| -rw-r--r-- | examples/generic_fn_method.rs | 58 | ||||
| -rw-r--r-- | examples/generic_method.rs | 36 | ||||
| -rw-r--r-- | examples/generic_trait.rs | 34 | ||||
| -rw-r--r-- | examples/simple.rs | 77 | 
4 files changed, 128 insertions, 77 deletions
| diff --git a/examples/generic_fn_method.rs b/examples/generic_fn_method.rs new file mode 100644 index 0000000..7cf0d78 --- /dev/null +++ b/examples/generic_fn_method.rs @@ -0,0 +1,58 @@ +use std::path::PathBuf; + +use ridicule::mock; + +trait Foo +{ +    fn do_something<Callback>(&self, cb: Callback) +    where +        Callback: Fn(u64) -> String; + +    fn do_something_else<Callback, Value>(&self, cb: Callback) +    where +        Callback: Fn(f32) -> Value; +} + +mock! { +    MockFoo {} + +    impl Foo for MockFoo +    { +        fn do_something<Callback>(&self, cb: Callback) +        where +            Callback: Fn(u64) -> String; + +        fn do_something_else<Callback, Value>(&self, cb: Callback) +        where +            Callback: Fn(f32) -> Value; +        } +} + +fn main() +{ +    let mut mock_foo = MockFoo::new(); + +    mock_foo +        .expect_do_something() +        .returning(|_me, cb: fn(_) -> _| { +            println!("do_something was called"); + +            let cb_res = cb(42); + +            println!("Received '{cb_res}' from callback"); +        }); + +    mock_foo +        .expect_do_something_else() +        .returning(|_me, cb: fn(_) -> PathBuf| { +            println!("do_something_else was called"); + +            let cb_res = cb(255.255); + +            println!("Received '{}' from callback", cb_res.display()); +        }); + +    mock_foo.do_something((|num| format!("Hello there {num}")) as fn(_) -> _); + +    mock_foo.do_something_else((|_| PathBuf::from(r"a/b/c.txt")) as fn(_) -> _); +} diff --git a/examples/generic_method.rs b/examples/generic_method.rs new file mode 100644 index 0000000..8dc4650 --- /dev/null +++ b/examples/generic_method.rs @@ -0,0 +1,36 @@ +use ridicule::mock; + +trait Foo +{ +    fn bar<Baz>(&self, num: u128) -> Baz; +} + +mock! { +    MockFoo {} + +    impl Foo for MockFoo +    { +        fn bar<Baz>(&self, num: u128) -> Baz; +    } +} + +fn main() +{ +    let mut mock_foo = MockFoo::new(); + +    mock_foo.expect_bar().returning(|_me, num| { +        println!("bar was called with {num}"); + +        "Hello".to_string() +    }); + +    mock_foo.expect_bar().returning(|_me, num| { +        println!("bar was called with {num}"); + +        128u8 +    }); + +    assert_eq!(mock_foo.bar::<String>(123), "Hello".to_string()); + +    assert_eq!(mock_foo.bar::<u8>(456), 128); +} diff --git a/examples/generic_trait.rs b/examples/generic_trait.rs new file mode 100644 index 0000000..d863d85 --- /dev/null +++ b/examples/generic_trait.rs @@ -0,0 +1,34 @@ +use std::fmt::Display; + +use ridicule::mock; + +trait Foo<Something: Display> +{ +    fn bar(&self, something: &Something); +} + +mock! { +    MockFoo {} + +    impl<Something: Display> Foo<Something> for MockFoo +    { +        fn bar(&self, something: &Something); +    } +} + +fn main() +{ +    let mut mock_foo = MockFoo::new(); + +    mock_foo.expect_bar().returning(|_me, something: &f32| { +        println!("bar was called with {something}"); +    }); + +    mock_foo.expect_bar().returning(|_me, something: &f64| { +        println!("bar was called with {something}"); +    }); + +    mock_foo.bar(&0.826f32); + +    mock_foo.bar(&0.410f64); +} diff --git a/examples/simple.rs b/examples/simple.rs deleted file mode 100644 index c5494da..0000000 --- a/examples/simple.rs +++ /dev/null @@ -1,77 +0,0 @@ -use std::fmt::Debug; - -use ridicule::mock; - -trait SomeFoobar -{ -    fn do_something(&self) -> bool; -} - -trait Foo<Xyz> -{ -    fn bar<Baz>(&self, num: u128) -> Baz; - -    fn biz<Fiz: Debug, Bar>(&self, fiz: Fiz) -> &Bar; - -    fn baz<Foobar>(&self, name: &str, foobar: Foobar) -    where -        Foobar: SomeFoobar + Debug; - -    fn hello(&self, xyz: Xyz); -} - -mock! { -    MockFoo {} - -    impl<Xyz> Foo<Xyz> for MockFoo { -        fn bar<Baz>(&self, num: u128) -> Baz; - -        fn biz<'a, Fiz: Debug, Bar>(&'a self, fiz: Fiz) -> &'a Bar; - -        fn baz<Foobar>(&self, name: &str, foobar: Foobar) -        where -            Foobar: SomeFoobar + Debug; - -        fn hello(&self, xyz: Xyz); -    } -} - -fn main() -{ -    let mut mock_foo = MockFoo::new(); - -    mock_foo.expect_bar::<_, Vec<u8>>().returning(|_me, num| { -        println!("bar was called with {num}"); - -        "Hello".to_string() -    }); - -    mock_foo -        .expect_bar::<u128, Vec<u8>>() -        .returning(|_me, num| { -            println!("bar was called with {num}"); - -            136322 -        }); - -    mock_foo.expect_bar::<_, String>().returning(|_me, num| { -        println!("bar was called with {num}"); - -        "Greetings".to_string() -    }); - -    assert_eq!( -        <MockFoo as Foo<Vec<u8>>>::bar::<String>(&mock_foo, 123), -        "Hello".to_string() -    ); - -    assert_eq!( -        <MockFoo as Foo<Vec<u8>>>::bar::<u128>(&mock_foo, 456), -        136322 -    ); - -    assert_eq!( -        <MockFoo as Foo<String>>::bar::<String>(&mock_foo, 789), -        "Greetings".to_string() -    ); -} | 
