generic-tests 0.1.3

Procedural macro to define tests and benchmarks generically
Documentation
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#![deny(unused)]
#![warn(clippy::all)]

#[generic_tests::define]
mod simple {
    use std::borrow::Cow;
    use std::fmt::Debug;

    #[test]
    fn equates_to_str<S: From<&'static str>>()
    where
        S: ?Sized + PartialEq<str> + Debug,
    {
        let s: S = "ab".into();
        assert_eq!(&s, "ab");
        assert_ne!(&s, "aa");
    }

    #[instantiate_tests(<String>)]
    mod string {}

    #[instantiate_tests(<Cow<'static, str>>)]
    mod cow {}
}

#[generic_tests::define]
mod fallible_protocol {
    use std::borrow::Cow;
    use std::fmt::Debug;
    use std::str::{self, Utf8Error};

    trait FromUtf8<'a>: Sized + 'a {
        fn from_utf8(bytes: &'a [u8]) -> Result<Self, Utf8Error>;
    }

    impl<'a> FromUtf8<'a> for &'a str {
        fn from_utf8(bytes: &'a [u8]) -> Result<Self, Utf8Error> {
            str::from_utf8(bytes)
        }
    }

    impl<'a> FromUtf8<'a> for String {
        fn from_utf8(bytes: &'a [u8]) -> Result<Self, Utf8Error> {
            String::from_utf8(bytes.to_vec()).map_err(|e| e.utf8_error())
        }
    }

    impl<'a> FromUtf8<'a> for Cow<'a, str> {
        fn from_utf8(bytes: &'a [u8]) -> Result<Self, Utf8Error> {
            str::from_utf8(bytes).map(Into::into)
        }
    }

    #[test]
    fn ok_from_valid_utf8<'a, T>() -> Result<(), Utf8Error>
    where
        T: FromUtf8<'a> + AsRef<str> + Debug,
    {
        let v = T::from_utf8(b"Hello, world!")?;
        assert_eq!(v.as_ref(), "Hello, world!");
        Ok(())
    }

    #[instantiate_tests(<&'static str>)]
    mod str_slice {}

    #[instantiate_tests(<String>)]
    mod string {}

    #[instantiate_tests(<Cow<'static, str>>)]
    mod cow {}
}

#[generic_tests::define]
mod nested {
    use std::fmt::{self, Display};

    #[test]
    fn print<T: Display + Default>() {
        let v = T::default();
        println!("{}", v);
    }

    #[derive(Default)]
    struct Foo;

    impl Display for Foo {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "Foo")
        }
    }

    mod from_prelude {
        #[instantiate_tests(<String>)]
        mod string {}
    }

    mod imported {
        use std::borrow::Cow;

        #[instantiate_tests(<Cow<'static, str>>)]
        mod cow {}
    }

    mod locally_defined {
        use std::fmt::{self, Display};

        #[derive(Default)]
        struct Bar;

        impl Display for Bar {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, "Bar")
            }
        }

        #[instantiate_tests(<Bar>)]
        mod bar {}
    }

    mod aliases_root {
        use super::Foo;

        #[instantiate_tests(<Foo>)]
        mod foo {}
    }

    mod shadows_root {
        use std::fmt::{self, Display};

        #[derive(Default)]
        struct Foo;

        impl Display for Foo {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, "nested Foo")
            }
        }

        #[instantiate_tests(<Foo>)]
        mod foo {}
    }
}

#[generic_tests::define]
mod modifier_attrs {
    #[test]
    #[ignore]
    fn ignore<T>() {
        panic!("tests instantiated from this function should not be run")
    }

    #[test]
    #[ignore = "tests the macro works in presence of attribute content"]
    fn ignore_with_reason<T>() {
        panic!("tests instantiated from this function should not be run")
    }

    #[ignore]
    #[test]
    fn ignore_above<T>() {
        panic!("tests instantiated from this function should not be run")
    }

    #[test]
    #[should_panic]
    fn should_panic<T>() {
        panic!("panicking as it should")
    }

    #[test]
    #[should_panic(expected = "panicking as it should")]
    fn should_panic_with_expected<T>() {
        panic!("panicking as it should")
    }

    #[test]
    #[ignore]
    #[should_panic]
    fn ignore_should_panic<T>() {
        // Does not panic, so should be ignored
    }

    #[instantiate_tests(<()>)]
    mod inst {}
}

#[generic_tests::define]
mod cfg_attr {
    #[cfg(test)]
    #[test]
    fn enabled<T>() {}

    #[cfg(not(test))]
    #[test]
    fn disabled_above<T>() {
        panic!("unexpectedly enabled")
    }

    #[test]
    #[cfg(not(test))]
    fn disabled_below<T>() {
        panic!("unexpectedly enabled")
    }

    // This should not be instantiated. If it is, it will trigger
    // the dead_code lint in the instantiation module.
    #[allow(dead_code)]
    #[cfg(test)]
    fn not_a_test_case<T>() {}

    #[deny(dead_code)]
    #[instantiate_tests(<()>)]
    mod inst {}
}

// To test how custom attributes work, abuse the system by listing `allow`
// as a test attribute.
// A function annotated with `allow` should get instantiated, and the
// `allow` attribute on the original function should get erased. This
// does not trigger the dead code lint, though, because the instantiated
// "test" function calls the generic one and itself sports the `allow` attribute
// disabling the lint.
#[generic_tests::define(attrs(allow, test, should_panic))]
#[deny(dead_code)]
mod custom_test_attrs {

    #[allow(dead_code)]
    fn custom_sig_with_builtin_types<T>(input: i32) -> String {
        input.to_string()
    }

