2014-04-27 17:17:05 -07:00
|
|
|
package neurosky
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2014-12-18 14:42:59 -08:00
|
|
|
"errors"
|
|
|
|
"io"
|
2017-04-06 10:58:34 +02:00
|
|
|
"strings"
|
2017-04-02 22:08:10 +02:00
|
|
|
"sync"
|
2014-06-13 13:40:24 -07:00
|
|
|
"testing"
|
2014-07-22 18:00:54 -07:00
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2016-10-01 17:44:12 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-08-27 11:56:01 +02:00
|
|
|
|
2017-04-02 22:08:10 +02:00
|
|
|
type NullReadWriteCloser struct {
|
|
|
|
mtx sync.Mutex
|
|
|
|
readError error
|
|
|
|
closeError error
|
|
|
|
}
|
2014-12-18 14:07:48 -08:00
|
|
|
|
2017-04-02 22:08:10 +02:00
|
|
|
func (n *NullReadWriteCloser) ReadError(e error) {
|
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
n.readError = e
|
2014-12-18 14:07:48 -08:00
|
|
|
}
|
2014-12-18 14:42:59 -08:00
|
|
|
|
2017-04-02 22:08:10 +02:00
|
|
|
func (n *NullReadWriteCloser) CloseError(e error) {
|
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
n.closeError = e
|
|
|
|
}
|
2014-12-18 14:42:59 -08:00
|
|
|
|
2017-04-02 22:08:10 +02:00
|
|
|
func (n *NullReadWriteCloser) Write(p []byte) (int, error) {
|
|
|
|
return len(p), nil
|
2014-12-18 14:07:48 -08:00
|
|
|
}
|
2014-12-18 14:42:59 -08:00
|
|
|
|
2017-04-02 22:08:10 +02:00
|
|
|
func (n *NullReadWriteCloser) Read(b []byte) (int, error) {
|
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
return len(b), n.readError
|
|
|
|
}
|
2014-12-18 14:42:59 -08:00
|
|
|
|
2017-04-02 22:08:10 +02:00
|
|
|
func (n *NullReadWriteCloser) Close() error {
|
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
return n.closeError
|
2014-12-18 14:07:48 -08:00
|
|
|
}
|
|
|
|
|
2016-10-01 17:44:12 +02:00
|
|
|
func initTestNeuroskyAdaptor() *Adaptor {
|
|
|
|
a := NewAdaptor("/dev/null")
|
|
|
|
a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) {
|
2014-12-18 14:42:59 -08:00
|
|
|
return &NullReadWriteCloser{}, nil
|
2014-07-22 18:00:54 -07:00
|
|
|
}
|
|
|
|
return a
|
2014-06-13 13:40:24 -07:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-12-18 14:42:59 -08:00
|
|
|
func TestNeuroskyAdaptor(t *testing.T) {
|
2016-10-01 17:44:12 +02:00
|
|
|
a := NewAdaptor("/dev/null")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "/dev/null", a.Port())
|
2014-12-18 14:42:59 -08:00
|
|
|
}
|
2016-10-01 17:44:12 +02:00
|
|
|
|
2017-04-06 10:58:34 +02:00
|
|
|
func TestNeuroskyAdaptorName(t *testing.T) {
|
|
|
|
a := NewAdaptor("/dev/null")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, strings.HasPrefix(a.Name(), "Neurosky"))
|
2017-04-06 10:58:34 +02:00
|
|
|
a.SetName("NewName")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "NewName", a.Name())
|
2017-04-06 10:58:34 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestNeuroskyAdaptorConnect(t *testing.T) {
|
|
|
|
a := initTestNeuroskyAdaptor()
|
2023-10-26 20:34:19 +02:00
|
|
|
assert.NoError(t, a.Connect())
|
2014-12-18 14:42:59 -08:00
|
|
|
|
2016-10-01 17:44:12 +02:00
|
|
|
a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) {
|
2014-12-18 14:42:59 -08:00
|
|
|
return nil, errors.New("connection error")
|
|
|
|
}
|
2023-10-25 20:21:18 +02:00
|
|
|
assert.ErrorContains(t, a.Connect(), "connection error")
|
2014-06-13 13:40:24 -07:00
|
|
|
}
|
2014-07-22 18:00:54 -07:00
|
|
|
|
|
|
|
func TestNeuroskyAdaptorFinalize(t *testing.T) {
|
2017-04-02 22:08:10 +02:00
|
|
|
rwc := &NullReadWriteCloser{}
|
|
|
|
a := NewAdaptor("/dev/null")
|
|
|
|
a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) {
|
|
|
|
return rwc, nil
|
|
|
|
}
|
2023-06-12 19:51:25 +02:00
|
|
|
_ = a.Connect()
|
2023-10-26 20:34:19 +02:00
|
|
|
assert.NoError(t, a.Finalize())
|
2014-12-18 14:42:59 -08:00
|
|
|
|
2017-04-02 22:08:10 +02:00
|
|
|
rwc.CloseError(errors.New("close error"))
|
2023-06-12 19:51:25 +02:00
|
|
|
_ = a.Connect()
|
2023-10-25 20:21:18 +02:00
|
|
|
assert.ErrorContains(t, a.Finalize(), "close error")
|
2014-07-22 18:00:54 -07:00
|
|
|
}
|