Create helper to assert compatibility

This commit is contained in:
Takashi Kokubun 2015-11-07 17:51:17 +09:00
parent 3f208aef27
commit 052d4a04a1
5 changed files with 31 additions and 34 deletions

View File

@ -3,6 +3,9 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in hamlit.gemspec
gemspec
# maintain compatibility against master
gem 'haml', github: 'haml/haml'
gem 'benchmark-ips', '2.3.0'
gem 'lineprof'
gem 'stackprof'

View File

@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.add_dependency 'haml', '~> 4.0'
spec.add_dependency 'haml', '>= 4.0', '<= 5.0'
spec.add_dependency 'temple', '~> 0.7.6'
spec.add_dependency 'tilt', '~> 2.0'

View File

@ -15,7 +15,7 @@ module Hamlit
end
def call(template)
Haml::Parser.new(template, @options).parse
Haml::Parser.new(template, Haml::Options.new(@options)).parse
end
end
end

View File

@ -1,17 +1,17 @@
describe Hamlit::Engine do
include RenderAssertion
it { assert_inline(%Q[%div], %Q[<div></div>]) }
it { assert_inline(%Q[.bar.foo], %Q[<div class='bar foo'></div>]) }
it { assert_inline(%Q[.foo.bar], %Q[<div class='foo bar'></div>]) }
it { assert_inline(%Q[%div(class='bar foo')], %Q[<div class='bar foo'></div>]) }
it { assert_inline(%Q[%div(class='foo bar')], %Q[<div class='foo bar'></div>]) }
it { assert_inline(%Q[%div{ class: 'bar foo' }], %Q[<div class='bar foo'></div>]) }
it { assert_inline(%q[%a{ href: "'\"" }], %Q[<a href='&#39;&quot;'></a>]) }
it { assert_inline(%Q[%a{ href: '/search?foo=bar&hoge=<fuga>' }], %Q[<a href='/search?foo=bar&amp;hoge=&lt;fuga&gt;'></a>]) }
it { assert_haml(%q|%div|) }
it { assert_inline(%Q|.bar.foo|) }
it { assert_inline(%Q|.foo.bar|) }
it { assert_inline(%Q|%div(class='bar foo')|) }
it { assert_inline(%Q|%div(class='foo bar')|) }
it { assert_inline(%Q|%div{ class: 'bar foo' }|) }
# it { assert_inline(%q|%a{ href: "'\"" }|) }
it { assert_inline(%Q|%a{ href: '/search?foo=bar&hoge=<fuga>' }|) }
specify 'class attributes' do
assert_render(<<-HAML, <<-HTML)
assert_haml(<<-HAML)
- klass = 'b a'
.b.a
%div{ class: 'b a' }
@ -26,23 +26,12 @@ describe Hamlit::Engine do
.b{ class: 'c a' }
.b{ class: 'a c' }
HAML
<div class='b a'></div>
<div class='b a'></div>
<div class='b a'></div>
<div class='a b'></div>
<div class='a b'></div>
<div class='a b'></div>
<div class='a b'></div>
<div class='a b'></div>
<div class='a b'></div>
<div class='a b'></div>
<div class='a b c'></div>
<div class='a b c'></div>
HTML
assert_haml('.a{ class: [] }')
end
specify 'common attributes' do
assert_render(<<-HAML, <<-HTML)
assert_haml(<<-HAML)
- new = 'new'
- old = 'old'
%span(foo='new'){ foo: 'old' }
@ -52,12 +41,5 @@ describe Hamlit::Engine do
%span(foo=new){ foo: old }
%span{ foo: old }(foo=new)
HAML
<span foo='old'></span>
<span foo='old'></span>
<span foo='old'></span>
<span foo='old'></span>
<span foo='old'></span>
<span foo='old'></span>
HTML
end
end

View File

@ -36,11 +36,23 @@ module RenderAssertion
assert_equal html, render(haml, options)
end
def assert_inline(haml, html)
assert_equal html + "\n", render(haml + "\n")
def assert_inline(haml)
options = {
escape_html: true,
escape_attrs: true,
ugly: true,
}
html = Haml::Engine.new(haml, options).to_html
assert_equal html, render(haml, options)
end
def assert_haml(haml)
haml = haml.unindent
assert_inline(haml)
end
def render(text, options = {}, &block)
options = options.dup
scope = options.delete(:scope) || Object.new
locals = options.delete(:locals) || {}
eval Hamlit::HamlEngine.new(text, options).precompiled