From d2a675ef70e3e8fd17816f2aec8d16dede5e5128 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Sun, 28 Oct 2018 12:06:25 +0100 Subject: [PATCH] Move macros to common file --- tests/common/mod.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ tests/ds323x.rs | 44 ++++---------------------------------------- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 37cd64a..f35e492 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,4 +1,8 @@ extern crate embedded_hal; +extern crate ds323x; +use self::ds323x::{ Ds323x, interface, ic }; +use hal::i2c::{ Mock as I2cMock, Transaction as I2cTrans }; +use hal::spi::{ Mock as SpiMock, Transaction as SpiTrans }; pub const DEVICE_ADDRESS: u8 = 0b110_1000; @@ -14,3 +18,42 @@ impl embedded_hal::digital::OutputPin for DummyOutputPin { fn set_low(&mut self) {} fn set_high(&mut self) {} } + + +pub fn new_ds3231(transactions: &[I2cTrans]) -> Ds323x, ic::DS3231> { + Ds323x::new_ds3231(I2cMock::new(&transactions)) +} + +pub fn new_ds3232(transactions: &[I2cTrans]) -> Ds323x, ic::DS3232> { + Ds323x::new_ds3232(I2cMock::new(&transactions)) +} + +pub fn new_ds3234(transactions: &[SpiTrans]) + -> Ds323x, ic::DS3234> { + Ds323x::new_ds3234(SpiMock::new(&transactions), DummyOutputPin) +} + +#[macro_export] +macro_rules! get_test { + ($name:ident, $method:ident, $create_method:ident, $expected:expr, $transaction:expr) => { + #[test] + fn $name() { + let transactions = [ $transaction ]; + let mut dev = $create_method(&transactions); + assert_eq!($expected, dev.$method().unwrap()); + } + }; +} + +#[macro_export] +macro_rules! set_test { + ($name:ident, $method:ident, $create_method:ident, $value:expr, $transaction:expr) => { + #[test] + fn $name() { + let transactions = [ $transaction ]; + let mut dev = $create_method(&transactions); + dev.$method($value).unwrap(); + } + }; +} + diff --git a/tests/ds323x.rs b/tests/ds323x.rs index df26eb0..4e62e51 100644 --- a/tests/ds323x.rs +++ b/tests/ds323x.rs @@ -1,45 +1,9 @@ extern crate embedded_hal_mock as hal; -extern crate ds323x; -use ds323x::{ Ds323x, interface, ic }; -use hal::i2c::{ Mock as I2cMock, Transaction as I2cTrans }; -use hal::spi::{ Mock as SpiMock, Transaction as SpiTrans }; +use hal::i2c::Transaction as I2cTrans; +use hal::spi::Transaction as SpiTrans; mod common; -use common::{ DEVICE_ADDRESS, Register, DummyOutputPin }; - -fn new_ds3231(transactions: &[I2cTrans]) -> Ds323x, ic::DS3231> { - Ds323x::new_ds3231(I2cMock::new(&transactions)) -} - -fn new_ds3232(transactions: &[I2cTrans]) -> Ds323x, ic::DS3232> { - Ds323x::new_ds3232(I2cMock::new(&transactions)) -} - -fn new_ds3234(transactions: &[SpiTrans]) - -> Ds323x, ic::DS3234> { - Ds323x::new_ds3234(SpiMock::new(&transactions), DummyOutputPin) -} - -macro_rules! get_test { - ($name:ident, $method:ident, $create_method:ident, $expected:expr, $transaction:expr) => { - #[test] - fn $name() { - let transactions = [ $transaction ]; - let mut dev = $create_method(&transactions); - assert_eq!($expected, dev.$method().unwrap()); - } - }; -} - -macro_rules! set_test { - ($name:ident, $method:ident, $create_method:ident, $value:expr, $transaction:expr) => { - #[test] - fn $name() { - let transactions = [ $transaction ]; - let mut dev = $create_method(&transactions); - dev.$method($value).unwrap(); - } - }; -} +use common::{ DEVICE_ADDRESS, Register, new_ds3231, + new_ds3232, new_ds3234 }; get_test!(can_get_seconds_ds3231, get_seconds, new_ds3231, 1, I2cTrans::write_read(DEVICE_ADDRESS, vec![Register::SECONDS], vec![1]));