summaryrefslogtreecommitdiff
path: root/examples/generic_method.rs
blob: c4e321463f4fd2cb97a41e91631cf2cc8fdaf2ed (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
78
79
80
81
82
83
84
use predicates::float::is_close;
use ridicule::mock;
use ridicule::predicate::function;

trait Haha
{
    type Hello;
}

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

    fn abc<ThingA, ThingB>(&mut self, thing_a: ThingA, thing_b: ThingB);
}

mock! {
    MockFoo {}

    impl Foo for MockFoo
    {
        fn bar<Baz: Haha>(&self, num: u128) -> Baz::Hello;

        fn abc<ThingA, ThingB>(&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::<u16>()
        .returning(|_me, num| {
            println!("bar was called with {num}");

            "Hello".to_string()
        })
        .times(3);

    mock_foo.expect_bar::<&str>().returning(|_me, num| {
        println!("bar was called with {num}");

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

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

    assert_eq!(mock_foo.bar::<&str>(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,
    );
}