forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_mock_test.go
79 lines (60 loc) · 1.77 KB
/
fs_mock_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
package sysfs
import (
"testing"
"gobot.io/x/gobot/gobottest"
)
func TestMockFilesystemOpen(t *testing.T) {
fs := NewMockFilesystem([]string{"foo"})
f1 := fs.Files["foo"]
gobottest.Assert(t, f1.Opened, false)
f2, err := fs.OpenFile("foo", 0, 0666)
gobottest.Assert(t, f1, f2)
gobottest.Assert(t, err, nil)
err = f2.Sync()
gobottest.Assert(t, err, nil)
_, err = fs.OpenFile("bar", 0, 0666)
gobottest.Refute(t, err, nil)
fs.Add("bar")
f4, _ := fs.OpenFile("bar", 0, 0666)
gobottest.Refute(t, f4.Fd(), f1.Fd())
}
func TestMockFilesystemStat(t *testing.T) {
fs := NewMockFilesystem([]string{"foo", "bar/baz"})
fileStat, err := fs.Stat("foo")
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fileStat.IsDir(), false)
dirStat, err := fs.Stat("bar")
gobottest.Assert(t, err, nil)
gobottest.Assert(t, dirStat.IsDir(), true)
_, err = fs.Stat("plonk")
gobottest.Refute(t, err, nil)
}
func TestMockFilesystemWrite(t *testing.T) {
fs := NewMockFilesystem([]string{"bar"})
f1 := fs.Files["bar"]
f2, err := fs.OpenFile("bar", 0, 0666)
gobottest.Assert(t, err, nil)
// Never been read or written.
gobottest.Assert(t, f1.Seq <= 0, true)
f2.WriteString("testing")
// Was written.
gobottest.Assert(t, f1.Seq > 0, true)
gobottest.Assert(t, f1.Contents, "testing")
}
func TestMockFilesystemRead(t *testing.T) {
fs := NewMockFilesystem([]string{"bar"})
f1 := fs.Files["bar"]
f1.Contents = "Yip"
f2, err := fs.OpenFile("bar", 0, 0666)
gobottest.Assert(t, err, nil)
// Never been read or written.
gobottest.Assert(t, f1.Seq <= 0, true)
buffer := make([]byte, 20)
n, _ := f2.Read(buffer)
// Was read.
gobottest.Assert(t, f1.Seq > 0, true)
gobottest.Assert(t, n, 3)
gobottest.Assert(t, string(buffer[:3]), "Yip")
n, _ = f2.ReadAt(buffer, 10)
gobottest.Assert(t, n, 3)
}