gobot.io/x/gobot/v2@v2.1.0/platforms/microbit/helpers_test.go (about) 1 package microbit 2 3 import ( 4 "sync" 5 6 "gobot.io/x/gobot/v2/platforms/ble" 7 ) 8 9 var _ ble.BLEConnector = (*bleTestClientAdaptor)(nil) 10 11 type bleTestClientAdaptor struct { 12 name string 13 address string 14 mtx sync.Mutex 15 withoutResponses bool 16 17 testSubscribe func([]byte, error) 18 testReadCharacteristic func(string) ([]byte, error) 19 testWriteCharacteristic func(string, []byte) error 20 } 21 22 func (t *bleTestClientAdaptor) Connect() (err error) { return } 23 func (t *bleTestClientAdaptor) Reconnect() (err error) { return } 24 func (t *bleTestClientAdaptor) Disconnect() (err error) { return } 25 func (t *bleTestClientAdaptor) Finalize() (err error) { return } 26 func (t *bleTestClientAdaptor) Name() string { return t.name } 27 func (t *bleTestClientAdaptor) SetName(n string) { t.name = n } 28 func (t *bleTestClientAdaptor) Address() string { return t.address } 29 func (t *bleTestClientAdaptor) WithoutResponses(use bool) { t.withoutResponses = use } 30 31 func (t *bleTestClientAdaptor) ReadCharacteristic(cUUID string) (data []byte, err error) { 32 t.mtx.Lock() 33 defer t.mtx.Unlock() 34 return t.testReadCharacteristic(cUUID) 35 } 36 37 func (t *bleTestClientAdaptor) WriteCharacteristic(cUUID string, data []byte) (err error) { 38 t.mtx.Lock() 39 defer t.mtx.Unlock() 40 return t.testWriteCharacteristic(cUUID, data) 41 } 42 43 //nolint:revive // in tests it is be helpful to see the meaning of the parameters by name 44 func (t *bleTestClientAdaptor) Subscribe(cUUID string, f func([]byte, error)) (err error) { 45 t.testSubscribe = f 46 return 47 } 48 49 func (t *bleTestClientAdaptor) TestReadCharacteristic(f func(cUUID string) (data []byte, err error)) { 50 t.mtx.Lock() 51 defer t.mtx.Unlock() 52 t.testReadCharacteristic = f 53 } 54 55 func (t *bleTestClientAdaptor) TestWriteCharacteristic(f func(cUUID string, data []byte) (err error)) { 56 t.mtx.Lock() 57 defer t.mtx.Unlock() 58 t.testWriteCharacteristic = f 59 } 60 61 func (t *bleTestClientAdaptor) TestReceiveNotification(data []byte, err error) { 62 t.mtx.Lock() 63 defer t.mtx.Unlock() 64 t.testSubscribe(data, err) 65 } 66 67 func NewBleTestAdaptor() *bleTestClientAdaptor { 68 return &bleTestClientAdaptor{ 69 address: "01:02:03:04:05:06", 70 testReadCharacteristic: func(cUUID string) (data []byte, e error) { 71 return 72 }, 73 testWriteCharacteristic: func(cUUID string, data []byte) (e error) { 74 return 75 }, 76 testSubscribe: func([]byte, error) {}, 77 } 78 }