builder_spec.cr 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. require "../../spec_helper.cr"
  2. MockLines = ["Line 1", "Line 2"]
  3. MockLinesWithEscapableCharacter = ["<Line 1>", "&Line 2>", '\u200E' + "Line\u200F 3", "\u00A0Line 4"]
  4. Spectator.describe "WebVTT::Builder" do
  5. it "correctly builds a vtt file" do
  6. result = WebVTT.build do |vtt|
  7. 2.times do |i|
  8. vtt.cue(
  9. Time::Span.new(seconds: i),
  10. Time::Span.new(seconds: i + 1),
  11. MockLines[i]
  12. )
  13. end
  14. end
  15. expect(result).to eq([
  16. "WEBVTT",
  17. "",
  18. "00:00:00.000 --> 00:00:01.000",
  19. "Line 1",
  20. "",
  21. "00:00:01.000 --> 00:00:02.000",
  22. "Line 2",
  23. "",
  24. "",
  25. ].join('\n'))
  26. end
  27. it "correctly builds a vtt file with setting fields" do
  28. setting_fields = {
  29. "Kind" => "captions",
  30. "Language" => "en",
  31. }
  32. result = WebVTT.build(setting_fields) do |vtt|
  33. 2.times do |i|
  34. vtt.cue(
  35. Time::Span.new(seconds: i),
  36. Time::Span.new(seconds: i + 1),
  37. MockLines[i]
  38. )
  39. end
  40. end
  41. expect(result).to eq([
  42. "WEBVTT",
  43. "Kind: captions",
  44. "Language: en",
  45. "",
  46. "00:00:00.000 --> 00:00:01.000",
  47. "Line 1",
  48. "",
  49. "00:00:01.000 --> 00:00:02.000",
  50. "Line 2",
  51. "",
  52. "",
  53. ].join('\n'))
  54. end
  55. it "properly escapes characters" do
  56. result = WebVTT.build do |vtt|
  57. 4.times do |i|
  58. vtt.cue(Time::Span.new(seconds: i), Time::Span.new(seconds: i + 1), MockLinesWithEscapableCharacter[i])
  59. end
  60. end
  61. expect(result).to eq([
  62. "WEBVTT",
  63. "",
  64. "00:00:00.000 --> 00:00:01.000",
  65. "&lt;Line 1&gt;",
  66. "",
  67. "00:00:01.000 --> 00:00:02.000",
  68. "&amp;Line 2&gt;",
  69. "",
  70. "00:00:02.000 --> 00:00:03.000",
  71. "&lrm;Line&rlm; 3",
  72. "",
  73. "00:00:03.000 --> 00:00:04.000",
  74. "&nbsp;Line 4",
  75. "",
  76. "",
  77. ].join('\n'))
  78. end
  79. end