summaryrefslogtreecommitdiffstats
path: root/spec/helpers/vtt/builder_spec.cr
blob: dc1f4613c25aaa867e441e04881ed57ce89943a2 (plain)
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
require "../../spec_helper.cr"

MockLines                       = ["Line 1", "Line 2"]
MockLinesWithEscapableCharacter = ["<Line 1>", "&Line 2>", '\u200E' + "Line\u200F 3", "\u00A0Line 4"]

Spectator.describe "WebVTT::Builder" do
  it "correctly builds a vtt file" do
    result = WebVTT.build do |vtt|
      2.times do |i|
        vtt.cue(
          Time::Span.new(seconds: i),
          Time::Span.new(seconds: i + 1),
          MockLines[i]
        )
      end
    end

    expect(result).to eq([
      "WEBVTT",
      "",
      "00:00:00.000 --> 00:00:01.000",
      "Line 1",
      "",
      "00:00:01.000 --> 00:00:02.000",
      "Line 2",
      "",
      "",
    ].join('\n'))
  end

  it "correctly builds a vtt file with setting fields" do
    setting_fields = {
      "Kind"     => "captions",
      "Language" => "en",
    }

    result = WebVTT.build(setting_fields) do |vtt|
      2.times do |i|
        vtt.cue(
          Time::Span.new(seconds: i),
          Time::Span.new(seconds: i + 1),
          MockLines[i]
        )
      end
    end

    expect(result).to eq([
      "WEBVTT",
      "Kind: captions",
      "Language: en",
      "",
      "00:00:00.000 --> 00:00:01.000",
      "Line 1",
      "",
      "00:00:01.000 --> 00:00:02.000",
      "Line 2",
      "",
      "",
    ].join('\n'))
  end

  it "properly escapes characters" do
    result = WebVTT.build do |vtt|
      4.times do |i|
        vtt.cue(Time::Span.new(seconds: i), Time::Span.new(seconds: i + 1), MockLinesWithEscapableCharacter[i])
      end
    end

    expect(result).to eq([
      "WEBVTT",
      "",
      "00:00:00.000 --> 00:00:01.000",
      "&lt;Line 1&gt;",
      "",
      "00:00:01.000 --> 00:00:02.000",
      "&amp;Line 2&gt;",
      "",
      "00:00:02.000 --> 00:00:03.000",
      "&lrm;Line&rlm; 3",
      "",
      "00:00:03.000 --> 00:00:04.000",
      "&nbsp;Line 4",
      "",
      "",
    ].join('\n'))
  end
end