2006-10-14 19:50:07 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2006-06-30 11:14:44 -04:00
|
|
|
require 'test/unit'
|
2006-12-03 18:56:47 -05:00
|
|
|
require File.dirname(__FILE__) + '/../../lib/haml'
|
|
|
|
require 'haml/engine'
|
2006-08-06 00:25:31 -04:00
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
class EngineTest < Test::Unit::TestCase
|
2006-08-05 23:18:54 -04:00
|
|
|
|
2006-09-29 15:57:35 -04:00
|
|
|
def render(text, options = {})
|
|
|
|
Haml::Engine.new(text, options).to_html
|
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
|
|
|
# This is ugly because Hashes are unordered; we don't always know the order
|
|
|
|
# in which attributes will be returned.
|
|
|
|
# There is probably a better way to do this.
|
|
|
|
def test_attributes_should_render_correctly
|
|
|
|
assert_equal("<div class='atlantis' style='ugly'>\n</div>", render(".atlantis{:style => 'ugly'}").chomp)
|
|
|
|
rescue
|
|
|
|
assert_equal("<div style='ugly' class='atlantis'>\n</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
|
|
|
|
assert_equal("<div class='no_attributes'>\n</div>",
|
|
|
|
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
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
def test_long_liner_should_not_print_on_one_line
|
|
|
|
assert_equal("<div>\n #{'x' * 51}\n</div>", render("%div #{'x' * 51}").chomp)
|
2006-08-13 18:10:05 -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
|
|
|
|
|
|
|
# Options tests
|
|
|
|
|
|
|
|
def test_stop_eval
|
|
|
|
assert_equal("", render("= 'Hello'", :suppress_eval => true))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_attr_wrapper
|
|
|
|
assert_equal("<p strange=*attrs*>\n</p>\n", render("%p{ :strange => 'attrs'}", :attr_wrapper => '*'))
|
2006-12-10 16:56:05 -05:00
|
|
|
assert_equal("<p escaped='quo\"te'>\n</p>\n", render("%p{ :escaped => 'quo\"te'}", :attr_wrapper => '"'))
|
|
|
|
assert_equal("<p escaped=\"q'uo"te\">\n</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
|
|
|
|
|
|
|
|
def test_locals
|
|
|
|
assert_equal("<p>Paragraph!</p>\n", render("%p= text", :locals => { :text => "Paragraph!" }))
|
|
|
|
end
|
2006-11-04 03:43:34 -05:00
|
|
|
|
|
|
|
def test_precompiled
|
|
|
|
precompiled = <<-END
|
|
|
|
def _haml_render
|
|
|
|
_hamlout = @haml_stack[-1]
|
|
|
|
_erbout = _hamlout.buffer
|
|
|
|
|
|
|
|
_hamlout.open_tag("p", 0, nil, true, "", nil, nil, false)
|
|
|
|
@haml_lineno = 1
|
|
|
|
haml_temp = "Haml Rocks Socks"
|
|
|
|
haml_temp = _hamlout.push_script(haml_temp, 1, false)
|
|
|
|
_hamlout.close_tag("p", 0)
|
|
|
|
end
|
|
|
|
END
|
|
|
|
|
|
|
|
assert_equal("<p>Haml Rocks Socks</p>\n", render("%h1 I shall not be rendered", :precompiled => precompiled))
|
|
|
|
end
|
2006-11-18 01:55:08 -05:00
|
|
|
|
|
|
|
def test_comps
|
|
|
|
assert_equal(-1, "foo" <=> nil)
|
|
|
|
assert_equal(1, nil <=> "foo")
|
|
|
|
end
|
2007-01-20 20:24:16 -05:00
|
|
|
|
|
|
|
def test_rec_merge
|
|
|
|
hash1 = {1=>2, 3=>{5=>7, 8=>9}}
|
|
|
|
hash1_2 = hash1.clone
|
|
|
|
hash2 = {4=>5, 3=>{5=>2, 16=>12}}
|
|
|
|
hash3 = {1=>2, 4=>5, 3=>{5=>2, 8=>9, 16=>12}}
|
|
|
|
|
|
|
|
assert_equal(hash3, hash1.rec_merge(hash2))
|
|
|
|
assert_equal(hash1_2, hash1)
|
|
|
|
hash1.rec_merge!(hash2)
|
|
|
|
assert_equal(hash3, hash1)
|
|
|
|
end
|
2007-01-26 21:08:08 -05:00
|
|
|
|
|
|
|
def test_exception_type
|
|
|
|
begin
|
|
|
|
render("%p hi\n= undefined")
|
|
|
|
rescue Exception => e
|
|
|
|
assert(e.is_a?(Haml::Error))
|
|
|
|
assert_equal(2, e.haml_line)
|
|
|
|
assert_equal(nil, e.haml_filename)
|
|
|
|
assert_equal('(haml):2', e.backtrace[0])
|
|
|
|
else
|
|
|
|
# Test failed... should have raised an exception
|
|
|
|
assert(false)
|
|
|
|
end
|
|
|
|
end
|
2006-06-30 11:14:44 -04:00
|
|
|
end
|