Skip to main content

use_test_assertion/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4/// Primitive assertion categories.
5#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
6pub enum TestAssertionKind {
7    Equal,
8    NotEqual,
9    Contains,
10    DoesNotContain,
11    GreaterThan,
12    GreaterThanOrEqual,
13    LessThan,
14    LessThanOrEqual,
15    ApproxEqual,
16    IsTrue,
17    IsFalse,
18    IsSome,
19    IsNone,
20    IsOk,
21    IsErr,
22}
23
24/// Assertion metadata without assertion execution.
25#[derive(Clone, Debug, Eq, Hash, PartialEq)]
26pub struct TestAssertion {
27    pub kind: TestAssertionKind,
28    pub label: Option<String>,
29}
30
31impl TestAssertion {
32    pub const fn new(kind: TestAssertionKind) -> Self {
33        Self { kind, label: None }
34    }
35
36    pub fn with_label(kind: TestAssertionKind, label: impl Into<String>) -> Self {
37        Self {
38            kind,
39            label: Some(label.into()),
40        }
41    }
42
43    pub const fn kind(&self) -> TestAssertionKind {
44        self.kind
45    }
46
47    pub fn label(&self) -> Option<&str> {
48        self.label.as_deref()
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::{TestAssertion, TestAssertionKind};
55
56    #[test]
57    fn creates_unlabeled_assertion() {
58        let assertion = TestAssertion::new(TestAssertionKind::IsOk);
59
60        assert_eq!(assertion.kind(), TestAssertionKind::IsOk);
61        assert_eq!(assertion.label(), None);
62    }
63
64    #[test]
65    fn creates_labeled_assertion() {
66        let assertion = TestAssertion::with_label(TestAssertionKind::Contains, "has item");
67
68        assert_eq!(assertion.kind(), TestAssertionKind::Contains);
69        assert_eq!(assertion.label(), Some("has item"));
70    }
71}