embedded_test/
lib.rs

1// Copied from https://github.com/knurling-rs/defmt/blob/main/firmware/defmt-test/src/lib.rs
2
3#![cfg_attr(not(feature = "std"), no_std)]
4#![allow(clippy::needless_doctest_main)]
5#![cfg_attr(not(doctest), doc = include_str!("../README.md"))]
6
7mod fmt;
8
9pub use embedded_test_macros::tests;
10
11#[cfg(all(feature = "panic-handler", not(feature = "ariel-os")))]
12#[panic_handler]
13fn panic(info: &core::panic::PanicInfo) -> ! {
14    error!("====================== PANIC ======================");
15
16    error!("{}", info);
17
18    semihosting::process::abort()
19}
20
21/// Private implementation details used by the proc macro.
22/// WARNING: This API is not stable and may change at any time.
23#[doc(hidden)]
24pub mod export;
25
26mod sealed {
27    pub trait Sealed {}
28    impl Sealed for () {}
29    impl<T, E> Sealed for Result<T, E> {}
30}
31
32/// Indicates whether a test succeeded or failed.
33///
34/// This is comparable to the `Termination` trait in libstd, except stable and tailored towards the
35/// needs of embedded-test. It is implemented for `()`, which always indicates success, and `Result`,
36/// where `Ok` indicates success.
37#[cfg(feature = "defmt")]
38pub trait TestOutcome: defmt::Format + sealed::Sealed {
39    fn is_success(&self) -> bool;
40}
41
42/// Indicates whether a test succeeded or failed.
43///
44/// This is comparable to the `Termination` trait in libstd, except stable and tailored towards the
45/// needs of embedded-test. It is implemented for `()`, which always indicates success, and `Result`,
46/// where `Ok` indicates success.
47#[cfg(feature = "log")]
48pub trait TestOutcome: core::fmt::Debug + sealed::Sealed {
49    fn is_success(&self) -> bool;
50}
51
52/// Indicates whether a test succeeded or failed.
53///
54/// This is comparable to the `Termination` trait in libstd, except stable and tailored towards the
55/// needs of embedded-test. It is implemented for `()`, which always indicates success, and `Result`,
56/// where `Ok` indicates success.
57#[cfg(all(not(feature = "log"), not(feature = "defmt")))]
58pub trait TestOutcome: sealed::Sealed {
59    fn is_success(&self) -> bool;
60}
61
62impl TestOutcome for () {
63    fn is_success(&self) -> bool {
64        true
65    }
66}
67
68#[cfg(feature = "log")]
69impl<T: core::fmt::Debug, E: core::fmt::Debug> TestOutcome for Result<T, E> {
70    fn is_success(&self) -> bool {
71        self.is_ok()
72    }
73}
74
75#[cfg(feature = "defmt")]
76impl<T: defmt::Format, E: defmt::Format> TestOutcome for Result<T, E> {
77    fn is_success(&self) -> bool {
78        self.is_ok()
79    }
80}
81
82#[cfg(all(not(feature = "log"), not(feature = "defmt")))]
83impl<T, E> TestOutcome for Result<T, E> {
84    fn is_success(&self) -> bool {
85        self.is_ok()
86    }
87}