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();
unsafe {
mock_foo.expect_bar::<u16>().returning(|_me, num| {
println!("bar was called with {num}");
"Hello".to_string()
})
}
.times(3);
unsafe {
mock_foo.expect_bar::<&str>().returning(|_me, num| {
println!("bar was called with {num}");
128u8
});
}
unsafe {
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());
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,
);
}