File: filters_test.rb

package info (click to toggle)
ruby-hamlit 2.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,332 kB
  • sloc: ruby: 10,583; ansic: 550; sh: 23; makefile: 9
file content (262 lines) | stat: -rw-r--r-- 8,325 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
require 'test_helper'

class FiltersTest < Haml::TestCase
  test "should be registered as filters when including Hamlit::Filters::Base" do; skip
    begin
      refute Hamlit::Filters.defined.has_key? "bar"
      Module.new {def self.name; "Foo::Bar"; end; include Hamlit::Filters::Base}
      assert Hamlit::Filters.defined.has_key? "bar"
    ensure
      Hamlit::Filters.remove_filter "Bar"
    end
  end

  test "should raise error when attempting to register a defined Tilt filter" do; skip
    begin
      assert_raises RuntimeError do
        2.times do
          Hamlit::Filters.register_tilt_filter "Foo"
        end
      end
    ensure
      Hamlit::Filters.remove_filter "Foo"
    end
  end

  test "should raise error when a Tilt filters dependencies are unavailable for extension" do; skip
    begin
      assert_raises Hamlit::Error do
        # ignore warnings from Tilt
        silence_warnings do
          Hamlit::Filters.register_tilt_filter "Textile"
          Hamlit::Filters.defined["textile"].template_class
        end
      end
    ensure
      Hamlit::Filters.remove_filter "Textile"
    end
  end

  test "should raise error when a Tilt filters dependencies are unavailable for filter without extension" do; skip
    begin
      assert_raises Hamlit::Error do
        Hamlit::Filters.register_tilt_filter "Maruku"
        Hamlit::Filters.defined["maruku"].template_class
      end
    ensure
      Hamlit::Filters.remove_filter "Maruku"
    end
  end

  test "should raise informative error about Maruku being moved to haml-contrib" do; skip
    begin
      render(":maruku\n  # foo")
      flunk("Should have raised error with message about the haml-contrib gem.")
    rescue Hamlit::Error => e
      assert_equal e.message, Hamlit::Error.message(:install_haml_contrib, "maruku")
    end
  end

  test "should raise informative error about Textile being moved to haml-contrib" do; skip
    begin
      render(":textile\n  h1. foo")
      flunk("Should have raised error with message about the haml-contrib gem.")
    rescue Hamlit::Error => e
      assert_equal e.message, Hamlit::Error.message(:install_haml_contrib, "textile")
    end
  end

  test "should respect escaped newlines and interpolation" do
    assert_haml_ugly(":plain\n  \\n\#{""}")
  end

  test "should process an filter with no content" do
    assert_equal("\n", render(':plain'))
  end

  test "should be compatible with ugly mode" do
    expectation = "foo\n"
    assert_equal(expectation, render(":plain\n  foo"))
  end

  test "should pass options to Tilt filters that precompile" do; skip
    begin
      orig_erb_opts = Hamlit::Filters::Erb.options
      haml  = ":erb\n  <%= 'foo' %>"
      refute_match('test_var', Haml::Engine.new(haml).compiler.precompiled)
      Hamlit::Filters::Erb.options = {:outvar => 'test_var'}
      assert_match('test_var', Haml::Engine.new(haml).compiler.precompiled)
    ensure
      Hamlit::Filters::Erb.options = orig_erb_opts
    end
  end

  test "should pass options to Tilt filters that don't precompile" do; skip
    begin
      filter = Class.new(Tilt::Template) do
        def self.name
          "Foo"
        end

        def prepare
          @engine = {:data => data, :options => options}
        end

        def evaluate(scope, locals, &block)
          @output = @engine[:options].to_a.join
        end
      end
      Hamlit::Filters.register_tilt_filter "Foo", :template_class => filter
      Hamlit::Filters::Foo.options[:foo] = "bar"
      haml = ":foo"
      assert_equal "foobar\n", render(haml)
    ensure
      Hamlit::Filters.remove_filter "Foo"
    end
  end

  test "interpolated code should be escaped if escape_html is set" do; skip
    assert_haml_ugly(":plain\n  \#{'<script>evil</script>'}")
  end

end

class ErbFilterTest < Haml::TestCase
  test "multiline expressions should work" do; skip
    assert_haml_ugly(%Q{:erb\n  <%= "foo" +\n      "bar" +\n      "baz" %>})
  end

  test "should evaluate in the same context as Haml" do; skip
    haml  = ":erb\n  <%= foo %>"
    html  = "bar\n"
    scope = Object.new.instance_eval {foo = "bar"; nil if foo; binding}
    assert_equal(html, render(haml, :scope => scope))
  end

  test "should use Rails's XSS safety features" do; skip
    assert_equal("&lt;img&gt;\n", render(":erb\n  <%= '<img>' %>"))
    assert_equal("<img>\n", render(":erb\n  <%= '<img>'.html_safe %>"))
  end

