forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphero_adaptor_test.go
104 lines (85 loc) · 2.33 KB
/
sphero_adaptor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package sphero
import (
"errors"
"io"
"strings"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Adaptor = (*Adaptor)(nil)
type nullReadWriteCloser struct {
testAdaptorRead func(p []byte) (int, error)
testAdaptorWrite func(b []byte) (int, error)
testAdaptorClose func() error
}
func (n *nullReadWriteCloser) Write(p []byte) (int, error) {
return n.testAdaptorWrite(p)
}
func (n *nullReadWriteCloser) Read(b []byte) (int, error) {
return n.testAdaptorRead(b)
}
func (n *nullReadWriteCloser) Close() error {
return n.testAdaptorClose()
}
func NewNullReadWriteCloser() *nullReadWriteCloser {
return &nullReadWriteCloser{
testAdaptorRead: func(p []byte) (int, error) {
return len(p), nil
},
testAdaptorWrite: func(b []byte) (int, error) {
return len(b), nil
},
testAdaptorClose: func() error {
return nil
},
}
}
func initTestSpheroAdaptor() (*Adaptor, *nullReadWriteCloser) {
a := NewAdaptor("/dev/null")
rwc := NewNullReadWriteCloser()
a.connect = func(string) (io.ReadWriteCloser, error) {
return rwc, nil
}
return a, rwc
}
func TestSpheroAdaptorName(t *testing.T) {
a, _ := initTestSpheroAdaptor()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Sphero"), true)
a.SetName("NewName")
gobottest.Assert(t, a.Name(), "NewName")
}
func TestSpheroAdaptor(t *testing.T) {
a, _ := initTestSpheroAdaptor()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Sphero"), true)
gobottest.Assert(t, a.Port(), "/dev/null")
}
func TestSpheroAdaptorReconnect(t *testing.T) {
a, _ := initTestSpheroAdaptor()
a.Connect()
gobottest.Assert(t, a.connected, true)
a.Reconnect()
gobottest.Assert(t, a.connected, true)
a.Disconnect()
gobottest.Assert(t, a.connected, false)
a.Reconnect()
gobottest.Assert(t, a.connected, true)
}
func TestSpheroAdaptorFinalize(t *testing.T) {
a, rwc := initTestSpheroAdaptor()
a.Connect()
gobottest.Assert(t, a.Finalize(), nil)
rwc.testAdaptorClose = func() error {
return errors.New("close error")
}
a.connected = true
gobottest.Assert(t, a.Finalize(), errors.New("close error"))
}
func TestSpheroAdaptorConnect(t *testing.T) {
a, _ := initTestSpheroAdaptor()
gobottest.Assert(t, a.Connect(), nil)
a.connect = func(string) (io.ReadWriteCloser, error) {
return nil, errors.New("connect error")
}
gobottest.Assert(t, a.Connect(), errors.New("connect error"))
}