-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding_test.go
104 lines (86 loc) · 2.73 KB
/
encoding_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
package embdr_test
import (
"bytes"
"io/ioutil"
"testing"
"github.com/heppu/embdr"
)
func TestEncodeFiles(t *testing.T) {
filenames := []string{"testdata/valid/some_text.tmpl", "testdata/valid/some_html.tmpl"}
files, err := embdr.EncodeFiles(filenames...)
noErrorf(t, err, "encoding failed")
for _, name := range filenames {
content, err := ioutil.ReadFile(name)
noErrorf(t, err, "reading file %s failed", name)
parsed, ok := files[name]
isTruef(t, ok, "map should contain %s", name)
decoded, err := embdr.Decode(parsed)
noErrorf(t, err, "decoding %s failed", name)
isTruef(t, string(decoded) == string(content), "content of file %s doesn't match stored data", name)
}
isTruef(t, len(files) == len(filenames), "map should have %d entries but it has %d", len(filenames), len(files))
}
func TestEncodeNonExistingFile(t *testing.T) {
_, err := embdr.EncodeFiles("testdata/exist/do/i/not")
isTruef(t, err != nil, "Encoding non existing file should have failed")
}
func TestEncodeNonUTF8File(t *testing.T) {
_, err := embdr.EncodeFiles("testdata/invalid/data.bin")
isTruef(t, err != nil, "Encoding non utf8 data should have failed")
}
func TestNLWriter_Write(t *testing.T) {
tests := []struct {
name string
input string
expectedOutput string
limit int
}{
{
name: "underLimit",
input: "somedata",
expectedOutput: "somedata",
limit: 9,
}, {
name: "overLimitOnce",
input: "somedata",
expectedOutput: "somedat\na",
limit: 7,
}, {
name: "overLimitTwice",
input: "somedata",
expectedOutput: "som\neda\nta",
limit: 3,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
w := embdr.NewNLWriter(buf, tt.limit)
_, err := w.Write([]byte(tt.input))
noErrorf(t, err, "writing failed")
isTruef(t, buf.String() == tt.expectedOutput, "input doesn't match expexted output\ninput:\n'%s'\noutput:\n'%s'", tt.input, buf.String())
})
}
}
func TestNLWriter_WriteMultipleTimes(t *testing.T) {
expectedOutput := "he\nll\not\nhe\nre"
buf := &bytes.Buffer{}
w := embdr.NewNLWriter(buf, 2)
_, err := w.Write([]byte("hello"))
noErrorf(t, err, "writing failed")
_, err = w.Write([]byte("there"))
noErrorf(t, err, "writing failed")
isTruef(t, buf.String() == expectedOutput, "input doesn't match expexted output\ninput:\n'%s'\noutput:\n'%s'", expectedOutput, buf.String())
}
func noErrorf(t testing.TB, err error, format string, args ...interface{}) {
if err != nil {
t.Logf(format, args...)
t.Fatal("Error:", err)
}
}
func isTruef(t testing.TB, b bool, format string, args ...interface{}) {
if !b {
t.Fatalf(format, args...)
}
}