forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
digital_pin_test.go
124 lines (95 loc) · 2.86 KB
/
digital_pin_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
package sysfs
import (
"errors"
"os"
"syscall"
"testing"
"gobot.io/x/gobot/gobottest"
)
func TestDigitalPin(t *testing.T) {
fs := NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio10/value",
"/sys/class/gpio/gpio10/direction",
})
SetFilesystem(fs)
pin := NewDigitalPin(10, "custom")
gobottest.Assert(t, pin.pin, "10")
gobottest.Assert(t, pin.label, "custom")
pin = NewDigitalPin(10)
gobottest.Assert(t, pin.pin, "10")
gobottest.Assert(t, pin.label, "gpio10")
gobottest.Assert(t, pin.value, nil)
err := pin.Unexport()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/gpio/unexport"].Contents, "10")
err = pin.Export()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/gpio/export"].Contents, "10")
gobottest.Refute(t, pin.value, nil)
err = pin.Write(1)
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio10/value"].Contents, "1")
err = pin.Direction(IN)
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio10/direction"].Contents, "in")
data, _ := pin.Read()
gobottest.Assert(t, 1, data)
pin2 := NewDigitalPin(30, "custom")
err = pin2.Write(1)
gobottest.Refute(t, err, nil)
data, err = pin2.Read()
gobottest.Refute(t, err, nil)
gobottest.Assert(t, data, 0)
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EINVAL}
}
err = pin.Unexport()
gobottest.Assert(t, err, nil)
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: errors.New("write error")}
}
err = pin.Unexport()
gobottest.Assert(t, err.(*os.PathError).Err, errors.New("write error"))
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}
err = pin.Export()
gobottest.Assert(t, err, nil)
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: errors.New("write error")}
}
err = pin.Export()
gobottest.Assert(t, err.(*os.PathError).Err, errors.New("write error"))
}
func TestDigitalPinExportError(t *testing.T) {
fs := NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio10/value",
"/sys/class/gpio/gpio10/direction",
})
SetFilesystem(fs)
pin := NewDigitalPin(10, "custom")
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}
err := pin.Export()
gobottest.Refute(t, err, nil)
}
func TestDigitalPinUnexportError(t *testing.T) {
fs := NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio10/value",
"/sys/class/gpio/gpio10/direction",
})
SetFilesystem(fs)
pin := NewDigitalPin(10, "custom")
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}
err := pin.Unexport()
gobottest.Refute(t, err, nil)
}