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
//! Tests that depend on other tests
//!
//! # Example
//!
//! ```
//! #[test]
//! #[flowtest]
//! fn init_complex_type() -> i32 {
//! // test that initialization works for our complex type
//! if false { panic!("oh no!") };
//! 42
//! }
//!
//! # #[derive(Debug)]
//! # struct ComplexTypeInitFailed;
//! # impl std::error::Error for ComplexTypeInitFailed {}
//! # impl std::fmt::Display for ComplexTypeInitFailed {
//! # fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
//! # unimplemented!()
//! # }
//! # }
//! #[test]
//! #[flowtest(init_complex_type: value)]
//! fn mutate_complex_type() -> Result<i32, ComplexTypeInitFailed> {
//! // mutate our type in a way that could fail
//! if false {
//! Err(ComplexTypeInitFailed)
//! } else {
//! Ok(value + 5)
//! }
//! }
//!
//! #[test]
//! #[flowtest(init_complex_type: value)]
//! fn mutate_complex_type_differently() -> i32 {
//! // mutate our type in a different way
//! if false {
//! panic!("oh no!")
//! } else {
//! Ok(value + 5)
//! }
//! }
//!
//! #[test]
//! #[flowtest(
//! mutate_complex_type,
//! mutate_complex_type_differently,
//! )]
//! fn ensure_mutations_are_consistent() {
//! assert_eq!(mutate_complex_type, mutate_complex_type_differently);
//! }
//! ```
//!
//! For details see [`#[flowtest]`](flowtest).
/// Test execution state holder
///
/// Note: this type should not be considered stable.
/// Test executor
///
/// Alternate test executors can be used via
/// [`#[flowtest]`](flowtest)'s hidden `executor` parameter:
///
/// ```
/// # use flowtest::flowtest;
/// #[flowtest(
/// executor = flowtest::standard_executor::StandardExecutor,
/// // ...
/// )]
/// # fn test() {}
/// ```
///
/// Note: this type should not be considered stable.
/// This macro allows a test to wait on dependencies and be waited on by dependents
///
/// # Usage
/// The `#[flowtest]` attribute should be added to test functions that produce or consume
/// values from other tests. Values returned by the function will be consumable by other
/// tests, and test failures will cause all dependent tasks to fail.
/// Note that values must be `Clone` to send between tests.
///
/// *Note that you must also specify a normal test macro,
/// for example* `#[test]` *or* `#[tokio::test]` *for the test to run.*
///
/// Tests may return a result. Returning `Err` is considered a test failue and is
/// passed to the test harness. A test is determined to return a result if the return
/// type's name is `Result`, for example `std::result::Result<_, _>` or `anyhow::Result<_>`.
/// If you want return a result as a value to be consumed by another test then the test's
/// *return handler* can be set to `data`.
/// If you want to treat a type not named `Result` as a result, then the test's
/// *return handler* can be set to `result`.
///
/// ## Syntax
/// ```ignore
/// #[flowtest((
/// dependency1,
/// dependency2: renamed_return_value,
/// dependency3: (pattern, matched, return_value),
/// ) -> return_handler)]
/// ```
///
/// There is also a short form if you do not need to change the return handler.
///
/// ```
/// # use flowtest::flowtest;
/// # #[flowtest] fn dependency1() {};
/// # #[flowtest] fn dependency2() {};
/// # #[flowtest] fn dependency3() -> ((), (), ()) { ((), (), ()) };
/// #[flowtest(
/// dependency1,
/// dependency2: renamed_return_value,
/// dependency3: (pattern, matched, return_value),
/// )]
/// # fn test() {}
/// ```
///
/// # Footguns
/// - **Test macro requirement**: Flowtest does not automatically add `#[test]`.
/// You should add `#[test]` or a stand-in above the `#[flowtest]` attribute.
/// Failure to do so will result in the test never running and deadlocking all
/// dependent tasks as explained below.
/// - **Deadlocks**: Tests will deadlock if the test harness does not run enough
/// concurrent tests for all of a test's dependencies to be satisfied,
/// for example if the test harness only has one thread and a test with
/// an unsatisifed dependency is run. **Under normal circumstances this will
/// not be an issue**.
/// Note: The rust test harness currently runs tests in alphabetical order.
/// If this is an issue for you, you can try alphabetically ordering your
/// tests to match the dependency order, or give the test harness more threads.
/// - **Attribute Macro Order**: Attribute macros are applied in the order they are
/// listed on a function as shown below.
/// ```
/// // Anything this macro does will be wrapped by flowtest's executor.
/// // This means it will not start until all test dependencies have been resolved.
/// // Most attributes should go here.
/// # use flowtest::flowtest;
/// # use test as runs_before;
/// # use test as runs_after;
/// #[runs_before]
/// #[flowtest]
/// // Anything this macro does will wrap flowtest's executor.
/// // This means it will wait an arbitrary amount of time for
/// // the thread to resume and the test to run.
/// #[runs_after]
/// fn test() {}
/// ```
///
/// # Example
/// See the [crate level docs](crate).
pub use flowtest;