refactor: tests on host with less stuff disabled

This commit is contained in:
cscherr 2025-04-30 16:32:45 +02:00
parent 65ae71efff
commit c82d194206
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7

View file

@ -1,15 +1,20 @@
//! This example shows how a embedded program can be written that is testable on the host with
//! libtest.
//!
//! The tests can be run with:
//! ```bash
//! cargo test --example=test-on-host --target=x86_64-unknown-linux-gnu
//! ```
#![cfg_attr(not(test), no_main)] #![cfg_attr(not(test), no_main)]
#![no_std] #![no_std]
#[cfg(not(test))] #[cfg(not(test))]
extern crate panic_halt; extern crate panic_halt;
#[cfg(not(test))]
use cortex_m_rt::entry;
use hal::{pac, prelude::*, rcc::Config}; use hal::{pac, prelude::*, rcc::Config};
#[cfg(not(test))] #[cfg_attr(not(test), cortex_m_rt::entry)] // this is the entrypoint unless testing
#[entry]
fn main() -> ! { fn main() -> ! {
let dp = pac::Peripherals::take().unwrap(); let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap(); let cp = cortex_m::Peripherals::take().unwrap();
@ -31,21 +36,24 @@ fn main() -> ! {
led.set_high().unwrap(); led.set_high().unwrap();
delay.delay_ms(500_u16); delay.delay_ms(500_u16);
let important_number = some_function(19);
delay.delay_ms(important_number as u16);
led.set_low().unwrap(); led.set_low().unwrap();
delay.delay_ms(500_u16); delay.delay_ms(500_u16);
} }
} }
#[cfg(test)] fn some_function(num: i32) -> i32 {
fn main() -> ! { num * 2
loop {}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::some_function;
#[test] #[test]
fn test_it_works() { fn test_it_works() {
assert!(true) assert_eq!(some_function(9), 18)
} }
} }