2006-10-14 19:50:07 -04:00
|
|
|
#!/usr/bin/env ruby
|
2008-06-08 12:54:36 -04:00
|
|
|
require File.dirname(__FILE__) + '/../test_helper'
|
2006-08-06 00:25:31 -04:00
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
class EngineTest < Test::Unit::TestCase
|
2008-04-24 16:47:59 -04:00
|
|
|
# A map of erroneous Sass documents to the error messages they should produce.
|
|
|
|
# The error messages may be arrays;
|
|
|
|
# if so, the second element should be the line number that should be reported for the error.
|
|
|
|
# If this isn't provided, the tests will assume the line number should be the last line of the document.
|
2008-04-18 14:08:49 -04:00
|
|
|
EXCEPTION_MAP = {
|
2008-04-18 14:43:29 -04:00
|
|
|
"!!!\n a" => "Illegal nesting: nesting within a header command is illegal.",
|
|
|
|
"a\n b" => "Illegal nesting: nesting within plain text is illegal.",
|
|
|
|
"/ a\n b" => "Illegal nesting: nesting within a tag that already has content is illegal.",
|
|
|
|
"% a" => 'Invalid tag: "% a".',
|
|
|
|
"%p a\n b" => "Illegal nesting: content can't be both given on the same line as %p and nested within it.",
|
2008-04-18 15:25:00 -04:00
|
|
|
"%p=" => "There's no Ruby code for = to evaluate.",
|
|
|
|
"%p~" => "There's no Ruby code for ~ to evaluate.",
|
|
|
|
"~" => "There's no Ruby code for ~ to evaluate.",
|
|
|
|
"=" => "There's no Ruby code for = to evaluate.",
|
2008-04-18 15:54:15 -04:00
|
|
|
"%p/\n a" => "Illegal nesting: nesting within a self-closing tag is illegal.",
|
2008-04-18 15:13:35 -04:00
|
|
|
"%p\n\ta" => <<END.strip,
|
|
|
|
A tab character was used for indentation. Haml must be indented using two spaces.
|
|
|
|
Are you sure you have soft tabs enabled in your editor?
|
|
|
|
END
|
|
|
|
"%p\n a" => "1 space was used for indentation. Haml must be indented using two spaces.",
|
|
|
|
"%p\n a" => "3 spaces were used for indentation. Haml must be indented using two spaces.",
|
|
|
|
"%p\n a" => "4 spaces were used for indentation. Haml must be indented using two spaces.",
|
2008-04-24 16:47:59 -04:00
|
|
|
":a\n b" => ['Filter "a" is not defined.', 1],
|
2008-04-18 14:43:29 -04:00
|
|
|
":a= b" => 'Invalid filter name ":a= b".',
|
2008-04-18 14:08:49 -04:00
|
|
|
"." => "Illegal element: classes and ids must have values.",
|
|
|
|
".#" => "Illegal element: classes and ids must have values.",
|
|
|
|
".{} a" => "Illegal element: classes and ids must have values.",
|
|
|
|
".= a" => "Illegal element: classes and ids must have values.",
|
|
|
|
"%p..a" => "Illegal element: classes and ids must have values.",
|
2008-04-18 15:54:15 -04:00
|
|
|
"%a/ b" => "Self-closing tags can't have content.",
|
2008-05-29 22:07:12 -04:00
|
|
|
" %p foo" => "Indenting at the beginning of the document is illegal.",
|
|
|
|
" %p foo" => "Indenting at the beginning of the document is illegal.",
|
2008-07-14 21:10:52 -04:00
|
|
|
"- end" => <<END.rstrip,
|
|
|
|
You don't need to use "- end" in Haml. Use indentation instead:
|
|
|
|
- if foo?
|
|
|
|
%strong Foo!
|
|
|
|
- else
|
|
|
|
Not foo.
|
|
|
|
END
|
2008-06-01 00:35:07 -04:00
|
|
|
" \n\t\n %p foo" => ["Indenting at the beginning of the document is illegal.", 3],
|
2008-04-29 00:28:01 -04:00
|
|
|
|
|
|
|
# Regression tests
|
|
|
|
"- raise 'foo'\n\n\n\nbar" => ["foo", 1],
|
2008-05-02 03:48:39 -04:00
|
|
|
"= 'foo'\n-raise 'foo'" => ["foo", 2],
|
|
|
|
"\n\n\n- raise 'foo'" => ["foo", 4],
|
2008-05-29 04:30:29 -04:00
|
|
|
"foo\n\n\n bar" => ["Illegal nesting: nesting within plain text is illegal.", 4],
|
|
|
|
"%p/\n\n bar" => ["Illegal nesting: nesting within a self-closing tag is illegal.", 3],
|
|
|
|
"%p foo\n\n bar" => ["Illegal nesting: content can't be both given on the same line as %p and nested within it.", 3],
|
|
|
|
"/ foo\n\n bar" => ["Illegal nesting: nesting within a tag that already has content is illegal.", 3],
|
|
|
|
"!!!\n\n bar" => ["Illegal nesting: nesting within a header command is illegal.", 3],
|
|
|
|
"foo\n\n\n\tbar" => [<<END.strip, 4],
|
|
|
|
A tab character was used for indentation. Haml must be indented using two spaces.
|
|
|
|
Are you sure you have soft tabs enabled in your editor?
|
|
|
|
END
|
2008-07-15 23:35:08 -04:00
|
|
|
"foo\n:ruby\n 1\n 2\n 3\n- raise 'foo'" => ["foo", 6],
|
2008-04-18 14:08:49 -04:00
|
|
|
}
|
2006-08-05 23:18:54 -04:00
|
|
|
|
2008-04-28 00:14:12 -04:00
|
|
|
User = Struct.new('User', :id)
|
|
|
|
|
2007-11-23 07:14:02 -05:00
|
|
|
def render(text, options = {}, &block)
|
2007-11-24 03:35:10 -05:00
|
|
|
scope = options.delete(:scope) || Object.new
|
|
|
|
locals = options.delete(:locals) || {}
|
|
|
|
Haml::Engine.new(text, options).to_html(scope, locals, &block)
|
2006-08-05 23:18:54 -04:00
|
|
|
end
|
2006-11-04 03:35:06 -05:00
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
def test_empty_render_should_remain_empty
|
|
|
|
assert_equal('', render(''))
|
2006-08-05 23:18:54 -04:00
|
|
|
end
|
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
def test_attributes_should_render_correctly
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal("<div class='atlantis' style='ugly'></div>", render(".atlantis{:style => 'ugly'}").chomp)
|
2006-08-05 23:18:54 -04:00
|
|
|
end
|
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
def test_ruby_code_should_work_inside_attributes
|
|
|
|
author = 'hcatlin'
|
|
|
|
assert_equal("<p class='3'>foo</p>", render("%p{:class => 1+2} foo").chomp)
|
2006-08-05 23:18:54 -04:00
|
|
|
end
|
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
def test_nil_should_render_empty_tag
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal("<div class='no_attributes'></div>",
|
2006-09-29 14:39:13 -04:00
|
|
|
render(".no_attributes{:nil => nil}").chomp)
|
2006-08-05 23:18:54 -04:00
|
|
|
end
|
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
def test_strings_should_get_stripped_inside_tags
|
|
|
|
assert_equal("<div class='stripped'>This should have no spaces in front of it</div>",
|
|
|
|
render(".stripped This should have no spaces in front of it").chomp)
|
2006-08-09 14:12:54 -04:00
|
|
|
end
|
2006-11-04 03:35:06 -05:00
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
def test_one_liner_should_be_one_line
|
|
|
|
assert_equal("<p>Hello</p>", render('%p Hello').chomp)
|
2006-08-13 18:02:04 -04:00
|
|
|
end
|
2006-08-13 18:10:05 -04:00
|
|
|
|
2008-04-24 22:14:52 -04:00
|
|
|
def test_one_liner_with_newline_shouldnt_be_one_line
|
|
|
|
assert_equal("<p>\n foo\n bar\n</p>", render('%p= "foo\nbar"').chomp)
|
2007-07-16 23:48:11 -04:00
|
|
|
end
|
2006-11-04 03:35:06 -05:00
|
|
|
|
2006-10-27 16:48:40 -04:00
|
|
|
def test_multi_render
|
|
|
|
engine = Haml::Engine.new("%strong Hi there!")
|
|
|
|
assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
|
|
|
|
assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
|
|
|
|
assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
|
|
|
|
end
|
2006-11-04 03:35:06 -05:00
|
|
|
|
2007-05-07 14:16:24 -04:00
|
|
|
def test_double_equals
|
|
|
|
assert_equal("<p>Hello World</p>\n", render('%p== Hello #{who}', :locals => {:who => 'World'}))
|
|
|
|
assert_equal("<p>\n Hello World\n</p>\n", render("%p\n == Hello \#{who}", :locals => {:who => 'World'}))
|
|
|
|
end
|
|
|
|
|
2007-08-11 15:10:28 -04:00
|
|
|
def test_double_equals_in_the_middle_of_a_string
|
|
|
|
assert_equal("\"title 'Title'. \"\n",
|
|
|
|
render("== \"title '\#{\"Title\"}'. \""))
|
|
|
|
end
|
|
|
|
|
2007-08-16 17:49:53 -04:00
|
|
|
def test_nil_tag_value_should_render_as_empty
|
|
|
|
assert_equal("<p></p>\n", render("%p= nil"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tag_with_failed_if_should_render_as_empty
|
|
|
|
assert_equal("<p></p>\n", render("%p= 'Hello' if false"))
|
|
|
|
end
|
|
|
|
|
2007-11-22 00:29:26 -05:00
|
|
|
def test_static_attributes_with_empty_attr
|
|
|
|
assert_equal("<img alt='' src='/foo.png' />\n", render("%img{:src => '/foo.png', :alt => ''}"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dynamic_attributes_with_empty_attr
|
|
|
|
assert_equal("<img alt='' src='/foo.png' />\n", render("%img{:width => nil, :src => '/foo.png', :alt => String.new}"))
|
|
|
|
end
|
|
|
|
|
2008-01-09 15:54:36 -05:00
|
|
|
def test_end_of_file_multiline
|
|
|
|
assert_equal("<p>0</p>\n<p>1</p>\n<p>2</p>\n", render("- for i in (0...3)\n %p= |\n i |"))
|
|
|
|
end
|
|
|
|
|
2008-01-31 01:21:24 -05:00
|
|
|
def test_cr_newline
|
|
|
|
assert_equal("<p>foo</p>\n<p>bar</p>\n<p>baz</p>\n<p>boom</p>\n", render("%p foo\r%p bar\r\n%p baz\n\r%p boom"))
|
|
|
|
end
|
|
|
|
|
2008-03-02 19:12:57 -05:00
|
|
|
def test_textareas
|
2008-04-24 15:59:03 -04:00
|
|
|
assert_equal("<textarea>Foo
 bar
 baz</textarea>\n",
|
2008-03-02 19:12:57 -05:00
|
|
|
render('%textarea= "Foo\n bar\n baz"'))
|
|
|
|
|
2008-04-24 15:59:03 -04:00
|
|
|
assert_equal("<pre>Foo
 bar
 baz</pre>\n",
|
2008-03-02 19:24:47 -05:00
|
|
|
render('%pre= "Foo\n bar\n baz"'))
|
|
|
|
|
2008-03-02 19:12:57 -05:00
|
|
|
assert_equal("<textarea>#{'a' * 100}</textarea>\n",
|
|
|
|
render("%textarea #{'a' * 100}"))
|
2008-05-11 04:39:02 -04:00
|
|
|
|
|
|
|
assert_equal("<p>\n <textarea>Foo\n Bar\n Baz</textarea>\n</p>\n", render(<<SOURCE))
|
|
|
|
%p
|
|
|
|
%textarea
|
|
|
|
Foo
|
|
|
|
Bar
|
|
|
|
Baz
|
|
|
|
SOURCE
|
2008-03-02 19:12:57 -05:00
|
|
|
end
|
|
|
|
|
2008-03-02 20:20:43 -05:00
|
|
|
def test_boolean_attributes
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal("<p bar baz='true' foo='bar'></p>\n",
|
2008-03-02 20:20:43 -05:00
|
|
|
render("%p{:foo => 'bar', :bar => true, :baz => 'true'}", :format => :html4))
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal("<p bar='bar' baz='true' foo='bar'></p>\n",
|
2008-03-02 20:20:43 -05:00
|
|
|
render("%p{:foo => 'bar', :bar => true, :baz => 'true'}", :format => :xhtml))
|
|
|
|
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal("<p baz='false' foo='bar'></p>\n",
|
2008-03-02 20:20:43 -05:00
|
|
|
render("%p{:foo => 'bar', :bar => false, :baz => 'false'}", :format => :html4))
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal("<p baz='false' foo='bar'></p>\n",
|
2008-03-02 20:20:43 -05:00
|
|
|
render("%p{:foo => 'bar', :bar => false, :baz => 'false'}", :format => :xhtml))
|
|
|
|
end
|
|
|
|
|
2008-05-10 06:23:47 -04:00
|
|
|
def test_both_whitespace_nukes_work_together
|
|
|
|
assert_equal(<<RESULT, render(<<SOURCE))
|
|
|
|
<p><q>Foo
|
|
|
|
Bar</q></p>
|
|
|
|
RESULT
|
|
|
|
%p
|
|
|
|
%q><= "Foo\\nBar"
|
|
|
|
SOURCE
|
|
|
|
end
|
|
|
|
|
2008-03-14 19:39:19 -04:00
|
|
|
# HTML escaping tests
|
|
|
|
|
2008-03-18 05:19:41 -04:00
|
|
|
def test_ampersand_equals_should_escape
|
2008-03-14 19:39:19 -04:00
|
|
|
assert_equal("<p>\n foo & bar\n</p>\n", render("%p\n &= 'foo & bar'", :escape_html => false))
|
|
|
|
end
|
|
|
|
|
2008-03-18 05:19:41 -04:00
|
|
|
def test_ampersand_equals_inline_should_escape
|
2008-03-14 19:39:19 -04:00
|
|
|
assert_equal("<p>foo & bar</p>\n", render("%p&= 'foo & bar'", :escape_html => false))
|
|
|
|
end
|
|
|
|
|
2008-03-18 05:19:41 -04:00
|
|
|
def test_bang_equals_should_not_escape
|
2008-03-14 19:39:19 -04:00
|
|
|
assert_equal("<p>\n foo & bar\n</p>\n", render("%p\n != 'foo & bar'", :escape_html => true))
|
|
|
|
end
|
|
|
|
|
2008-03-18 05:19:41 -04:00
|
|
|
def test_bang_equals_inline_should_not_escape
|
2008-03-14 19:39:19 -04:00
|
|
|
assert_equal("<p>foo & bar</p>\n", render("%p!= 'foo & bar'", :escape_html => true))
|
|
|
|
end
|
2008-03-18 05:19:41 -04:00
|
|
|
|
|
|
|
def test_static_attributes_should_be_escaped
|
|
|
|
assert_equal("<img class='atlantis' style='ugly&stupid' />\n",
|
2008-07-27 20:44:02 -04:00
|
|
|
render("%img.atlantis{:style => 'ugly&stupid'}"))
|
2008-03-18 05:19:41 -04:00
|
|
|
assert_equal("<div class='atlantis' style='ugly&stupid'>foo</div>\n",
|
2008-07-27 20:44:02 -04:00
|
|
|
render(".atlantis{:style => 'ugly&stupid'} foo"))
|
2008-03-18 05:19:41 -04:00
|
|
|
assert_equal("<p class='atlantis' style='ugly&stupid'>foo</p>\n",
|
2008-07-27 20:44:02 -04:00
|
|
|
render("%p.atlantis{:style => 'ugly&stupid'}= 'foo'"))
|
2008-03-18 05:19:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dynamic_attributes_should_be_escaped
|
|
|
|
assert_equal("<img alt='' src='/foo.png' />\n",
|
2008-07-27 20:44:02 -04:00
|
|
|
render("%img{:width => nil, :src => '/foo.png', :alt => String.new}"))
|
2008-03-18 05:19:41 -04:00
|
|
|
assert_equal("<p alt='' src='/foo.png'>foo</p>\n",
|
2008-07-27 20:44:02 -04:00
|
|
|
render("%p{:width => nil, :src => '/foo.png', :alt => String.new} foo"))
|
2008-03-18 05:19:41 -04:00
|
|
|
assert_equal("<div alt='' src='/foo.png'>foo</div>\n",
|
2008-07-27 20:44:02 -04:00
|
|
|
render("%div{:width => nil, :src => '/foo.png', :alt => String.new}= 'foo'"))
|
2008-03-18 05:19:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_string_interpolation_should_be_esaped
|
|
|
|
assert_equal("<p>4&3</p>\n", render("%p== #{2+2}&#{2+1}", :escape_html => true))
|
|
|
|
assert_equal("<p>4&3</p>\n", render("%p== #{2+2}&#{2+1}", :escape_html => false))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_escaped_inline_string_interpolation
|
|
|
|
assert_equal("<p>4&3</p>\n", render("%p&== #{2+2}&#{2+1}", :escape_html => true))
|
|
|
|
assert_equal("<p>4&3</p>\n", render("%p&== #{2+2}&#{2+1}", :escape_html => false))
|
|
|
|
end
|
2008-03-14 19:39:19 -04:00
|
|
|
|
2008-03-18 05:19:41 -04:00
|
|
|
def test_unescaped_inline_string_interpolation
|
|
|
|
assert_equal("<p>4&3</p>\n", render("%p!== #{2+2}&#{2+1}", :escape_html => true))
|
|
|
|
assert_equal("<p>4&3</p>\n", render("%p!== #{2+2}&#{2+1}", :escape_html => false))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_escaped_string_interpolation
|
|
|
|
assert_equal("<p>\n 4&3\n</p>\n", render("%p\n &== #{2+2}&#{2+1}", :escape_html => true))
|
|
|
|
assert_equal("<p>\n 4&3\n</p>\n", render("%p\n &== #{2+2}&#{2+1}", :escape_html => false))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unescaped_string_interpolation
|
|
|
|
assert_equal("<p>\n 4&3\n</p>\n", render("%p\n !== #{2+2}&#{2+1}", :escape_html => true))
|
|
|
|
assert_equal("<p>\n 4&3\n</p>\n", render("%p\n !== #{2+2}&#{2+1}", :escape_html => false))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_scripts_should_respect_escape_html_option
|
2008-03-14 19:39:19 -04:00
|
|
|
assert_equal("<p>\n foo & bar\n</p>\n", render("%p\n = 'foo & bar'", :escape_html => true))
|
|
|
|
assert_equal("<p>\n foo & bar\n</p>\n", render("%p\n = 'foo & bar'", :escape_html => false))
|
|
|
|
end
|
|
|
|
|
2008-03-18 05:19:41 -04:00
|
|
|
def test_inline_scripts_should_respect_escape_html_option
|
2008-03-14 19:39:19 -04:00
|
|
|
assert_equal("<p>foo & bar</p>\n", render("%p= 'foo & bar'", :escape_html => true))
|
|
|
|
assert_equal("<p>foo & bar</p>\n", render("%p= 'foo & bar'", :escape_html => false))
|
|
|
|
end
|
|
|
|
|
2008-03-18 05:19:41 -04:00
|
|
|
def test_script_ending_in_comment_should_render_when_html_is_escaped
|
|
|
|
assert_equal("foo&bar\n", render("= 'foo&bar' #comment", :escape_html => true))
|
|
|
|
end
|
|
|
|
|
2006-11-04 03:35:06 -05:00
|
|
|
# Options tests
|
|
|
|
|
2008-05-02 02:54:10 -04:00
|
|
|
def test_filename_and_line
|
|
|
|
begin
|
|
|
|
render("\n\n = abc", :filename => 'test', :line => 2)
|
|
|
|
rescue Exception => e
|
|
|
|
assert_kind_of Haml::SyntaxError, e
|
|
|
|
assert_match /test:4/, e.backtrace.first
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
render("\n\n= 123\n\n= nil[]", :filename => 'test', :line => 2)
|
|
|
|
rescue Exception => e
|
|
|
|
assert_kind_of NoMethodError, e
|
|
|
|
assert_match /test:6/, e.backtrace.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-11-04 03:35:06 -05:00
|
|
|
def test_stop_eval
|
|
|
|
assert_equal("", render("= 'Hello'", :suppress_eval => true))
|
2007-04-09 13:03:12 -04:00
|
|
|
assert_equal("", render("- puts 'foo'", :suppress_eval => true))
|
2007-06-02 06:45:24 -04:00
|
|
|
assert_equal("<div id='foo' yes='no' />\n", render("#foo{:yes => 'no'}/", :suppress_eval => true))
|
|
|
|
assert_equal("<div id='foo' />\n", render("#foo{:yes => 'no', :call => a_function() }/", :suppress_eval => true))
|
2007-04-09 13:03:12 -04:00
|
|
|
assert_equal("<div />\n", render("%div[1]/", :suppress_eval => true))
|
2008-05-23 04:16:24 -04:00
|
|
|
assert_equal("", render(":ruby\n puts 'hello'", :suppress_eval => true))
|
2006-11-04 03:35:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_attr_wrapper
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal("<p strange=*attrs*></p>\n", render("%p{ :strange => 'attrs'}", :attr_wrapper => '*'))
|
|
|
|
assert_equal("<p escaped='quo\"te'></p>\n", render("%p{ :escaped => 'quo\"te'}", :attr_wrapper => '"'))
|
|
|
|
assert_equal("<p escaped=\"quo'te\"></p>\n", render("%p{ :escaped => 'quo\\'te'}", :attr_wrapper => '"'))
|
|
|
|
assert_equal("<p escaped=\"q'uo"te\"></p>\n", render("%p{ :escaped => 'q\\'uo\"te'}", :attr_wrapper => '"'))
|
2006-11-07 20:54:26 -05:00
|
|
|
assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n", render("!!! XML", :attr_wrapper => '"'))
|
2006-11-04 03:35:06 -05:00
|
|
|
end
|
|
|
|
|
2007-06-02 06:45:24 -04:00
|
|
|
def test_attrs_parsed_correctly
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal("<p boom=>biddly='bar => baz'></p>\n", render("%p{'boom=>biddly' => 'bar => baz'}"))
|
|
|
|
assert_equal("<p foo,bar='baz, qux'></p>\n", render("%p{'foo,bar' => 'baz, qux'}"))
|
|
|
|
assert_equal("<p escaped='quo\nte'></p>\n", render("%p{ :escaped => \"quo\\nte\"}"))
|
|
|
|
assert_equal("<p escaped='quo4te'></p>\n", render("%p{ :escaped => \"quo\#{2 + 2}te\"}"))
|
2007-06-02 06:45:24 -04:00
|
|
|
end
|
2007-07-24 22:46:43 -04:00
|
|
|
|
2008-02-10 19:45:36 -05:00
|
|
|
def test_correct_parsing_with_brackets
|
|
|
|
assert_equal("<p class='foo'>{tada} foo</p>\n", render("%p{:class => 'foo'} {tada} foo"))
|
|
|
|
assert_equal("<p class='foo'>deep {nested { things }}</p>\n", render("%p{:class => 'foo'} deep {nested { things }}"))
|
|
|
|
assert_equal("<p class='bar foo'>{a { d</p>\n", render("%p{{:class => 'foo'}, :class => 'bar'} {a { d"))
|
|
|
|
assert_equal("<p foo='bar'>a}</p>\n", render("%p{:foo => 'bar'} a}"))
|
|
|
|
|
|
|
|
foo = []
|
|
|
|
foo[0] = Struct.new('Foo', :id).new
|
|
|
|
assert_equal("<p class='struct_foo' id='struct_foo_new'>New User]</p>\n",
|
|
|
|
render("%p[foo[0]] New User]", :locals => {:foo => foo}))
|
2008-04-10 06:37:22 -04:00
|
|
|
assert_equal("<p class='prefix_struct_foo' id='prefix_struct_foo_new'>New User]</p>\n",
|
|
|
|
render("%p[foo[0], :prefix] New User]", :locals => {:foo => foo}))
|
|
|
|
|
|
|
|
foo[0].id = 1
|
|
|
|
assert_equal("<p class='struct_foo' id='struct_foo_1'>New User]</p>\n",
|
|
|
|
render("%p[foo[0]] New User]", :locals => {:foo => foo}))
|
|
|
|
assert_equal("<p class='prefix_struct_foo' id='prefix_struct_foo_1'>New User]</p>\n",
|
|
|
|
render("%p[foo[0], :prefix] New User]", :locals => {:foo => foo}))
|
2008-02-10 19:45:36 -05:00
|
|
|
end
|
|
|
|
|
2007-07-24 22:46:43 -04:00
|
|
|
def test_empty_attrs
|
|
|
|
assert_equal("<p attr=''>empty</p>\n", render("%p{ :attr => '' } empty"))
|
|
|
|
assert_equal("<p attr=''>empty</p>\n", render("%p{ :attr => x } empty", :locals => {:x => ''}))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nil_attrs
|
|
|
|
assert_equal("<p>nil</p>\n", render("%p{ :attr => nil } nil"))
|
|
|
|
assert_equal("<p>nil</p>\n", render("%p{ :attr => x } nil", :locals => {:x => nil}))
|
|
|
|
end
|
2007-06-02 06:45:24 -04:00
|
|
|
|
2008-01-10 03:40:00 -05:00
|
|
|
def test_nil_id_with_syntactic_id
|
|
|
|
assert_equal("<p id='foo'>nil</p>\n", render("%p#foo{:id => nil} nil"))
|
|
|
|
assert_equal("<p id='foo_bar'>nil</p>\n", render("%p#foo{{:id => 'bar'}, :id => nil} nil"))
|
|
|
|
assert_equal("<p id='foo_bar'>nil</p>\n", render("%p#foo{{:id => nil}, :id => 'bar'} nil"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nil_class_with_syntactic_class
|
|
|
|
assert_equal("<p class='foo'>nil</p>\n", render("%p.foo{:class => nil} nil"))
|
|
|
|
assert_equal("<p class='bar foo'>nil</p>\n", render("%p.bar.foo{:class => nil} nil"))
|
|
|
|
assert_equal("<p class='bar foo'>nil</p>\n", render("%p.foo{{:class => 'bar'}, :class => nil} nil"))
|
|
|
|
assert_equal("<p class='bar foo'>nil</p>\n", render("%p.foo{{:class => nil}, :class => 'bar'} nil"))
|
|
|
|
end
|
|
|
|
|
2006-11-04 03:35:06 -05:00
|
|
|
def test_locals
|
|
|
|
assert_equal("<p>Paragraph!</p>\n", render("%p= text", :locals => { :text => "Paragraph!" }))
|
|
|
|
end
|
2007-05-10 04:12:20 -04:00
|
|
|
|
2007-08-26 13:12:22 -04:00
|
|
|
def test_dynamic_attrs_shouldnt_register_as_literal_values
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal("<p a='b2c'></p>\n", render('%p{:a => "b#{1 + 1}c"}'))
|
|
|
|
assert_equal("<p a='b2c'></p>\n", render("%p{:a => 'b' + (1 + 1).to_s + 'c'}"))
|
2007-08-21 21:42:19 -04:00
|
|
|
end
|
2007-01-20 20:24:16 -05:00
|
|
|
|
2008-01-17 13:41:27 -05:00
|
|
|
def test_dynamic_attrs_with_self_closed_tag
|
|
|
|
assert_equal("<a b='2' />\nc\n", render("%a{'b' => 1 + 1}/\n= 'c'\n"))
|
|
|
|
end
|
|
|
|
|
2008-04-18 14:08:49 -04:00
|
|
|
def test_exceptions
|
|
|
|
EXCEPTION_MAP.each do |key, value|
|
2007-01-27 05:29:14 -05:00
|
|
|
begin
|
2008-04-18 14:08:49 -04:00
|
|
|
render(key)
|
2008-04-29 00:28:01 -04:00
|
|
|
rescue Exception => err
|
2008-04-24 16:47:59 -04:00
|
|
|
value = [value] unless value.is_a?(Array)
|
|
|
|
|
|
|
|
assert_equal(value.first, err.message, "Line: #{key}")
|
|
|
|
assert_equal(value[1] || key.split("\n").length, err.backtrace[0].gsub('(haml):', '').to_i, "Line: #{key}")
|
2007-01-27 05:29:14 -05:00
|
|
|
else
|
2008-04-29 00:28:01 -04:00
|
|
|
assert(false, "Exception not raised for\n#{key}")
|
2007-01-27 05:29:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-18 14:08:49 -04:00
|
|
|
def test_exception_line
|
2007-11-24 03:35:10 -05:00
|
|
|
render("a\nb\n!!!\n c\nd")
|
|
|
|
rescue Haml::SyntaxError => e
|
2008-04-17 15:35:47 -04:00
|
|
|
assert_equal("(haml):4", e.backtrace[0])
|
2007-11-24 03:35:10 -05:00
|
|
|
else
|
|
|
|
assert(false, '"a\nb\n!!!\n c\nd" doesn\'t produce an exception')
|
|
|
|
end
|
|
|
|
|
2007-11-25 22:26:16 -05:00
|
|
|
def test_exception
|
|
|
|
render("%p\n hi\n %a= undefined\n= 12")
|
2007-11-24 03:35:10 -05:00
|
|
|
rescue Exception => e
|
2007-11-25 22:26:16 -05:00
|
|
|
assert_match("(haml):3", e.backtrace[0])
|
2007-11-24 03:35:10 -05:00
|
|
|
else
|
|
|
|
# Test failed... should have raised an exception
|
|
|
|
assert(false)
|
2007-11-23 03:24:11 -05:00
|
|
|
end
|
|
|
|
|
2007-01-27 05:29:14 -05:00
|
|
|
def test_compile_error
|
2007-11-25 22:26:16 -05:00
|
|
|
render("a\nb\n- fee)\nc")
|
2007-11-24 03:35:10 -05:00
|
|
|
rescue Exception => e
|
2007-11-25 22:26:16 -05:00
|
|
|
assert_match(/^compile error\n\(haml\):3: syntax error/i, e.message)
|
2007-11-24 03:35:10 -05:00
|
|
|
else
|
|
|
|
assert(false,
|
2007-11-25 22:26:16 -05:00
|
|
|
'"a\nb\n- fee)\nc" doesn\'t produce an exception!')
|
2007-01-27 05:29:14 -05:00
|
|
|
end
|
2007-01-31 01:38:23 -05:00
|
|
|
|
2007-11-25 20:36:57 -05:00
|
|
|
def test_unbalanced_brackets
|
|
|
|
render('== #{1 + 5} foo #{6 + 7 bar #{8 + 9}')
|
|
|
|
rescue Haml::SyntaxError => e
|
|
|
|
assert_equal("Unbalanced brackets.", e.message)
|
|
|
|
end
|
|
|
|
|
2008-04-28 23:59:43 -04:00
|
|
|
def test_balanced_conditional_comments
|
|
|
|
assert_equal("<!--[if !(IE 6)|(IE 7)]> Bracket: ] <![endif]-->\n",
|
|
|
|
render("/[if !(IE 6)|(IE 7)] Bracket: ]"))
|
|
|
|
end
|
|
|
|
|
2007-01-31 01:38:23 -05:00
|
|
|
def test_no_bluecloth
|
|
|
|
Kernel.module_eval do
|
2007-08-11 17:04:51 -04:00
|
|
|
def gem_original_require_with_bluecloth(file)
|
2007-01-31 01:38:23 -05:00
|
|
|
raise LoadError if file == 'bluecloth'
|
2007-08-11 17:04:51 -04:00
|
|
|
gem_original_require_without_bluecloth(file)
|
2007-01-31 01:38:23 -05:00
|
|
|
end
|
2007-08-11 17:04:51 -04:00
|
|
|
alias_method :gem_original_require_without_bluecloth, :gem_original_require
|
|
|
|
alias_method :gem_original_require, :gem_original_require_with_bluecloth
|
2007-01-31 01:38:23 -05:00
|
|
|
end
|
|
|
|
|
2007-08-11 17:04:51 -04:00
|
|
|
begin
|
|
|
|
assert_equal("<h1>Foo</h1>\t<p>- a\n- b</p>\n",
|
|
|
|
Haml::Engine.new(":markdown\n Foo\n ===\n - a\n - b").to_html)
|
2008-04-19 13:07:40 -04:00
|
|
|
rescue Haml::Error => e
|
2007-08-11 17:04:51 -04:00
|
|
|
if e.message == "Can't run Markdown filter; required 'bluecloth' or 'redcloth', but none were found"
|
|
|
|
puts "\nCouldn't require 'bluecloth' or 'redcloth'; skipping a test."
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
2007-01-31 01:38:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
Kernel.module_eval do
|
2007-08-11 17:04:51 -04:00
|
|
|
alias_method :gem_original_require, :gem_original_require_without_bluecloth
|
2007-01-31 01:38:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_redcloth
|
|
|
|
Kernel.module_eval do
|
2007-08-11 17:04:51 -04:00
|
|
|
def gem_original_require_with_redcloth(file)
|
2007-01-31 01:38:23 -05:00
|
|
|
raise LoadError if file == 'redcloth'
|
2007-08-11 17:04:51 -04:00
|
|
|
gem_original_require_without_redcloth(file)
|
2007-01-31 01:38:23 -05:00
|
|
|
end
|
2007-08-11 17:04:51 -04:00
|
|
|
alias_method :gem_original_require_without_redcloth, :gem_original_require
|
|
|
|
alias_method :gem_original_require, :gem_original_require_with_redcloth
|
2007-01-31 01:38:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
Haml::Engine.new(":redcloth\n _foo_").to_html
|
2008-04-19 13:07:40 -04:00
|
|
|
rescue Haml::Error
|
2007-01-31 01:38:23 -05:00
|
|
|
else
|
|
|
|
assert(false, "No exception raised!")
|
|
|
|
end
|
|
|
|
|
|
|
|
Kernel.module_eval do
|
2007-08-11 17:04:51 -04:00
|
|
|
alias_method :gem_original_require, :gem_original_require_without_redcloth
|
2007-01-31 01:38:23 -05:00
|
|
|
end
|
2007-08-11 17:04:51 -04:00
|
|
|
end
|
2007-01-31 01:38:23 -05:00
|
|
|
|
2007-08-11 17:04:51 -04:00
|
|
|
def test_no_redcloth_or_bluecloth
|
|
|
|
Kernel.module_eval do
|
|
|
|
def gem_original_require_with_redcloth_and_bluecloth(file)
|
|
|
|
raise LoadError if file == 'redcloth' || file == 'bluecloth'
|
|
|
|
gem_original_require_without_redcloth_and_bluecloth(file)
|
|
|
|
end
|
|
|
|
alias_method :gem_original_require_without_redcloth_and_bluecloth, :gem_original_require
|
|
|
|
alias_method :gem_original_require, :gem_original_require_with_redcloth_and_bluecloth
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
Haml::Engine.new(":markdown\n _foo_").to_html
|
2008-04-19 13:07:40 -04:00
|
|
|
rescue Haml::Error
|
2007-08-11 17:04:51 -04:00
|
|
|
else
|
|
|
|
assert(false, "No exception raised!")
|
|
|
|
end
|
|
|
|
|
|
|
|
Kernel.module_eval do
|
|
|
|
alias_method :gem_original_require, :gem_original_require_without_redcloth_and_bluecloth
|
|
|
|
end
|
2007-01-31 01:38:23 -05:00
|
|
|
end
|
2007-04-15 21:38:25 -04:00
|
|
|
|
2008-04-18 14:37:34 -04:00
|
|
|
def test_empty_filter
|
|
|
|
assert_equal(<<END, render(':javascript'))
|
|
|
|
<script type='text/javascript'>
|
|
|
|
//<![CDATA[
|
|
|
|
|
|
|
|
//]]>
|
|
|
|
</script>
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
2008-07-10 23:38:39 -04:00
|
|
|
def test_ugly_filter
|
|
|
|
assert_equal(<<END, render(":sass\n #foo\n bar: baz", :ugly => true))
|
|
|
|
#foo {
|
|
|
|
bar: baz; }
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
2007-04-15 21:38:25 -04:00
|
|
|
def test_local_assigns_dont_modify_class
|
|
|
|
assert_equal("bar\n", render("= foo", :locals => {:foo => 'bar'}))
|
|
|
|
assert_equal(nil, defined?(foo))
|
|
|
|
end
|
2007-04-17 02:49:07 -04:00
|
|
|
|
|
|
|
def test_object_ref_with_nil_id
|
2008-04-28 00:14:12 -04:00
|
|
|
user = User.new
|
2007-04-17 02:49:07 -04:00
|
|
|
assert_equal("<p class='struct_user' id='struct_user_new'>New User</p>\n",
|
|
|
|
render("%p[user] New User", :locals => {:user => user}))
|
|
|
|
end
|
2007-11-23 02:02:07 -05:00
|
|
|
|
2008-04-28 00:14:12 -04:00
|
|
|
def test_object_ref_before_attrs
|
|
|
|
user = User.new 42
|
|
|
|
assert_equal("<p class='struct_user' id='struct_user_42' style='width: 100px;'>New User</p>\n",
|
|
|
|
render("%p[user]{:style => 'width: 100px;'} New User", :locals => {:user => user}))
|
|
|
|
end
|
|
|
|
|
2007-12-19 04:41:33 -05:00
|
|
|
def test_non_literal_attributes
|
|
|
|
assert_equal("<p a1='foo' a2='bar' a3='baz' />\n",
|
|
|
|
render("%p{a2, a1, :a3 => 'baz'}/",
|
|
|
|
:locals => {:a1 => {:a1 => 'foo'}, :a2 => {:a2 => 'bar'}}))
|
|
|
|
end
|
|
|
|
|
2007-11-23 02:02:07 -05:00
|
|
|
def test_render_should_accept_a_binding_as_scope
|
|
|
|
string = "This is a string!"
|
|
|
|
string.instance_variable_set("@var", "Instance variable")
|
2007-11-23 02:42:59 -05:00
|
|
|
b = string.instance_eval do
|
|
|
|
var = "Local variable"
|
|
|
|
binding
|
|
|
|
end
|
2007-11-23 02:02:07 -05:00
|
|
|
|
2007-11-23 02:42:59 -05:00
|
|
|
assert_equal("<p>THIS IS A STRING!</p>\n<p>Instance variable</p>\n<p>Local variable</p>\n",
|
|
|
|
render("%p= upcase\n%p= @var\n%p= var", :scope => b))
|
2007-11-23 02:02:07 -05:00
|
|
|
end
|
2007-11-23 07:14:02 -05:00
|
|
|
|
|
|
|
def test_yield_should_work_with_binding
|
|
|
|
assert_equal("12\nFOO\n", render("= yield\n= upcase", :scope => "foo".instance_eval{binding}) { 12 })
|
|
|
|
end
|
2007-11-23 21:32:18 -05:00
|
|
|
|
|
|
|
def test_yield_should_work_with_def_method
|
|
|
|
s = "foo"
|
|
|
|
Haml::Engine.new("= yield\n= upcase").def_method(s, :render)
|
|
|
|
assert_equal("12\nFOO\n", s.render { 12 })
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_def_method_with_module
|
|
|
|
Haml::Engine.new("= yield\n= upcase").def_method(String, :render_haml)
|
|
|
|
assert_equal("12\nFOO\n", "foo".render_haml { 12 })
|
|
|
|
end
|
2007-11-24 03:35:10 -05:00
|
|
|
|
|
|
|
def test_def_method_locals
|
|
|
|
obj = Object.new
|
|
|
|
Haml::Engine.new("%p= foo\n.bar{:baz => baz}= boom").def_method(obj, :render, :foo, :baz, :boom)
|
|
|
|
assert_equal("<p>1</p>\n<div baz='2' class='bar'>3</div>\n", obj.render(:foo => 1, :baz => 2, :boom => 3))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_proc_locals
|
|
|
|
proc = Haml::Engine.new("%p= foo\n.bar{:baz => baz}= boom").render_proc(Object.new, :foo, :baz, :boom)
|
|
|
|
assert_equal("<p>1</p>\n<div baz='2' class='bar'>3</div>\n", proc[:foo => 1, :baz => 2, :boom => 3])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_proc_with_binding
|
|
|
|
assert_equal("FOO\n", Haml::Engine.new("= upcase").render_proc("foo".instance_eval{binding}).call)
|
|
|
|
end
|
2008-02-13 04:30:21 -05:00
|
|
|
|
|
|
|
def test_ugly_true
|
2008-02-13 23:13:12 -05:00
|
|
|
assert_equal("<div id='outer'>\n<div id='inner'>\n<p>hello world</p>\n</div>\n</div>\n",
|
|
|
|
render("#outer\n #inner\n %p hello world", :ugly => true))
|
|
|
|
|
|
|
|
assert_equal("<p>#{'s' * 75}</p>\n",
|
|
|
|
render("%p #{'s' * 75}", :ugly => true))
|
2008-02-13 04:30:21 -05:00
|
|
|
|
2008-02-13 23:13:12 -05:00
|
|
|
assert_equal("<p>#{'s' * 75}</p>\n",
|
|
|
|
render("%p= 's' * 75", :ugly => true))
|
2008-02-13 04:30:21 -05:00
|
|
|
end
|
2008-02-24 17:10:30 -05:00
|
|
|
|
2008-05-27 15:16:24 -04:00
|
|
|
def test_auto_preserve_unless_ugly
|
|
|
|
assert_equal("<pre>foo
bar</pre>\n", render('%pre="foo\nbar"'))
|
|
|
|
assert_equal("<pre>foo\nbar</pre>\n", render("%pre\n foo\n bar"))
|
|
|
|
assert_equal("<pre>foo\nbar</pre>\n", render('%pre="foo\nbar"', :ugly => true))
|
|
|
|
assert_equal("<pre>foo\nbar</pre>\n", render("%pre\n foo\n bar", :ugly => true))
|
|
|
|
end
|
|
|
|
|
2008-02-26 13:57:45 -05:00
|
|
|
def test_xhtml_output_option
|
2008-02-29 14:00:03 -05:00
|
|
|
assert_equal "<p>\n <br />\n</p>\n", render("%p\n %br", :format => :xhtml)
|
|
|
|
assert_equal "<a />\n", render("%a/", :format => :xhtml)
|
2008-02-26 13:57:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_arbitrary_output_option
|
2008-02-29 14:00:03 -05:00
|
|
|
assert_raise(Haml::Error, "Invalid output format :html1") { Haml::Engine.new("%br", :format => :html1) }
|
2008-02-26 13:57:45 -05:00
|
|
|
end
|
|
|
|
|
2008-02-26 13:43:37 -05:00
|
|
|
# HTML 4.0
|
|
|
|
|
|
|
|
def test_html_has_no_self_closing_tags
|
2008-02-29 14:00:03 -05:00
|
|
|
assert_equal "<p>\n <br>\n</p>\n", render("%p\n %br", :format => :html4)
|
|
|
|
assert_equal "<br>\n", render("%br/", :format => :html4)
|
2008-02-26 13:43:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_html_renders_empty_node_with_closing_tag
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal "<div class='foo'></div>\n", render(".foo", :format => :html4)
|
2008-02-26 13:43:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_html_ignores_explicit_self_closing_declaration
|
2008-05-09 19:00:08 -04:00
|
|
|
assert_equal "<a></a>\n", render("%a/", :format => :html4)
|
2008-02-26 13:43:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_html_ignores_xml_prolog_declaration
|
2008-02-29 14:00:03 -05:00
|
|
|
assert_equal "", render('!!! XML', :format => :html4)
|
2008-02-26 13:43:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_html_has_different_doctype
|
|
|
|
assert_equal %{<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n},
|
2008-02-29 14:00:03 -05:00
|
|
|
render('!!!', :format => :html4)
|
2008-02-24 17:10:30 -05:00
|
|
|
end
|
2008-02-27 09:16:21 -05:00
|
|
|
|
|
|
|
# because anything before the doctype triggers quirks mode in IE
|
|
|
|
def test_xml_prolog_and_doctype_dont_result_in_a_leading_whitespace_in_html
|
2008-02-29 14:00:03 -05:00
|
|
|
assert_no_match /^\s+/, render("!!! xml\n!!!", :format => :html4)
|
2008-02-27 09:16:21 -05:00
|
|
|
end
|
2008-02-27 09:20:44 -05:00
|
|
|
|
|
|
|
# HTML5
|
|
|
|
def test_html5_doctype
|
2008-02-29 14:00:03 -05:00
|
|
|
assert_equal %{<!DOCTYPE html>\n}, render('!!!', :format => :html5)
|
2008-02-27 09:20:44 -05:00
|
|
|
end
|
2006-06-30 11:14:44 -04:00
|
|
|
end
|