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

use predicates::float::is_close;
use ridicule::mock;
use ridicule::predicate::function;

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

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

mock! {
    MockFoo {}

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

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

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
    });

    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::<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);

    mock_foo.abc(
        concat!(
            "Good morning, and in case I don't see ya, good afternoon,",
            " good evening, and good night!"
        ),
        7.13081f64,
    );
}