diff --git a/gpiozero/devices.py b/gpiozero/devices.py index a1feb4e..5e5b29b 100644 --- a/gpiozero/devices.py +++ b/gpiozero/devices.py @@ -243,7 +243,7 @@ class GPIODevice(ValuesMixin, GPIOBase): def _read(self): try: return self.pin.state == self._active_state - except TypeError: + except (AttributeError, TypeError): self._check_open() raise diff --git a/tests/test_devices.py b/tests/test_devices.py index 9dff553..0312c7e 100644 --- a/tests/test_devices.py +++ b/tests/test_devices.py @@ -13,5 +13,55 @@ from gpiozero.pins.mock import MockPin from gpiozero import * -# TODO devices tests! +# TODO add more devices tests! + +def test_device_no_pin(): + with pytest.raises(GPIOPinMissing): + device = GPIODevice() + +def test_device_init(): + pin = MockPin(2) + device = GPIODevice(pin) + assert not device.closed + assert device.pin == pin + +def test_device_init_twice_same_pin(): + pin = MockPin(2) + device = GPIODevice(pin) + with pytest.raises(GPIOPinInUse): + device2 = GPIODevice(pin) + +def test_device_init_twice_different_pin(): + pin = MockPin(2) + device = GPIODevice(pin) + pin2 = MockPin(3) + device2 = GPIODevice(pin2) + +def test_device_close(): + pin = MockPin(2) + device = GPIODevice(pin) + device.close() + assert device.closed + assert device.pin is None + +def test_device_reopen_same_pin(): + pin = MockPin(2) + device = GPIODevice(pin) + device.close() + device2 = GPIODevice(pin) + assert not device2.closed + assert device2.pin == pin + assert device.closed + assert device.pin is None + +def test_device_repr(): + pin = MockPin(2) + device = GPIODevice(pin) + assert repr(device) == '' % pin + +def test_device_repr_after_close(): + pin = MockPin(2) + device = GPIODevice(pin) + device.close() + assert repr(device) == ''