extern crate simulacrum_macros;
use simulacrum_macros::*;
trait CoolTrait {
fn foo(&self);
fn bar(&mut self);
fn goop(&mut self, flag: bool) -> u32;
fn zing(&self, first: i32, second: bool);
fn boop(&self, name: &'static str);
fn store(&self, val: &i64);
fn toggle(&self, bit: &mut bool);
}
create_mock_struct! {
struct CoolTraitMock: {
expect_foo("foo");
expect_bar("bar");
expect_goop("goop") bool => u32;
expect_zing("zing") (i32, bool);
expect_boop("boop") &'static str;
expect_store("store") *const i64;
expect_toggle("toggle") *mut bool;
}
}
impl CoolTrait for CoolTraitMock {
fn foo(&self) {
was_called!(self, "foo")
}
fn bar(&mut self) {
was_called!(self, "bar")
}
fn goop(&mut self, flag: bool) -> u32 {
was_called!(self, "goop", (flag: bool) -> u32)
}
fn zing(&self, first: i32, second: bool) {
was_called!(self, "zing", (first: i32, second: bool))
}
fn boop(&self, name: &'static str) {
was_called!(self, "boop", (name: &'static str))
}
fn store(&self, val: &i64) {
was_called!(self, "store", (val: *const i64))
}
fn toggle(&self, bit: &mut bool) {
was_called!(self, "toggle", (bit: *mut bool))
}
}
fn main() {
let mut m = CoolTraitMock::new();
m.expect_bar().called_never();
m.expect_foo().called_once();
m.then().expect_goop().called_once().with(true).returning(|_| 5);
m.then().expect_zing().called_once().with(params!(13, false));
m.expect_boop().called_times(2);
m.expect_store().called_once().with(deref(777));
m.expect_toggle().called_once().with(deref(true))
.modifying(|&mut arg| { unsafe { *arg = false } });
m.foo();
assert_eq!(m.goop(true), 5);
m.zing(13, false);
m.boop("hey");
m.boop("yo");
m.store(&777);
let mut b = true;
m.toggle(&mut b);
assert_eq!(b, false);
}