extern crate mock_derive;
use mock_derive::mock;
#[mock]
pub trait CustomTrait {
fn get_int(&self) -> u32;
fn opt_int(&self) -> Option<u32>;
fn default_method(&self, x: i32, y: i32) -> i32 {
x + y
}
}
struct Foo {}
impl Foo {
fn new() -> Foo {
Foo{}
}
}
impl CustomTrait for Foo {
fn get_int(&self) -> u32 {
1
}
fn opt_int(&self) -> Option<u32> {
Some(self.get_int())
}
}
#[cfg(test)]
mod test {
#[test]
fn test_stable() {
assert_eq!(1,1);
}
}
#[cfg(all(test, feature = "nightly"))]
mod test_mocks {
use super::*;
#[test]
fn it_works() {
let foo = Foo::new(); let mut mock = MockCustomTrait::new();
mock.set_fallback(foo);
let method = mock.method_get_int()
.first_call()
.set_result(3)
.second_call()
.set_result(4)
.nth_call(3) .set_result(5);
mock.set_get_int(method); let result = mock.get_int();
assert!(result == 3);
let result2 = mock.get_int();
assert!(result2 == 4);
let result3 = mock.get_int();
assert!(result3 == 5);
let result4 = mock.get_int();
assert!(result4 == 1);
}
#[test]
fn return_result_of() {
let mut x = 15;
let mut mock = MockCustomTrait::new();
let method = mock.method_opt_int()
.return_result_of(move || {
x += 1;
Some(x)
});
mock.set_opt_int(method);
assert!(mock.opt_int() == Some(16));
assert!(mock.opt_int() == Some(17));
assert!(mock.opt_int() == Some(18));
}
#[test]
#[should_panic(expected = "called at least")]
fn min_calls_not_met() {
let mut mock = MockCustomTrait::new();
let method = mock.method_get_int()
.called_at_least(10)
.return_result_of(|| 10);
mock.set_get_int(method);
for _ in 0..9 {
mock.get_int();
}
}
#[test]
fn called_once() {
let mut mock = MockCustomTrait::new();
let method = mock.method_get_int()
.called_once()
.return_result_of(|| 10);
mock.set_get_int(method);
mock.get_int(); }
}