Skip to content

Commit 0eda7e3

Browse files
Paolo Calaopolldo
Paolo Calao
authored andcommitted
Add unit tests (#13)
1 parent 802f03a commit 0eda7e3

File tree

5 files changed

+380
-0
lines changed

5 files changed

+380
-0
lines changed

Diff for: command/device/create_test.go

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package device
2+
3+
import (
4+
"testing"
5+
6+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
7+
)
8+
9+
// Test variables
10+
var (
11+
portsNoBoards = []*rpc.DetectedPort{
12+
{
13+
Address: "ACM0",
14+
Boards: []*rpc.BoardListItem{},
15+
},
16+
{
17+
Address: "ACM1",
18+
Boards: []*rpc.BoardListItem{},
19+
},
20+
}
21+
22+
portsTwoBoards = []*rpc.DetectedPort{
23+
{
24+
Address: "ACM0",
25+
Boards: []*rpc.BoardListItem{
26+
{Fqbn: "arduino:samd:nano_33_iot"},
27+
},
28+
},
29+
{
30+
Address: "ACM1",
31+
Boards: []*rpc.BoardListItem{
32+
{Fqbn: "arduino:avr:uno"},
33+
},
34+
},
35+
}
36+
)
37+
38+
func TestDeviceFromPorts(t *testing.T) {
39+
tests := []struct {
40+
name string
41+
filter *CreateParams
42+
ports []*rpc.DetectedPort
43+
want *device
44+
}{
45+
46+
{
47+
name: "port-filter",
48+
filter: &CreateParams{Fqbn: "", Port: "ACM1"},
49+
ports: portsTwoBoards,
50+
want: &device{fqbn: "arduino:avr:uno", port: "ACM1"},
51+
},
52+
53+
{
54+
name: "fqbn-filter",
55+
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: ""},
56+
ports: portsTwoBoards,
57+
want: &device{fqbn: "arduino:avr:uno", port: "ACM1"},
58+
},
59+
60+
{
61+
name: "no-filter-noboards",
62+
filter: &CreateParams{Fqbn: "", Port: ""},
63+
ports: portsNoBoards,
64+
want: nil,
65+
},
66+
67+
{
68+
name: "no-filter",
69+
filter: &CreateParams{Fqbn: "", Port: ""},
70+
ports: portsTwoBoards,
71+
// first device found is selected
72+
want: &device{fqbn: "arduino:samd:nano_33_iot", port: "ACM0"},
73+
},
74+
75+
{
76+
name: "both-filter-noboards",
77+
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: "ACM1"},
78+
ports: portsNoBoards,
79+
want: nil,
80+
},
81+
82+
{
83+
name: "both-filter-found",
84+
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: "ACM1"},
85+
ports: portsTwoBoards,
86+
want: &device{fqbn: "arduino:avr:uno", port: "ACM1"},
87+
},
88+
89+
{
90+
name: "both-filter-notfound",
91+
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: "ACM0"},
92+
ports: portsTwoBoards,
93+
want: nil,
94+
},
95+
}
96+
97+
for _, tt := range tests {
98+
t.Run(tt.name, func(t *testing.T) {
99+
got := deviceFromPorts(tt.ports, tt.filter)
100+
101+
if got == nil && tt.want == nil {
102+
return
103+
104+
} else if got != nil && tt.want == nil {
105+
t.Errorf("Expected nil device, received not nil device with port %s and fqbn %s", got.port, got.fqbn)
106+
107+
} else if got == nil && tt.want != nil {
108+
t.Errorf("Expected not nil device with port %s and fqbn %s, received a nil device", tt.want.port, tt.want.fqbn)
109+
110+
} else if got.port != tt.want.port || got.fqbn != tt.want.fqbn {
111+
t.Errorf("Expected device with port %s and fqbn %s, received device with port %s and fqbn %s",
112+
tt.want.port, tt.want.fqbn, got.port, got.fqbn)
113+
}
114+
})
115+
}
116+
}

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/sirupsen/logrus v1.8.1
1414
github.com/spf13/cobra v1.1.3
1515
github.com/spf13/viper v1.7.0
16+
github.com/stretchr/testify v1.6.1
1617
go.bug.st/serial v1.3.0
1718
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125 // indirect
1819
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914

Diff for: go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
489489
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
490490
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
491491
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
492+
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
492493
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
493494
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
494495
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

Diff for: internal/serial/mocks/Port.go

+178
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: internal/serial/serial_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package serial
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/bcmi-labs/iot-cloud-cli/internal/serial/mocks"
8+
"github.com/stretchr/testify/mock"
9+
)
10+
11+
func TestSendReceive(t *testing.T) {
12+
mockPort := &mocks.Port{}
13+
mockSerial := &Serial{mockPort}
14+
15+
want := []byte{1, 2, 3}
16+
resp := encode(Response, want)
17+
respIdx := 0
18+
19+
mockRead := func(msg []uint8) int {
20+
if respIdx >= len(resp) {
21+
return 0
22+
}
23+
copy(msg, resp[respIdx:respIdx+2])
24+
respIdx += 2
25+
return 2
26+
}
27+
28+
mockPort.On("Write", mock.AnythingOfType("[]uint8")).Return(0, nil)
29+
mockPort.On("Read", mock.AnythingOfType("[]uint8")).Return(mockRead, nil)
30+
31+
res, err := mockSerial.SendReceive(BeginStorage, []byte{1, 2})
32+
if err != nil {
33+
t.Error(err)
34+
}
35+
36+
if !bytes.Equal(res, want) {
37+
t.Errorf("Expected %v but received %v", want, res)
38+
}
39+
}
40+
41+
func TestSend(t *testing.T) {
42+
mockPort := &mocks.Port{}
43+
mockSerial := &Serial{mockPort}
44+
mockPort.On("Write", mock.AnythingOfType("[]uint8")).Return(0, nil)
45+
46+
payload := []byte{1, 2}
47+
cmd := SetDay
48+
want := []byte{msgStart[0], msgStart[1], 1, 0, 5, 10, 1, 2, 143, 124, msgEnd[0], msgEnd[1]}
49+
50+
err := mockSerial.Send(cmd, payload)
51+
if err != nil {
52+
t.Error(err)
53+
}
54+
55+
mockPort.AssertCalled(t, "Write", want)
56+
}
57+
58+
func TestEncode(t *testing.T) {
59+
tests := []struct {
60+
name string
61+
msg []byte
62+
want []byte
63+
}{
64+
{
65+
name: "begin-storage",
66+
msg: []byte{byte(BeginStorage)},
67+
want: []byte{msgStart[0], msgStart[1], 1, 0, 3, 6, 0x95, 0x4e, msgEnd[0], msgEnd[1]},
68+
},
69+
70+
{
71+
name: "set-year",
72+
msg: append([]byte{byte(SetYear)}, []byte("2021")...),
73+
want: []byte{msgStart[0], msgStart[1], 1, 0, 7, 0x8, 0x32, 0x30, 0x32, 0x31, 0xc3, 0x65, msgEnd[0], msgEnd[1]},
74+
},
75+
}
76+
for _, tt := range tests {
77+
t.Run(tt.name, func(t *testing.T) {
78+
got := encode(Cmd, tt.msg)
79+
if !bytes.Equal(tt.want, got) {
80+
t.Errorf("Expected %v, received %v", tt.want, got)
81+
}
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)