end

class JavascriptFilterTest < Haml::TestCase
  test "should interpolate" do; skip
    scope = Object.new.instance_eval {foo = "bar"; nil if foo; binding}
    haml  = ":javascript\n  \#{foo}"
    html  = render(haml, :scope => scope)
    assert_match(/bar/, html)
  end

  test "should never HTML-escape non-interpolated ampersands" do; skip
    html = "<script>\n  & < > &amp;\n</script>\n"
    haml = %Q{:javascript\n  & < > \#{"&"}}
    assert_equal(html, render(haml, :escape_html => true))
  end

  test "should not include type in HTML 5 output" do
    html = "<script>\n  foo bar\n</script>\n"
    haml = ":javascript\n  foo bar"
    assert_equal(html, render(haml, :format => :html5))
  end

  test "should always include CDATA when format is xhtml" do
    html = "<script type='text/javascript'>\n  //<![CDATA[\n    foo bar\n  //]]>\n</script>\n"
    haml = ":javascript\n  foo bar"
    assert_equal(html, render(haml, :format => :xhtml, :cdata => false))
  end

  test "should omit CDATA when cdata option is false" do
    html = "<script>\n  foo bar\n</script>\n"
    haml = ":javascript\n  foo bar"
    assert_equal(html, render(haml, :format => :html5, :cdata => false))
  end

  test "should include CDATA when cdata option is true" do; skip
    html = "<script>\n  //<![CDATA[\n    foo bar\n  //]]>\n</script>\n"
    haml = ":javascript\n  foo bar"
    assert_equal(html, render(haml, :format => :html5, :cdata => true))
  end

  test "should default to no CDATA when format is html5" do
    haml = ":javascript\n  foo bar"
    out = render(haml, :format => :html5)
    refute_match('//<![CDATA[', out)
    refute_match('//]]>', out)
  end
end

class CSSFilterTest < Haml::TestCase
  test "should wrap output in CDATA and a CSS tag when output is XHTML" do
    html = "<style type='text/css'>\n  /*<![CDATA[*/\n    foo\n  /*]]>*/\n</style>\n"
    haml = ":css\n  foo"
    assert_equal(html, render(haml, :format => :xhtml))
  end

  test "should not include type in HTML 5 output" do
    html = "<style>\n  foo bar\n</style>\n"
    haml = ":css\n  foo bar"
    assert_equal(html, render(haml, :format => :html5))
  end

  test "should always include CDATA when format is xhtml" do
    html = "<style type='text/css'>\n  /*<![CDATA[*/\n    foo bar\n  /*]]>*/\n</style>\n"
    haml = ":css\n  foo bar"
    assert_equal(html, render(haml, :format => :xhtml, :cdata => false))
  end

  test "should omit CDATA when cdata option is false" do
    html = "<style>\n  foo bar\n</style>\n"
    haml = ":css\n  foo bar"
    assert_equal(html, render(haml, :format => :html5, :cdata => false))
  end

  test "should include CDATA when cdata option is true" do; skip
    html = "<style>\n  /*<![CDATA[*/\n    foo bar\n  /*]]>*/\n</style>\n"
    haml = ":css\n  foo bar"
    assert_equal(html, render(haml, :format => :html5, :cdata => true))
  end

  test "should default to no CDATA when format is html5" do
    haml = ":css\n  foo bar"
    out = render(haml, :format => :html5)
    refute_match('<![CDATA[', out)
    refute_match(']]>', out)
  end
end

class CDATAFilterTest < Haml::TestCase
  test "should wrap output in CDATA tag" do
    html = "<![CDATA[\n    foo\n]]>\n"
    haml = ":cdata\n  foo"
    assert_equal(html, render(haml))
  end
end

class EscapedFilterTest < Haml::TestCase
  test "should escape ampersands" do
    html = "&amp;\n"
    haml = ":escaped\n  &"
    assert_equal(html, render(haml))
  end
end

class RubyFilterTest < Haml::TestCase
  test "can write to haml_io" do; skip
    haml = ":ruby\n  haml_io.puts 'hello'\n"
    html = "hello\n"
    assert_equal(html, render(haml))
  end

  test "haml_io appends to output" do; skip
    haml = "hello\n:ruby\n  haml_io.puts 'hello'\n"
    html = "hello\nhello\n"
    assert_equal(html, render(haml))
  end

  test "can create local variables" do; skip
    haml = ":ruby\n  a = 7\n=a"
    html = "7\n"
    assert_equal(html, render(haml))
  end
end