1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Use minitest DSL

This commit is contained in:
Takashi Kokubun 2015-10-24 21:43:34 +09:00
parent 26942e24c2
commit 4fd8ab6152
6 changed files with 262 additions and 229 deletions

View file

@ -1,54 +1,58 @@
class Hamlit::CommentTest < Haml::TestCase
test 'renders html comment' do
assert_render(<<-HAML, <<-HTML)
/ comments
HAML
<!-- comments -->
HTML
end
describe Hamlit::Engine do
include RenderAssertion
test 'strips html comment ignoring around spcaes' do
assert_render('/ comments ', <<-HTML)
<!-- comments -->
HTML
end
describe 'comment' do
it 'renders html comment' do
assert_render(<<-HAML, <<-HTML)
/ comments
HAML
<!-- comments -->
HTML
end
test 'accepts backslash-only line in a comment' do
assert_render(<<-'HAML', <<-HTML)
/
\
HAML
<!--
it 'strips html comment ignoring around spcaes' do
assert_render('/ comments ', <<-HTML)
<!-- comments -->
HTML
end
-->
HTML
end
it 'accepts backslash-only line in a comment' do
assert_render(<<-'HAML', <<-HTML)
/
\
HAML
<!--
test 'renders a deeply indented comment starting with backslash' do
assert_render(<<-'HAML', <<-HTML)
/
\ a
/
-->
HTML
end
it 'renders a deeply indented comment starting with backslash' do
assert_render(<<-'HAML', <<-HTML)
/
\ a
/
a
HAML
<!--
a
-->
<!--
a
HAML
<!--
a
-->
<!--
a
-->
HTML
end
-->
HTML
end
test 'ignores multiline comment' do
assert_render(<<-'HAML', <<-HTML)
-# if true
- raise 'ng'
= invalid script
too deep indent
ok
HAML
ok
HTML
it 'ignores multiline comment' do
assert_render(<<-'HAML', <<-HTML)
-# if true
- raise 'ng'
= invalid script
too deep indent
ok
HAML
ok
HTML
end
end
end

View file

@ -1,17 +1,21 @@
class Hamlit::DoctypeTest < Haml::TestCase
test 'renders html5 doctype' do
assert_render(<<-HAML, <<-HTML)
!!!
HAML
<!DOCTYPE html>
HTML
end
describe Hamlit::Engine do
include RenderAssertion
test 'renders xml doctype' do
assert_render(<<-HAML, <<-HTML, format: :xhtml)
!!! XML
HAML
<?xml version='1.0' encoding='utf-8' ?>
HTML
describe 'doctype' do
it 'renders html5 doctype' do
assert_render(<<-HAML, <<-HTML)
!!!
HAML
<!DOCTYPE html>
HTML
end
it 'renders xml doctype' do
assert_render(<<-HAML, <<-HTML, format: :xhtml)
!!! XML
HAML
<?xml version='1.0' encoding='utf-8' ?>
HTML
end
end
end

View file

@ -1,40 +1,44 @@
class Hamlit::IndentTest < Haml::TestCase
test 'accepts tab indentation' do
assert_render(<<-HAML, <<-HTML)
%p
\t%a
HAML
<p>
<a></a>
</p>
HTML
end
describe Hamlit::Engine do
include RenderAssertion
test 'accepts N-space indentation' do
assert_render(<<-HAML, <<-HTML)
%p
%span
foo
HAML
<p>
<span>
foo
</span>
</p>
HTML
end
describe 'tab indent' do
it 'accepts tab indentation' do
assert_render(<<-HAML, <<-HTML)
%p
\t%a
HAML
<p>
<a></a>
</p>
HTML
end
test 'accepts N-tab indentation' do
assert_render(<<-HAML, <<-HTML)
%p
\t%span
\t\tfoo
HAML
<p>
<span>
foo
</span>
</p>
HTML
it 'accepts N-space indentation' do
assert_render(<<-HAML, <<-HTML)
%p
%span
foo
HAML
<p>
<span>
foo
</span>
</p>
HTML
end
it 'accepts N-tab indentation' do
assert_render(<<-HAML, <<-HTML)
%p
\t%span
\t\tfoo
HAML
<p>
<span>
foo
</span>
</p>
HTML
end
end
end

