Skip to main content

test_r/
lib.rs

1pub use test_r_macro::add_test;
2pub use test_r_macro::always_capture;
3pub use test_r_macro::always_ensure_time;
4pub use test_r_macro::always_report_time;
5pub use test_r_macro::bench;
6pub use test_r_macro::define_matrix_dimension;
7pub use test_r_macro::flaky;
8pub use test_r_macro::ignore_detached_panics;
9pub use test_r_macro::inherit_test_dep;
10pub use test_r_macro::never_capture;
11pub use test_r_macro::never_ensure_time;
12pub use test_r_macro::never_report_time;
13pub use test_r_macro::non_flaky;
14pub use test_r_macro::sequential;
15pub use test_r_macro::sequential_suite;
16pub use test_r_macro::tag;
17pub use test_r_macro::tag_suite;
18pub use test_r_macro::test;
19pub use test_r_macro::test_dep;
20pub use test_r_macro::test_gen;
21pub use test_r_macro::timeout;
22pub use test_r_macro::timeout_suite;
23pub use test_r_macro::uses_test_r as enable;
24
25#[cfg(feature = "tokio")]
26pub use test_r_core::bench::AsyncBencher;
27pub use test_r_core::bench::Bencher;
28#[cfg(feature = "tokio")]
29pub use test_r_core::spawn::spawn;
30pub use test_r_core::spawn::spawn_thread;
31
32pub mod core {
33    use std::time::Duration;
34    pub use test_r_core::internal::{
35        CaptureControl, DependencyConstructor, DependencyView, DetachedPanicPolicy,
36        DynamicTestRegistration, FailureCause, FlakinessControl, GeneratedTest, ReportTimeControl,
37        ShouldPanic, TestFunction, TestGeneratorFunction, TestProperties, TestReturnValue,
38        TestType,
39    };
40    pub use test_r_core::*;
41
42    #[allow(clippy::too_many_arguments)]
43    pub fn register_test(
44        name: &str,
45        module_path: &str,
46        is_ignored: bool,
47        should_panic: ShouldPanic,
48        test_type: TestType,
49        timeout: Option<Duration>,
50        flakiness_control: FlakinessControl,
51        capture_control: CaptureControl,
52        tags: Vec<String>,
53        report_time_control: ReportTimeControl,
54        ensure_time_control: ReportTimeControl,
55        detached_panic_policy: DetachedPanicPolicy,
56        run: TestFunction,
57        dependencies: Option<Vec<String>>,
58    ) {
59        let (crate_name, module_path) = split_module_path(module_path);
60
61        internal::REGISTERED_TESTS
62            .lock()
63            .unwrap()
64            .push(internal::RegisteredTest {
65                name: name.to_string(),
66                crate_name,
67                module_path,
68                run,
69                props: internal::TestProperties {
70                    should_panic,
71                    test_type,
72                    timeout,
73                    flakiness_control,
74                    capture_control,
75                    report_time_control,
76                    ensure_time_control,
77                    tags,
78                    is_ignored,
79                    detached_panic_policy,
80                },
81                dependencies,
82            });
83    }
84
85    pub fn register_dependency_constructor(
86        name: &str,
87        module_path: &str,
88        cons: DependencyConstructor,
89        dependencies: Vec<String>,
90    ) {
91        let (crate_name, module_path) = split_module_path(module_path);
92
93        internal::REGISTERED_DEPENDENCY_CONSTRUCTORS
94            .lock()
95            .unwrap()
96            .push(internal::RegisteredDependency {
97                name: name.to_string(),
98                crate_name,
99                module_path,
100                constructor: cons,
101                dependencies,
102            });
103    }
104
105    pub fn register_suite_sequential(name: &str, module_path: &str) {
106        let (crate_name, module_path) = split_module_path(module_path);
107
108        internal::REGISTERED_TESTSUITE_PROPS.lock().unwrap().push(
109            internal::RegisteredTestSuiteProperty::Sequential {
110                name: name.to_string(),
111                crate_name,
112                module_path,
113            },
114        );
115    }
116
117    pub fn register_suite_timeout(name: &str, module_path: &str, timeout: Duration) {
118        let (crate_name, module_path) = split_module_path(module_path);
119
120        internal::REGISTERED_TESTSUITE_PROPS.lock().unwrap().push(
121            internal::RegisteredTestSuiteProperty::Timeout {
122                name: name.to_string(),
123                crate_name,
124                module_path,
125                timeout,
126            },
127        );
128    }
129
130    pub fn register_suite_tag(name: &str, module_path: &str, tag: String) {
131        let (crate_name, module_path) = split_module_path(module_path);
132
133        internal::REGISTERED_TESTSUITE_PROPS.lock().unwrap().push(
134            internal::RegisteredTestSuiteProperty::Tag {
135                name: name.to_string(),
136                crate_name,
137                module_path,
138                tag,
139            },
140        );
141    }
142
143    pub fn register_test_generator(
144        name: &str,
145        module_path: &str,
146        is_ignored: bool,
147        run: TestGeneratorFunction,
148    ) {
149        let (crate_name, module_path) = split_module_path(module_path);
150
151        internal::REGISTERED_TEST_GENERATORS.lock().unwrap().push(
152            internal::RegisteredTestGenerator {
153                name: name.to_string(),
154                crate_name,
155                module_path,
156                run,
157                is_ignored,
158            },
159        );
160    }
161
162    fn split_module_path(module_path: &str) -> (String, String) {
163        let (crate_name, module_path) =
164            if let Some((crate_name, module_path)) = module_path.split_once("::") {
165                (crate_name.to_string(), module_path.to_string())
166            } else {
167                (module_path.to_string(), String::new())
168            };
169        (crate_name, module_path)
170    }
171}
172
173pub use ::ctor;