-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_test.go
67 lines (55 loc) · 1.26 KB
/
file_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
package enu_test
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yuemori/enu"
)
func TestFileReader(t *testing.T) {
t.Parallel()
is := assert.New(t)
f, err := os.CreateTemp(os.TempDir(), "enu-filetest-")
if err != nil {
t.Fatal(err)
}
for _, s := range []string{"foo\n", "bar\n", "baz"} {
if _, err = f.Write([]byte(s)); err != nil {
t.Fatal(err)
}
}
reader := enu.FromFile(f.Name())
r1, ok := reader.First()
is.True(ok)
is.Equal("foo", r1)
is.NoError(reader.Err())
r2 := reader.ToSlice()
is.Equal([]string{"foo", "bar", "baz"}, r2)
is.NoError(reader.Err())
var r3 []string
err = reader.Result(&r3).Err()
is.Equal([]string{"foo", "bar", "baz"}, r3)
is.NoError(err)
err = reader.Each(func(line string, index int) {
switch index {
case 0:
is.Equal("foo", line)
case 1:
is.Equal("bar", line)
case 2:
is.Equal("baz", line)
default:
t.Fatalf("unknown index %d", index)
}
}).Err()
is.NoError(err)
if err := os.Remove(f.Name()); err != nil {
t.Fatal(err)
}
r4 := reader.ToSlice()
is.Equal([]string{}, r4)
is.ErrorContains(reader.Err(), "file already closed")
reader2 := enu.FromFile(f.Name())
_, ok = reader2.First()
is.False(ok)
is.ErrorContains(reader2.Err(), "no such file or directory")
}