File: tilt_commonmarkertemplate_test.rb

package info (click to toggle)
ruby-tilt 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 648 kB
  • sloc: ruby: 4,998; makefile: 7
file content (98 lines) | stat: -rw-r--r-- 3,217 bytes parent folder | download
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
require_relative 'test_helper'

checked_describe 'tilt/commonmarker' do
  it "preparing and evaluating templates on #render" do
    template = Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }
    assert_includes [
      "<h1>Hello World!</h1>\n",
      "<h1><a href=\"#hello-world\" aria-hidden=\"true\" class=\"anchor\" id=\"hello-world\"></a>Hello World!</h1>\n",
      "<h1><a inert href=\"#hello-world\" aria-hidden=\"true\" class=\"anchor\" id=\"hello-world\"></a>Hello World!</h1>\n"
    ], template.render
  end

  it "can be rendered more than once" do
    template = Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }
    3.times do
      assert_includes [
        "<h1>Hello World!</h1>\n",
        "<h1><a href=\"#hello-world\" aria-hidden=\"true\" class=\"anchor\" id=\"hello-world\"></a>Hello World!</h1>\n",
        "<h1><a inert href=\"#hello-world\" aria-hidden=\"true\" class=\"anchor\" id=\"hello-world\"></a>Hello World!</h1>\n"
      ], template.render
    end
  end

  it "smartypants when :smartypants is set" do
    template = Tilt::CommonMarkerTemplate.new(:smartypants => true) do |t|
      "OKAY -- 'Smarty Pants'"
    end
    assert_match('<p>OKAY – ‘Smarty Pants’</p>', template.render)
  end

  it 'Renders unsafe HTML when :UNSAFE is set' do
    template = Tilt::CommonMarkerTemplate.new(UNSAFE: true) do |_t|
      <<MARKDOWN
<div class="alert alert-info full-width">
<h5 class="card-title">TL;DR</h5>
<p>This is an unsafe HTML block</p>
</div>

And then some **other** Markdown
MARKDOWN
    end

    expected = <<EXPECTED_HTML
<div class="alert alert-info full-width">
<h5 class="card-title">TL;DR</h5>
<p>This is an unsafe HTML block</p>
</div>
<p>And then some <strong>other</strong> Markdown</p>
EXPECTED_HTML

    assert_match(expected, template.render)
  end

  it "sets allows_script metadata set to false" do
    assert_equal false, Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }.metadata[:allows_script]
  end

  if defined?(::Commonmarker)
    it "render unsafe HTML with pre version's option name" do
      template = Tilt::CommonMarkerTemplate.new(UNSAFE: true) do |_t|
        <<MARKDOWN
<div class="alert alert-info full-width">
<h5 class="card-title">TL;DR</h5>
<p>This is an unsafe HTML block</p>
</div>

And then some **other** Markdown
MARKDOWN
      end

      expected = <<EXPECTED_HTML
<div class="alert alert-info full-width">
<h5 class="card-title">TL;DR</h5>
<p>This is an unsafe HTML block</p>
</div>
<p>And then some <strong>other</strong> Markdown</p>
EXPECTED_HTML

      assert_match(expected, template.render)
    end

    it "smartypants when :smartypants is set (pre version's option name)" do
      template = Tilt::CommonMarkerTemplate.new(:smartypants => true) do |t|
        "OKAY -- 'Smarty Pants'"
      end
      assert_match('<p>OKAY – ‘Smarty Pants’</p>', template.render)
    end

    it "render markdown with custom prefixed-header id" do
      template = Tilt::CommonMarkerTemplate.new(header_ids: "prefix-") do |t|
        "# Foo"
      end
      assert_match(<<HTML, template.render.sub(" inert", ""))
<h1><a href="#foo" aria-hidden="true" class="anchor" id="prefix-foo"></a>Foo</h1>
HTML
    end
  end
end