From 052d4a04a160faca4ed4e276b132219d35b6d008 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sat, 7 Nov 2015 17:51:17 +0900 Subject: [PATCH] Create helper to assert compatibility --- Gemfile | 3 ++ hamlit.gemspec | 2 +- lib/hamlit/parser.rb | 2 +- test/hamlit/engine/attributes_test.rb | 42 ++++++++------------------- test/test_helper.rb | 16 ++++++++-- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/Gemfile b/Gemfile index a02aea72..520c052b 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/hamlit.gemspec b/hamlit.gemspec index 77d96091..9fe4a2ab 100644 --- a/hamlit.gemspec +++ b/hamlit.gemspec @@ -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' diff --git a/lib/hamlit/parser.rb b/lib/hamlit/parser.rb index a429e242..7fbe54d2 100644 --- a/lib/hamlit/parser.rb +++ b/lib/hamlit/parser.rb @@ -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 diff --git a/test/hamlit/engine/attributes_test.rb b/test/hamlit/engine/attributes_test.rb index e59d9c60..8aad34f3 100644 --- a/test/hamlit/engine/attributes_test.rb +++ b/test/hamlit/engine/attributes_test.rb @@ -1,17 +1,17 @@ describe Hamlit::Engine do include RenderAssertion - it { assert_inline(%Q[%div], %Q[
]) } - it { assert_inline(%Q[.bar.foo], %Q[
]) } - it { assert_inline(%Q[.foo.bar], %Q[
]) } - it { assert_inline(%Q[%div(class='bar foo')], %Q[
]) } - it { assert_inline(%Q[%div(class='foo bar')], %Q[
]) } - it { assert_inline(%Q[%div{ class: 'bar foo' }], %Q[
]) } - it { assert_inline(%q[%a{ href: "'\"" }], %Q[]) } - it { assert_inline(%Q[%a{ href: '/search?foo=bar&hoge=' }], %Q[]) } + 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=' }|) } 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 -
-
-
-
-
-
-
-
-
-
-
-
- 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 - - - - - - - HTML end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 5faf1a04..bcadc90c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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