2006-10-14 23:50:07 +00:00
|
|
|
#!/usr/bin/env ruby
|
2008-06-08 09:54:36 -07:00
|
|
|
require File.dirname(__FILE__) + '/../test_helper'
|
2006-12-03 23:56:47 +00:00
|
|
|
require 'haml/template'
|
2006-09-12 04:14:21 +00:00
|
|
|
|
2006-09-29 18:39:13 +00:00
|
|
|
class HelperTest < Test::Unit::TestCase
|
2006-09-12 04:14:21 +00:00
|
|
|
include Haml::Helpers
|
2008-03-02 16:12:57 -08:00
|
|
|
Post = Struct.new('Post', :body)
|
2006-11-16 01:15:01 +00:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@base = ActionView::Base.new
|
|
|
|
@base.controller = ActionController::Base.new
|
2008-03-02 16:12:57 -08:00
|
|
|
@base.instance_variable_set('@post', Post.new("Foo bar\nbaz"))
|
2006-11-16 01:15:01 +00:00
|
|
|
end
|
2006-08-08 13:42:19 +00:00
|
|
|
|
2006-11-08 03:27:37 +00:00
|
|
|
def render(text, options = {})
|
2006-11-16 01:15:01 +00:00
|
|
|
if options == :action_view
|
|
|
|
@base.render :inline => text, :type => :haml
|
|
|
|
else
|
2007-05-10 08:12:20 +00:00
|
|
|
scope = options.delete :scope_object
|
|
|
|
Haml::Engine.new(text, options).to_html(scope ? scope : Object.new)
|
2006-11-16 01:15:01 +00:00
|
|
|
end
|
2006-11-08 03:27:37 +00:00
|
|
|
end
|
|
|
|
|
2006-11-06 03:01:04 +00:00
|
|
|
def test_flatten
|
2008-04-24 12:07:52 -07:00
|
|
|
assert_equal("FooBar", flatten("FooBar"))
|
2006-09-29 18:39:13 +00:00
|
|
|
|
2008-04-24 12:07:52 -07:00
|
|
|
assert_equal("FooBar", flatten("Foo\rBar"))
|
2006-09-29 18:39:13 +00:00
|
|
|
|
2008-04-24 12:07:52 -07:00
|
|
|
assert_equal("Foo
Bar", flatten("Foo\nBar"))
|
2006-09-29 18:39:13 +00:00
|
|
|
|
2008-04-24 12:07:52 -07:00
|
|
|
assert_equal("Hello
World!
YOU ARE FLAT?
OMGZ!",
|
|
|
|
flatten("Hello\nWorld!\nYOU ARE \rFLAT?\n\rOMGZ!"))
|
2006-08-08 21:38:50 +00:00
|
|
|
end
|
|
|
|
|
2006-09-29 18:39:13 +00:00
|
|
|
def test_list_of_should_render_correctly
|
2006-11-28 01:44:40 +00:00
|
|
|
assert_equal("<li>1</li>\n<li>2</li>\n", render("= list_of([1, 2]) do |i|\n = i"))
|
2007-05-15 15:51:50 +00:00
|
|
|
assert_equal("<li>[1]</li>\n", render("= list_of([[1]]) do |i|\n = i.inspect"))
|
2006-11-28 01:44:40 +00:00
|
|
|
assert_equal("<li>\n <h1>Fee</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fi</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fo</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fum</h1>\n <p>A word!</p>\n</li>\n",
|
|
|
|
render("= list_of(['Fee', 'Fi', 'Fo', 'Fum']) do |title|\n %h1= title\n %p A word!"))
|
2006-09-19 14:13:49 +00:00
|
|
|
end
|
2006-11-08 03:27:37 +00:00
|
|
|
|
|
|
|
def test_buffer_access
|
|
|
|
assert(render("= buffer") =~ /#<Haml::Buffer:0x[a-z0-9]+>/)
|
2006-11-08 04:59:24 +00:00
|
|
|
assert_equal(render("= (buffer == _hamlout)"), "true\n")
|
2006-11-08 03:27:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tabs
|
2007-07-13 04:03:33 +00:00
|
|
|
assert_equal("foo\n bar\nbaz\n", render("foo\n- tab_up\nbar\n- tab_down\nbaz"))
|
|
|
|
assert_equal(" <p>tabbed</p>\n", render("- buffer.tabulation=5\n%p tabbed"))
|
2006-11-08 03:27:37 +00:00
|
|
|
end
|
2006-11-14 15:52:54 +00:00
|
|
|
|
2006-11-14 18:35:02 +00:00
|
|
|
def test_helpers_dont_leak
|
2006-11-14 15:52:54 +00:00
|
|
|
# Haml helpers shouldn't be accessible from ERB
|
|
|
|
render("foo")
|
|
|
|
proper_behavior = false
|
2006-11-14 18:35:02 +00:00
|
|
|
|
2006-11-14 15:52:54 +00:00
|
|
|
begin
|
|
|
|
ActionView::Base.new.render(:inline => "<%= flatten('Foo\\nBar') %>")
|
|
|
|
rescue NoMethodError
|
|
|
|
proper_behavior = true
|
|
|
|
end
|
|
|
|
assert(proper_behavior)
|
2006-11-14 18:35:02 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
ActionView::Base.new.render(:inline => "<%= concat('foo') %>")
|
2007-03-30 02:52:57 +00:00
|
|
|
rescue ArgumentError, NameError
|
2006-11-14 18:35:02 +00:00
|
|
|
proper_behavior = true
|
|
|
|
end
|
|
|
|
assert(proper_behavior)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_action_view_included
|
|
|
|
assert(Haml::Helpers.action_view?)
|
|
|
|
end
|
|
|
|
|
2006-11-16 01:15:01 +00:00
|
|
|
def test_form_tag
|
2007-12-11 02:00:06 +00:00
|
|
|
# This is usually provided by ActionController::Base.
|
|
|
|
def @base.protect_against_forgery?; false; end
|
2006-11-16 01:15:01 +00:00
|
|
|
result = render("- form_tag 'foo' do\n %p bar\n %strong baz", :action_view)
|
2007-01-31 03:17:57 +00:00
|
|
|
should_be = "<form action=\"foo\" method=\"post\">\n <p>bar</p>\n <strong>baz</strong>\n</form>\n"
|
|
|
|
assert_equal(should_be, result)
|
2006-11-16 01:15:01 +00:00
|
|
|
end
|
2008-03-02 14:24:29 -08:00
|
|
|
|
2008-03-02 16:12:57 -08:00
|
|
|
def test_text_area
|
2008-04-24 12:59:03 -07:00
|
|
|
assert_equal(%(<textarea id="body" name="body">Foo
Bar
 Baz
 Boom</textarea>\n),
|
2008-03-02 14:24:29 -08:00
|
|
|
render('= text_area_tag "body", "Foo\nBar\n Baz\n Boom"', :action_view))
|
2008-03-02 16:12:57 -08:00
|
|
|
|
2008-04-24 12:59:03 -07:00
|
|
|
assert_equal(%(<textarea cols="40" id="post_body" name="post[body]" rows="20">Foo bar
baz</textarea>\n),
|
2008-03-02 16:12:57 -08:00
|
|
|
render('= text_area :post, :body', :action_view))
|
2008-03-02 16:24:47 -08:00
|
|
|
|
2008-04-24 12:59:03 -07:00
|
|
|
assert_equal(%(<pre>Foo bar
 baz</pre>\n),
|
2008-03-02 16:24:47 -08:00
|
|
|
render('= content_tag "pre", "Foo bar\n baz"', :action_view))
|
2008-03-02 14:24:29 -08:00
|
|
|
end
|
2006-11-20 06:11:16 +00:00
|
|
|
|
|
|
|
def test_capture_haml
|
|
|
|
assert_equal("\"<p>13</p>\\n\"\n", render("- foo = capture_haml(13) do |a|\n %p= a\n= foo.dump"))
|
|
|
|
end
|
2008-03-18 02:19:41 -07:00
|
|
|
|
|
|
|
def test_haml_tag_attribute_html_escaping
|
|
|
|
assert_equal("<p id='foo&bar'>baz</p>\n", render("%p{:id => 'foo&bar'} baz", :escape_html => true))
|
|
|
|
end
|
2007-01-27 10:29:14 +00:00
|
|
|
|
2008-04-11 09:44:15 -07:00
|
|
|
def test_haml_tag_autoclosed_tags_are_closed
|
|
|
|
assert_equal("<br class='foo' />\n", render("- haml_tag :br, :class => 'foo'"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_haml_tag_non_autoclosed_tags_arent_closed
|
2008-06-05 18:16:27 -07:00
|
|
|
assert_equal("<p></p>\n", render("- haml_tag :p"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_haml_tag_renders_text_on_a_single_line
|
|
|
|
assert_equal("<p>#{'a' * 100}</p>\n", render("- haml_tag :p, 'a' * 100"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_haml_tag_raises_error_for_multiple_content
|
|
|
|
assert_raise(Haml::Error) { render("- haml_tag :p, 'foo' do\n bar") }
|
2008-04-11 09:44:15 -07:00
|
|
|
end
|
|
|
|
|
2008-06-05 18:32:17 -07:00
|
|
|
def test_haml_tag_flags
|
|
|
|
assert_equal("<p />\n", render("- haml_tag :p, :/"))
|
|
|
|
assert_equal("<p>kumquat</p>\n", render("- haml_tag :p, :< do\n kumquat"))
|
|
|
|
|
|
|
|
assert_raise(Haml::Error) { render("- haml_tag :p, 'foo', :/") }
|
|
|
|
assert_raise(Haml::Error) { render("- haml_tag :p, :/ do\n foo") }
|
|
|
|
end
|
|
|
|
|
2007-01-27 10:29:14 +00:00
|
|
|
def test_is_haml
|
|
|
|
assert(!ActionView::Base.new.is_haml?)
|
2007-04-05 19:09:07 +00:00
|
|
|
assert_equal("true\n", render("= is_haml?"))
|
|
|
|
assert_equal("true\n", render("= is_haml?", :action_view))
|
|
|
|
assert_equal("false", @base.render(:inline => '<%= is_haml? %>'))
|
|
|
|
assert_equal("false\n", render("= render :inline => '<%= is_haml? %>'", :action_view))
|
2007-01-27 10:29:14 +00:00
|
|
|
end
|
2007-03-17 03:13:20 +00:00
|
|
|
|
|
|
|
def test_page_class
|
|
|
|
controller = Struct.new(:controller_name, :action_name).new('troller', 'tion')
|
2007-05-10 08:12:20 +00:00
|
|
|
scope = Struct.new(:controller).new(controller)
|
|
|
|
result = render("%div{:class => page_class} MyDiv", :scope_object => scope)
|
2007-03-17 03:13:20 +00:00
|
|
|
expected = "<div class='troller tion'>MyDiv</div>\n"
|
|
|
|
assert_equal expected, result
|
|
|
|
end
|
2007-05-12 21:33:58 +00:00
|
|
|
|
|
|
|
def test_indented_capture
|
|
|
|
assert_equal(" \n Foo\n ", @base.render(:inline => " <% res = capture do %>\n Foo\n <% end %><%= res %>"))
|
|
|
|
end
|
2007-05-15 15:51:50 +00:00
|
|
|
|
|
|
|
def test_capture_deals_properly_with_collections
|
|
|
|
Haml::Helpers.module_eval do
|
|
|
|
def trc(collection, &block)
|
|
|
|
collection.each do |record|
|
|
|
|
puts capture_haml(record, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal("1\n\n2\n\n3\n\n", render("- trc([1, 2, 3]) do |i|\n = i.inspect"))
|
|
|
|
end
|
2007-07-14 09:00:55 +00:00
|
|
|
|
2008-01-05 01:19:04 +00:00
|
|
|
def test_find_and_preserve_with_block
|
2008-05-11 01:39:02 -07:00
|
|
|
assert_equal("<pre>Foo
Bar</pre>\nFoo\nBar\n",
|
2008-01-05 01:19:04 +00:00
|
|
|
render("= find_and_preserve do\n %pre\n Foo\n Bar\n Foo\n Bar"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_preserve_with_block
|
2008-05-11 01:39:02 -07:00
|
|
|
assert_equal("<pre>Foo
Bar</pre>
Foo
Bar\n",
|
2008-01-05 01:19:04 +00:00
|
|
|
render("= preserve do\n %pre\n Foo\n Bar\n Foo\n Bar"))
|
|
|
|
end
|
|
|
|
|
2007-07-14 09:00:55 +00:00
|
|
|
def test_init_haml_helpers
|
|
|
|
context = Object.new
|
|
|
|
class << context
|
|
|
|
include Haml::Helpers
|
|
|
|
end
|
|
|
|
context.init_haml_helpers
|
|
|
|
|
|
|
|
result = context.capture_haml do
|
2008-02-22 18:33:49 -08:00
|
|
|
context.haml_tag :p, :attr => "val" do
|
2007-07-14 09:00:55 +00:00
|
|
|
context.puts "Blah"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal("<p attr='val'>\n Blah\n</p>\n", result)
|
|
|
|
end
|
2008-04-24 10:23:01 -07:00
|
|
|
|
|
|
|
def test_non_haml
|
|
|
|
assert_equal("false\n", render("= non_haml { is_haml? }"))
|
|
|
|
end
|
2008-04-29 13:30:20 -07:00
|
|
|
|
|
|
|
class ActsLikeTag
|
2008-04-29 19:13:30 -07:00
|
|
|
# We want to be able to have people include monkeypatched ActionView helpers
|
|
|
|
# without redefining is_haml?.
|
|
|
|
# This is accomplished via Object#is_haml?, and this is a test for it.
|
2008-04-29 13:30:20 -07:00
|
|
|
include ActionView::Helpers::TagHelper
|
|
|
|
def to_s
|
|
|
|
content_tag :p, 'some tag content'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_random_class_includes_tag_helper
|
|
|
|
assert_equal "<p>some tag content</p>", ActsLikeTag.new.to_s
|
|
|
|
end
|
2006-08-08 13:42:19 +00:00
|
|
|
end
|
2006-11-14 18:35:02 +00:00
|
|
|
|