-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisa_test.go
More file actions
145 lines (140 loc) · 4.17 KB
/
visa_test.go
File metadata and controls
145 lines (140 loc) · 4.17 KB
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
138
139
140
141
142
143
144
145
// Copyright (c) 2017-2026 The asrl developers. All rights reserved.
// Project site: https://github.com/gotmc/asrl
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package asrl
import (
"errors"
"testing"
"go.bug.st/serial"
)
func TestParsingVisaResourceString(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
resourceString string
interfaceType string
address string
baud int
dataBits int
parity serial.Parity
stopBits serial.StopBits
resourceClass string
wantErr error
}{
{
name: "9600 baud 8N2",
resourceString: "ASRL::/dev/tty.usbserial-PX484GRU::9600::8N2::INSTR",
interfaceType: "ASRL",
address: "/dev/tty.usbserial-PX484GRU",
baud: 9600,
dataBits: 8,
parity: serial.NoParity,
stopBits: serial.TwoStopBits,
resourceClass: "INSTR",
},
{
name: "115200 baud 8N1",
resourceString: "ASRL::/dev/tty.usbserial-PX484GRU::115200::8N1::INSTR",
interfaceType: "ASRL",
address: "/dev/tty.usbserial-PX484GRU",
baud: 115200,
dataBits: 8,
parity: serial.NoParity,
stopBits: serial.OneStopBit,
resourceClass: "INSTR",
},
{
name: "115200 baud 7E2",
resourceString: "ASRL::/dev/tty.usbserial-PX484GRU::115200::7E2::INSTR",
interfaceType: "ASRL",
address: "/dev/tty.usbserial-PX484GRU",
baud: 115200,
dataBits: 7,
parity: serial.EvenParity,
stopBits: serial.TwoStopBits,
resourceClass: "INSTR",
},
{
name: "115200 baud 7E1",
resourceString: "ASRL::/dev/tty.usbserial-PX484GRU::115200::7E1::INSTR",
interfaceType: "ASRL",
address: "/dev/tty.usbserial-PX484GRU",
baud: 115200,
dataBits: 7,
parity: serial.EvenParity,
stopBits: serial.OneStopBit,
resourceClass: "INSTR",
},
{
name: "115200 baud 7O1",
resourceString: "ASRL::/dev/tty.usbserial-PX484GRU::115200::7O1::INSTR",
interfaceType: "ASRL",
address: "/dev/tty.usbserial-PX484GRU",
baud: 115200,
dataBits: 7,
parity: serial.OddParity,
stopBits: serial.OneStopBit,
resourceClass: "INSTR",
},
{
name: "completely invalid string",
resourceString: "not-a-visa-string",
wantErr: ErrInvalidResource,
},
{
name: "unsupported dataflow",
resourceString: "ASRL::/dev/tty.usbserial-PX484GRU::9600::9N1::INSTR",
wantErr: ErrUnsupportedDataflow,
},
{
name: "empty string",
resourceString: "",
wantErr: ErrInvalidResource,
},
{
name: "missing INSTR resource class",
resourceString: "ASRL::/dev/tty.usbserial-PX484GRU::9600::8N1::OTHER",
wantErr: ErrInvalidResource,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
resource, err := NewVisaResource(tc.resourceString)
if tc.wantErr != nil {
if err == nil {
t.Fatalf("expected error %v, got nil", tc.wantErr)
}
if !errors.Is(err, tc.wantErr) {
t.Fatalf("err = %v, want %v", err, tc.wantErr)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resource.interfaceType != tc.interfaceType {
t.Errorf("interfaceType = %s, want %s", resource.interfaceType, tc.interfaceType)
}
if resource.address != tc.address {
t.Errorf("address = %s, want %s", resource.address, tc.address)
}
if resource.baud != tc.baud {
t.Errorf("baud = %d, want %d", resource.baud, tc.baud)
}
if resource.dataBits != tc.dataBits {
t.Errorf("dataBits = %d, want %d", resource.dataBits, tc.dataBits)
}
if resource.parity != tc.parity {
t.Errorf("parity = %d, want %d", resource.parity, tc.parity)
}
if resource.stopBits != tc.stopBits {
t.Errorf("stopBits = %d, want %d", resource.stopBits, tc.stopBits)
}
if resource.resourceClass != tc.resourceClass {
t.Errorf("resourceClass = %s, want %s", resource.resourceClass, tc.resourceClass)
}
})
}
}