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/// Runtime mode for scenario test execution.
8///
9/// # Examples
10///
11/// ```
12/// use rstest_bdd_policy::RuntimeMode;
13///
14/// let mode = RuntimeMode::default();
15/// assert!(!mode.is_async());
16/// ```
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
18pub enum RuntimeMode {
19    /// Synchronous execution (default).
20    #[default]
21    Sync,
22    /// Tokio current-thread runtime (`#[tokio::test(flavor = "current_thread")]`).
23    TokioCurrentThread,
24}
25
26impl RuntimeMode {
27    /// Returns `true` if this mode requires async test generation.
28    ///
29    /// # Examples
30    ///
31    /// ```
32    /// use rstest_bdd_policy::RuntimeMode;
33    ///
34    /// assert!(!RuntimeMode::Sync.is_async());
35    /// assert!(RuntimeMode::TokioCurrentThread.is_async());
36    /// ```
37    #[must_use]
38    pub const fn is_async(self) -> bool {
39        matches!(self, Self::TokioCurrentThread)
40    }
41
42    /// Returns a hint for which test attributes to generate.
43    ///
44    /// # Examples
45    ///
46    /// ```
47    /// use rstest_bdd_policy::{RuntimeMode, TestAttributeHint};
48    ///
49    /// assert_eq!(
50    ///     RuntimeMode::Sync.test_attribute_hint(),
51    ///     TestAttributeHint::RstestOnly
52    /// );
53    /// assert_eq!(
54    ///     RuntimeMode::TokioCurrentThread.test_attribute_hint(),
55    ///     TestAttributeHint::RstestWithTokioCurrentThread
56    /// );
57    /// ```
58    #[must_use]
59    pub const fn test_attribute_hint(self) -> TestAttributeHint {
60        match self {
61            Self::Sync => TestAttributeHint::RstestOnly,
62            Self::TokioCurrentThread => TestAttributeHint::RstestWithTokioCurrentThread,
63        }
64    }
65}
66
67/// Hint for which test attributes the macro layer should generate.
68#[derive(Clone, Copy, Debug, PartialEq, Eq)]
69pub enum TestAttributeHint {
70    /// Generate only `#[rstest::rstest]`.
71    RstestOnly,
72    /// Generate `#[rstest::rstest]` and `#[tokio::test(flavor = "current_thread")]`.
73    RstestWithTokioCurrentThread,
74}
75
76#[cfg(test)]
77mod tests {
78    use super::{RuntimeMode, TestAttributeHint};
79
80    #[test]
81    fn runtime_mode_sync_is_default() {
82        assert_eq!(RuntimeMode::default(), RuntimeMode::Sync);
83    }
84
85    #[test]
86    fn runtime_mode_sync_is_not_async() {
87        assert!(!RuntimeMode::Sync.is_async());
88    }
89
90    #[test]
91    fn runtime_mode_tokio_current_thread_is_async() {
92        assert!(RuntimeMode::TokioCurrentThread.is_async());
93    }
94
95    #[test]
96    fn runtime_mode_sync_hint_is_rstest_only() {
97        assert_eq!(
98            RuntimeMode::Sync.test_attribute_hint(),
99            TestAttributeHint::RstestOnly
100        );
101    }
102
103    #[test]
104    fn runtime_mode_tokio_hint_is_rstest_with_tokio() {
105        assert_eq!(
106            RuntimeMode::TokioCurrentThread.test_attribute_hint(),
107            TestAttributeHint::RstestWithTokioCurrentThread
108        );
109    }
110}