forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
104 lines (88 loc) · 1.89 KB
/
helpers_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 mp4
import (
"bytes"
"flag"
"os"
"testing"
"github.com/edgeware/mp4ff/bits"
"github.com/go-test/deep"
)
// Helpers to tests. By including t.Helper(), the right failing line in the test
// itself is reported.
var (
update = flag.Bool("update", false, "update the golden files of this test")
)
func boxDiffAfterEncodeAndDecode(t *testing.T, box Box) {
t.Helper()
// First do encode in a slice via SliceWriter
size := box.Size()
sw := bits.NewFixedSliceWriter(int(size))
err := box.EncodeSW(sw)
if err != nil {
t.Error(err)
}
buf := bytes.NewBuffer(sw.Bytes())
boxDec, err := DecodeBox(0, buf)
if err != nil {
t.Error(err)
}
if diff := deep.Equal(boxDec, box); diff != nil {
t.Error(diff)
}
// Then do encode using io.Writer
midBuf := bytes.Buffer{}
err = box.Encode(&midBuf)
if err != nil {
t.Error(err)
}
boxDec, err = DecodeBox(0, &midBuf)
if err != nil {
t.Error(err)
}
if diff := deep.Equal(boxDec, box); diff != nil {
t.Error(diff)
}
}
func boxAfterEncodeAndDecode(t *testing.T, box Box) Box {
t.Helper()
buf := bytes.Buffer{}
err := box.Encode(&buf)
if err != nil {
t.Error(err)
}
boxDec, err := DecodeBox(0, &buf)
if err != nil {
t.Error(err)
}
return boxDec
}
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("Got error %s but expected none", err)
}
}
func assertError(t *testing.T, err error, msg string) {
t.Helper()
if err == nil {
t.Errorf(msg)
}
}
// writeGolden - write golden file that to be used for later tests
func writeGolden(t *testing.T, goldenAssetPath string, data []byte) error {
t.Helper()
fd, err := os.Create(goldenAssetPath)
if err != nil {
return err
}
_, err = fd.Write(data)
if err != nil {
return err
}
return nil
}
// TestMain is to set flags for tests. In particular, the update flag to update golden files.
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(m.Run())
}