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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! Support for generic test definitions with a procedural attribute macro.
//!
//! The `define` macro provided by this crate allows the test writer to
//! reuse code between test cases or benchmarks that use the same test protocol
//! with different types or constant values supplied to specific tests.
//! As in general programming with Rust, this is achieved by using generic
//! parameters and trait bounds. A module processed by the `define` macro
//! contains generic test functions that are annotated with attributes consumed
//! by the test framework, such as `test` or `bench`.
//! The actual test cases can be instantiated in multiple submodules
//! annotated with the `instantiate_tests` attribute providing specific
//! argument types for the tests.
use ParsedMacroOpts;
use TokenStream;
use parse_macro_input;
use ;
/// Populates a module tree with test cases parameterizing generic definitions.
///
/// This macro is used to annotate a module containing test case definitions.
/// All functions defined directly in the module and marked with
/// a [test attribute][test-attributes] must have the same number and order
/// of generic type parameters.
///
/// Empty submodules defined inline at any depth under the module on which
/// the macro is invoked can be annotated with the `instantiate_tests`
/// attribute. The macro populates these submodules with functions having names,
/// signatures, and test attributes mirroring the generic test functions at the
/// macro invocation's root module. Each of the instantiated functions calls
/// its generic namesake in the root module, parameterized with the arguments
/// given in `instantiate_tests`.
///
/// # Basic example
///
/// ```
/// #[generic_tests::define]
/// mod tests {
/// use std::borrow::Cow;
/// use std::fmt::Display;
///
/// #[test]
/// fn print<S>()
/// where
/// S: From<&'static str> + Display,
/// {
/// let s = S::from("Hello, world!");
/// println!("{}", s);
/// }
///
/// #[instantiate_tests(<String>)]
/// mod string {}
///
/// #[instantiate_tests(<&'static str>)]
/// mod str_slice {}
///
/// #[instantiate_tests(<Cow<'static, str>>)]
/// mod cow {}
/// }
/// # fn main() {}
/// ```
///
/// # Test attributes
///
/// [test-attributes]: #test-attributes
///
/// The macro checks attributes of the function items directly contained
/// by the module against a customizable set of attribute paths that annotate
/// test cases. Functions with at least one of the attributes found in this set
/// are selected for instantiation.
/// These attributes are replicated to the instantiated test case functions and
/// erased from the original generic definitions. By default, the
/// `test`, `bench`, `ignore`, and `should_panic` attributes get this
/// treatment. To recognize other test attributes, their paths can be
/// listed in the `attrs()` parameter of the `define` attribute. Use of the
/// `attrs()` parameter overrides the default set.
///
/// ```
/// #[generic_tests::define(attrs(tokio::test))]
/// mod async_tests {
/// use bytes::{Buf, Bytes};
/// use tokio::io::{self, AsyncWriteExt};
///
/// #[tokio::test]
/// async fn test_write_buf<T: Buf>() -> io::Result<()>
/// where
/// T: From<&'static str>,
/// {
/// let mut buf = T::from("Hello, world!");
/// io::sink().write_buf(&mut buf).await?;
/// Ok(())
/// }
///
/// #[instantiate_tests(<Vec<u8>>)]
/// mod test_vec {}
///
/// #[instantiate_tests(<Bytes>)]
/// mod test_bytes {}
/// }
/// # fn main() {}
/// ```
///
/// The `copy_attrs()` list parameter can be used to specify item attributes
/// that are both copied to the instantiated test case functions and preserved
/// on the generic functions. By default, this set consists of `cfg`,
/// enabling consistent conditional compilation.
///
/// ```
/// # struct Foo;
/// #
/// #[generic_tests::define(copy_attrs(cfg, cfg_attr))]
/// mod tests {
/// use super::Foo;
///
/// #[test]
/// #[cfg(windows)]
/// fn test_only_on_windows<T>() {
/// // ...
/// }
///
/// #[test]
/// #[cfg_attr(feature = "my-fn-enhancer", bells_and_whistles)]
/// fn test_with_optional_bells_and_whistles<T>() {
/// // ...
/// }
///
/// #[instantiate_tests(<Foo>)]
/// mod foo {}
/// }
/// # fn main() {}
/// ```
///
/// The attribute sets can be customized for an individual generic test
/// function with the `generic_test` attribute.
///
/// ```
/// # struct Foo;
/// #
/// #[generic_tests::define]
/// mod tests {
/// use super::Foo;
///
/// #[generic_test(attrs(test, cfg_attr), copy_attrs(allow))]
/// #[test]
/// #[cfg_attr(windows, ignore)]
/// #[allow(dead_code)]
/// fn works_everywhere_except_windows<T>() {
/// // ...
/// }
///
/// #[instantiate_tests(<Foo>)]
/// mod foo {}
/// }
/// # fn main() {}
/// ```
///
/// Finally, all function parameter attributes on the generic test functions
/// are always copied into the signatures of the instantiated functions.
///
/// # Const generics
///
/// Since Rust 1.51, const generic parameters can be used to parameterize test
/// cases in addition to type parameters.
///
/// ```
/// #[generic_tests::define]
/// mod tests {
/// use std::iter;
///
/// #[test]
/// fn test_fill_vec<const LEN: usize>() {
/// let _v: Vec<u8> = iter::repeat(0xA5).take(LEN).collect();
/// }
///
/// #[instantiate_tests(<16>)]
/// mod small {}
///
/// #[instantiate_tests(<65536>)]
/// mod large {}
/// }
/// ```
///