gobot.io/x/gobot/v2@v2.1.0/platforms/neurosky/neurosky_adaptor_test.go (about) 1 package neurosky 2 3 import ( 4 "errors" 5 "io" 6 "strings" 7 "sync" 8 "testing" 9 10 "gobot.io/x/gobot/v2" 11 "gobot.io/x/gobot/v2/gobottest" 12 ) 13 14 var _ gobot.Adaptor = (*Adaptor)(nil) 15 16 type NullReadWriteCloser struct { 17 mtx sync.Mutex 18 readError error 19 closeError error 20 } 21 22 // func NewNullReadWriteCloser() *NullReadWriteCloser { 23 // return NullReadWriteCloser{ 24 // 25 // } 26 // } 27 28 func (n *NullReadWriteCloser) ReadError(e error) { 29 n.mtx.Lock() 30 defer n.mtx.Unlock() 31 n.readError = e 32 } 33 34 func (n *NullReadWriteCloser) CloseError(e error) { 35 n.mtx.Lock() 36 defer n.mtx.Unlock() 37 n.closeError = e 38 } 39 40 func (n *NullReadWriteCloser) Write(p []byte) (int, error) { 41 return len(p), nil 42 } 43 44 func (n *NullReadWriteCloser) Read(b []byte) (int, error) { 45 n.mtx.Lock() 46 defer n.mtx.Unlock() 47 return len(b), n.readError 48 } 49 50 func (n *NullReadWriteCloser) Close() error { 51 n.mtx.Lock() 52 defer n.mtx.Unlock() 53 return n.closeError 54 } 55 56 func initTestNeuroskyAdaptor() *Adaptor { 57 a := NewAdaptor("/dev/null") 58 a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) { 59 return &NullReadWriteCloser{}, nil 60 } 61 return a 62 } 63 64 func TestNeuroskyAdaptor(t *testing.T) { 65 a := NewAdaptor("/dev/null") 66 gobottest.Assert(t, a.Port(), "/dev/null") 67 } 68 69 func TestNeuroskyAdaptorName(t *testing.T) { 70 a := NewAdaptor("/dev/null") 71 gobottest.Assert(t, strings.HasPrefix(a.Name(), "Neurosky"), true) 72 a.SetName("NewName") 73 gobottest.Assert(t, a.Name(), "NewName") 74 } 75 76 func TestNeuroskyAdaptorConnect(t *testing.T) { 77 a := initTestNeuroskyAdaptor() 78 gobottest.Assert(t, a.Connect(), nil) 79 80 a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) { 81 return nil, errors.New("connection error") 82 } 83 gobottest.Assert(t, a.Connect(), errors.New("connection error")) 84 } 85 86 func TestNeuroskyAdaptorFinalize(t *testing.T) { 87 rwc := &NullReadWriteCloser{} 88 a := NewAdaptor("/dev/null") 89 a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) { 90 return rwc, nil 91 } 92 a.Connect() 93 gobottest.Assert(t, a.Finalize(), nil) 94 95 rwc.CloseError(errors.New("close error")) 96 a.Connect() 97 gobottest.Assert(t, a.Finalize(), errors.New("close error")) 98 }