View file

@ -1,42 +1,46 @@
class Hamlit::MultilineTest < Haml::TestCase
test 'joins multi-lines ending with pipe' do
assert_render(<<-HAML, <<-HTML)
a |
b |
HAML
a b
HTML
end
describe Hamlit::Engine do
include RenderAssertion
test 'renders multi lines' do
assert_render(<<-HAML, <<-HTML)
= 'a' + |
'b' + |
'c' |
'd'
HAML
abc
'd'
HTML
end
describe 'multiline' do
it 'joins multi-lines ending with pipe' do
assert_render(<<-HAML, <<-HTML)
a |
b |
HAML
a b
HTML
end
test 'accepts invalid indent' do
assert_render(<<-HAML, <<-HTML)
%span
%div
= '1' + |
'2' |
%div
3
HAML
<span>
<div>
12
</div>
<div>
3
</div>
</span>
HTML
it 'renders multi lines' do
assert_render(<<-HAML, <<-HTML)
= 'a' + |
'b' + |
'c' |
'd'
HAML
abc
'd'
HTML
end
it 'accepts invalid indent' do
assert_render(<<-HAML, <<-HTML)
%span
%div
= '1' + |
'2' |
%div
3
HAML
<span>
<div>
12
</div>
<div>
3
</div>
</span>
HTML
end
end
end

View file

