-
Notifications
You must be signed in to change notification settings - Fork 2
/
writer_test.go
123 lines (108 loc) · 2.34 KB
/
writer_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package pbzip2
import (
"bytes"
"compress/bzip2"
"io/ioutil"
"testing"
)
func TestWriter(t *testing.T) {
var buf bytes.Buffer
writer, err := NewWriter(&buf)
if err != nil {
t.Fatal(err)
}
in := testString(t)
if _, err := writer.Write([]byte(in)); err != nil {
t.Fatal(err)
}
if err := writer.Close(); err != nil {
t.Fatal(err)
}
reader := bzip2.NewReader(&buf)
body, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
out := string(body)
if out != in {
t.Errorf("%q != %q", out, in)
}
}
func TestWriterConfig(t *testing.T) {
var buf bytes.Buffer
conf := &WriterConfig{Level: 6}
writer, err := NewWriterConfig(&buf, conf)
if err != nil {
t.Fatal(err)
}
in := testString(t)
if _, err := writer.Write([]byte(in)); err != nil {
t.Fatal(err)
}
if err := writer.Close(); err != nil {
t.Fatal(err)
}
reader := bzip2.NewReader(&buf)
body, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
out := string(body)
if out != in {
t.Errorf("%q != %q", out, in)
}
}
func TestWriterConfigNil(t *testing.T) {
var buf bytes.Buffer
writer, err := NewWriterConfig(&buf, nil)
if err != nil {
t.Fatal(err)
}
in := testString(t)
if _, err := writer.Write([]byte(in)); err != nil {
t.Fatal(err)
}
if err := writer.Close(); err != nil {
t.Fatal(err)
}
reader := bzip2.NewReader(&buf)
body, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
out := string(body)
if out != in {
t.Errorf("%q != %q", out, in)
}
}
func TestNewDefaultWriterConfig(t *testing.T) {
defaultConf := newDefaultWriterConfig()
if defaultConf.Level != DefaultCompression {
t.Errorf("expected default config level %d, got: %d",
DefaultCompression, defaultConf.Level)
}
}
func TestWriterConfigValidate(t *testing.T) {
// Level of 0 gets set to default
conf1 := &WriterConfig{Level: 0}
err := conf1.validate()
if err != nil {
t.Fatal(err)
}
if conf1.Level != DefaultCompression {
t.Errorf("expected default config level %d, got: %d",
DefaultCompression, conf1.Level)
}
// Level out of lower range
conf2 := &WriterConfig{Level: -1}
err = conf2.validate()
if err == nil {
t.Error("exptected error with writer config Level of -1")
}
// Level out of upper range
conf3 := &WriterConfig{Level: 10}
err = conf3.validate()
if err == nil {
t.Error("exptected error with writer config Level of 10")
}
}