summaryrefslogtreecommitdiff
path: root/examples/simple.rs
blob: c5494da3b5fee792c3707864bb7a89364bf7d3a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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()
    );
}