1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! Mock implementations of [InputPin](embedded_hal::digital::v2::InputPin) and [OutputPin](embedded_hal::digital::v2::OutputPin).
//!
//! WARNING: May be removed if `embedded_hal_mock` crate is improved.
//! https://github.com/dbrgn/embedded-hal-mock/issues/30
//!
//! This is part of the main crate so it is accessible to doctests.
//! Otherwise, I would have created a tests/mock/mod.rs file.
use embedded_hal::digital::v2::toggleable;
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};

#[derive(PartialEq, Eq, Debug)]
pub enum State {
    Low,
    High,
}

pub struct Pin {
    state: Option<State>,
}

impl Pin {
    pub fn new() -> Self {
        Pin { state: None }
    }

    pub fn with_state(state: State) -> Self {
        Pin { state: Some(state) }
    }
}

type MockError = &'static str;

impl InputPin for Pin {
    type Error = MockError;

    fn is_high(&self) -> Result<bool, Self::Error> {
        match self.state {
            Some(State::High) => Ok(true),
            Some(State::Low) => Ok(false),
            None => Err("state not set"),
        }
    }

    fn is_low(&self) -> Result<bool, Self::Error> {
        match self.is_high() {
            Ok(v) => Ok(!v),
            Err(e) => Err(e),
        }
    }
}

impl OutputPin for Pin {
    type Error = MockError;

    fn set_low(&mut self) -> Result<(), Self::Error> {
        self.state = Some(State::Low);
        Ok(())
    }

    fn set_high(&mut self) -> Result<(), Self::Error> {
        self.state = Some(State::High);
        Ok(())
    }
}

impl StatefulOutputPin for Pin {
    fn is_set_low(&self) -> Result<bool, Self::Error> {
        self.is_low()
    }

    fn is_set_high(&self) -> Result<bool, Self::Error> {
        self.is_high()
    }
}

impl toggleable::Default for Pin {}

#[cfg(test)]
mod test {
    use super::*;

    mod new {
        use super::*;

        #[test]
        fn state_is_unitialized() {
            let pin = Pin::new();
            assert_eq!(None, pin.state);
            pin.is_low().expect_err("Expected unitialized pin");
        }
    }

    mod input_pin {
        use super::*;

        #[test]
        fn error_when_uninitialized() {
            let pin = Pin { state: None };
            pin.is_high().expect_err("Expected unitialized pin");
        }

        mod is_high {
            use super::*;

            #[test]
            fn returns_true_when_state_is_high() {
                let pin = Pin::with_state(State::High);
                assert_eq!(true, pin.is_high().unwrap());
            }

            #[test]
            fn returns_false_when_state_is_low() {
                let pin = Pin::with_state(State::Low);
                assert_eq!(false, pin.is_high().unwrap());
            }
        }

        mod is_low {
            use super::*;

            #[test]
            fn returns_false_when_state_is_high() {
                let pin = Pin::with_state(State::High);
                assert_eq!(false, pin.is_low().unwrap());
            }

            #[test]
            fn returns_true_when_state_is_high() {
                let pin = Pin::with_state(State::Low);
                assert_eq!(true, pin.is_low().unwrap());
            }
        }
    }

    mod output_pin {
        use super::*;

        #[test]
        fn set_low() {
            let mut pin = Pin::new();
            pin.set_low().unwrap();

            assert_eq!(true, pin.is_low().unwrap());
        }

        #[test]
        fn set_high() {
            let mut pin = Pin::new();
            pin.set_high().unwrap();

            assert_eq!(true, pin.is_high().unwrap());
        }
    }

    mod statful_output_pin {
        use super::*;

        #[test]
        fn error_when_uninitialized() {
            let pin = Pin { state: None };
            pin.is_set_high().expect_err("Expected unitialized pin");
        }

        mod is_set_low {
            use super::*;

            #[test]
            fn returns_false_when_state_is_high() {
                let pin = Pin::with_state(State::High);
                assert_eq!(false, pin.is_set_low().unwrap());
            }

            #[test]
            fn returns_true_when_state_is_high() {
                let pin = Pin::with_state(State::Low);
                assert_eq!(true, pin.is_set_low().unwrap());
            }
        }

        mod is_set_high {
            use super::*;

            #[test]
            fn returns_true_when_state_is_high() {
                let pin = Pin::with_state(State::High);
                assert_eq!(true, pin.is_set_high().unwrap());
            }

            #[test]
            fn returns_false_when_state_is_low() {
                let pin = Pin::with_state(State::Low);
                assert_eq!(false, pin.is_set_high().unwrap());
            }
        }

        mod toggleable {
            use super::*;
            use embedded_hal::digital::v2::ToggleableOutputPin;

            #[test]
            fn default_toggleable_impl() {
                let mut pin = Pin::with_state(State::Low);
                pin.toggle().unwrap();
                assert_eq!(true, pin.is_set_high().unwrap());
            }
        }
    }
}