aboutsummaryrefslogtreecommitdiff
path: root/testsuite/minitest/src/lib.rs
blob: d98fb64c36e08b2fb14188a24043d852351907ac (plain) (blame)
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
#![no_std]

use core::fmt::Debug;
pub use minitest_macros::tests;

/// Private implementation details used by the proc macro.
#[doc(hidden)]
pub mod export;

mod sealed {
    pub trait Sealed {}
    impl Sealed for () {}
    impl<T, E> Sealed for Result<T, E> {}
}

/// Indicates whether a test succeeded or failed.
///
/// This is comparable to the `Termination` trait in libstd, except stable and tailored towards the
/// needs of defmt-test. It is implemented for `()`, which always indicates success, and `Result`,
/// where `Ok` indicates success.
pub trait TestOutcome: Debug + sealed::Sealed {
    fn is_success(&self) -> bool;
}

impl TestOutcome for () {
    fn is_success(&self) -> bool {
        true
    }
}

impl<T: Debug, E: Debug> TestOutcome for Result<T, E> {
    fn is_success(&self) -> bool {
        self.is_ok()
    }
}

#[macro_export]
macro_rules! log {
    ($s:literal $(, $x:expr)* $(,)?)  => {
        {
            #[cfg(feature = "semihosting")]
            ::cortex_m_semihosting::hprintln!($s $(, $x)*);
            #[cfg(feature = "rtt")]
            ::rtt_target::rprintln!($s $(, $x)*);
            #[cfg(not(any(feature = "semihosting", feature="rtt")))]
            let _ = ($( & $x ),*);
        }
    };
}

/// Stop all tests without failure.
pub fn exit() -> ! {
    #[cfg(feature = "rtt")]
    cortex_m::asm::bkpt();
    #[cfg(feature = "semihosting")]
    cortex_m_semihosting::debug::exit(cortex_m_semihosting::debug::EXIT_SUCCESS);

    unreachable!()
}

/// Stop all tests and report a failure.
pub fn fail() -> ! {
    #[cfg(feature = "rtt")]
    cortex_m::asm::udf();
    #[cfg(feature = "semihosting")]
    cortex_m_semihosting::debug::exit(cortex_m_semihosting::debug::EXIT_FAILURE);

    #[cfg(not(feature = "rtt"))]
    unreachable!()
}