Skip to main content

rstest_bdd_policy/
lib.rs

1//! Shared execution policy types for rstest-bdd.
2//!
3//! This crate centralizes runtime policy enums so both the runtime crate and
4//! the proc-macro crate can depend on a single, canonical definition without
5//! creating a proc-macro dependency cycle.
6//!
7//! It also provides canonical attribute-policy path resolution helpers used by
8//! macro codegen.
9
10/// Runtime mode for scenario test execution.
11///
12/// # Examples
13///
14/// ```
15/// use rstest_bdd_policy::RuntimeMode;
16///
17/// let mode = RuntimeMode::default();
18/// assert!(!mode.is_async());
19/// ```
20#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
21pub enum RuntimeMode {
22    /// Synchronous execution (default).
23    #[default]
24    Sync,
25    /// Tokio current-thread runtime (`#[tokio::test(flavor = "current_thread")]`).
26    TokioCurrentThread,
27}
28
29impl RuntimeMode {
30    /// Returns `true` if this mode requires async test generation.
31    ///
32    /// # Examples
33    ///
34    /// ```
35    /// use rstest_bdd_policy::RuntimeMode;
36    ///
37    /// assert!(!RuntimeMode::Sync.is_async());
38    /// assert!(RuntimeMode::TokioCurrentThread.is_async());
39    /// ```
40    #[must_use]
41    pub const fn is_async(self) -> bool { matches!(self, Self::TokioCurrentThread) }
42
43    /// Returns a hint for which test attributes to generate.
44    ///
45    /// # Examples
46    ///
47    /// ```
48    /// use rstest_bdd_policy::{RuntimeMode, TestAttributeHint};
49    ///
50    /// assert_eq!(
51    ///     RuntimeMode::Sync.test_attribute_hint(),
52    ///     TestAttributeHint::RstestOnly
53    /// );
54    /// assert_eq!(
55    ///     RuntimeMode::TokioCurrentThread.test_attribute_hint(),
56    ///     TestAttributeHint::RstestWithTokioCurrentThread
57    /// );
58    /// ```
59    #[must_use]
60    pub const fn test_attribute_hint(self) -> TestAttributeHint {
61        match self {
62            Self::Sync => TestAttributeHint::RstestOnly,
63            Self::TokioCurrentThread => TestAttributeHint::RstestWithTokioCurrentThread,
64        }
65    }
66}
67
68/// Hint for which test attributes the macro layer should generate.
69#[derive(Clone, Copy, Debug, PartialEq, Eq)]
70pub enum TestAttributeHint {
71    /// Generate only `#[rstest::rstest]`.
72    RstestOnly,
73    /// Generate `#[rstest::rstest]` and `#[tokio::test(flavor = "current_thread")]`.
74    RstestWithTokioCurrentThread,
75    /// Generate `#[rstest::rstest]` and `#[gpui::test]`.
76    RstestWithGpuiTest,
77}
78
79/// Canonical path segments for `DefaultAttributePolicy`.
80pub const DEFAULT_ATTRIBUTE_POLICY_PATH: &[&str] =
81    &["rstest_bdd_harness", "DefaultAttributePolicy"];
82
83/// Canonical path segments for `StdHarness`.
84pub const STD_HARNESS_PATH: &[&str] = &["rstest_bdd_harness", "StdHarness"];
85
86/// Canonical path segments for `TokioAttributePolicy`.
87pub const TOKIO_ATTRIBUTE_POLICY_PATH: &[&str] =
88    &["rstest_bdd_harness_tokio", "TokioAttributePolicy"];
89
90/// Canonical path segments for `TokioHarness`.
91pub const TOKIO_HARNESS_PATH: &[&str] = &["rstest_bdd_harness_tokio", "TokioHarness"];
92
93/// Canonical path segments for `GpuiAttributePolicy`.
94pub const GPUI_ATTRIBUTE_POLICY_PATH: &[&str] = &["rstest_bdd_harness_gpui", "GpuiAttributePolicy"];
95
96/// Canonical path segments for `GpuiHarness`.
97pub const GPUI_HARNESS_PATH: &[&str] = &["rstest_bdd_harness_gpui", "GpuiHarness"];
98
99/// Reserved `StepContext` fixture key under which harness adapters publish
100/// `HarnessAdapter::Context`.
101///
102/// Defined here rather than in `rstest-bdd` so the procedural macro crate can
103/// synthesize the key without depending on the runtime crate, which would
104/// create a dependency cycle. The runtime crate re-exports this value as
105/// `rstest_bdd::RSTEST_BDD_HARNESS_CONTEXT_FIXTURE`.
106///
107/// # Examples
108///
109/// ```
110/// use rstest_bdd_policy::HARNESS_CONTEXT_FIXTURE;
111///
112/// assert_eq!(HARNESS_CONTEXT_FIXTURE, "rstest_bdd_harness_context");
113/// ```
114pub const HARNESS_CONTEXT_FIXTURE: &str = "rstest_bdd_harness_context";
115
116/// Maps known attribute-policy paths to the test-attribute hints they require.
117const KNOWN_ATTRIBUTE_POLICY_HINTS: [(&[&str], TestAttributeHint); 3] = [
118    (DEFAULT_ATTRIBUTE_POLICY_PATH, TestAttributeHint::RstestOnly),
119    (
120        TOKIO_ATTRIBUTE_POLICY_PATH,
121        TestAttributeHint::RstestWithTokioCurrentThread,
122    ),
123    (
124        GPUI_ATTRIBUTE_POLICY_PATH,
125        TestAttributeHint::RstestWithGpuiTest,
126    ),
127];
128
129/// Maps known harness paths to the test-attribute hints they require.
130const KNOWN_HARNESS_HINTS: [(&[&str], TestAttributeHint); 3] = [
131    (STD_HARNESS_PATH, TestAttributeHint::RstestOnly),
132    (
133        TOKIO_HARNESS_PATH,
134        TestAttributeHint::RstestWithTokioCurrentThread,
135    ),
136    (GPUI_HARNESS_PATH, TestAttributeHint::RstestWithGpuiTest),
137];
138
139/// Resolves a canonical attribute policy path into a test-attribute hint.
140///
141/// Path segments should be provided without a leading `::`.
142///
143/// # Examples
144///
145/// ```
146/// use rstest_bdd_policy::{TestAttributeHint, resolve_test_attribute_hint_for_policy_path};
147///
148/// assert_eq!(
149///     resolve_test_attribute_hint_for_policy_path(&[
150///         "rstest_bdd_harness_tokio",
151///         "TokioAttributePolicy",
152///     ]),
153///     Some(TestAttributeHint::RstestWithTokioCurrentThread)
154/// );
155/// assert_eq!(
156///     resolve_test_attribute_hint_for_policy_path(&["my", "Policy"]),
157///     None
158/// );
159/// ```
160#[must_use]
161pub fn resolve_test_attribute_hint_for_policy_path(
162    path_segments: &[&str],
163) -> Option<TestAttributeHint> {
164    KNOWN_ATTRIBUTE_POLICY_HINTS
165        .iter()
166        .find_map(|(known_path, hint)| (path_segments == *known_path).then_some(*hint))
167}
168
169/// Resolves a canonical harness path into a test-attribute hint.
170///
171/// Path segments should be provided without a leading `::`.
172///
173/// # Examples
174///
175/// ```
176/// use rstest_bdd_policy::{TestAttributeHint, resolve_test_attribute_hint_for_harness_path};
177///
178/// assert_eq!(
179///     resolve_test_attribute_hint_for_harness_path(
180///         &["rstest_bdd_harness_tokio", "TokioHarness",]
181///     ),
182///     Some(TestAttributeHint::RstestWithTokioCurrentThread)
183/// );
184/// assert_eq!(
185///     resolve_test_attribute_hint_for_harness_path(&["my", "Harness"]),
186///     None
187/// );
188/// ```
189#[must_use]
190pub fn resolve_test_attribute_hint_for_harness_path(
191    path_segments: &[&str],
192) -> Option<TestAttributeHint> {
193    KNOWN_HARNESS_HINTS
194        .iter()
195        .find_map(|(known_path, hint)| (path_segments == *known_path).then_some(*hint))
196}
197
198#[cfg(test)]
199mod tests {
200    //! Unit tests for policy configuration parsing and defaults.
201
202    use rstest::rstest;
203
204    use super::{
205        DEFAULT_ATTRIBUTE_POLICY_PATH,
206        GPUI_ATTRIBUTE_POLICY_PATH,
207        GPUI_HARNESS_PATH,
208        RuntimeMode,
209        STD_HARNESS_PATH,
210        TOKIO_ATTRIBUTE_POLICY_PATH,
211        TOKIO_HARNESS_PATH,
212        TestAttributeHint,
213        resolve_test_attribute_hint_for_harness_path,
214        resolve_test_attribute_hint_for_policy_path,
215    };
216
217    #[test]
218    fn runtime_mode_sync_is_default() {
219        assert_eq!(RuntimeMode::default(), RuntimeMode::Sync);
220    }
221
222    #[test]
223    fn runtime_mode_sync_is_not_async() {
224        assert!(!RuntimeMode::Sync.is_async());
225    }
226
227    #[test]
228    fn runtime_mode_tokio_current_thread_is_async() {
229        assert!(RuntimeMode::TokioCurrentThread.is_async());
230    }
231
232    #[test]
233    fn runtime_mode_sync_hint_is_rstest_only() {
234        assert_eq!(
235            RuntimeMode::Sync.test_attribute_hint(),
236            TestAttributeHint::RstestOnly
237        );
238    }
239
240    #[test]
241    fn runtime_mode_tokio_hint_is_rstest_with_tokio() {
242        assert_eq!(
243            RuntimeMode::TokioCurrentThread.test_attribute_hint(),
244            TestAttributeHint::RstestWithTokioCurrentThread
245        );
246    }
247
248    #[rstest]
249    #[case(DEFAULT_ATTRIBUTE_POLICY_PATH, Some(TestAttributeHint::RstestOnly))]
250    #[case(
251        TOKIO_ATTRIBUTE_POLICY_PATH,
252        Some(TestAttributeHint::RstestWithTokioCurrentThread)
253    )]
254    #[case(
255        GPUI_ATTRIBUTE_POLICY_PATH,
256        Some(TestAttributeHint::RstestWithGpuiTest)
257    )]
258    // Unknown policies and unqualified names are third-party code to the
259    // macro, even when the terminal segment looks first-party.
260    #[case(&["my", "Policy"], None)]
261    #[case(&["TokioAttributePolicy"], None)]
262    #[case(&["my", "TokioAttributePolicy"], None)]
263    // Misqualified and overqualified first-party-looking paths must not be
264    // accepted, otherwise a third-party crate could be mistaken for an adapter.
265    #[case(&["rstest_bdd_harness", "TokioAttributePolicy"], None)]
266    #[case(&["rstest_bdd_harness_tokio", "TokioAttributePolicy", "Extra"], None)]
267    #[case(&["third_party_harness", "GpuiAttributePolicy"], None)]
268    fn resolves_attribute_policy_paths(
269        #[case] path: &[&str],
270        #[case] expected: Option<TestAttributeHint>,
271    ) {
272        assert_eq!(resolve_test_attribute_hint_for_policy_path(path), expected);
273    }
274
275    #[rstest]
276    #[case(STD_HARNESS_PATH, Some(TestAttributeHint::RstestOnly))]
277    #[case(
278        TOKIO_HARNESS_PATH,
279        Some(TestAttributeHint::RstestWithTokioCurrentThread)
280    )]
281    #[case(GPUI_HARNESS_PATH, Some(TestAttributeHint::RstestWithGpuiTest))]
282    #[case(&["my", "Harness"], None)]
283    #[case(&["TokioHarness"], None)]
284    #[case(&["my", "TokioHarness"], None)]
285    #[case(&["rstest_bdd_harness", "TokioHarness"], None)]
286    #[case(&["rstest_bdd_harness_tokio", "TokioHarness", "Extra"], None)]
287    fn resolves_harness_paths(#[case] path: &[&str], #[case] expected: Option<TestAttributeHint>) {
288        assert_eq!(resolve_test_attribute_hint_for_harness_path(path), expected);
289    }
290
291    #[rstest]
292    #[case(DEFAULT_ATTRIBUTE_POLICY_PATH, STD_HARNESS_PATH)]
293    #[case(TOKIO_ATTRIBUTE_POLICY_PATH, TOKIO_HARNESS_PATH)]
294    #[case(GPUI_ATTRIBUTE_POLICY_PATH, GPUI_HARNESS_PATH)]
295    fn first_party_harness_paths_match_their_attribute_policy_hints(
296        #[case] policy_path: &[&str],
297        #[case] harness_path: &[&str],
298    ) {
299        assert_eq!(
300            resolve_test_attribute_hint_for_harness_path(harness_path),
301            resolve_test_attribute_hint_for_policy_path(policy_path)
302        );
303    }
304}