forked from asticode/go-astisub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebvtt_test.go
164 lines (128 loc) · 4.09 KB
/
webvtt_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
package astisub_test
import (
"bytes"
"io/ioutil"
"strings"
"testing"
"github.com/asticode/go-astisub"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWebVTT(t *testing.T) {
// Open
s, err := astisub.OpenFile("./testdata/example-in.vtt")
assert.NoError(t, err)
assertSubtitleItems(t, s)
// Comments
assert.Equal(t, []string{"this a nice example", "of a VTT"}, s.Items[0].Comments)
assert.Equal(t, []string{"This a comment inside the VTT", "and this is the second line"}, s.Items[1].Comments)
// Regions
assert.Equal(t, 2, len(s.Regions))
assert.Equal(t, astisub.Region{ID: "fred", InlineStyle: &astisub.StyleAttributes{WebVTTLines: 3, WebVTTRegionAnchor: "0%,100%", WebVTTScroll: "up", WebVTTViewportAnchor: "10%,90%", WebVTTWidth: "40%"}}, *s.Regions["fred"])
assert.Equal(t, astisub.Region{ID: "bill", InlineStyle: &astisub.StyleAttributes{WebVTTLines: 3, WebVTTRegionAnchor: "100%,100%", WebVTTScroll: "up", WebVTTViewportAnchor: "90%,90%", WebVTTWidth: "40%"}}, *s.Regions["bill"])
assert.Equal(t, s.Regions["bill"], s.Items[0].Region)
assert.Equal(t, s.Regions["fred"], s.Items[1].Region)
// Styles
assert.Equal(t, astisub.StyleAttributes{WebVTTAlign: "left", WebVTTPosition: "10%,start", WebVTTSize: "35%"}, *s.Items[1].InlineStyle)
// No subtitles to write
w := &bytes.Buffer{}
err = astisub.Subtitles{}.WriteToWebVTT(w)
assert.EqualError(t, err, astisub.ErrNoSubtitlesToWrite.Error())
// Write
c, err := ioutil.ReadFile("./testdata/example-out.vtt")
assert.NoError(t, err)
err = s.WriteToWebVTT(w)
assert.NoError(t, err)
assert.Equal(t, string(c), w.String())
}
func TestBroken1WebVTT(t *testing.T) {
// Open bad, broken WebVTT file
_, err := astisub.OpenFile("./testdata/broken-1-in.vtt")
assert.Nil(t, err)
}
func TestWebVTTWithVoiceName(t *testing.T) {
testData := `WEBVTT
NOTE this a example with voicename
1
00:02:34.000 --> 00:02:35.000
<v.first.local Roger Bingham> I'm the fist speaker
2
00:02:34.000 --> 00:02:35.000
<v Bingham> I'm the second speaker
3
00:00:04.000 --> 00:00:08.000
<v Lee>What are you doing here?</v>
4
00:00:04.000 --> 00:00:08.000
<v Bob>Incorrect tag?</vi>`
s, err := astisub.ReadFromWebVTT(strings.NewReader(testData))
assert.NoError(t, err)
assert.Len(t, s.Items, 4)
assert.Equal(t, "Roger Bingham", s.Items[0].Lines[0].VoiceName)
assert.Equal(t, "Bingham", s.Items[1].Lines[0].VoiceName)
assert.Equal(t, "Lee", s.Items[2].Lines[0].VoiceName)
assert.Equal(t, "Bob", s.Items[3].Lines[0].VoiceName)
b := &bytes.Buffer{}
err = s.WriteToWebVTT(b)
assert.NoError(t, err)
assert.Equal(t, `WEBVTT
NOTE this a example with voicename
1
00:02:34.000 --> 00:02:35.000
<v Roger Bingham>I'm the fist speaker
2
00:02:34.000 --> 00:02:35.000
<v Bingham>I'm the second speaker
3
00:00:04.000 --> 00:00:08.000
<v Lee>What are you doing here?
4
00:00:04.000 --> 00:00:08.000
<v Bob>Incorrect tag?
`, b.String())
}
func TestWebVTTWithTimestampMap(t *testing.T) {
testData := `WEBVTT
X-TIMESTAMP-MAP=MPEGTS:180000, LOCAL:00:00:00.000
00:00.933 --> 00:02.366
♪ ♪
00:02.400 --> 00:03.633
Evening.`
s, err := astisub.ReadFromWebVTT(strings.NewReader(testData))
assert.NoError(t, err)
assert.Len(t, s.Items, 2)
b := &bytes.Buffer{}
err = s.WriteToWebVTT(b)
assert.NoError(t, err)
assert.Equal(t, `WEBVTT
1
00:00:02.933 --> 00:00:04.366
♪ ♪
2
00:00:04.400 --> 00:00:05.633
Evening.
`, b.String())
}
func TestWebVTTEscape(t *testing.T) {
testData := `WEBVTT
00:01:00.000 --> 00:02:00.000
Sentence with an & in the middle
00:02:00.000 --> 00:03:00.000
Sentence with an < in the middle`
s, err := astisub.ReadFromWebVTT(strings.NewReader(testData))
require.NoError(t, err)
require.Len(t, s.Items, 2)
require.Equal(t, "Sentence with an & in the middle", s.Items[0].String())
require.Equal(t, "Sentence with an < in the middle", s.Items[1].String())
b := &bytes.Buffer{}
err = s.WriteToWebVTT(b)
require.NoError(t, err)
require.Equal(t, `WEBVTT
1
00:01:00.000 --> 00:02:00.000
Sentence with an & in the middle
2
00:02:00.000 --> 00:03:00.000
Sentence with an < in the middle
`, b.String())
}