-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathparser.rb
More file actions
280 lines (241 loc) · 8.52 KB
/
Copy pathparser.rb
File metadata and controls
280 lines (241 loc) · 8.52 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# frozen_string_literal: true
$LOAD_PATH << "lib/"
require "minitest/autorun"
require "webvtt"
class ParserTest < Minitest::Test
def test_can_read_webvtt
webvtt = WebVTT.read("tests/subtitles/test.vtt")
assert_equal "test.vtt", webvtt.filename
end
def test_cant_read_webvtt
assert_raises(WebVTT::InputError) {
webvtt = WebVTT.read("tests/subtitles/test_.vtt")
}
end
def test_is_not_valid_webvtt
assert_raises(WebVTT::MalformedFile) {
webvtt = WebVTT.read("tests/subtitles/notvalid.vtt")
}
end
def test_can_create_empty_webvtt
webvtt = WebVTT::Blob.new
assert_equal 'WEBVTT', webvtt.header
assert_equal [], webvtt.cues
end
def test_list_cues
webvtt = WebVTT.read("tests/subtitles/test.vtt")
assert_instance_of Array, webvtt.cues
assert !webvtt.cues.empty?, "Cues should not be empty"
assert_instance_of WebVTT::Cue, webvtt.cues[0]
assert_equal 15, webvtt.cues.size
end
def test_header
webvtt = WebVTT.read("tests/subtitles/test.vtt")
assert_equal "WEBVTT\nX-TIMESTAMP-MAP=MPEGTS:900000,LOCAL:00:00:00.000", webvtt.header
end
def test_cue
webvtt = WebVTT.read("tests/subtitles/test.vtt")
cue = webvtt.cues[0]
assert_equal "00:00:29.000", cue.start.to_s
assert_equal "00:00:31.000", cue.end.to_s
assert_instance_of Hash, cue.style
assert_equal "75%", cue.style["line"]
assert_equal "English subtitle 15 -Forced- (00:00:27.000)\nline:75%", cue.text
end
def test_cue_multiple_spaces_in_cue
webvtt = WebVTT.read("tests/subtitles/test_multiple_spaces_in_cue.vtt")
cue = webvtt.cues[0]
assert_equal "00:00:01.000", cue.start.to_s
assert_equal "00:00:02.000", cue.end.to_s
assert_instance_of Hash, cue.style
assert_equal "Some text", cue.text
cue = webvtt.cues[1]
assert_equal "00:00:02.000", cue.start.to_s
assert_equal "00:00:03.000", cue.end.to_s
assert_instance_of Hash, cue.style
assert_equal "Some other text", cue.text
end
def test_cue_non_hours
webvtt = WebVTT.read("tests/subtitles/test_mmss_format.vtt")
cue = webvtt.cues[0]
assert_equal "00:00:29.000", cue.start.to_s
assert_equal "00:00:31.000", cue.end.to_s
assert_instance_of Hash, cue.style
assert_equal "75%", cue.style["line"]
assert_equal "English subtitle 15 -Forced- (00:00:27.000)\nline:75%", cue.text
end
def test_cue_identifier
webvtt = WebVTT.read("tests/subtitles/test.vtt")
cue = webvtt.cues[1]
assert_equal "2", cue.identifier
assert_equal "00:00:31.000", cue.start.to_s
assert_equal "00:00:33.000", cue.end.to_s
assert_equal ["align", "line"].sort, cue.style.keys.sort
assert_equal ["start", "0%"].sort, cue.style.values.sort
assert_equal "English subtitle 16 -Unforced- (00:00:31.000)\nalign:start line:0%", cue.text
end
def test_multiple_line_separators
webvtt = WebVTT.read("tests/subtitles/test_multiple_line_separators.vtt")
assert_equal 2, webvtt.cues.length
end
def test_ignore_if_note
webvtt = WebVTT.read("tests/subtitles/withnote.vtt")
assert_equal 3, webvtt.cues.size
# ignoring the first cue which is a NOTE
assert_equal "1", webvtt.cues[0].identifier
end
def test_ignore_style_and_remove_style_tag_from_cue_text
webvtt = WebVTT.read("tests/subtitles/withstyle.vtt")
assert_equal 1, webvtt.cues.size
# ignoring the first cue which is a NOTE
assert_equal "hello", webvtt.cues[0].identifier
assert_equal "Hello world.", webvtt.cues[0].plain_text
assert_equal "Hello <b>world</b>.", webvtt.cues[0].text
end
def test_timestamp_in_sec
assert_equal 60.0, WebVTT::Cue.timestamp_in_sec("00:01:00.000")
assert_equal 126.23, WebVTT::Cue.timestamp_in_sec("00:02:06.230")
assert_equal 5159.892, WebVTT::Cue.timestamp_in_sec("01:25:59.892")
end
def test_total_length
webvtt = WebVTT.read("tests/subtitles/test.vtt")
assert_equal 359, webvtt.total_length
end
def test_cue_length
webvtt = WebVTT.read("tests/subtitles/test.vtt")
assert_equal 2.0, webvtt.cues[2].length
end
def test_file_to_webvtt
webvtt = WebVTT.read("tests/subtitles/test.vtt")
assert_equal webvtt.to_webvtt, File.read("tests/subtitles/test.vtt")
end
def test_cue_to_webvtt
webvtt = WebVTT.read("tests/subtitles/test.vtt")
assert_equal webvtt.cues[0].to_webvtt, %(00:00:29.000 --> 00:00:31.000 line:75%
English subtitle 15 -Forced- (00:00:27.000)
line:75%)
assert_equal webvtt.cues[1].to_webvtt, %(2
00:00:31.000 --> 00:00:33.000 align:start line:0%
English subtitle 16 -Unforced- (00:00:31.000)
align:start line:0%)
end
def test_updating_webvtt
webvtt = WebVTT.read("tests/subtitles/test.vtt")
cue = webvtt.cues[0]
cue.identifier = "1"
cue.text = "The text should change"
cue.start = "00:00:01.000"
cue.style = {}
webvtt.cues = [cue]
assert_equal webvtt.to_webvtt, %(WEBVTT
X-TIMESTAMP-MAP=MPEGTS:900000,LOCAL:00:00:00.000
1
00:00:01.000 --> 00:00:31.000
The text should change)
end
def test_reading_all_cues
return
webvtt = WebVTT.read("tests/subtitles/withnote.vtt")
webvtt.cues.each_with_index do |cue,i|
puts "#{i}"
puts "identifier: #{cue.identifier}"
puts "Timestamps: #{cue.start} --> #{cue.end}"
puts "Style: #{cue.style.inspect}"
puts "Text :#{cue.text}\n*"
puts
end
end
def test_convert_srt_to_webvtt
webvtt = WebVTT.convert_from_srt("tests/subtitles/test_from_srt.srt")
assert_instance_of WebVTT::File, webvtt
assert_equal 2, webvtt.cues.size
end
def test_convert_weird_format_srt_to_webvtt
webvtt = WebVTT.convert_from_srt("tests/subtitles/weird_format.srt")
correct_webvtt = WebVTT.read("tests/subtitles/weird_format_corrected.vtt")
converted_cues = webvtt.cues.map { |cue| cue.start.to_s }
correct_cues = correct_webvtt.cues.map { |cue| cue.start.to_s }
assert_instance_of WebVTT::File, webvtt
assert_equal correct_cues, converted_cues
end
def test_parse_big_file
return
webvtt = WebVTT.read("tests/subtitles/big_srt.vtt")
webvtt.cues.each_with_index do |cue,i|
puts "*#{i}"
puts "identifier: #{cue.identifier}"
puts "Timestamps: #{cue.start} --> #{cue.end}"
puts "Style: #{cue.style.inspect}"
puts "Text :#{cue.text}\n*"
puts
end
end
def test_parse_cue_with_no_text
webvtt = WebVTT.read("tests/subtitles/no_text.vtt")
assert_equal 2, webvtt.cues.size
assert_equal "265", webvtt.cues[0].identifier
assert_equal "00:08:57.409", webvtt.cues[0].start.to_s
assert_equal "00:09:00.592", webvtt.cues[0].end.to_s
assert_equal "", webvtt.cues[0].text
assert_equal "266", webvtt.cues[1].identifier
assert_equal "00:09:00.593", webvtt.cues[1].start.to_s
assert_equal "00:09:02.373", webvtt.cues[1].end.to_s
assert_equal "", webvtt.cues[1].text
end
def test_cue_offset_by
cue = WebVTT::Cue.parse <<-CUE
00:00:01.000 --> 00:00:25.432
Test Cue
CUE
assert_equal 1.0, cue.start.to_f
assert_equal 25.432, cue.end.to_f
cue.offset_by( 12.0 )
assert_equal 13.0, cue.start.to_f
assert_equal 37.432, cue.end.to_f
end
def test_timestamp_from_string
ts_str = "00:05:31.522"
ts = WebVTT::Timestamp.new( ts_str )
assert_equal ts_str, ts.to_s
assert_equal (5*60 + 31.522), ts.to_f
end
def test_timestamp_from_number
ts_f = (7*60 + 12.111)
ts = WebVTT::Timestamp.new( ts_f )
assert_equal "00:07:12.111", ts.to_s
assert_equal ts_f, ts.to_f
end
def test_timestamp_errors_from_unknown_type
assert_raises ArgumentError do
WebVTT::Timestamp.new( nil )
end
end
def test_timestamp_addition
ts = WebVTT::Timestamp.new( "01:47:32.004" )
ts2 = ts + (4*60 + 30)
assert_equal "01:52:02.004", ts2.to_s
ts3 = ts + ts2
assert_equal "03:39:34.008", ts3.to_s
end
def test_build_cue
cue = WebVTT::Cue.new
cue.start = WebVTT::Timestamp.new 0
cue.end = WebVTT::Timestamp.new 12
cue.text = "Built from scratch"
output = String.new
output << "00:00:00.000 --> 00:00:12.000\n"
output << "Built from scratch"
assert_equal output, cue.to_webvtt
end
def test_invalid_cue
webvtt = WebVTT.convert_from_srt("tests/subtitles/invalid_cue.srt")
assert_equal 1, webvtt.cues.size
end
def test_can_validate_webvtt_with_carriage_returns
webvtt = WebVTT::File.new("tests/subtitles/test_carriage_returns.vtt")
assert_instance_of Array, webvtt.cues
assert !webvtt.cues.empty?, "Cues should not be empty"
assert_instance_of WebVTT::Cue, webvtt.cues[0]
assert_equal 15, webvtt.cues.size
end
end