Add tests for invalid values

pull/4/head
Diego Barrios Romero 2018-10-28 12:52:24 +01:00
parent b9610b6e73
commit e79b38f629
2 changed files with 27 additions and 1 deletions

View File

@ -59,3 +59,16 @@ macro_rules! set_test {
}; };
} }
#[macro_export]
macro_rules! set_invalid_test {
($name:ident, $method:ident, $create_method:ident, $value:expr) => {
#[test]
fn $name() {
let mut dev = $create_method(&[]);
match dev.$method($value) {
Err(Error::InvalidInputData) => (),
_ => panic!("InvalidInputData error not returned.")
}
}
};
}

View File

@ -7,7 +7,7 @@ mod common;
use common::{ DEVICE_ADDRESS, Register, new_ds3231, use common::{ DEVICE_ADDRESS, Register, new_ds3231,
new_ds3232, new_ds3234 }; new_ds3232, new_ds3234 };
extern crate ds323x; extern crate ds323x;
use ds323x::{ Hours }; use ds323x::{ Hours, Error };
mod seconds { mod seconds {
use super::*; use super::*;
@ -29,6 +29,10 @@ mod seconds {
set_test!(can_set_ds3234, set_seconds, new_ds3234, 1, set_test!(can_set_ds3234, set_seconds, new_ds3234, 1,
SpiTrans::write(vec![Register::SECONDS + 0x80, 1])); SpiTrans::write(vec![Register::SECONDS + 0x80, 1]));
set_invalid_test!(cannot_set_invalid_ds3231, set_seconds, new_ds3231, 60);
set_invalid_test!(cannot_set_invalid_ds3232, set_seconds, new_ds3232, 60);
set_invalid_test!(cannot_set_invalid_ds3234, set_seconds, new_ds3234, 60);
} }
mod minutes { mod minutes {
@ -52,6 +56,11 @@ mod minutes {
set_test!(can_set_ds3234, set_minutes, new_ds3234, 1, set_test!(can_set_ds3234, set_minutes, new_ds3234, 1,
SpiTrans::write(vec![Register::MINUTES + 0x80, 1])); SpiTrans::write(vec![Register::MINUTES + 0x80, 1]));
set_invalid_test!(cannot_set_invalid_ds3231, set_minutes, new_ds3231, 60);
set_invalid_test!(cannot_set_invalid_ds3232, set_minutes, new_ds3232, 60);
set_invalid_test!(cannot_set_invalid_ds3234, set_minutes, new_ds3234, 60);
}
mod hours { mod hours {
use super::*; use super::*;
get_test!(can_get_ds3231, get_hours, new_ds3231, Hours::H24(21), get_test!(can_get_ds3231, get_hours, new_ds3231, Hours::H24(21),
@ -72,4 +81,8 @@ mod hours {
set_test!(can_set_ds3234, set_hours, new_ds3234, Hours::H24(21), set_test!(can_set_ds3234, set_hours, new_ds3234, Hours::H24(21),
SpiTrans::write(vec![Register::HOURS + 0x80, 0b0010_0001])); SpiTrans::write(vec![Register::HOURS + 0x80, 0b0010_0001]));
set_invalid_test!(cannot_set_invalid_ds3231, set_hours, new_ds3231, Hours::H24(24));
set_invalid_test!(cannot_set_invalid_ds3232, set_hours, new_ds3232, Hours::H24(24));
set_invalid_test!(cannot_set_invalid_ds3234, set_hours, new_ds3234, Hours::H24(24));
} }