-
Notifications
You must be signed in to change notification settings - Fork 14
/
when_arguments.rs
144 lines (119 loc) · 3.35 KB
/
when_arguments.rs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#[faux::create]
pub struct Foo {
a: u32,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Data {
a: i32,
b: u32,
}
#[faux::methods]
impl Foo {
pub fn new(a: u32) -> Self {
Foo { a }
}
pub fn no_args(&self) -> u32 {
self.a
}
pub fn one_ref_arg(&self, data: &Data) -> u32 {
data.b + self.a
}
pub fn two_args(&self, data: &Data, b: i32) -> u32 {
data.b + self.a * b as u32
}
}
#[test]
fn no_args() {
let mut mock = Foo::faux();
faux::when!(mock.no_args()).then_return(10);
}
#[test]
fn any() {
let mut mock = Foo::faux();
faux::when!(mock.one_ref_arg(_)).then_return(1337);
assert_eq!(mock.one_ref_arg(&Data { a: 2, b: 3 }), 1337);
}
#[test]
fn eq() {
let mut mock = Foo::faux();
let data = Data { a: 2, b: 3 };
faux::when!(mock.one_ref_arg(data.clone())).then_return(1337);
assert_eq!(mock.one_ref_arg(&data), 1337);
}
#[test]
fn eq_against() {
let mut mock = Foo::faux();
#[derive(Debug)]
struct OtherData {
a: i32,
b: u32,
}
impl PartialEq<Data> for OtherData {
fn eq(&self, rhs: &Data) -> bool {
self.a == rhs.a && self.b == rhs.b
}
}
faux::when!(mock.one_ref_arg(*_ == OtherData { a: 1, b: 5 })).then_return(789);
let data = Data { a: 1, b: 5 };
assert_eq!(mock.one_ref_arg(&data), 789);
}
#[test]
fn custom_matcher() {
use faux::matcher::ArgMatcher;
use std::fmt::{self, Formatter};
let mut mock = Foo::faux();
struct AddsToLessThan20;
impl ArgMatcher<Data> for AddsToLessThan20 {
fn matches(&self, arg: &Data) -> bool {
(arg.a + arg.b as i32) < 20
}
}
impl fmt::Display for AddsToLessThan20 {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "(_.a + _.b) < 20")
}
}
faux::when!(mock.one_ref_arg(*_ = AddsToLessThan20)).then_return(123);
let data = Data { a: 8, b: 11 };
assert_eq!(mock.one_ref_arg(&data), 123);
}
#[test]
fn pattern() {
let mut mock = Foo::faux();
faux::when!(mock.one_ref_arg(_ = faux::pattern!(&Data => Data { a: 2, .. }))).then_return(123);
assert_eq!(mock.one_ref_arg(&Data { a: 2, b: 789 }), 123);
}
#[test]
fn from_fn() {
let mut mock = Foo::faux();
faux::when!(mock.one_ref_arg(_ = faux::from_fn!(|data: &&Data| data.b == 3))).then_return(123);
assert_eq!(mock.one_ref_arg(&Data { a: 123, b: 3 }), 123);
}
#[test]
fn mixed_args() {
let mut mock = Foo::faux();
let data = Data { a: 2, b: 3 };
faux::when!(mock.two_args(_, 4)).then_return(777);
assert_eq!(mock.two_args(&data, 4), 777);
}
#[test]
fn multiple_mocks() {
let mut mock = Foo::faux();
let data = Data { a: 2, b: 3 };
faux::when!(mock.two_args).then_return(0);
faux::when!(mock.two_args(_, 4)).then_return(1);
faux::when!(mock.two_args(_, 10)).then_return(2);
faux::when!(mock.two_args(data.clone(), 4)).then_return(3);
assert_eq!(mock.two_args(&Data { a: 4, b: 3 }, 8), 0);
assert_eq!(mock.two_args(&Data { a: 0, b: 4 }, 4), 1);
assert_eq!(mock.two_args(&Data { a: 1, b: 4 }, 10), 2);
assert_eq!(mock.two_args(&data, 4), 3);
}
#[test]
#[should_panic]
fn unmatched_args() {
let mut mock = Foo::faux();
let data = Data { a: 2, b: 3 };
faux::when!(mock.two_args(_, 4)).then_return(777);
mock.two_args(&data, 2);
}