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
//! # Test helpers should be simple.
//!
//! You don't want to have to worry about bugs in your test suite or unexpected behaviour leading
//! to failing tests silently passing. With that in mind, `simple_test_case` aims to do the bare
//! minimum to eliminate the boilerplate of writing parameterised tests and no more.
//!
//! The `test_case` attribute macro handles generating multiple test functions for you which are
//! parameterised by the inputs you provide. You still need to provide the `#[test]` attribute (or
//! an alternative such as `#[tokio::test]`) and all test cases _must_ be provided before any
//! additional attribute macros you wish to apply.
//!
//! And that's it.
//!
//! There is no additional support for custom assertions, fixtures etc. That said, if you want or
//! need a more complicated testing set up, additional attribute macros should play nice with
//! `simple_test_case` provided you follow the advice below.
//!
//!
//! ## Usage
//!
//! ### Valid
//! Here the `#[test]` attribute is provided after all instances of `test_case`. This will work.
//! ```rust
//! use simple_test_case::test_case;
//!
//! fn double(n: usize) -> usize {
//!     n * 2
//! }
//!
//! #[test_case(1, 2; "case 1")]
//! #[test_case(3, 6; "case 2")]
//! #[test]
//! fn double_test(n: usize, double: usize) {
//!     assert_eq!(double(n), double)
//! }
//! ```
//!
//! ### Invalid
//! Here the `#[test]` attribute is provided before all instances of `test_case`. This will cause
//! the compiler to complain about functions used as tests not being allowed to have any arguments.
//! ```ignore
//! use simple_test_case::test_case;
//!
//! fn double(n: usize) -> usize {
//!     n * 2
//! }
//!
//! #[test]
//! #[test_case(1, 2; "case 1")]
//! #[test_case(3, 6; "case 2")]
//! fn double_test(n: usize, double: usize) {
//!     assert_eq!(double(n), double)
//! }
//! ```
//!
//! ## Additional attributes
//!
//! `test_case` preserves all attributes beneath it, forwarding them on to the individual generated
//! test functions. As an example, the standard library `should_panic` attribute works just fine as
//! shown below (just make sure to provide your test cases first as described above):
//!
//! ```rust
//! use simple_test_case::test_case;
//!
//! #[test_case(1, 2; "case 1")]
//! #[test_case(3, 6; "case 2")]
//! #[test]
//! #[should_panic(expected = "this works")]
//! fn panic_test(n: usize, double: usize) {
//!     assert_eq!(double(a), b);
//!     panic!("this works")
//! }
//! ```
//!
//! ### Async tests
//!
//! Async tests are supported in the same way that all other attributes are supported: add your
//! tests cases first and then apply the async testing macro of your choice beneath.
//! ```rust
//! use simple_test_case::test_case;
//!
//! async fn async_double(n: usize) -> usize {
//!     n * 2
//! }
//!
//! #[test_case(1, 2; "case 1")]
//! #[test_case(3, 6; "case 2")]
//! #[tokio::test]
//! async fn double_test(n: usize, double: usize) {
//!     assert_eq!(double(n).await, double)
//! }
//! ```
//!
//! ## How does it work?
//!
//! You are encouraged to read the source of the macro itself (the macro plus associated helper
//! functions are under 150 lines of code) but the general idea is as follows:
//!
//! - Collect all `test_case` (or `simple_test_case::test_case`) attributes, each of which maps a
//!   set of function arguments to a test case name.
//! - For each test case create a copy of the original test function with the function arguments
//!   replaced with explicit variable bindings at the top of the function body.
//! - Write out each of the cases as their own test inside of a new module that is named using the
//!   original test function name.
//!
//! You can use [cargo expand](https://github.com/dtolnay/cargo-expand) to see what the generated
//! tests look like using the example provided in the `examples` directory like so:
//!
//! ```bash
//! $ cargo expand --example=expand_me --tests
//!   Compiling simple_test_case v0.1.0 (/home/innes/repos/personal/simple_test_case)
//!    Finished test [unoptimized + debuginfo] target(s) in 0.12s
//!
//! #![feature(prelude_import)]
//! #[prelude_import]
//! use std::prelude::rust_2021::*;
//! #[macro_use]
//! extern crate std;
//! use simple_test_case::test_case;
//! mod example {
//!     #[allow(unused_imports)]
//!     use super::*;
//!     extern crate test;
//!     #[cfg(test)]
//!     #[rustc_test_marker]
//!     pub const small_example: test::TestDescAndFn = test::TestDescAndFn {
//!         desc: test::TestDesc {
//!             name: test::StaticTestName("example::small_example"),
//!             ignore: false,
//!             allow_fail: false,
//!             compile_fail: false,
//!             no_run: false,
//!             should_panic: test::ShouldPanic::No,
//!             test_type: test::TestType::Unknown,
//!         },
//!         testfn: test::StaticTestFn(|| test::assert_test_result(small_example())),
//!     };
//!     fn small_example() {
//!         let a: usize = 1;
//!         let b: usize = 2;
//!         if !(a < b) {
//!             ::core::panicking::panic("assertion failed: a < b")
//!         }
//!     }
//!     extern crate test;
//!     #[cfg(test)]
//!     #[rustc_test_marker]
//!     pub const large_example: test::TestDescAndFn = test::TestDescAndFn {
//!         desc: test::TestDesc {
//!             name: test::StaticTestName("example::large_example"),
//!             ignore: false,
//!             allow_fail: false,
//!             compile_fail: false,
//!             no_run: false,
//!             should_panic: test::ShouldPanic::No,
//!             test_type: test::TestType::Unknown,
//!         },
//!         testfn: test::StaticTestFn(|| test::assert_test_result(large_example())),
//!     };
//!     fn large_example() {
//!         let a: usize = 100;
//!         let b: usize = 200;
//!         if !(a < b) {
//!             ::core::panicking::panic("assertion failed: a < b")
//!         }
//!     }
//! }
//! #[allow(dead_code)]
//! fn main() {}
//! #[rustc_main]
//! pub fn main() -> () {
//!     extern crate test;
//!     test::test_main_static(&[&small_example, &large_example])
//! }
//! ```
use proc_macro::TokenStream;

mod dir_cases;
mod test_case;
mod util;

/// A simple parameterised test helper
///
/// See the main module documentation for usage details.
#[proc_macro_attribute]
pub fn test_case(args: TokenStream, input: TokenStream) -> TokenStream {
    test_case::inner(args, input)
}

/// Generate a set of parameterised tests based on the contents of a directory
///
/// NOTE: The path given will be resolved relative to the root of your cargo workspace and the test
/// function that you provide must accept to `&str` arguments: the path to the file loaded for the
/// test case and the contents of that file. The files are read at compile time so
/// adding/removing/modifying files in the given directory should trigger a recompile of your
/// tests.
///
/// ```ignore
/// #[dir_cases("resources/test_data")]
/// #[test]
/// fn example(path: &str, contents: &str) {
///   // ..
/// }
/// ```
#[proc_macro_attribute]
pub fn dir_cases(args: TokenStream, input: TokenStream) -> TokenStream {
    dir_cases::inner(args, input)
}