-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathchapter_frame_test.go
169 lines (160 loc) · 4.06 KB
/
chapter_frame_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package id3v2
import (
"io"
"io/ioutil"
"log"
"os"
"testing"
"time"
)
func prepareTestFile() (*os.File, error) {
src, err := os.Open("./testdata/test.mp3")
if err != nil {
return nil, err
}
defer src.Close()
tmpFile, err := ioutil.TempFile("", "chapter_test")
if err != nil {
return nil, err
}
_, err = io.Copy(tmpFile, src)
if err != nil {
return nil, err
}
return tmpFile, nil
}
func TestAddChapterFrame(t *testing.T) {
type fields struct {
ElementID string
StartTime time.Duration
EndTime time.Duration
StartOffset uint32
EndOffset uint32
Title *TextFrame
Description *TextFrame
}
tests := []struct {
name string
fields fields
}{
{
name: "element id only",
fields: fields{
ElementID: "chap0",
StartTime: 0,
EndTime: time.Duration(1000 * nanosInMillis),
StartOffset: 0,
EndOffset: 0,
},
},
{
name: "with title",
fields: fields{
ElementID: "chap0",
StartTime: 0,
EndTime: time.Duration(1000 * nanosInMillis),
StartOffset: 0,
EndOffset: 0,
Title: &TextFrame{
Encoding: EncodingUTF8,
Text: "chapter 0",
},
},
},
{
name: "with description",
fields: fields{
ElementID: "chap0",
StartTime: 0,
EndTime: time.Duration(1000 * nanosInMillis),
StartOffset: 0,
EndOffset: 0,
Description: &TextFrame{
Encoding: EncodingUTF8,
Text: "chapter 0",
},
},
},
{
name: "with title and description",
fields: fields{
ElementID: "chap0",
StartTime: 0,
EndTime: time.Duration(1000 * nanosInMillis),
StartOffset: 0,
EndOffset: 0,
Title: &TextFrame{
Encoding: EncodingUTF8,
Text: "chapter 0 title",
},
Description: &TextFrame{
Encoding: EncodingUTF8,
Text: "chapter 0 description",
},
},
},
{
name: "non-zero time and offset",
fields: fields{
ElementID: "chap0",
StartTime: time.Duration(1000 * nanosInMillis),
EndTime: time.Duration(1000 * nanosInMillis),
StartOffset: 10,
EndOffset: 10,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpFile, err := prepareTestFile()
if err != nil {
t.Error(err)
}
defer os.Remove(tmpFile.Name())
tag, err := Open(tmpFile.Name(), Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
cf := ChapterFrame{
ElementID: tt.fields.ElementID,
StartTime: tt.fields.StartTime,
EndTime: tt.fields.EndTime,
StartOffset: tt.fields.StartOffset,
EndOffset: tt.fields.EndOffset,
Title: tt.fields.Title,
Description: tt.fields.Description,
}
tag.AddChapterFrame(cf)
if err := tag.Save(); err != nil {
t.Error(err)
}
tag.Close()
tag, err = Open(tmpFile.Name(), Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
frame := tag.GetLastFrame("CHAP").(ChapterFrame)
if frame.ElementID != tt.fields.ElementID {
t.Errorf("Expected element ID: %s, but got %s", tt.fields.ElementID, frame.ElementID)
}
if tt.fields.Title != nil && frame.Title.Text != tt.fields.Title.Text {
t.Errorf("Expected title: %s, but got %s", tt.fields.Title.Text, frame.Title)
}
if tt.fields.Description != nil && frame.Description.Text != tt.fields.Description.Text {
t.Errorf("Expected description: %s, but got %s", tt.fields.Description.Text, frame.Description.Text)
}
if frame.StartTime != tt.fields.StartTime {
t.Errorf("Expected start time: %s, but got %s", tt.fields.StartTime, frame.StartTime)
}
if frame.EndTime != tt.fields.EndTime {
t.Errorf("Expected end time: %s, but got %s", tt.fields.EndTime, frame.EndTime)
}
if frame.StartOffset != tt.fields.StartOffset {
t.Errorf("Expected start offset: %d, but got %d", tt.fields.StartOffset, frame.StartOffset)
}
if frame.EndOffset != tt.fields.EndOffset {
t.Errorf("Expected end offset: %d, but got %d", tt.fields.EndOffset, frame.EndOffset)
}
})
}
}