From 7a5b4ba2a1c8ea94aa0524fa5b3c43b8dbedb935 Mon Sep 17 00:00:00 2001
From: Philipp Kerling <pkerling@casix.org>
Date: Mon, 18 Feb 2019 15:38:08 +0100
Subject: [PATCH] Add tests for paint-on caption combining

---
 spec/scc/transformer_spec.rb | 110 +++++++++++++++++++++++++++++++++++
 1 file changed, 110 insertions(+)

diff --git a/spec/scc/transformer_spec.rb b/spec/scc/transformer_spec.rb
index c1aa212..6d5cd79 100644
--- a/spec/scc/transformer_spec.rb
+++ b/spec/scc/transformer_spec.rb
@@ -163,5 +163,115 @@ module Subconv
       )
                                )
     end
+
+    context 'when combining paint-on captions' do
+      it 'should not eat single paint-on captions' do
+        expect(@transformer.combine_paint_on_captions(grids_to_captions(:paint_on, 1 => 'Test'))).to eq(grids_to_captions(:pop_on, 1 => 'Test'))
+      end
+
+      it 'should correctly combine simple paint-on captions into one' do
+        expect(@transformer.combine_paint_on_captions(
+          grids_to_captions(:paint_on, 1 => 'Te', 2 => 'Test', 3 => 'TestTe', 4 => 'TestText')
+        )).to eq(
+          grids_to_captions(1 => 'TestText')
+        )
+      end
+
+      it 'should produce combined paint-on captions when an empty grid is encountered' do
+        expect(@transformer.combine_paint_on_captions(
+          grids_to_captions(:paint_on, 1 => 'Te', 2 => 'Test', 3 => nil)
+        )).to eq(
+          grids_to_captions(1 => 'Test', 3 => nil)
+        )
+      end
+
+      it 'should not combine paint-on captions with an empty grid' do
+        expect(@transformer.combine_paint_on_captions(
+          grids_to_captions(:paint_on, 1 => 'Te', 2 => 'Test', 3 => nil, 4 => 'Text')
+        )).to eq(
+          grids_to_captions(1 => 'Test', 3 => nil, 4 => 'Text')
+        )
+      end
+
+      it 'should start a new caption segment when paint-on captions disappear' do
+        expect(@transformer.combine_paint_on_captions(
+          grids_to_captions(:paint_on, 1 => 'Te', 2 => 'Test', 3 => 'Tes', 4 => 'Te', 5 => 'Text')
+        )).to eq(
+          grids_to_captions(1 => 'Test', 3 => 'Tes', 4 => 'Text')
+        )
+      end
+
+      it 'should insert a new caption when paint-on captions change' do
+        expect(@transformer.combine_paint_on_captions(
+          grids_to_captions(:paint_on, 1 => 'Te', 2 => 'Test', 5 => 'Text')
+        )).to eq(
+          grids_to_captions(1 => 'Test', 5 => 'Text')
+        )
+      end
+
+      it 'should start a new caption segment when paint-on captions change' do
+        expect(@transformer.combine_paint_on_captions(
+          grids_to_captions(:paint_on, 1 => 'Te', 2 => 'Test', 5 => 'Text', 7 => 'TextTest')
+        )).to eq(
+          grids_to_captions(1 => 'Test', 5 => 'TextTest')
+        )
+      end
+
+      it 'should flush single paint-on captions when switching to pop-on captions momentarily' do
+        expect(@transformer.combine_paint_on_captions([
+          Scc::Caption.new(timecode: Timecode.new(1, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Test'), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(2, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Text'), mode: :pop_on)
+        ])).to eq(
+          grids_to_captions(1 => 'Test', 2 => 'Text')
+        )
+      end
+
+      it 'should flush combined paint-on captions when switching to pop-on captions momentarily' do
+        expect(@transformer.combine_paint_on_captions([
+          Scc::Caption.new(timecode: Timecode.new(1, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Test'), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(2, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'TestText'), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(3, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'asdf'), mode: :pop_on)
+        ])).to eq(
+          grids_to_captions(1 => 'TestText', 3 => 'asdf')
+        )
+      end
+
+      it 'should flush combined paint-on captions when switching to pop-on captions after an empty paint-on caption' do
+        expect(@transformer.combine_paint_on_captions([
+          Scc::Caption.new(timecode: Timecode.new(1, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Test'), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(2, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'TestText'), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(3, default_fps), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(4, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'asdf'), mode: :pop_on)
+        ])).to eq(
+          grids_to_captions(1 => 'TestText', 3 => nil, 4 => 'asdf')
+        )
+      end
+
+      it 'should flush combined paint-on captions when switching to pop-on captions after an empty pop-on caption' do
+        expect(@transformer.combine_paint_on_captions([
+          Scc::Caption.new(timecode: Timecode.new(1, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Test'), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(2, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'TestText'), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(3, default_fps), mode: :pop_on),
+          Scc::Caption.new(timecode: Timecode.new(4, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'asdf'), mode: :pop_on)
+        ])).to eq(
+          grids_to_captions(1 => 'TestText', 3 => nil, 4 => 'asdf')
+        )
+      end
+
+      it 'should handle combinations of pop-on and paint-on captions' do
+        expect(@transformer.combine_paint_on_captions([
+          Scc::Caption.new(timecode: Timecode.new(1, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Test1'), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(2, default_fps), mode: :pop_on),
+          Scc::Caption.new(timecode: Timecode.new(3, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Test2'), mode: :pop_on),
+          Scc::Caption.new(timecode: Timecode.new(4, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Test3'), mode: :pop_on),
+          Scc::Caption.new(timecode: Timecode.new(5, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Test4'), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(6, default_fps), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(7, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Test'), mode: :paint_on),
+          Scc::Caption.new(timecode: Timecode.new(8, default_fps), grid: Scc::Grid.new.insert_text(0, 0, 'Test5'), mode: :paint_on)
+        ])).to eq(
+          grids_to_captions(1 => 'Test1', 2 => nil, 3 => 'Test2', 4 => 'Test3', 5 => 'Test4', 6 => nil, 7 => 'Test5')
+        )
+      end
+    end
   end
 end
-- 
GitLab