-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvisa_test.go
More file actions
154 lines (150 loc) · 4.18 KB
/
Copy pathvisa_test.go
File metadata and controls
154 lines (150 loc) · 4.18 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
146
147
148
149
150
151
152
153
154
// Copyright (c) 2017-2026 The lxi developers. All rights reserved.
// Project site: https://github.com/gotmc/lxi
// 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 lxi
import (
"errors"
"testing"
)
func TestParsingVisaResourceString(t *testing.T) {
testCases := []struct {
name string
resourceString string
interfaceType string
boardIndex int
hostAddress string
port int
resourceClass string
isError bool
wantErr error
}{
{
name: "valid with board index 0",
resourceString: "TCPIP0::10.12.100.15::5025::SOCKET",
interfaceType: "TCPIP",
boardIndex: 0,
hostAddress: "10.12.100.15",
port: 5025,
resourceClass: "SOCKET",
},
{
name: "valid without board index",
resourceString: "TCPIP::192.168.1.100::5025::SOCKET",
interfaceType: "TCPIP",
boardIndex: 0,
hostAddress: "192.168.1.100",
port: 5025,
resourceClass: "SOCKET",
},
{
name: "valid with board index 1",
resourceString: "TCPIP1::10.0.0.1::7777::SOCKET",
interfaceType: "TCPIP",
boardIndex: 1,
hostAddress: "10.0.0.1",
port: 7777,
resourceClass: "SOCKET",
},
{
name: "valid lowercase input",
resourceString: "tcpip0::10.12.100.15::5025::socket",
interfaceType: "TCPIP",
boardIndex: 0,
hostAddress: "10.12.100.15",
port: 5025,
resourceClass: "SOCKET",
},
{
name: "valid hostname instead of IP",
resourceString: "TCPIP0::MY-INSTRUMENT::5025::SOCKET",
interfaceType: "TCPIP",
boardIndex: 0,
hostAddress: "MY-INSTRUMENT",
port: 5025,
resourceClass: "SOCKET",
},
{
name: "invalid interface type",
resourceString: "GPIB0::10.12.100.15::5025::SOCKET",
isError: true,
wantErr: ErrResourceFormat,
},
{
name: "invalid resource class",
resourceString: "TCPIP0::10.12.100.15::5025::INSTR",
isError: true,
wantErr: ErrResourceFormat,
},
{
name: "empty string",
resourceString: "",
isError: true,
wantErr: ErrResourceFormat,
},
{
name: "missing port and resource class",
resourceString: "TCPIP0::10.12.100.15",
isError: true,
wantErr: ErrResourceFormat,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resource, err := NewVisaResource(tc.resourceString)
if tc.isError {
if err == nil {
t.Fatalf("expected error %v, got nil", tc.wantErr)
}
if tc.wantErr != nil && !errors.Is(err, tc.wantErr) {
t.Errorf("error = %v, want %v", err, tc.wantErr)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resource.InterfaceType() != tc.interfaceType {
t.Errorf(
"InterfaceType() = %q, want %q", resource.InterfaceType(), tc.interfaceType,
)
}
if resource.BoardIndex() != tc.boardIndex {
t.Errorf("BoardIndex() = %d, want %d", resource.BoardIndex(), tc.boardIndex)
}
if resource.HostAddress() != tc.hostAddress {
t.Errorf("HostAddress() = %q, want %q", resource.HostAddress(), tc.hostAddress)
}
if resource.Port() != tc.port {
t.Errorf("Port() = %d, want %d", resource.Port(), tc.port)
}
if resource.ResourceClass() != tc.resourceClass {
t.Errorf(
"ResourceClass() = %q, want %q", resource.ResourceClass(), tc.resourceClass,
)
}
})
}
}
func TestVisaResourceStringRoundTrip(t *testing.T) {
inputs := []string{
"TCPIP0::192.168.1.100::5025::SOCKET",
"TCPIP1::10.0.0.1::7777::SOCKET",
"TCPIP0::MY-INSTRUMENT::5025::SOCKET",
}
for _, input := range inputs {
t.Run(input, func(t *testing.T) {
v, err := NewVisaResource(input)
if err != nil {
t.Fatalf("NewVisaResource(%q) error: %v", input, err)
}
roundTripped, err := NewVisaResource(v.String())
if err != nil {
t.Fatalf("NewVisaResource(%q) round-trip error: %v", v.String(), err)
}
if v.String() != roundTripped.String() {
t.Errorf("round-trip mismatch: %q != %q", v.String(), roundTripped.String())
}
})
}
}