forked from albenik/go-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_test.go
131 lines (107 loc) · 2.98 KB
/
serial_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
package serial_test
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/albenik/go-serial/v2"
)
func TestPortNilReciever_Error(t *testing.T) {
checkError := func(err error) {
var portErr *serial.PortError
require.ErrorAs(t, err, &portErr)
assert.Equal(t, serial.PortClosed, portErr.Code())
assert.ErrorIs(t, err, os.ErrInvalid)
}
t.Run("Close", func(t *testing.T) {
checkError((*serial.Port)(nil).Close())
})
t.Run("Reconfigure", func(t *testing.T) {
checkError((*serial.Port)(nil).Reconfigure())
})
t.Run("ReadyToRead", func(t *testing.T) {
_, err := (*serial.Port)(nil).ReadyToRead()
checkError(err)
})
t.Run("Read", func(t *testing.T) {
_, err := (*serial.Port)(nil).Read(make([]byte, 16))
checkError(err)
})
t.Run("Write", func(t *testing.T) {
_, err := (*serial.Port)(nil).Write([]byte{1, 2, 3, 4, 5, 6, 7, 8})
checkError(err)
})
t.Run("ResetInputBuffer", func(t *testing.T) {
checkError((*serial.Port)(nil).ResetInputBuffer())
})
t.Run("ResetOutputBuffer", func(t *testing.T) {
checkError((*serial.Port)(nil).ResetOutputBuffer())
})
t.Run("SetDTR", func(t *testing.T) {
checkError((*serial.Port)(nil).SetDTR(false))
})
t.Run("SetRTS", func(t *testing.T) {
checkError((*serial.Port)(nil).SetRTS(false))
})
t.Run("SetReadTimeout", func(t *testing.T) {
checkError((*serial.Port)(nil).SetReadTimeout(1))
})
t.Run("SetReadTimeoutEx", func(t *testing.T) {
checkError((*serial.Port)(nil).SetReadTimeoutEx(1, 0))
})
t.Run("SetFirstByteReadTimeout", func(t *testing.T) {
checkError((*serial.Port)(nil).SetFirstByteReadTimeout(1))
})
t.Run("SetWriteTimeout", func(t *testing.T) {
checkError((*serial.Port)(nil).SetWriteTimeout(1))
})
t.Run("GetModemStatusBits", func(t *testing.T) {
_, err := (*serial.Port)(nil).GetModemStatusBits()
checkError(err)
})
}
func TestPortTestPortNilReciever_String(t *testing.T) {
var p *serial.Port
assert.Equal(t, "Error: <nil> port instance", p.String())
}
/*
func TestSerial_Operations(t *testing.T) {
ports, err := serial.GetPortsList()
require.NoError(t, err)
if len(ports) == 0 {
t.Log("No serial ports found")
}
for _, name := range ports {
t.Logf("Found port: %q", name)
port, err := serial.Open(name,
serial.WithBaudrate(9600),
serial.WithDataBits(8),
serial.WithParity(serial.NoParity),
serial.WithStopBits(serial.OneStopBit),
serial.WithReadTimeout(1000),
serial.WithWriteTimeout(1000),
serial.WithHUPCL(false),
)
if err == nil {
t.Logf("Port %q opened", name)
if assert.NoError(t, port.Close()) {
t.Logf("Port %q closed without errors", name)
}
continue
}
var portErr serial.PortError
expected := errors.As(err, &portErr) && !in(portErr.Code(), serial.OsError, serial.InvalidSerialPort)
if !expected {
assert.NoError(t, err)
}
}
}
func in(code serial.PortErrorCode, list ...serial.PortErrorCode) bool {
for _, c := range list {
if code == c {
return true
}
}
return false
}
*/