-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathwhen_struct_regular_method_generic.rs
More file actions
171 lines (141 loc) · 5.33 KB
/
when_struct_regular_method_generic.rs
File metadata and controls
171 lines (141 loc) · 5.33 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use super::*;
struct Struct(u8);
#[mockable]
impl Struct {
fn static_method<T: Display>(arg: bool, method_generic: T) -> String {
format!("{} {}", arg, method_generic)
}
fn ref_method<T: Display>(&self, arg: bool, method_generic: T) -> String {
format!("{} {} {}", self.0, arg, method_generic)
}
fn ref_mut_method<T: Display>(&mut self, arg: bool, method_generic: T) -> String {
self.0 *= 2;
format!("{} {} {}", self.0, arg, method_generic)
}
fn val_method<T: Display>(self, arg: bool, method_generic: T) -> String {
format!("{} {} {}", self.0, arg, method_generic)
}
}
mod and_method_is_static {
use super::*;
#[test]
fn and_not_mocked_then_runs_normally() {
assert_eq!("true 1.5", Struct::static_method(true, 1.5f32));
assert_eq!("true abc", Struct::static_method(true, "abc"));
}
#[test]
fn and_continue_mocked_then_runs_with_modified_args_for_mocked_type_only() {
unsafe {
Struct::static_method::<f32>.mock_raw(|a, b| MockResult::Continue((!a, b + 1.)));
}
assert_eq!("false 3.5", Struct::static_method(true, 2.5f32));
assert_eq!("true abc", Struct::static_method(true, "abc"));
}
#[test]
fn and_return_mocked_then_returns_mocking_result_for_mocked_type_only() {
unsafe {
Struct::static_method::<f32>
.mock_raw(|a, b| MockResult::Return(format!("mocked {} {}", a, b)));
}
assert_eq!("mocked true 2.5", Struct::static_method(true, 2.5f32));
assert_eq!("true abc", Struct::static_method(true, "abc"));
}
}
mod and_method_is_ref_method {
use super::*;
#[test]
fn and_not_mocked_then_runs_normally() {
assert_eq!("2 true 1.5", Struct(2).ref_method(true, 1.5f32));
assert_eq!("2 true abc", Struct(2).ref_method(true, "abc"));
}
#[test]
fn and_continue_mocked_then_runs_with_modified_args() {
let struct_2 = Struct(2);
let struct_3 = Struct(3);
unsafe {
Struct::ref_method::<f32>
.mock_raw(|_, b, c| MockResult::Continue((&struct_3, !b, c + 1.)));
}
assert_eq!("3 false 2.5", struct_2.ref_method(true, 1.5f32));
assert_eq!(2, struct_2.0);
assert_eq!(3, struct_3.0);
assert_eq!("2 true abc", Struct(2).ref_method(true, "abc"));
}
#[test]
fn and_return_mocked_then_returns_mocking_result() {
let struct_2 = Struct(2);
unsafe {
Struct::ref_method::<f32>
.mock_raw(|a, b, c| MockResult::Return(format!("mocked {} {} {}", a.0, b, c)));
}
assert_eq!("mocked 2 true 1.5", struct_2.ref_method(true, 1.5f32));
assert_eq!(2, struct_2.0);
assert_eq!("2 true abc", Struct(2).ref_method(true, "abc"));
}
}
mod and_method_is_ref_mut_method {
use super::*;
#[test]
fn and_not_mocked_then_runs_normally() {
let mut struct_2 = Struct(2);
let mut struct_4 = Struct(4);
assert_eq!("4 true 1.5", struct_2.ref_mut_method(true, 1.5f32));
assert_eq!(4, struct_2.0);
assert_eq!("8 true abc", struct_4.ref_mut_method(true, "abc"));
assert_eq!(8, struct_4.0);
}
#[test]
fn and_continue_mocked_then_runs_with_modified_args() {
let mut struct_2 = Struct(2);
let mut struct_3 = OnceMutCell::new(Struct(3));
let mut struct_4 = Struct(4);
unsafe {
Struct::ref_mut_method::<f32>
.mock_raw(|_, b, c| MockResult::Continue((struct_3.borrow(), !b, c + 1.)));
}
assert_eq!("6 false 2.5", struct_2.ref_mut_method(true, 1.5f32));
assert_eq!(2, struct_2.0);
assert_eq!(6, struct_3.get_mut().0);
assert_eq!("8 true abc", struct_4.ref_mut_method(true, "abc"));
assert_eq!(8, struct_4.0);
}
#[test]
fn and_return_mocked_then_returns_mocking_result() {
let mut struct_2 = Struct(2);
let mut struct_4 = Struct(4);
unsafe {
Struct::ref_mut_method::<f32>
.mock_raw(|a, b, c| MockResult::Return(format!("mocked {} {} {}", a.0, b, c)));
}
assert_eq!("mocked 2 true 1.5", struct_2.ref_mut_method(true, 1.5f32));
assert_eq!(2, struct_2.0);
assert_eq!("8 true abc", struct_4.ref_mut_method(true, "abc"));
assert_eq!(8, struct_4.0);
}
}
mod and_method_is_val_method {
use super::*;
#[test]
fn and_not_mocked_then_runs_normally() {
assert_eq!("2 true 1.5", Struct(2).val_method(true, 1.5f32));
assert_eq!("2 true abc", Struct(2).val_method(true, "abc"));
}
#[test]
fn and_continue_mocked_then_runs_with_modified_args() {
unsafe {
Struct::val_method::<f32>
.mock_raw(move |_, b, c| MockResult::Continue((Struct(3), !b, c + 1.)));
}
assert_eq!("3 false 2.5", Struct(2).val_method(true, 1.5f32));
assert_eq!("2 true abc", Struct(2).val_method(true, "abc"));
}
#[test]
fn and_return_mocked_then_returns_mocking_result() {
unsafe {
Struct::val_method::<f32>
.mock_raw(|a, b, c| MockResult::Return(format!("mocked {} {} {}", a.0, b, c)));
}
assert_eq!("mocked 2 true 1.5", Struct(2).val_method(true, 1.5f32));
assert_eq!("2 true abc", Struct(2).val_method(true, "abc"));
}
}