summaryrefslogtreecommitdiff
path: root/examples/generic_method.rs
blob: 995c67d9c621d5ecc2af5b19688f42ca43f0455a (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
use std::fmt::Display;

use ridicule::mock;

trait Foo
{
    fn bar<Baz: Display>(&self, num: u128) -> Baz;
}

mock! {
    MockFoo {}

    impl Foo for MockFoo
    {
        fn bar<Baz: Display>(&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()
        })
        .times(3);

    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::<String>(123), "Hello".to_string());
    assert_eq!(mock_foo.bar::<String>(123), "Hello".to_string());

    // Would panic
    // mock_foo.bar::<String>(123);

    assert_eq!(mock_foo.bar::<u8>(456), 128);
}