-
Notifications
You must be signed in to change notification settings - Fork 42
/
serial_test.go
82 lines (78 loc) · 1.47 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
// SPDX-License-Identifier: MIT
//
// Copyright © 2018 Kent Gibson <[email protected]>.
package serial_test
import (
"errors"
"os"
"syscall"
"testing"
"github.com/stretchr/testify/require"
"github.com/warthog618/modem/serial"
)
func modemExists(name string) func(t *testing.T) {
return func(t *testing.T) {
if _, err := os.Stat(name); os.IsNotExist(err) {
t.Skip("no modem available")
}
}
}
func TestNew(t *testing.T) {
patterns := []struct {
name string
prereq func(t *testing.T)
options []serial.Option
err error
}{
{
"default",
modemExists("/dev/ttyUSB0"),
nil,
nil,
},
{
"empty",
modemExists("/dev/ttyUSB0"),
[]serial.Option{},
nil,
},
{
"baud",
modemExists("/dev/ttyUSB0"),
[]serial.Option{serial.WithBaud(9600)},
nil,
},
{
"port",
modemExists("/dev/ttyUSB0"),
[]serial.Option{serial.WithPort("/dev/ttyUSB0")},
nil,
},
{
"bad port",
nil,
[]serial.Option{serial.WithPort("nosuchmodem")},
&os.PathError{Op: "open", Path: "nosuchmodem", Err: syscall.Errno(2)},
},
{
"bad baud",
modemExists("/dev/ttyUSB0"),
[]serial.Option{serial.WithBaud(1234)},
errors.New("Unrecognized baud rate"),
},
}
for _, p := range patterns {
f := func(t *testing.T) {
if p.prereq != nil {
p.prereq(t)
}
m, err := serial.New(p.options...)
require.Equal(t, p.err, err)
require.Equal(t, err == nil, m != nil)
if m != nil {
m.Close()
}
}
t.Run(p.name, f)
}
}