-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlocal_control_test.go
More file actions
155 lines (124 loc) · 3.81 KB
/
local_control_test.go
File metadata and controls
155 lines (124 loc) · 3.81 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
155
// Copyright (c) 2017-2026 The ivi developers. All rights reserved.
// Project site: https://github.com/gotmc/ivi
// 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 ivi
import (
"context"
"testing"
)
// mockLocalControlInst simulates an instrument for local control testing.
type mockLocalControlInst struct {
commandsSent []string
shouldError bool
}
func (m *mockLocalControlInst) ReadBinary(_ context.Context, p []byte) (int, error) {
return 0, nil
}
func (m *mockLocalControlInst) WriteBinary(_ context.Context, p []byte) (int, error) {
return len(p), nil
}
func (m *mockLocalControlInst) Command(_ context.Context, format string, a ...any) error {
m.commandsSent = append(m.commandsSent, format)
if m.shouldError {
return ErrUnexpectedResponse
}
return nil
}
func (m *mockLocalControlInst) Query(_ context.Context, s string) (value string, err error) {
return "", nil
}
func TestInherent_Disable(t *testing.T) {
mock := &mockLocalControlInst{}
inherent := NewInherent(mock, InherentBase{ReturnToLocal: true}, 0)
err := inherent.Disable()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(mock.commandsSent) != 1 {
t.Errorf("Expected 1 command, got %d", len(mock.commandsSent))
}
if mock.commandsSent[0] != "SYST:LOC" {
t.Errorf("Expected SYST:LOC command, got %s", mock.commandsSent[0])
}
}
func TestInherent_Disable_CustomCommand(t *testing.T) {
mock := &mockLocalControlInst{}
inherent := NewInherent(mock, InherentBase{
ReturnToLocal: true,
LocalControlCommand: "SYST:COMM:RLST LOC",
}, 0)
err := inherent.Disable()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(mock.commandsSent) != 1 {
t.Fatalf("Expected 1 command, got %d", len(mock.commandsSent))
}
if mock.commandsSent[0] != "SYST:COMM:RLST LOC" {
t.Errorf(
"Expected SYST:COMM:RLST LOC command, got %s",
mock.commandsSent[0],
)
}
}
func TestInherent_Disable_ErrorIgnored(t *testing.T) {
mock := &mockLocalControlInst{shouldError: true}
inherent := NewInherent(mock, InherentBase{ReturnToLocal: true}, 0)
err := inherent.Disable()
if err != nil {
t.Errorf("Expected no error (best-effort), got %v", err)
}
if len(mock.commandsSent) != 1 {
t.Errorf("Expected 1 command attempt, got %d", len(mock.commandsSent))
}
}
func TestInherent_Close(t *testing.T) {
mock := &mockLocalControlInst{}
inherent := NewInherent(mock, InherentBase{ReturnToLocal: true}, 0)
err := inherent.Close()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// Close should send the local control command but not close the transport.
if len(mock.commandsSent) != 1 {
t.Errorf("Expected 1 command, got %d", len(mock.commandsSent))
}
if mock.commandsSent[0] != "SYST:LOC" {
t.Errorf("Expected SYST:LOC command, got %s", mock.commandsSent[0])
}
}
func TestInherent_ReturnToLocal_Control(t *testing.T) {
mock := &mockLocalControlInst{}
inherent := NewInherent(mock, InherentBase{ReturnToLocal: true}, 0)
if !inherent.IsReturnToLocal() {
t.Error("Expected ReturnToLocal to be true")
}
inherent.SetReturnToLocal(false)
if inherent.IsReturnToLocal() {
t.Error("Expected ReturnToLocal to be false after setting")
}
// Disable should not send commands when ReturnToLocal is false.
err := inherent.Disable()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(mock.commandsSent) != 0 {
t.Errorf(
"Expected no commands when ReturnToLocal is false, got %d",
len(mock.commandsSent),
)
}
// Re-enable and verify it works.
inherent.SetReturnToLocal(true)
err = inherent.Disable()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(mock.commandsSent) != 1 {
t.Errorf(
"Expected 1 command when ReturnToLocal is true, got %d",
len(mock.commandsSent),
)
}
}