mycotest/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Shared defs between the kernel and the test runner.
//!
//! This is in its own crate mainly so the constants are the same and I can't
//! have the kernel write the wrong strings (which I did lol).

#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;

use core::{
    cmp,
    fmt::{self, Write},
    marker::PhantomData,
};
pub mod assert;
pub mod report;
#[cfg(feature = "runner")]
pub mod runner;

pub type TestResult = Result<(), assert::Failed>;
pub type Outcome = Result<(), Failure>;
pub use report::Failure;

#[derive(Clone, Eq, PartialEq)]
pub struct TestName<'a, S = &'a str> {
    name: S,
    module: S,
    _lt: PhantomData<&'a str>,
}

/// Test descriptor created by `decl_test!`. Describes and allows running an
/// individual test.
pub struct Test {
    #[doc(hidden)]
    pub descr: TestName<'static>,
    #[doc(hidden)]
    pub run: fn() -> Outcome,
}

/// Type which may be used as a test return type.
pub trait TestReport {
    /// Report any errors to `tracing`, then returns either `true` for a
    /// success, or `false` for a failure.
    fn report(self) -> Outcome;
}

impl TestReport for () {
    fn report(self) -> Outcome {
        Ok(())
    }
}

impl<T: fmt::Debug> TestReport for Result<(), T> {
    fn report(self) -> Outcome {
        match self {
            Ok(_) => Ok(()),
            Err(err) => {
                tracing::error!("FAIL {:?}", err);
                Err(Failure::Fail)
            }
        }
    }
}

/// Declare a new test, sort-of like the `#[test]` attribute.
// FIXME: Declare a `#[test]` custom attribute instead?
#[macro_export]
macro_rules! decl_test {
    (fn $name:ident $($t:tt)*) => {
        // Clippy will complain about these functions returning `Result<(),
        // ()>` unnecessarily, but this is actually required by the test
        // harness.
        #[allow(clippy::unnecessary_wraps)]
        fn $name $($t)*

        // Introduce an anonymous const to avoid name conflicts. The `#[used]`
        // will prevent the symbol from being dropped, and `link_section` will
        // make it visible.
        const _: () = {
            #[used]
            #[link_section = "MyceliumTests"]
            static TEST: $crate::Test = $crate::Test {
                descr: $crate::TestName::new(module_path!(), stringify!($name)),
                run: || $crate::TestReport::report($name()),
            };
        };
    }
}

// === impl TestName ===

impl<S> TestName<'_, S> {
    pub const fn new(module: S, name: S) -> Self {
        Self {
            name,
            module,
            _lt: PhantomData,
        }
    }
}

impl<S> TestName<'_, S>
where
    S: AsRef<str>,
{
    pub fn name(&self) -> &str {
        self.name.as_ref()
    }

    pub fn module(&self) -> &str {
        self.module.as_ref()
    }
}

impl<S: Ord> cmp::PartialOrd for TestName<'_, S> {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<S: Ord> cmp::Ord for TestName<'_, S> {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.module
            .cmp(&other.module)
            .then_with(|| self.name.cmp(&other.name))
    }
}

// Custom impl to skip `PhantomData` field.
impl<S: fmt::Debug> fmt::Debug for TestName<'_, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self { name, module, _lt } = self;
        f.debug_struct("Test")
            .field("name", name)
            .field("module", module)
            .finish()
    }
}

impl<S: fmt::Display> fmt::Display for TestName<'_, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self { name, module, _lt } = self;
        write!(f, "{module}::{name}")
    }
}

// === impl Test ===

impl Test {
    pub fn write_outcome(&self, outcome: Outcome, mut writer: impl Write) -> fmt::Result {
        tracing::trace!(?self.descr, ?outcome, "write_outcome",);
        match outcome {
            Ok(()) => writeln!(
                writer,
                "{} {} {}",
                report::PASS_TEST,
                self.descr.module,
                self.descr.name
            ),
            Err(fail) => writeln!(
                writer,
                "{} {} {} {}",
                report::FAIL_TEST,
                fail.as_str(),
                self.descr.module,
                self.descr.name
            ),
        }
    }
}

decl_test! {
    fn it_works() -> Result<(), ()> {
        tracing::info!("I'm running in a test!");
        Ok(())
    }
}