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
//! # s_test_fixture
//!
//! `s_test_fixture` or simple test fixture is a macro library to implement test fixture with no hassle.
//!
//! Their is four macros: `before`, `after`, `before_each`, `after_each`. The function pass as argument must return nothing.
//! `before` and `after` are added before the target test function . `before_each` and `after_each` are added before the target module.
use TokenStream;
use quote;
use parse_macro_input;
use ;
/// execute a function after the test is executed even if the test failled
/// # Examples
/// ```
/// #[test]
/// #[after(function_to_run(66))]
/// fn test() -> Result<(), ()> {
/// println!("before");
/// let i = -55;
/// if i == 0 {
/// Ok(())
/// } else if i == 2 {
/// return Ok(());
/// } else {
/// panic!("oh no!");
/// Ok(())
/// }
/// }
///
/// fn function_to_run(i:i32){
/// println!("I did {} thing(s)",i);
/// }
/// ```
///
/// will return
/// ```text
/// before
/// {panic statement}
/// I did 66 thing(s)
/// ```
///
/// execute a function before the test start
/// # Examples
/// ```
/// #[test]
/// #[before(function_to_run(2))]
/// fn test() {
/// let a = 2;
/// assert_eq!(a,2);
/// println!("ending");
/// }
///
/// fn function_to_run(i:i32){
/// println!("I did {} thing(s)",i);
/// }
/// ```
/// will return
/// ```text
/// before
/// I did 2 thing(s)
/// ending
/// ```
/// for each tests of a module execute the function before the test start
/// # Examples
/// ```
/// use s_test_fixture::before_each;
///
/// #[before_each(function_to_run(2))]
/// mod tests{
/// #[test]
/// fn test() {
/// let a = 2;
/// assert_eq!(a,2);
/// println!("ending");
/// }
///
/// #[test]
/// fn another_test() {
/// let a = 2;
/// assert_eq!(a,2);
/// println!("ending");
/// }
///
/// fn function_to_run(i:i32){
/// println!("I did {} thing(s)",i);
/// }
/// }
/// ```
/// for each tests of a module execute the function after the test end even if the test failled
/// # Examples
/// ```
/// use s_test_fixture::after_each;
///
/// #[after_each(function_to_run(66))]
/// mod tests{
/// #[test]
/// fn test() -> Result<(), ()> {
/// println!("before");
/// let i = -55;
/// if i == 0 {
/// Ok(())
/// } else if i == 2 {
/// return Ok(());
/// } else {
/// panic!("oh no!");
/// Ok(())
/// }
/// }
///
/// #[test]
/// fn another_test() -> Result<(), ()> {
/// println!("before");
/// let i = 0;
/// if i == 0 {
/// Ok(())
/// } else if i == 2 {
/// return Ok(());
/// } else {
/// panic!("oh no!");
/// Ok(())
/// }
/// }
///
/// fn function_to_run(i:i32){
/// println!("I did {} thing(s)",i);
/// }
/// }
/// ```