forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
digital_pin_test.go
86 lines (65 loc) · 2.07 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
package sysfs
import (
"errors"
"os"
"syscall"
"testing"
"github.com/hybridgroup/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").(*digitalPin)
gobottest.Assert(t, pin.pin, "10")
gobottest.Assert(t, pin.label, "custom")
pin = NewDigitalPin(10).(*digitalPin)
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"))
}