-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate_test.go
116 lines (99 loc) · 2.55 KB
/
create_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
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)
}
})
}
}