forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster_test.go
126 lines (102 loc) · 3.35 KB
/
master_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package gobot
import (
"errors"
"log"
"os"
"testing"
"github.com/hybridgroup/gobot/gobottest"
)
func TestConnectionEach(t *testing.T) {
r := newTestRobot("Robot1")
i := 0
r.Connections().Each(func(conn Connection) {
i++
})
gobottest.Assert(t, r.Connections().Len(), i)
}
func initTestMaster() *Master {
log.SetOutput(&NullReadWriteCloser{})
g := NewMaster()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
g.AddRobot(newTestRobot("Robot1"))
g.AddRobot(newTestRobot("Robot2"))
g.AddRobot(newTestRobot(""))
return g
}
func initTestMaster1Robot() *Master {
log.SetOutput(&NullReadWriteCloser{})
g := NewMaster()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
g.AddRobot(newTestRobot("Robot99"))
return g
}
func TestVersion(t *testing.T) {
gobottest.Assert(t, version, Version())
}
func TestNullReadWriteCloser(t *testing.T) {
n := &NullReadWriteCloser{}
i, _ := n.Write([]byte{1, 2, 3})
gobottest.Assert(t, i, 3)
i, _ = n.Read(make([]byte, 10))
gobottest.Assert(t, i, 10)
gobottest.Assert(t, n.Close(), nil)
}
func TestGobotRobot(t *testing.T) {
g := initTestMaster()
gobottest.Assert(t, g.Robot("Robot1").Name, "Robot1")
gobottest.Assert(t, g.Robot("Robot4"), (*Robot)(nil))
gobottest.Assert(t, g.Robot("Robot4").Device("Device1"), (Device)(nil))
gobottest.Assert(t, g.Robot("Robot4").Connection("Connection1"), (Connection)(nil))
gobottest.Assert(t, g.Robot("Robot1").Device("Device4"), (Device)(nil))
gobottest.Assert(t, g.Robot("Robot1").Device("Device1").Name(), "Device1")
gobottest.Assert(t, g.Robot("Robot1").Devices().Len(), 3)
gobottest.Assert(t, g.Robot("Robot1").Connection("Connection4"), (Connection)(nil))
gobottest.Assert(t, g.Robot("Robot1").Connections().Len(), 3)
}
func TestGobotToJSON(t *testing.T) {
g := initTestMaster()
g.AddCommand("test_function", func(params map[string]interface{}) interface{} {
return nil
})
json := NewJSONMaster(g)
gobottest.Assert(t, len(json.Robots), g.Robots().Len())
gobottest.Assert(t, len(json.Commands), len(g.Commands()))
}
func TestMasterStart(t *testing.T) {
g := initTestMaster()
gobottest.Assert(t, g.Start(), nil)
gobottest.Assert(t, g.Stop(), nil)
}
func TestMasterStartDriverErrors(t *testing.T) {
g := initTestMaster1Robot()
testDriverStart = func() (err error) {
return errors.New("driver start error 1")
}
gobottest.Assert(t, g.Start().Error(), "3 error(s) occurred:\n\n* driver start error 1\n* driver start error 1\n* driver start error 1")
gobottest.Assert(t, g.Stop(), nil)
testDriverStart = func() (err error) { return }
}
func TestMasterStartAdaptorErrors(t *testing.T) {
g := initTestMaster1Robot()
testAdaptorConnect = func() (err error) {
return errors.New("adaptor start error 1")
}
gobottest.Assert(t, g.Start().Error(), "3 error(s) occurred:\n\n* adaptor start error 1\n* adaptor start error 1\n* adaptor start error 1")
gobottest.Assert(t, g.Stop(), nil)
testAdaptorConnect = func() (err error) { return }
}
func TestMasterHaltErrors(t *testing.T) {
g := initTestMaster1Robot()
testDriverHalt = func() (err error) {
return errors.New("driver halt error 2")
}
testAdaptorFinalize = func() (err error) {
return errors.New("adaptor finalize error 2")
}
gobottest.Assert(t, g.Start(), nil)
gobottest.Assert(t, g.Stop().Error(), "3 error(s) occurred:\n\n* adaptor finalize error 2\n* adaptor finalize error 2\n* adaptor finalize error 2")
}