-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathwhen_struct_generic_method_regular.rs
More file actions
168 lines (138 loc) · 4.9 KB
/
when_struct_generic_method_regular.rs
File metadata and controls
168 lines (138 loc) · 4.9 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
use super::*;
struct Struct<T>(T);
#[mockable]
impl<T: Display + Default> Struct<T> {
fn static_method(arg: bool) -> String {
format!("{}", arg)
}
fn ref_method(&self, arg: bool) -> String {
format!("{} {}", self.0, arg)
}
fn ref_mut_method(&mut self, arg: bool) -> String {
self.0 = T::default();
format!("{} {}", self.0, arg)
}
fn val_method(self, arg: bool) -> String {
format!("{} {}", self.0, arg)
}
}
mod and_method_is_static {
use super::*;
#[test]
fn and_not_mocked_then_runs_normally() {
assert_eq!("true", Struct::<u8>::static_method(true));
assert_eq!("true", Struct::<&str>::static_method(true));
}
#[test]
fn and_continue_mocked_then_runs_with_modified_args_for_mocked_type_only() {
unsafe {
Struct::<u8>::static_method.mock_raw(|a| MockResult::Continue((!a,)));
}
assert_eq!("false", Struct::<u8>::static_method(true));
assert_eq!("true", Struct::<&str>::static_method(true));
}
#[test]
fn and_return_mocked_then_returns_mocking_result_for_mocked_type_only() {
unsafe {
Struct::<u8>::static_method.mock_raw(|a| MockResult::Return(format!("mocked {}", a)));
}
assert_eq!("mocked true", Struct::<u8>::static_method(true));
assert_eq!("true", Struct::<&str>::static_method(true));
}
}
mod and_method_is_ref_method {
use super::*;
#[test]
fn and_not_mocked_then_runs_normally() {
assert_eq!("2 true", Struct(2u8).ref_method(true));
assert_eq!("abc true", Struct("abc").ref_method(true));
}
#[test]
fn and_continue_mocked_then_runs_with_modified_args() {
let struct_2 = Struct(2u8);
let struct_3 = Struct(3u8);
unsafe {
Struct::<u8>::ref_method.mock_raw(|_, b| MockResult::Continue((&struct_3, !b)));
}
assert_eq!("3 false", struct_2.ref_method(true));
assert_eq!(2, struct_2.0);
assert_eq!(3, struct_3.0);
assert_eq!("abc true", Struct("abc").ref_method(true));
}
#[test]
fn and_return_mocked_then_returns_mocking_result() {
let struct_2 = Struct(2u8);
unsafe {
Struct::<u8>::ref_method
.mock_raw(|a, b| MockResult::Return(format!("mocked {} {}", a.0, b)));
}
assert_eq!("mocked 2 true", struct_2.ref_method(true));
assert_eq!(2, struct_2.0);
assert_eq!("abc true", Struct("abc").ref_method(true));
}
}
mod and_method_is_ref_mut_method {
use super::*;
#[test]
fn and_not_mocked_then_runs_normally() {
let mut struct_2 = Struct(2u8);
let mut struct_str = Struct("str");
assert_eq!("0 true", struct_2.ref_mut_method(true));
assert_eq!(0, struct_2.0);
assert_eq!(" true", struct_str.ref_mut_method(true));
assert_eq!("", struct_str.0);
}
#[test]
fn and_continue_mocked_then_runs_with_modified_args() {
let mut struct_2 = Struct(2u8);
let mut struct_3 = OnceMutCell::new(Struct(3u8));
let mut struct_str = Struct("str");
unsafe {
Struct::<u8>::ref_mut_method
.mock_raw(|_, b| MockResult::Continue((struct_3.borrow(), !b)));
}
assert_eq!("0 false", struct_2.ref_mut_method(true));
assert_eq!(2, struct_2.0);
assert_eq!(0, struct_3.get_mut().0);
assert_eq!(" true", struct_str.ref_mut_method(true));
assert_eq!("", struct_str.0);
}
#[test]
fn and_return_mocked_then_returns_mocking_result() {
let mut struct_2 = Struct(2u8);
let mut struct_str = Struct("str");
unsafe {
Struct::<u8>::ref_mut_method
.mock_raw(|a, b| MockResult::Return(format!("mocked {} {}", a.0, b)));
}
assert_eq!("mocked 2 true", struct_2.ref_mut_method(true));
assert_eq!(2, struct_2.0);
assert_eq!(" true", struct_str.ref_mut_method(true));
assert_eq!("", struct_str.0);
}
}
mod and_method_is_val_method {
use super::*;
#[test]
fn and_not_mocked_then_runs_normally() {
assert_eq!("2 true", Struct(2u8).val_method(true));
assert_eq!("abc true", Struct("abc").val_method(true));
}
#[test]
fn and_continue_mocked_then_runs_with_modified_args() {
unsafe {
Struct::<u8>::val_method.mock_raw(move |_, b| MockResult::Continue((Struct(3u8), !b)));
}
assert_eq!("3 false", Struct(2u8).val_method(true));
assert_eq!("abc true", Struct("abc").val_method(true));
}
#[test]
fn and_return_mocked_then_returns_mocking_result() {
unsafe {
Struct::<u8>::val_method
.mock_raw(|a, b| MockResult::Return(format!("mocked {} {}", a.0, b)));
}
assert_eq!("mocked 2 true", Struct(2u8).val_method(true));
assert_eq!("abc true", Struct("abc").val_method(true));
}
}