Files
rust-otus/smart-house/src/device/thermometer.rs

46 lines
974 B
Rust

#![allow(unused)]
use std::fmt::Display;
pub struct Thermometer {
temperature: f32,
}
impl Thermometer {
pub fn new(temperature: f32) -> Self {
Self { temperature }
}
pub fn get_temperature(&self) -> f32 {
self.temperature
}
pub fn display(&self) -> impl Display {
format!("Thermometer[ {:02.1} ]", self.get_temperature())
}
}
impl super::Device for Thermometer {
fn print_status(&self) {
println!("{}", self.display())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn smoke_test() {
let thermometer = Thermometer::new(20.0);
assert_eq!(thermometer.temperature, 20.0);
assert_eq!(thermometer.get_temperature(), 20.0);
}
#[test]
fn display_test() {
assert_eq!(format!("{}", Thermometer::new(19.550).display()), "Thermometer[ 19.5 ]");
assert_eq!(format!("{}", Thermometer::new(19.551).display()), "Thermometer[ 19.6 ]");
}
}