@ -1,104 +1,114 @@
class Hamlit::NewAttributeTest < Haml::TestCase
test 'renders attributes' do
assert_render(<<-HAML, <<-HTML)
%p(class='foo') bar
HAML
<p class='foo'>bar</p>
HTML
end
describe Hamlit::Engine do
include RenderAssertion
test 'renders multiple attributes' do
assert_render(<<-HAML, <<-HTML)
%p(a=1 b=2) bar
HAML
<p a='1' b='2'>bar</p>
HTML
end
describe 'new attributes' do
it 'renders attributes' do
assert_render(<<-HAML, <<-HTML)
%p(class='foo') bar
HAML
<p class='foo'>bar</p>
HTML
end
test 'renders multi-line attributes properly' do
skip
assert_render(<<-HAML, <<-HTML)
%span(a=__LINE__
b=__LINE__)
= __LINE__
HAML
<span a='1' b='2'></span>
3
HTML
end
it 'renders multiple attributes' do
assert_render(<<-HAML, <<-HTML)
%p(a=1 b=2) bar
HAML
<p a='1' b='2'>bar</p>
HTML
end
test 'renders hyphenated attributes properly' do
assert_render(<<-HAML, <<-HTML)
%p(data-foo='bar') bar
HAML
<p data-foo='bar'>bar</p>
HTML
end
it 'renders multi-line attributes properly' do
skip
assert_render(<<-HAML, <<-HTML)
%span(a=__LINE__
b=__LINE__)
= __LINE__
HAML
<span a='1' b='2'></span>
3
HTML
end
test 'renders multiply hyphenated attributes properly' do
assert_render(<<-HAML, <<-HTML)
%p(data-x-foo='bar') bar
HAML
<p data-x-foo='bar'>bar</p>
HTML
end
it 'renders hyphenated attributes properly' do
assert_render(<<-HAML, <<-HTML)
%p(data-foo='bar') bar
HAML
<p data-foo='bar'>bar</p>
HTML
end
test 'escapes attribute values on static attributes' do
skip
assert_render(<<-'HAML', <<-HTML)
%a(title="'")
%a(title = "'\"")
%a(href='/search?foo=bar&hoge=<fuga>')
HAML
<a title='&#39;'></a>
<a title='&#39;&quot;'></a>
<a href='/search?foo=bar&amp;hoge=&lt;fuga&gt;'></a>
HTML
end
it 'renders multiply hyphenated attributes properly' do
assert_render(<<-HAML, <<-HTML)
%p(data-x-foo='bar') bar
HAML
<p data-x-foo='bar'>bar</p>
HTML
end
test 'escapes attribute values on dynamic attributes' do
assert_render(<<-'HAML', <<-HTML)
- title = "'\""
- href = '/search?foo=bar&hoge=<fuga>'
%a(title=title)
%a(href=href)
HAML
<a title='&#39;&quot;'></a>
<a href='/search?foo=bar&amp;hoge=&lt;fuga&gt;'></a>
HTML
end
describe 'html escape' do
it 'escapes attribute values on static attributes' do
skip
assert_render(<<-'HAML', <<-HTML)
%a(title="'")
%a(title = "'\"")
%a(href='/search?foo=bar&hoge=<fuga>')
HAML
<a title='&#39;'></a>
<a title='&#39;&quot;'></a>
<a href='/search?foo=bar&amp;hoge=&lt;fuga&gt;'></a>
HTML
end
test 'does not generate double classes' do
assert_render(<<-HAML, <<-HTML)
.item(class='first')
HAML
<div class='first item'></div>
HTML
end
it 'escapes attribute values on dynamic attributes' do
assert_render(<<-'HAML', <<-HTML)
- title = "'\""
- href = '/search?foo=bar&hoge=<fuga>'
%a(title=title)
%a(href=href)
HAML
<a title='&#39;&quot;'></a>
<a href='/search?foo=bar&amp;hoge=&lt;fuga&gt;'></a>
HTML
end
end
test 'does not generate double classes for a variable' do
assert_render(<<-HAML, <<-HTML)
- val = 'val'
.element(class=val)
HAML
<div class='element val'></div>
HTML
end
describe 'element class with attribute class' do
it 'does not generate double classes' do
assert_render(<<-HAML, <<-HTML)
.item(class='first')
HAML
<div class='first item'></div>
HTML
end
test 'concatenates ids with underscore' do
assert_render(<<-HAML, <<-HTML)
#item(id='first')
HAML
<div id='item_first'></div>
HTML
end
it 'does not generate double classes for a variable' do
assert_render(<<-HAML, <<-HTML)
- val = 'val'
.element(class=val)
HAML
<div class='element val'></div>
HTML
end
end
test 'concatenates ids with underscore for a variable' do
assert_render(<<-HAML, <<-HTML)
- val = 'first'
#item(id=val)
HAML
<div id='item_first'></div>
HTML
describe 'element id with attribute id' do
it 'concatenates ids with underscore' do
assert_render(<<-HAML, <<-HTML)
#item(id='first')
HAML
<div id='item_first'></div>
HTML
end
it 'concatenates ids with underscore for a variable' do
assert_render(<<-HAML, <<-HTML)
- val = 'first'
#item(id=val)
HAML
<div id='item_first'></div>
HTML
end
end
end
end

View file

@ -20,21 +20,28 @@ BASE_TEST_CLASS = if defined?(Minitest::Test)
module Declarative
def test(name, &block)
define_method("test_hamlit #{name}", &block)
define_method("test_ #{name}", &block)
end
end
module HamlitTest
module RenderAssertion
def assert_render(haml, html, options = {})
options = { escape_html: true, ugly: true}.merge(options)
haml, html = haml.unindent, html.unindent
assert_equal render(haml, options), html
end
def render(text, options = {}, base = nil, &block)
scope = options.delete(:scope) || Object.new
locals = options.delete(:locals) || {}
engine = Hamlit::HamlEngine.new(text, options)
return engine.to_html(base) if base
engine.to_html(scope, locals, &block)
end
end
class Haml::TestCase < BASE_TEST_CLASS
extend Declarative
include HamlitTest
def render(text, options = {}, base = nil, &block)
scope = options.delete(:scope) || Object.new