forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
149 lines (129 loc) · 3.03 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package mp4
import (
"bytes"
"flag"
"os"
"testing"
"github.com/Eyevinn/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")
)
// cmpAfterDecodeEncodeBox compares bytes after a box has been decoded and encoded.
func cmpAfterDecodeEncodeBox(t *testing.T, data []byte) {
t.Helper()
// First SliceReader + SliceWriter
sr := bits.NewFixedSliceReader(data)
box, err := DecodeBoxSR(0, sr)
if err != nil {
t.Error(err)
}
sw := bits.NewFixedSliceWriter(int(box.Size()))
err = box.EncodeSW(sw)
if err != nil {
t.Error(err)
}
if !bytes.Equal(data, sw.Bytes()) {
t.Error("non-matching DecodeBoxSR/EncodeSW")
}
bufIn := bytes.NewBuffer(data)
box, err = DecodeBox(0, bufIn)
if err != nil {
t.Error(err)
}
bufOut := bytes.Buffer{}
err = box.Encode(&bufOut)
if err != nil {
t.Error(err)
}
if !bytes.Equal(data, bufOut.Bytes()) {
t.Error("non-matchin DecodeBox/Encode")
}
}
// boxDiffAfterEncodeAndDecode compares a box after encoding and decoding using deep.Equal().
// Further check that Info can be run without an error.
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)
}
// and decode using SliceReader
sr := bits.NewFixedSliceReader(midBuf.Bytes())
boxDec, err = DecodeBoxSR(0, sr)
if err != nil {
t.Error(err)
}
if diff := deep.Equal(boxDec, box); diff != nil {
t.Error(diff)
}
// Finally check that the Info method does not render an error
infoBuf := bytes.Buffer{}
err = box.Info(&infoBuf, "all:1", "", " ")
if err != nil {
t.Error(err)
}
}
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.Error(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())
}