-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_test.go
86 lines (75 loc) · 1.84 KB
/
reader_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 kaito
import (
"bufio"
"io"
"os"
"testing"
)
func TestGzip(t *testing.T) {
testOneToTen(t, "test/one-ten.txt.gz")
}
func TestXz(t *testing.T) {
testOneToTen(t, "test/one-ten.txt.gz")
}
func TestBzip2Disabled(t *testing.T) {
file, err := os.Open("test/one-ten.txt.bz2")
if err != nil {
t.Fatal(err)
}
rd := NewWithOptions(file, DisableBzip2)
buf := make([]byte, 6)
n, err := rd.Read(buf)
if err != nil {
t.Fatal(err)
}
if n < 3 {
t.Fatalf("Expected at least 3 bytes, but actually only %d bytes are read", n)
}
if buf[0] != 'B' || buf[1] != 'Z' || buf[2] != 'h' {
t.Fatalf(`Expected "BZh" as magic number of Bzip2, but actually got %s`, string(buf))
}
}
func TestBzip2(t *testing.T) {
testOneToTen(t, "test/one-ten.txt.bz2")
}
func TestGzipNative(t *testing.T) {
testOneToTenWithOpts(t, "test/one-ten.txt.gz", ForceNative)
}
func TestXzNative(t *testing.T) {
testOneToTenWithOpts(t, "test/one-ten.txt.xz", ForceNative)
}
func TestBzip2Native(t *testing.T) {
testOneToTenWithOpts(t, "test/one-ten.txt.bz2", ForceNative)
}
func testOneToTen(t *testing.T, name string) {
file, err := os.Open(name)
if err != nil {
t.Fatal(err)
}
rd := New(file)
testOneToTenAux(t, rd)
}
func testOneToTenWithOpts(t *testing.T, name string, opts Options) {
file, err := os.Open(name)
if err != nil {
t.Fatal(err)
}
rd := NewWithOptions(file, opts)
testOneToTenAux(t, rd)
}
func testOneToTenAux(t *testing.T, rd io.Reader) {
brd := bufio.NewReader(rd)
for _, num := range []string{"One\n", "Two\n", "Three\n", "Four\n", "Five\n", "Six\n", "Seven\n", "Eight\n", "Nine\n", "Ten\n"} {
line, err := brd.ReadString('\n')
if err != nil {
t.Fatal(err)
}
if line != num {
t.Fatalf("Expected %v, but actually %v", num, line)
}
}
_, err := brd.ReadString('\n')
if err != io.EOF {
t.Fatalf("Expected EOF, but actually %v", err)
}
}