-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer_test.go
105 lines (75 loc) · 1.78 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
package yenc
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"testing"
)
func TestWrite(t *testing.T) {
testWrite := &bytes.Buffer{}
test, err := ioutil.ReadFile("./test/testfile.txt")
if err != nil {
t.Fatalf("Error reading file for test setup")
}
f, err := os.Create("./test/test.out")
if err != nil {
t.Fatalf("Error creating out file")
}
defer f.Close()
mw := io.MultiWriter(f, testWrite)
w := NewWriter(mw)
w.Name = "testfile.txt"
_, err = io.Copy(w, bytes.NewReader(test))
if err != nil {
t.Errorf("Error copying to writer: %v", err)
}
err = w.Close()
if err != nil {
t.Fatalf("Error closing: %v", err)
}
expected, err := ioutil.ReadFile("./test/00000005.nh.ntx")
if err != nil {
t.Fatalf("Error reading encoded file for test comparison: %v", err)
}
ourBytes := stripLines(testWrite.Bytes())
expected = stripLines(expected)
if !bytes.Equal(ourBytes, expected) {
t.Errorf("Wrong encoding produced.")
}
n, err := w.Write([]byte("Foo"))
if err != io.ErrClosedPipe {
t.Errorf("Did not throw closed pipe error.")
}
if n > 0 {
t.Errorf("Wrote greater than 0 after close: %d", n)
}
}
func TestErrorOnWrite(t *testing.T) {
w := NewWriter(&errWriter{})
_, err := w.Write([]byte("Foo"))
if err != nil {
t.Errorf("Error writing (expected error on close only)")
}
err = w.Close()
if err != internalErr {
t.Errorf("Wrong or no error thrown on close(): %v", err)
}
}
type errWriter struct{}
var internalErr = fmt.Errorf("E")
func (ew *errWriter) Write(p []byte) (int, error) {
return 0, internalErr
}
func stripLines(b []byte) []byte {
bs := bytes.Split(b, []byte("\n"))
bs = bs[1:]
bs = bs[:len(bs)-2]
return bytes.Join(bs, []byte("\n"))
}
func TestIsWriter(t *testing.T) {
takesWriter(NewWriter(nil))
}
func takesWriter(w io.Writer) {
}