-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboard_test.go
137 lines (118 loc) · 3.62 KB
/
board_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
127
128
129
130
131
132
133
134
135
136
137
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package device
import (
"testing"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
)
// Test variables
var (
portsNoBoards = []*rpc.DetectedPort{
{
Port: &rpc.Port{Address: "ACM0"},
MatchingBoards: []*rpc.BoardListItem{},
},
{
Port: &rpc.Port{Address: "ACM1"},
MatchingBoards: []*rpc.BoardListItem{},
},
}
portsTwoBoards = []*rpc.DetectedPort{
{
Port: &rpc.Port{Address: "ACM0"},
MatchingBoards: []*rpc.BoardListItem{
{Fqbn: "arduino:samd:nano_33_iot"},
},
},
{
Port: &rpc.Port{Address: "ACM1"},
MatchingBoards: []*rpc.BoardListItem{
{Fqbn: "arduino:avr:uno"},
},
},
}
)
func stringPointer(s string) *string {
return &s
}
func TestBoardFromPorts(t *testing.T) {
tests := []struct {
name string
filter *CreateParams
ports []*rpc.DetectedPort
want *board
}{
{
name: "port-filter",
filter: &CreateParams{FQBN: nil, Port: stringPointer("ACM1")},
ports: portsTwoBoards,
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
},
{
name: "fqbn-filter",
filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: nil},
ports: portsTwoBoards,
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
},
{
name: "no-filter-noboards",
filter: &CreateParams{FQBN: nil, Port: nil},
ports: portsNoBoards,
want: nil,
},
{
name: "no-filter",
filter: &CreateParams{FQBN: nil, Port: nil},
ports: portsTwoBoards,
// first board found is selected
want: &board{fqbn: "arduino:samd:nano_33_iot", address: "ACM0"},
},
{
name: "both-filter-noboards",
filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM1")},
ports: portsNoBoards,
want: nil,
},
{
name: "both-filter-found",
filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM1")},
ports: portsTwoBoards,
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
},
{
name: "both-filter-notfound",
filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM0")},
ports: portsTwoBoards,
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := boardFromPorts(tt.ports, tt.filter)
if got == nil && tt.want == nil {
return
} else if got != nil && tt.want == nil {
t.Errorf("Expected nil board, received not nil board with port %s and fqbn %s", got.address, got.fqbn)
} else if got == nil && tt.want != nil {
t.Errorf("Expected not nil board with port %s and fqbn %s, received a nil board", tt.want.address, tt.want.fqbn)
} else if got.address != tt.want.address || got.fqbn != tt.want.fqbn {
t.Errorf("Expected board with port %s and fqbn %s, received board with port %s and fqbn %s",
tt.want.address, tt.want.fqbn, got.address, got.fqbn)
}
})
}
}