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
//! # Yare
//!
//! Procedural macro based parameterized testing library. Run a test case with many different inputs.
//! Test cases can be defined using the 'parameterized' attribute instead of the 'test' attribute.
//!
//! ### Examples:
//!
//! <br>
//!
//! **Example: Add5**
//!
//! ```rust, no_run
//! fn add5<T: Into<u32>>(component: T) -> u32 {
//!     component.into() + 5
//! }
//!
//! #[cfg(test)]
//! mod tests {
//!     # use std::assert_eq;
//! use super::*;
//!     use yare::parameterized;
//!
//!     #[parameterized(
//!         zero_plus_five = { 0, 5 },
//!         one_plus_five = { 1, 6 },
//!         two_plus_five = { 2, 7 },
//!     )]
//!     fn test_add5(input: u16, expected: u32) {
//!         assert_eq!(add5(input), expected);
//!     }
//! }
//! ```
//!
//! **Example: Fruits**
//!
//! ```rust, no_run
//! enum Fruit {
//!     Apple,
//!     Bramble(BrambleFruit),
//!     Pear,
//! }
//!
//! trait NameOf {
//!     fn name_of(&self) -> &str;
//! }
//!
//! impl NameOf for Fruit {
//!     fn name_of(&self) -> &str {
//!         match self {
//!             Fruit::Apple => "apple",
//!             Fruit::Bramble(fruit) => fruit.name_of(),
//!             Fruit::Pear => "pear",
//!         }
//!     }
//! }
//!
//! enum BrambleFruit {
//!     Blackberry,
//! }
//!
//! impl NameOf for BrambleFruit {
//!     fn name_of(&self) -> &str {
//!         match self {
//!             BrambleFruit::Blackberry => "blackberry",
//!         }
//!     }
//! }
//!
//! #[cfg(test)]
//! mod tests {
//!     # use std::assert_eq;
//!     use super::*;
//!     use yare::parameterized;
//!
//!     #[parameterized(
//!         apple = { Fruit::Apple, "apple" },
//!         pear = { Fruit::Pear, "pear" },
//!         blackberry = { Fruit::Bramble(BrambleFruit::Blackberry), "blackberry" },
//!     )]
//!     fn a_fruity_test(fruit: Fruit, name: &str) {
//!         assert_eq!(fruit.name_of(), name)
//!     }
//! }
//! ```
//!
//! <br>
//!
//! ### Imports
//!
//! If you prefer not to import this library (with `use yare::parameterized;`) in every test module, you can put
//! the following snippet at the top of your crate root:
//! ```rust, no_run
//! #[cfg(test)]
//! #[macro_use]
//! extern crate yare;
//! ```
//!
//! ### License
//!
//! Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
//! 2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
//!
//! <br>
//!
//! Unless you explicitly state otherwise, any contribution intentionally submitted
//! for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
//! be dual licensed as above, without any additional terms or conditions.
//!

extern crate yare_macro as yare;

pub use yare::parameterized;

/// Attribute macro's such as 'parameterized' do not enable the run tests intent for a module
/// marked as cfg(test) (or a #[test] function for that matter) in Intellij.
///
/// To enable the intent within a module, we need at least a single test marked with `#[test]`.
/// The `ide!()` macro is a work around for this issue and creates this empty test. It can be called
/// within every module where we wish to run test cases using the run configuration / run test context
/// menu.
///
/// Using the intellij-rust new macro expansion engine, if this macro is called within a module,
/// the module will be marked as test, and the 'run as test' context menu will be provided in the
/// gutter.
#[doc(hidden)]
#[deprecated]
#[macro_export]
macro_rules! ide {
    () => {
        #[test]
        fn __mark_with_test_intent() {}
    };
}

#[cfg(test)]
mod fruits;

#[cfg(test)]
mod tests {
    use crate::parameterized as pm;

    fn add5<T: Into<u32>>(component: T) -> u32 {
        component.into() + 5
    }

    mod readme_test {
        use super::*;

        #[pm(zero = {
            0, 5
        }, one = {
            1, 6
        }, two = {
            2, 7
        })]
        fn test_add5(eh: u16, expected: u32) {
            assert_eq!(add5(eh), expected)
        }
    }

    mod marked_as_test_module {
        use super::*;

        #[pm(two = { 2, 4 }, six = { 6, 12 }, eight = { 8, 16 })]
        fn test_times2(input: i32, output: i32) {
            let times2 = |receiver: i32| receiver * 2;

            assert_eq!(times2(input), output);
        }
    }

    mod transitive_attrs {
        use super::*;

        #[pm(none = { None })]
        #[should_panic]
        fn numbers(input: Option<()>) {
            input.unwrap()
        }
    }
}