forked from n10v/id3v2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter_frame_test.go
More file actions
187 lines (178 loc) · 4.41 KB
/
chapter_frame_test.go
File metadata and controls
187 lines (178 loc) · 4.41 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
wantElementId string
wantTitle string
wantDescription string
}{
{
name: "element id only",
fields: fields{
ElementID: "chap0",
StartTime: 0,
EndTime: time.Duration(1000 * nanosInMillis),
StartOffset: 0,
EndOffset: 0,
},
wantElementId: "chap0",
wantTitle: "",
wantDescription: "",
},
{
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",
},
},
wantElementId: "chap0",
wantTitle: "chapter 0",
wantDescription: "",
},
{
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",
},
},
wantElementId: "chap0",
wantTitle: "",
wantDescription: "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",
},
},
wantElementId: "chap0",
wantTitle: "chapter 0 title",
wantDescription: "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,
},
wantElementId: "chap0",
wantTitle: "",
wantDescription: "",
},
}
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.wantElementId {
t.Errorf("expected: %s, but got %s", tt.wantElementId, frame.ElementID)
}
if frame.Title.Text != tt.wantTitle {
t.Errorf("expected: %s, but got %s", tt.wantTitle, frame.Title)
}
if frame.Description.Text != tt.wantDescription {
t.Errorf("expected: %s, but got %s", tt.wantDescription, frame.Description.Text)
}
if frame.StartTime != tt.fields.StartTime {
t.Errorf("expected: %s, but got %s", tt.fields.StartTime, frame.StartTime)
}
if frame.EndTime != tt.fields.EndTime {
t.Errorf("expected: %s, but got %s", tt.fields.EndTime, frame.EndTime)
}
if frame.StartOffset != tt.fields.StartOffset {
t.Errorf("expected: %d, but got %d", tt.fields.StartOffset, frame.StartOffset)
}
if frame.EndOffset != tt.fields.EndOffset {
t.Errorf("expected: %d, but got %d", tt.fields.EndOffset, frame.EndOffset)
}
})
}
}