Skip to content

Add unit tests #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions command/device/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package device

import (
"testing"

rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
)

// Test variables
var (
portsNoBoards = []*rpc.DetectedPort{
{
Address: "ACM0",
Boards: []*rpc.BoardListItem{},
},
{
Address: "ACM1",
Boards: []*rpc.BoardListItem{},
},
}

portsTwoBoards = []*rpc.DetectedPort{
{
Address: "ACM0",
Boards: []*rpc.BoardListItem{
{Fqbn: "arduino:samd:nano_33_iot"},
},
},
{
Address: "ACM1",
Boards: []*rpc.BoardListItem{
{Fqbn: "arduino:avr:uno"},
},
},
}
)

func TestDeviceFromPorts(t *testing.T) {
tests := []struct {
name string
filter *CreateParams
ports []*rpc.DetectedPort
want *device
}{

{
name: "port-filter",
filter: &CreateParams{Fqbn: "", Port: "ACM1"},
ports: portsTwoBoards,
want: &device{fqbn: "arduino:avr:uno", port: "ACM1"},
},

{
name: "fqbn-filter",
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: ""},
ports: portsTwoBoards,
want: &device{fqbn: "arduino:avr:uno", port: "ACM1"},
},

{
name: "no-filter-noboards",
filter: &CreateParams{Fqbn: "", Port: ""},
ports: portsNoBoards,
want: nil,
},

{
name: "no-filter",
filter: &CreateParams{Fqbn: "", Port: ""},
ports: portsTwoBoards,
// first device found is selected
want: &device{fqbn: "arduino:samd:nano_33_iot", port: "ACM0"},
},

{
name: "both-filter-noboards",
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: "ACM1"},
ports: portsNoBoards,
want: nil,
},

{
name: "both-filter-found",
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: "ACM1"},
ports: portsTwoBoards,
want: &device{fqbn: "arduino:avr:uno", port: "ACM1"},
},

{
name: "both-filter-notfound",
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: "ACM0"},
ports: portsTwoBoards,
want: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := deviceFromPorts(tt.ports, tt.filter)

if got == nil && tt.want == nil {
return

} else if got != nil && tt.want == nil {
t.Errorf("Expected nil device, received not nil device with port %s and fqbn %s", got.port, got.fqbn)

} else if got == nil && tt.want != nil {
t.Errorf("Expected not nil device with port %s and fqbn %s, received a nil device", tt.want.port, tt.want.fqbn)

} else if got.port != tt.want.port || got.fqbn != tt.want.fqbn {
t.Errorf("Expected device with port %s and fqbn %s, received device with port %s and fqbn %s",
tt.want.port, tt.want.fqbn, got.port, got.fqbn)
}
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.0
github.com/stretchr/testify v1.6.1
go.bug.st/serial v1.3.0
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125 // indirect
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
178 changes: 178 additions & 0 deletions internal/serial/mocks/Port.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions internal/serial/serial_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package serial

import (
"bytes"
"testing"

"github.com/bcmi-labs/iot-cloud-cli/internal/serial/mocks"
"github.com/stretchr/testify/mock"
)

func TestSendReceive(t *testing.T) {
mockPort := &mocks.Port{}
mockSerial := &Serial{mockPort}

want := []byte{1, 2, 3}
resp := encode(Response, want)
respIdx := 0

mockRead := func(msg []uint8) int {
if respIdx >= len(resp) {
return 0
}
copy(msg, resp[respIdx:respIdx+2])
respIdx += 2
return 2
}

mockPort.On("Write", mock.AnythingOfType("[]uint8")).Return(0, nil)
mockPort.On("Read", mock.AnythingOfType("[]uint8")).Return(mockRead, nil)

res, err := mockSerial.SendReceive(BeginStorage, []byte{1, 2})
if err != nil {
t.Error(err)
}

if !bytes.Equal(res, want) {
t.Errorf("Expected %v but received %v", want, res)
}
}

func TestSend(t *testing.T) {
mockPort := &mocks.Port{}
mockSerial := &Serial{mockPort}
mockPort.On("Write", mock.AnythingOfType("[]uint8")).Return(0, nil)

payload := []byte{1, 2}
cmd := SetDay
want := []byte{msgStart[0], msgStart[1], 1, 0, 5, 10, 1, 2, 143, 124, msgEnd[0], msgEnd[1]}

err := mockSerial.Send(cmd, payload)
if err != nil {
t.Error(err)
}

mockPort.AssertCalled(t, "Write", want)
}

func TestEncode(t *testing.T) {
tests := []struct {
name string
msg []byte
want []byte
}{
{
name: "begin-storage",
msg: []byte{byte(BeginStorage)},
want: []byte{msgStart[0], msgStart[1], 1, 0, 3, 6, 0x95, 0x4e, msgEnd[0], msgEnd[1]},
},

{
name: "set-year",
msg: append([]byte{byte(SetYear)}, []byte("2021")...),
want: []byte{msgStart[0], msgStart[1], 1, 0, 7, 0x8, 0x32, 0x30, 0x32, 0x31, 0xc3, 0x65, msgEnd[0], msgEnd[1]},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := encode(Cmd, tt.msg)
if !bytes.Equal(tt.want, got) {
t.Errorf("Expected %v, received %v", tt.want, got)
}
})
}
}