gobot.io/x/gobot@v1.16.0/platforms/microbit/io_pin_driver_test.go (about) 1 package microbit 2 3 import ( 4 "errors" 5 "strings" 6 "testing" 7 8 "gobot.io/x/gobot" 9 "gobot.io/x/gobot/drivers/aio" 10 "gobot.io/x/gobot/drivers/gpio" 11 "gobot.io/x/gobot/gobottest" 12 ) 13 14 // the IOPinDriver is a Driver 15 var _ gobot.Driver = (*IOPinDriver)(nil) 16 17 // that supports the DigitalReader, DigitalWriter, & AnalogReader interfaces 18 var _ gpio.DigitalReader = (*IOPinDriver)(nil) 19 var _ gpio.DigitalWriter = (*IOPinDriver)(nil) 20 var _ aio.AnalogReader = (*IOPinDriver)(nil) 21 22 func initTestIOPinDriver() *IOPinDriver { 23 d := NewIOPinDriver(NewBleTestAdaptor()) 24 return d 25 } 26 27 func TestIOPinDriver(t *testing.T) { 28 d := initTestIOPinDriver() 29 gobottest.Assert(t, strings.HasPrefix(d.Name(), "Microbit IO Pin"), true) 30 d.SetName("NewName") 31 gobottest.Assert(t, d.Name(), "NewName") 32 } 33 34 func TestIOPinDriverStartAndHalt(t *testing.T) { 35 a := NewBleTestAdaptor() 36 d := NewIOPinDriver(a) 37 a.TestReadCharacteristic(func(cUUID string) ([]byte, error) { 38 return []byte{0, 1, 1, 0}, nil 39 }) 40 gobottest.Assert(t, d.Start(), nil) 41 gobottest.Assert(t, d.Halt(), nil) 42 } 43 44 func TestIOPinDriverStartError(t *testing.T) { 45 a := NewBleTestAdaptor() 46 d := NewIOPinDriver(a) 47 a.TestReadCharacteristic(func(cUUID string) ([]byte, error) { 48 return nil, errors.New("read error") 49 }) 50 gobottest.Assert(t, d.Start(), errors.New("read error")) 51 } 52 53 func TestIOPinDriverDigitalRead(t *testing.T) { 54 a := NewBleTestAdaptor() 55 d := NewIOPinDriver(a) 56 a.TestReadCharacteristic(func(cUUID string) ([]byte, error) { 57 return []byte{0, 1, 1, 0, 2, 1}, nil 58 }) 59 60 val, _ := d.DigitalRead("0") 61 gobottest.Assert(t, val, 1) 62 63 val, _ = d.DigitalRead("1") 64 gobottest.Assert(t, val, 0) 65 } 66 67 func TestIOPinDriverDigitalReadInvalidPin(t *testing.T) { 68 a := NewBleTestAdaptor() 69 d := NewIOPinDriver(a) 70 71 _, err := d.DigitalRead("A3") 72 gobottest.Refute(t, err, nil) 73 74 _, err = d.DigitalRead("6") 75 gobottest.Assert(t, err, errors.New("Invalid pin.")) 76 } 77 78 func TestIOPinDriverDigitalWrite(t *testing.T) { 79 a := NewBleTestAdaptor() 80 d := NewIOPinDriver(a) 81 82 // TODO: a better test 83 gobottest.Assert(t, d.DigitalWrite("0", 1), nil) 84 } 85 86 func TestIOPinDriverDigitalWriteInvalidPin(t *testing.T) { 87 a := NewBleTestAdaptor() 88 d := NewIOPinDriver(a) 89 90 gobottest.Refute(t, d.DigitalWrite("A3", 1), nil) 91 gobottest.Assert(t, d.DigitalWrite("6", 1), errors.New("Invalid pin.")) 92 } 93 94 func TestIOPinDriverAnalogRead(t *testing.T) { 95 a := NewBleTestAdaptor() 96 d := NewIOPinDriver(a) 97 a.TestReadCharacteristic(func(cUUID string) ([]byte, error) { 98 return []byte{0, 0, 1, 128, 2, 1}, nil 99 }) 100 101 val, _ := d.AnalogRead("0") 102 gobottest.Assert(t, val, 0) 103 104 val, _ = d.AnalogRead("1") 105 gobottest.Assert(t, val, 128) 106 } 107 108 func TestIOPinDriverAnalogReadInvalidPin(t *testing.T) { 109 a := NewBleTestAdaptor() 110 d := NewIOPinDriver(a) 111 112 _, err := d.AnalogRead("A3") 113 gobottest.Refute(t, err, nil) 114 115 _, err = d.AnalogRead("6") 116 gobottest.Assert(t, err, errors.New("Invalid pin.")) 117 } 118 119 func TestIOPinDriverDigitalAnalogRead(t *testing.T) { 120 a := NewBleTestAdaptor() 121 d := NewIOPinDriver(a) 122 a.TestReadCharacteristic(func(cUUID string) ([]byte, error) { 123 return []byte{0, 0, 1, 128, 2, 1}, nil 124 }) 125 126 val, _ := d.DigitalRead("0") 127 gobottest.Assert(t, val, 0) 128 129 val, _ = d.AnalogRead("0") 130 gobottest.Assert(t, val, 0) 131 } 132 133 func TestIOPinDriverDigitalWriteAnalogRead(t *testing.T) { 134 a := NewBleTestAdaptor() 135 d := NewIOPinDriver(a) 136 a.TestReadCharacteristic(func(cUUID string) ([]byte, error) { 137 return []byte{0, 0, 1, 128, 2, 1}, nil 138 }) 139 140 gobottest.Assert(t, d.DigitalWrite("1", 0), nil) 141 142 val, _ := d.AnalogRead("1") 143 gobottest.Assert(t, val, 128) 144 } 145 146 func TestIOPinDriverAnalogReadDigitalWrite(t *testing.T) { 147 a := NewBleTestAdaptor() 148 d := NewIOPinDriver(a) 149 a.TestReadCharacteristic(func(cUUID string) ([]byte, error) { 150 return []byte{0, 0, 1, 128, 2, 1}, nil 151 }) 152 153 val, _ := d.AnalogRead("1") 154 gobottest.Assert(t, val, 128) 155 156 gobottest.Assert(t, d.DigitalWrite("1", 0), nil) 157 }