Skip to main content

icedwright_probe/
test_id.rs

1//! Typed test ids.
2
3use std::borrow::Cow;
4use std::fmt;
5
6/// A test id the view and the tests name with the same value, so a typo does not compile.
7///
8/// Derive it on an enum with `#[derive(TestId)]`: unit variants become their name in kebab
9/// case, and `#[test_id("delete-{0}")]` sets a string or a format string over the fields.
10pub trait TestId: Copy + fmt::Debug + 'static {
11    /// The string written into the tree.
12    fn test_id(&self) -> Cow<'static, str>;
13
14    /// The Rust path of the variant, such as `Ids::Delete`, for error messages and the
15    /// recorder's output.
16    fn rust_path(&self) -> &'static str;
17
18    /// The variant's path from its crate root, such as `todo::ui::Ids::Delete`, which the
19    /// raw tree reports so the recorder can name the variant from a test. The derive builds it
20    /// with `module_path!()`; an impl written by hand gives [`rust_path`](TestId::rust_path).
21    fn qualified_path(&self) -> &'static str {
22        self.rust_path()
23    }
24}
25
26/// A test id as the tree and the locators carry it.
27#[derive(Debug, Clone, PartialEq, Eq, Hash)]
28pub struct TestIdRef<'a> {
29    /// The string.
30    pub id: Cow<'a, str>,
31    /// The Rust path of the variant it came from, when it came from a [`TestId`].
32    pub rust_path: Option<&'static str>,
33}
34
35impl TestIdRef<'_> {
36    /// The same id with its string owned.
37    #[must_use]
38    pub fn into_static(self) -> TestIdRef<'static> {
39        TestIdRef {
40            id: Cow::Owned(self.id.into_owned()),
41            rust_path: self.rust_path,
42        }
43    }
44}
45
46impl fmt::Display for TestIdRef<'_> {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        f.write_str(&self.id)
49    }
50}
51
52/// What `.test_id(..)`, [`test_id`](crate::test_id) and `get_by_test_id(..)` accept: a
53/// [`TestId`] value, or a string for an id built at run time.
54pub trait IntoTestId<'a> {
55    /// The id as the tree carries it.
56    fn into_test_id(self) -> TestIdRef<'a>;
57
58    /// The [`TestId::qualified_path`] of a typed id; `None` for a string.
59    fn qualified_rust_path(&self) -> Option<&'static str> {
60        None
61    }
62}
63
64impl<'a> IntoTestId<'a> for &'a str {
65    fn into_test_id(self) -> TestIdRef<'a> {
66        TestIdRef {
67            id: Cow::Borrowed(self),
68            rust_path: None,
69        }
70    }
71}
72
73impl<'a> IntoTestId<'a> for &'a String {
74    fn into_test_id(self) -> TestIdRef<'a> {
75        self.as_str().into_test_id()
76    }
77}
78
79impl IntoTestId<'_> for String {
80    fn into_test_id(self) -> TestIdRef<'static> {
81        TestIdRef {
82            id: Cow::Owned(self),
83            rust_path: None,
84        }
85    }
86}
87
88impl<'a> IntoTestId<'a> for Cow<'a, str> {
89    fn into_test_id(self) -> TestIdRef<'a> {
90        TestIdRef {
91            id: self,
92            rust_path: None,
93        }
94    }
95}
96
97impl<'a> IntoTestId<'a> for TestIdRef<'a> {
98    fn into_test_id(self) -> TestIdRef<'a> {
99        self
100    }
101}
102
103impl<T: TestId> IntoTestId<'_> for T {
104    fn into_test_id(self) -> TestIdRef<'static> {
105        TestIdRef {
106            id: self.test_id(),
107            rust_path: Some(self.rust_path()),
108        }
109    }
110
111    fn qualified_rust_path(&self) -> Option<&'static str> {
112        Some(self.qualified_path())
113    }
114}