From 4fd8ab61522c06afc54cfd4267670edbd4fe5fe2 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sat, 24 Oct 2015 21:43:34 +0900 Subject: [PATCH] Use minitest DSL --- test/hamlit/engine/comment_test.rb | 96 +++++------ test/hamlit/engine/doctype_test.rb | 32 ++-- test/hamlit/engine/indent_test.rb | 76 ++++----- test/hamlit/engine/multiline_test.rb | 80 +++++----- test/hamlit/engine/new_attribute_test.rb | 194 ++++++++++++----------- test/test_helper.rb | 13 +- 6 files changed, 262 insertions(+), 229 deletions(-) diff --git a/test/hamlit/engine/comment_test.rb b/test/hamlit/engine/comment_test.rb index 0959ea5f..b66cff78 100644 --- a/test/hamlit/engine/comment_test.rb +++ b/test/hamlit/engine/comment_test.rb @@ -1,54 +1,58 @@ -class Hamlit::CommentTest < Haml::TestCase - test 'renders html comment' do - assert_render(<<-HAML, <<-HTML) - / comments - HAML - - HTML - end +describe Hamlit::Engine do + include RenderAssertion - test 'strips html comment ignoring around spcaes' do - assert_render('/ comments ', <<-HTML) - - HTML - end + describe 'comment' do + it 'renders html comment' do + assert_render(<<-HAML, <<-HTML) + / comments + HAML + + HTML + end - test 'accepts backslash-only line in a comment' do - assert_render(<<-'HAML', <<-HTML) - / - \ - HAML - + HTML + end - --> - HTML - end + it 'accepts backslash-only line in a comment' do + assert_render(<<-'HAML', <<-HTML) + / + \ + HAML + + HTML + end + + it 'renders a deeply indented comment starting with backslash' do + assert_render(<<-'HAML', <<-HTML) + / + \ a + / + a + HAML + + - - 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 diff --git a/test/hamlit/engine/doctype_test.rb b/test/hamlit/engine/doctype_test.rb index f4953d75..475fc406 100644 --- a/test/hamlit/engine/doctype_test.rb +++ b/test/hamlit/engine/doctype_test.rb @@ -1,17 +1,21 @@ -class Hamlit::DoctypeTest < Haml::TestCase - test 'renders html5 doctype' do - assert_render(<<-HAML, <<-HTML) - !!! - HAML - - HTML - end +describe Hamlit::Engine do + include RenderAssertion - test 'renders xml doctype' do - assert_render(<<-HAML, <<-HTML, format: :xhtml) - !!! XML - HAML - - HTML + describe 'doctype' do + it 'renders html5 doctype' do + assert_render(<<-HAML, <<-HTML) + !!! + HAML + + HTML + end + + it 'renders xml doctype' do + assert_render(<<-HAML, <<-HTML, format: :xhtml) + !!! XML + HAML + + HTML + end end end diff --git a/test/hamlit/engine/indent_test.rb b/test/hamlit/engine/indent_test.rb index d15e9eaf..57123f95 100644 --- a/test/hamlit/engine/indent_test.rb +++ b/test/hamlit/engine/indent_test.rb @@ -1,40 +1,44 @@ -class Hamlit::IndentTest < Haml::TestCase - test 'accepts tab indentation' do - assert_render(<<-HAML, <<-HTML) - %p - \t%a - HAML -

- -

- HTML - end +describe Hamlit::Engine do + include RenderAssertion - test 'accepts N-space indentation' do - assert_render(<<-HAML, <<-HTML) - %p - %span - foo - HAML -

- - foo - -

- HTML - end + describe 'tab indent' do + it 'accepts tab indentation' do + assert_render(<<-HAML, <<-HTML) + %p + \t%a + HAML +

+ +

+ HTML + end - test 'accepts N-tab indentation' do - assert_render(<<-HAML, <<-HTML) - %p - \t%span - \t\tfoo - HAML -

- - foo - -

- HTML + it 'accepts N-space indentation' do + assert_render(<<-HAML, <<-HTML) + %p + %span + foo + HAML +

+ + foo + +

+ HTML + end + + it 'accepts N-tab indentation' do + assert_render(<<-HAML, <<-HTML) + %p + \t%span + \t\tfoo + HAML +

+ + foo + +