    struct Foo;
    struct Bar;

    #[allow(dead_code)]
    fn custom_sig_with_locally_defined_types<T>(_input: Foo) -> Bar {
        Bar
    }

    #[test]
    fn test_attr_works_too_when_listed<T>() {}

    #[test]
    #[should_panic]
    fn two_test_attrs_listed<T>() {
        panic!("panicking as it should");
    }

    #[instantiate_tests(<()>)]
    mod inst {}
}

#[generic_tests::define(attrs(allow))]
mod custom_attrs_override_test {
    #[allow(dead_code)]
    fn foo<T>() {}

    #[instantiate_tests(<()>)]
    mod inst {}

    #[test]
    fn test_is_not_instantiated() {}
}

#[generic_tests::define(copy_attrs(doc, cfg_attr))]
mod custom_copy_attrs {

    /// This illustrates how doc comments can be copied
    /// onto the instantiated tests.
    #[test]
    fn copy_doc_attrs<T>() {}

    /// This should not be instantiated. If it is, it will trigger
    /// the dead_code lint in the instantiation module.
    #[allow(dead_code)]
    fn not_a_test_case_but_has_doc<T>() {}

    #[cfg_attr(test, doc = "This should not be instantiated")]
    #[allow(dead_code)]
    fn not_a_test_case_but_has_cfg_attr<T>() {}

    #[test]
    #[cfg_attr(test, should_panic)]
    #[allow(unused_attributes)]
    fn custom_attr_gets_copied_to_instantiation<T>() {
        panic!("panicking as it should");
    }

    #[instantiate_tests(<()>)]
    #[deny(dead_code)]
    mod inst {}
}

mod fn_level_attr {

    #[generic_tests::define]
    mod macro_level_defaults {

        #[generic_test(attrs(test, cfg_attr))]
        #[test]
        #[cfg_attr(test, should_panic)]
        fn custom_inst_attrs<T>() {
            panic!("panicking as it should");
        }

        #[generic_test(copy_attrs(cfg_attr))]
        #[test]
        #[cfg_attr(test, should_panic)]
        #[allow(unused_attributes)]
        fn custom_copy_attrs<T>() {
            panic!("panicking as it should");
        }

        #[generic_test(copy_attrs())]
        #[test]
        #[cfg(not(test))]
        fn copy_attrs_overrides_default<T>() {}

        #[cfg(test)]
        fn copy_attrs_overrides_default<T>() {}

        #[instantiate_tests(<()>)]
        mod inst {}
    }

    #[generic_tests::define(attrs(allow), copy_attrs(cfg_attr))]
    mod macro_level_custom {

        #[generic_test(attrs(test))]
        #[test]
        fn custom_inst_attrs<T>() {}

        #[generic_test(attrs(test))]
        #[test]
        #[cfg_attr(test, should_panic)]
        #[allow(unused_attributes)]
        fn custom_inst_attrs_mod_level_copy_attrs<T>() {
            panic!("panicking as it should");
        }

        #[generic_test(copy_attrs(cfg))]
        #[allow()]
        #[cfg(not(test))]
        fn custom_copy_attrs<T>() {
            panic!("should be disabled");
        }

        #[instantiate_tests(<()>)]
        mod inst {}
    }
}

#[generic_tests::define(attrs(allow))]
#[allow(clippy::needless_lifetimes)]
mod lifetimes_in_signature {

    struct Borrowed<'a> {
        #[allow(dead_code)]
        a: &'a str,
    }

    #[allow(dead_code)]
    fn elided_in_input<T>(_s: &str) {}

    #[allow(dead_code)]
    fn explicit_in_input<'a, T>(_s: &'a str) {}

    #[allow(dead_code)]
    fn elided_and_explicit_type_param_in_input<'b, T>(_s: &str, _b: Borrowed<'b>) {}

    #[allow(dead_code)]
    fn two_args_sharing_a_lifetime<'b, T>(s: &'b str, mut b: Borrowed<'b>) {
        b.a = s;
    }

    #[allow(dead_code)]
    fn two_args_sharing_a_lifetime_one_also_elides<'b, T>(s: &'b str, b: &mut Borrowed<'b>) {
        b.a = s;
    }

    #[allow(dead_code)]
    fn explicit_in_input_placeholder_in_output<'a, T>(a: &'a str) -> Borrowed<'_> {
        Borrowed { a }
    }

    #[allow(dead_code)]
    fn elided_in_input_placeholder_in_output<T>(a: &str) -> Borrowed<'_> {
        Borrowed { a }
    }

    #[allow(dead_code)]
    fn one_explicit_other_elided<'a, T>(a: &'a str, _b: &str) -> Borrowed<'a> {
        Borrowed { a }
    }

    #[allow(dead_code)]
    fn two_explicit<'a, 'b, T>(_a: &'a str, b: &'b str) -> Borrowed<'b> {
        Borrowed { a: b }
    }

    #[instantiate_tests(<()>)]
    mod inst {}
}

#[generic_tests::define(attrs(allow))]
mod mut_in_signature {
    #[allow(dead_code)]
    fn mut_is_erased_in_instantiation<T>(mut a: i32) {
        a += 1;
        let _ = a;
    }

    #[deny(unused_mut)]
    #[instantiate_tests(<()>)]
    mod inst {}
}

// Use a stand-in attribute because standard test does not allow unsafe
#[generic_tests::define(attrs(inline))]
mod unsafe_fn {
    #[inline]
    #[allow(dead_code)]
    unsafe fn unsafe_fn<T>() {}

    #[instantiate_tests(<()>)]
    mod inst {}
}