File: tilt_markdown_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 (134 lines) | stat: -rw-r--r-- 3,885 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require_relative 'test_helper'

begin
  require 'nokogiri'
rescue LoadError
  warn "Markdown tests need Nokogiri"
else
_MarkdownTests = Module.new do
  extend Minitest::Spec::DSL

  def self.included(mod)
    class << mod
      def template(t = nil)
        t.nil? ? @template : @template = t
      end
    end
  end

  def render(text, options = {})
    self.class.template.new(options) { text }.render
  end

  def normalize(html)
    Nokogiri::HTML.fragment(html).to_s.strip
  end

  def nrender(text, options = {})
    html = render(text, options)
    html.encode!("UTF-8")
    normalize(html)
  end

  it "should not escape html by default" do
    html = nrender "Hello <b>World</b>"
    assert_equal "<p>Hello <b>World</b></p>", html
  end

  it "should not escape html for :escape_html => false" do
    html = nrender "Hello <b>World</b>", :escape_html => false
    assert_equal "<p>Hello <b>World</b></p>", html
  end

  it "should escape html for :escape_html => true" do
    html = nrender "Hello <b>World</b>", :escape_html => true
    assert_equal "<p>Hello &lt;b&gt;World&lt;/b&gt;</p>", html
  end

  it "should not use smart quotes by default" do
    html = nrender 'Hello "World"'
    assert_equal '<p>Hello "World"</p>', html
  end

  it "should not use smart quotes if :smartypants => false" do
    html = nrender 'Hello "World"', :smartypants => false
    assert_equal '<p>Hello "World"</p>', html
  end

  it "should use smart quotes if :smartypants => true" do
    with_utf8_default_encoding do
      html = nrender 'Hello "World"', :smartypants => true
      assert_equal '<p>Hello “World”</p>', html
    end
  end

  it "should not use smartypants by default" do
    html = nrender "Hello ``World'' -- This is --- a it ..."
    assert_equal "<p>Hello ``World'' -- This is --- a it ...</p>", html
  end

  it "should not use smartypants if :smartypants => false" do
    html = nrender "Hello ``World'' -- This is --- a it ...", :smartypants => false
    assert_equal "<p>Hello ``World'' -- This is --- a it ...</p>", html
  end
end

markdown_describe = ->(lib, constant, &block) do
  begin
    require lib
  rescue LoadError
    # do nothing, main tests for the lib already warn
  else
    describe("#{lib} (markdown)") do
      include _MarkdownTests
      template Tilt.const_get(constant)
      instance_exec(&block) if block
    end
  end
end


markdown_describe.call 'tilt/rdiscount', :RDiscountTemplate do
  it "should use smartypants if :smartypants => true" do
    html = nrender "Hello ``World'' -- This is --- a it ...", :smartypants => true
    assert_equal "<p>Hello “World” – This is — a it …</p>", html
  end
end

markdown_describe.call 'tilt/redcarpet', :RedcarpetTemplate do
  it "should use smartypants if :smartypants => true" do
    # Various versions of Redcarpet support various versions of Smart pants.
    html = nrender "Hello ``World'' -- This is --- a it ...", :smartypants => true
    assert_match %r!<p>Hello “World(''|”) – This is — a it …<\/p>!, html
  end

  it "should support :no_links option" do
    html = nrender "Hello [World](http://example.com)", :smartypants => true, :no_links => true
    assert_equal "<p>Hello [World](http://example.com)</p>", html
  end

  it "should support fenced code blocks with lang" do
    code = <<-COD.gsub(/^\s+/,"")
    ```ruby
    puts "hello world"
    ```
    COD

    html = nrender code, :fenced_code_blocks => true
    assert_equal %Q{<pre><code class="ruby">puts "hello world"\n</code></pre>}, html
  end
end

markdown_describe.call 'tilt/kramdown', :KramdownTemplate do
  skip_tests = [
    ':escape_html => true',
    'smartypants by default',
    'smartypants if :smartypants => false',
  ]
  instance_methods.grep(/#{Regexp.union(skip_tests)}\z/).each do |method|
    undef_method method
  end
end

markdown_describe.call 'tilt/pandoc', :PandocTemplate
end