+ HTML + end end end diff --git a/test/hamlit/engine/multiline_test.rb b/test/hamlit/engine/multiline_test.rb index 1676cd7b..9c871c1f 100644 --- a/test/hamlit/engine/multiline_test.rb +++ b/test/hamlit/engine/multiline_test.rb @@ -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 - -
- 12 -
-
- 3 -
-
- 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 + +
+ 12 +
+
+ 3 +
+
+ HTML + end end end diff --git a/test/hamlit/engine/new_attribute_test.rb b/test/hamlit/engine/new_attribute_test.rb index 5eb085ae..f0ef11c5 100644 --- a/test/hamlit/engine/new_attribute_test.rb +++ b/test/hamlit/engine/new_attribute_test.rb @@ -1,104 +1,114 @@ -class Hamlit::NewAttributeTest < Haml::TestCase - test 'renders attributes' do - assert_render(<<-HAML, <<-HTML) - %p(class='foo') bar - HAML -

bar

- HTML - end +describe Hamlit::Engine do + include RenderAssertion - test 'renders multiple attributes' do - assert_render(<<-HAML, <<-HTML) - %p(a=1 b=2) bar - HAML -

bar

- HTML - end + describe 'new attributes' do + it 'renders attributes' do + assert_render(<<-HAML, <<-HTML) + %p(class='foo') bar + HAML +

bar

+ HTML + end - test 'renders multi-line attributes properly' do - skip - assert_render(<<-HAML, <<-HTML) - %span(a=__LINE__ - b=__LINE__) - = __LINE__ - HAML - - 3 - HTML - end + it 'renders multiple attributes' do + assert_render(<<-HAML, <<-HTML) + %p(a=1 b=2) bar + HAML +

bar

+ HTML + end - test 'renders hyphenated attributes properly' do - assert_render(<<-HAML, <<-HTML) - %p(data-foo='bar') bar - HAML -

bar

- HTML - end + it 'renders multi-line attributes properly' do + skip + assert_render(<<-HAML, <<-HTML) + %span(a=__LINE__ + b=__LINE__) + = __LINE__ + HAML + + 3 + HTML + end - test 'renders multiply hyphenated attributes properly' do - assert_render(<<-HAML, <<-HTML) - %p(data-x-foo='bar') bar - HAML -

bar

- HTML - end + it 'renders hyphenated attributes properly' do + assert_render(<<-HAML, <<-HTML) + %p(data-foo='bar') bar + HAML +

bar

+ 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=') - HAML - - - - HTML - end + it 'renders multiply hyphenated attributes properly' do + assert_render(<<-HAML, <<-HTML) + %p(data-x-foo='bar') bar + HAML +

bar

+ HTML + end - test 'escapes attribute values on dynamic attributes' do - assert_render(<<-'HAML', <<-HTML) - - title = "'\"" - - href = '/search?foo=bar&hoge=' - %a(title=title) - %a(href=href) - HAML - - - 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=') + HAML + + + + HTML + end - test 'does not generate double classes' do - assert_render(<<-HAML, <<-HTML) - .item(class='first') - HAML -
- HTML - end + it 'escapes attribute values on dynamic attributes' do + assert_render(<<-'HAML', <<-HTML) + - title = "'\"" + - href = '/search?foo=bar&hoge=' + %a(title=title) + %a(href=href) + HAML + + + HTML + end + end - test 'does not generate double classes for a variable' do - assert_render(<<-HAML, <<-HTML) - - val = 'val' - .element(class=val) - HAML -
- HTML - end + describe 'element class with attribute class' do + it 'does not generate double classes' do + assert_render(<<-HAML, <<-HTML) + .item(class='first') + HAML +
+ HTML + end - test 'concatenates ids with underscore' do - assert_render(<<-HAML, <<-HTML) - #item(id='first') - HAML -
- HTML - end + it 'does not generate double classes for a variable' do + assert_render(<<-HAML, <<-HTML) + - val = 'val' + .element(class=val) + HAML +
+ HTML + end + end - test 'concatenates ids with underscore for a variable' do - assert_render(<<-HAML, <<-HTML) - - val = 'first' - #item(id=val) - HAML -
- HTML + describe 'element id with attribute id' do + it 'concatenates ids with underscore' do + assert_render(<<-HAML, <<-HTML) + #item(id='first') + HAML +
+ HTML + end + + it 'concatenates ids with underscore for a variable' do + assert_render(<<-HAML, <<-HTML) + - val = 'first' + #item(id=val) + HAML +
+ HTML + end + end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 114a5231..42a49251 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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