-
Notifications
You must be signed in to change notification settings - Fork 190
/
opread_test.go
85 lines (65 loc) · 1.82 KB
/
opread_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
package fsutil_test
import (
"strings"
"testing"
"text/scanner"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/goutil/testutil/fakeobj"
)
func TestDiscardReader(t *testing.T) {
sr := strings.NewReader("hello")
bs, err := fsutil.ReadOrErr(sr)
assert.NoErr(t, err)
assert.Eq(t, []byte("hello"), bs)
sr = strings.NewReader("hello")
assert.Eq(t, []byte("hello"), fsutil.GetContents(sr))
sr = strings.NewReader("hello")
fsutil.DiscardReader(sr)
assert.Empty(t, fsutil.ReadReader(sr))
assert.Empty(t, fsutil.ReadAll(sr))
}
func TestReadReader(t *testing.T) {
fr := fakeobj.NewReader()
assert.Empty(t, fsutil.ReadReader(fr))
fr.ErrOnRead = true
assert.Panics(t, func() {
fsutil.ReadReader(fr)
})
_, err := fsutil.ReadStringOrErr(fr)
assert.Err(t, err)
_, err = fsutil.ReadStringOrErr([]string{"invalid-type"})
assert.Err(t, err)
}
func TestGetContents(t *testing.T) {
fpath := "./testdata/get-contents.txt"
assert.NoErr(t, fsutil.RmFileIfExist(fpath))
_, err := fsutil.PutContents(fpath, "hello")
assert.NoErr(t, err)
assert.Nil(t, fsutil.ReadExistFile("/path-not-exist"))
assert.Eq(t, []byte("hello"), fsutil.ReadExistFile(fpath))
assert.Panics(t, func() {
fsutil.GetContents(45)
})
assert.Panics(t, func() {
fsutil.ReadFile("/path-not-exist")
})
}
func TestTextScanner(t *testing.T) {
r := strings.NewReader("hello\ngolang")
ts := fsutil.TextScanner(r)
assert.Neq(t, scanner.EOF, ts.Scan())
assert.Eq(t, "hello", ts.TokenText())
assert.Panics(t, func() {
fsutil.TextScanner([]string{"invalid-type"})
})
}
func TestLineScanner(t *testing.T) {
r := strings.NewReader("hello\ngolang")
ls := fsutil.LineScanner(r)
assert.True(t, ls.Scan())
assert.Eq(t, "hello", ls.Text())
assert.Panics(t, func() {
fsutil.LineScanner([]string{"invalid-type"})
})
}