mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-12-08 20:39:01 +00:00
Fix #459 - properly support remote SPI with pigpio
Sorry! Dave's messing around with the pin implementations again. Hopefully the last time. The pin_factory is now really a factory object which can be asked to produce individual pins or pin-based interfaces like SPI (which can be supported properly via pigpio).
This commit is contained in:
@@ -11,25 +11,24 @@ from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
from gpiozero.pins.mock import MockPin, MockPWMPin
|
||||
from gpiozero.pins.mock import MockPWMPin, MockPin
|
||||
from gpiozero import *
|
||||
|
||||
|
||||
def teardown_function(function):
|
||||
MockPin.clear_pins()
|
||||
Device._pin_factory.reset()
|
||||
|
||||
|
||||
# Some rough tests to make sure our MockPin is up to snuff. This is just
|
||||
# enough to get reasonable coverage but it's by no means comprehensive...
|
||||
|
||||
def test_mock_pin_init():
|
||||
with pytest.raises(TypeError):
|
||||
MockPin()
|
||||
with pytest.raises(ValueError):
|
||||
MockPin(60)
|
||||
assert MockPin(2).number == 2
|
||||
Device._pin_factory.pin(60)
|
||||
assert Device._pin_factory.pin(2).number == 2
|
||||
|
||||
def test_mock_pin_defaults():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
assert pin.bounce == None
|
||||
assert pin.edges == 'both'
|
||||
assert pin.frequency == None
|
||||
@@ -39,30 +38,23 @@ def test_mock_pin_defaults():
|
||||
assert pin.when_changed == None
|
||||
|
||||
def test_mock_pin_open_close():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
pin.close()
|
||||
|
||||
def test_mock_pin_init_twice_same_pin():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(pin1.number)
|
||||
pin1 = Device._pin_factory.pin(2)
|
||||
pin2 = Device._pin_factory.pin(pin1.number)
|
||||
assert pin1 is pin2
|
||||
|
||||
def test_mock_pin_init_twice_different_pin():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(pin1.number+1)
|
||||
pin1 = Device._pin_factory.pin(2)
|
||||
pin2 = Device._pin_factory.pin(pin1.number+1)
|
||||
assert pin1 != pin2
|
||||
assert pin1.number == 2
|
||||
assert pin2.number == pin1.number+1
|
||||
|
||||
def test_mock_pwm_pin_init():
|
||||
with pytest.raises(TypeError):
|
||||
MockPWMPin()
|
||||
with pytest.raises(ValueError):
|
||||
MockPWMPin(60)
|
||||
assert MockPWMPin(2).number == 2
|
||||
|
||||
def test_mock_pwm_pin_defaults():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
assert pin.bounce == None
|
||||
assert pin.edges == 'both'
|
||||
assert pin.frequency == None
|
||||
@@ -72,38 +64,38 @@ def test_mock_pwm_pin_defaults():
|
||||
assert pin.when_changed == None
|
||||
|
||||
def test_mock_pwm_pin_open_close():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
pin.close()
|
||||
|
||||
def test_mock_pwm_pin_init_twice_same_pin():
|
||||
pin1 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(pin1.number)
|
||||
pin1 = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
pin2 = Device._pin_factory.pin(pin1.number, pin_class=MockPWMPin)
|
||||
assert pin1 is pin2
|
||||
|
||||
def test_mock_pwm_pin_init_twice_different_pin():
|
||||
pin1 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(pin1.number+1)
|
||||
pin1 = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
pin2 = Device._pin_factory.pin(pin1.number + 1, pin_class=MockPWMPin)
|
||||
assert pin1 != pin2
|
||||
assert pin1.number == 2
|
||||
assert pin2.number == pin1.number+1
|
||||
|
||||
def test_mock_pin_init_twice_different_modes():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPWMPin(pin1.number+1)
|
||||
pin1 = Device._pin_factory.pin(2, pin_class=MockPin)
|
||||
pin2 = Device._pin_factory.pin(pin1.number + 1, pin_class=MockPWMPin)
|
||||
assert pin1 != pin2
|
||||
with pytest.raises(ValueError):
|
||||
pin3 = MockPWMPin(pin1.number)
|
||||
Device._pin_factory.pin(pin1.number, pin_class=MockPWMPin)
|
||||
with pytest.raises(ValueError):
|
||||
pin4 = MockPin(pin2.number)
|
||||
Device._pin_factory.pin(pin2.number, pin_class=MockPin)
|
||||
|
||||
def test_mock_pin_frequency_unsupported():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
pin.frequency = None
|
||||
with pytest.raises(PinPWMUnsupported):
|
||||
pin.frequency = 100
|
||||
|
||||
def test_mock_pin_frequency_supported():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
pin.function = 'output'
|
||||
assert pin.frequency is None
|
||||
pin.frequency = 100
|
||||
@@ -112,7 +104,7 @@ def test_mock_pin_frequency_supported():
|
||||
assert not pin.state
|
||||
|
||||
def test_mock_pin_pull():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
pin.function = 'input'
|
||||
assert pin.pull == 'floating'
|
||||
pin.pull = 'up'
|
||||
@@ -121,7 +113,7 @@ def test_mock_pin_pull():
|
||||
assert not pin.state
|
||||
|
||||
def test_mock_pin_state():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with pytest.raises(PinSetInput):
|
||||
pin.state = 1
|
||||
pin.function = 'output'
|
||||
@@ -134,7 +126,7 @@ def test_mock_pin_state():
|
||||
assert pin.state == 1
|
||||
|
||||
def test_mock_pwm_pin_state():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with pytest.raises(PinSetInput):
|
||||
pin.state = 1
|
||||
pin.function = 'output'
|
||||
@@ -147,7 +139,7 @@ def test_mock_pwm_pin_state():
|
||||
assert pin.state == 0.5
|
||||
|
||||
def test_mock_pin_edges():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
assert pin.when_changed is None
|
||||
fired = Event()
|
||||
pin.function = 'input'
|
||||
|
||||
Reference in New Issue
Block a user