Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/srt/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ def timeshift(options)
end
end

def has_sequence_error?
sequence = lines.map { |line| line.sequence }
sequence_error_after = nil

sequence.each_cons(2) { |x,y|
if y - x != 1
sequence_error_after = x
break
end
}

"Sequence error after #{sequence_error_after}" if sequence_error_after
end

def to_s(time_str_function=:time_str)
lines.map { |l| l.to_s(time_str_function) }.join("\n")
end
Expand Down
29 changes: 29 additions & 0 deletions spec/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,35 @@
end
end

describe "has_sequence_error?" do
context "when sequence is ok" do
let(:file1) { SRT::File.parse(File.open("./spec/fixtures/blackswan-part1.srt")) }
let(:file2) { SRT::File.parse(File.open("./spec/fixtures/bsg-s01e01.srt")) }
let(:file3) { SRT::File.parse(File.open("./spec/fixtures/blackswan-part2.srt")) }
it "should return nil" do
expect(file1.has_sequence_error?).to eq nil
expect(file2.has_sequence_error?).to eq nil
expect(file3.has_sequence_error?).to eq nil
end
end

context "when sequence is non-consecutive" do
let(:file) { SRT::File.parse(File.open("./spec/fixtures/broken_sequence.srt")) }

it "should return a useful error message about the sequence" do
expect(file.has_sequence_error?).to eq("Sequence error after 1")
end
end

context "when there are unexpected blank lines" do
let(:file) { SRT::File.parse(File.open("./spec/fixtures/unexpected_blanks.srt")) }

it "should return a useful error message about the sequence" do
expect(file.has_sequence_error?).to eq("Sequence error after 3")
end
end
end

describe "#timeshift" do
context "when calling it on a properly formatted BSG SRT file" do
let(:file) { SRT::File.parse(File.open("./spec/fixtures/bsg-s01e01.srt")) }
Expand Down
13 changes: 13 additions & 0 deletions spec/fixtures/broken_sequence.srt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1
00:00:02,110 --> 00:00:04,578
<i>(male narrator) Previously
on Battlestar Galactica.</i>

1
00:00:05,313 --> 00:00:06,871
Now you're telling me
you're a machine.

3
00:00:07,014 --> 00:00:08,003
The robot.
14 changes: 14 additions & 0 deletions spec/fixtures/unexpected_blanks.srt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1
00:00:02,110 --> 00:00:04,578
<i>(male narrator) Previously
on Battlestar Galactica.</i>

2
00:00:05,313 --> 00:00:06,871
Now you're telling me

3
00:00:07,014 --> 00:00:08,003
The robot.

you're a machine.