github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/command/device/board_test.go (about) 1 // This file is part of arduino-cloud-cli. 2 // 3 // Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published 7 // by the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 package device 19 20 import ( 21 "testing" 22 23 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" 24 ) 25 26 // Test variables 27 var ( 28 portsNoBoards = []*rpc.DetectedPort{ 29 { 30 Port: &rpc.Port{Address: "ACM0"}, 31 MatchingBoards: []*rpc.BoardListItem{}, 32 }, 33 { 34 Port: &rpc.Port{Address: "ACM1"}, 35 MatchingBoards: []*rpc.BoardListItem{}, 36 }, 37 } 38 39 portsTwoBoards = []*rpc.DetectedPort{ 40 { 41 Port: &rpc.Port{Address: "ACM0"}, 42 MatchingBoards: []*rpc.BoardListItem{ 43 {Fqbn: "arduino:samd:nano_33_iot"}, 44 }, 45 }, 46 { 47 Port: &rpc.Port{Address: "ACM1"}, 48 MatchingBoards: []*rpc.BoardListItem{ 49 {Fqbn: "arduino:avr:uno"}, 50 }, 51 }, 52 } 53 ) 54 55 func stringPointer(s string) *string { 56 return &s 57 } 58 59 func TestBoardFromPorts(t *testing.T) { 60 tests := []struct { 61 name string 62 filter *CreateParams 63 ports []*rpc.DetectedPort 64 want *board 65 }{ 66 67 { 68 name: "port-filter", 69 filter: &CreateParams{FQBN: nil, Port: stringPointer("ACM1")}, 70 ports: portsTwoBoards, 71 want: &board{fqbn: "arduino:avr:uno", address: "ACM1"}, 72 }, 73 74 { 75 name: "fqbn-filter", 76 filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: nil}, 77 ports: portsTwoBoards, 78 want: &board{fqbn: "arduino:avr:uno", address: "ACM1"}, 79 }, 80 81 { 82 name: "no-filter-noboards", 83 filter: &CreateParams{FQBN: nil, Port: nil}, 84 ports: portsNoBoards, 85 want: nil, 86 }, 87 88 { 89 name: "no-filter", 90 filter: &CreateParams{FQBN: nil, Port: nil}, 91 ports: portsTwoBoards, 92 // first board found is selected 93 want: &board{fqbn: "arduino:samd:nano_33_iot", address: "ACM0"}, 94 }, 95 96 { 97 name: "both-filter-noboards", 98 filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM1")}, 99 ports: portsNoBoards, 100 want: nil, 101 }, 102 103 { 104 name: "both-filter-found", 105 filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM1")}, 106 ports: portsTwoBoards, 107 want: &board{fqbn: "arduino:avr:uno", address: "ACM1"}, 108 }, 109 110 { 111 name: "both-filter-notfound", 112 filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM0")}, 113 ports: portsTwoBoards, 114 want: nil, 115 }, 116 } 117 118 for _, tt := range tests { 119 t.Run(tt.name, func(t *testing.T) { 120 got := boardFromPorts(tt.ports, tt.filter) 121 122 if got == nil && tt.want == nil { 123 return 124 125 } else if got != nil && tt.want == nil { 126 t.Errorf("Expected nil board, received not nil board with port %s and fqbn %s", got.address, got.fqbn) 127 128 } else if got == nil && tt.want != nil { 129 t.Errorf("Expected not nil board with port %s and fqbn %s, received a nil board", tt.want.address, tt.want.fqbn) 130 131 } else if got.address != tt.want.address || got.fqbn != tt.want.fqbn { 132 t.Errorf("Expected board with port %s and fqbn %s, received board with port %s and fqbn %s", 133 tt.want.address, tt.want.fqbn, got.address, got.fqbn) 134 } 135 }) 136 } 137 }