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-12-03 18:56:47 -05:00
|
|
|
require 'haml/template'
|
2006-09-12 00:14:21 -04:00
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
class HelperTest < Test::Unit::TestCase
|
2006-09-12 00:14:21 -04:00
|
|
|
include Haml::Helpers
|
2008-03-02 19:12:57 -05:00
|
|
|
Post = Struct.new('Post', :body)
|
2006-11-15 20:15:01 -05:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@base = ActionView::Base.new
|
|
|
|
@base.controller = ActionController::Base.new
|
2008-03-02 19:12:57 -05:00
|
|
|
@base.instance_variable_set('@post', Post.new("Foo bar\nbaz"))
|
2006-11-15 20:15:01 -05:00
|
|
|
end
|
2006-08-08 09:42:19 -04:00
|
|
|
|
2006-11-07 22:27:37 -05:00
|
|
|
def render(text, options = {})
|
2006-11-15 20:15:01 -05:00
|
|
|
if options == :action_view
|
|
|
|
@base.render :inline => text, :type => :haml
|
|
|
|
else
|
2007-05-10 04:12:20 -04:00
|
|
|
scope = options.delete :scope_object
|
|
|
|
Haml::Engine.new(text, options).to_html(scope ? scope : Object.new)
|
2006-11-15 20:15:01 -05:00
|
|
|
end
|
2006-11-07 22:27:37 -05:00
|
|
|
end
|
|
|
|
|
2006-11-05 22:01:04 -05:00
|
|
|
def test_flatten
|
2008-04-24 15:07:52 -04:00
|
|
|
assert_equal("FooBar", flatten("FooBar"))
|
2006-09-29 14:39:13 -04:00
|
|
|
|
2008-04-24 15:07:52 -04:00
|
|
|
assert_equal("FooBar", flatten("Foo\rBar"))
|
2006-09-29 14:39:13 -04:00
|
|
|
|
2008-04-24 15:07:52 -04:00
|
|
|
assert_equal("Foo
Bar", flatten("Foo\nBar"))
|
2006-09-29 14:39:13 -04:00
|
|
|
|
2008-04-24 15:07:52 -04:00
|
|
|
assert_equal("Hello
World!
YOU ARE FLAT?
OMGZ!",
|
|
|
|
flatten("Hello\nWorld!\nYOU ARE \rFLAT?\n\rOMGZ!"))
|
2006-08-08 17:38:50 -04:00
|
|
|
end
|
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
def test_list_of_should_render_correctly
|
2006-11-27 20:44:40 -05:00
|
|
|
assert_equal("<li>1</li>\n<li>2</li>\n", render("= list_of([1, 2]) do |i|\n = i"))
|
2007-05-15 11:51:50 -04:00
|
|
|
assert_equal("<li>[1]</li>\n", render("= list_of([[1]]) do |i|\n = i.inspect"))
|
2006-11-27 20:44:40 -05: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 10:13:49 -04:00
|
|
|
end
|
2006-11-07 22:27:37 -05:00
|
|
|
|
|
|
|
def test_buffer_access
|
|
|
|
assert(render("= buffer") =~ /#<Haml::Buffer:0x[a-z0-9]+>/)
|
2006-11-07 23:59:24 -05:00
|
|
|
assert_equal(render("= (buffer == _hamlout)"), "true\n")
|
2006-11-07 22:27:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tabs
|
2007-07-13 00:03:33 -04: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-07 22:27:37 -05:00
|
|
|
end
|
2006-11-14 10:52:54 -05:00
|
|
|
|
2006-11-14 13:35:02 -05:00
|
|
|
def test_helpers_dont_leak
|
2006-11-14 10:52:54 -05:00
|
|
|
# Haml helpers shouldn't be accessible from ERB
|
|
|
|
render("foo")
|
|
|
|
proper_behavior = false
|
2006-11-14 13:35:02 -05:00
|
|
|
|
2006-11-14 10:52:54 -05:00
|
|
|
begin
|
|
|
|
ActionView::Base.new.render(:inline => "<%= flatten('Foo\\nBar') %>")
|
|
|
|
rescue NoMethodError
|
|
|
|
proper_behavior = true
|
|
|
|
end
|
|
|
|
assert(proper_behavior)
|
2006-11-14 13:35:02 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
ActionView::Base.new.render(:inline => "<%= concat('foo') %>")
|
2007-03-29 22:52:57 -04:00
|
|
|
rescue ArgumentError, NameError
|
2006-11-14 13:35:02 -05:00
|
|
|
proper_behavior = true
|
|
|
|
end
|
|
|
|
assert(proper_behavior)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_action_view_included
|
|
|
|
assert(Haml::Helpers.action_view?)
|
|
|
|
end
|
|
|
|
|
2006-11-15 20:15:01 -05:00
|
|
|
def test_form_tag
|
2007-12-10 21:00:06 -05:00
|
|
|
# This is usually provided by ActionController::Base.
|
|
|
|
def @base.protect_against_forgery?; false; end
|
2006-11-15 20:15:01 -05:00
|
|
|
result = render("- form_tag 'foo' do\n %p bar\n %strong baz", :action_view)
|
2007-01-30 22:17:57 -05: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-15 20:15:01 -05:00
|
|
|
end
|
2008-03-02 17:24:29 -05:00
|
|
|
|
2008-03-02 19:12:57 -05:00
|
|
|
def test_text_area
|
2008-04-24 15:59:03 -04:00
|
|
|
assert_equal(%(<textarea id="body" name="body">Foo
Bar
 Baz
 Boom</textarea>\n),
|
2008-03-02 17:24:29 -05:00
|
|
|
render('= text_area_tag "body", "Foo\nBar\n Baz\n Boom"', :action_view))
|
2008-03-02 19:12:57 -05:00
|
|
|
|
2008-04-24 15:59:03 -04:00
|
|
|
assert_equal(%(<textarea cols="40" id="post_body" name="post[body]" rows="20">Foo bar
baz</textarea>\n),
|
2008-03-02 19:12:57 -05:00
|
|
|
render('= text_area :post, :body', :action_view))
|
2008-03-02 19:24:47 -05:00
|
|
|
|
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('= content_tag "pre", "Foo bar\n baz"', :action_view))
|
2008-03-02 17:24:29 -05:00
|
|
|
end
|
2006-11-20 01:11:16 -05: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-11-22 23:53:19 -05:00
|
|
|
|
|
|
|
def test_content_tag_block
|
|
|
|
assert_equal(<<HTML.strip, render(<<HAML, :action_view))
|
|
|
|
<div><p>bar</p>
|
|
|
|
<strong>bar</strong>
|
|
|
|
</div>
|
|
|
|
HTML
|
|
|
|
- content_tag :div do
|
|
|
|
%p bar
|
|
|
|
%strong bar
|
|
|
|
HAML
|
|
|
|
end
|
|
|
|
|
2008-03-18 05:19:41 -04: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 05:29:14 -05:00
|
|
|
|
2008-04-11 12:44:15 -04: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 21:16:27 -04: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 12:44:15 -04:00
|
|
|
end
|
|
|
|
|
2008-06-05 21:32:17 -04: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 05:29:14 -05:00
|
|
|
def test_is_haml
|
|
|
|
assert(!ActionView::Base.new.is_haml?)
|
2007-04-05 15:09:07 -04: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 05:29:14 -05:00
|
|
|
end
|
2007-03-16 23:13:20 -04:00
|
|
|
|
|
|
|
def test_page_class
|
|
|
|
controller = Struct.new(:controller_name, :action_name).new('troller', 'tion')
|
2007-05-10 04:12:20 -04:00
|
|
|
scope = Struct.new(:controller).new(controller)
|
|
|
|
result = render("%div{:class => page_class} MyDiv", :scope_object => scope)
|
2007-03-16 23:13:20 -04:00
|
|
|
expected = "<div class='troller tion'>MyDiv</div>\n"
|
|
|
|
assert_equal expected, result
|
|
|
|
end
|
2007-05-12 17:33:58 -04: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 11:51:50 -04:00
|
|
|
|
|
|
|
def test_capture_deals_properly_with_collections
|
|
|
|
Haml::Helpers.module_eval do
|
|
|
|
def trc(collection, &block)
|
|
|
|
collection.each do |record|
|
2008-10-29 23:39:34 -04:00
|
|
|
haml_concat capture_haml(record, &block)
|
2007-05-15 11:51:50 -04:00
|
|
|
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 05:00:55 -04:00
|
|
|
|
2008-01-04 20:19:04 -05:00
|
|
|
def test_find_and_preserve_with_block
|
2008-05-11 04:39:02 -04:00
|
|
|
assert_equal("<pre>Foo
Bar</pre>\nFoo\nBar\n",
|
2008-01-04 20:19:04 -05: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 04:39:02 -04:00
|
|
|
assert_equal("<pre>Foo
Bar</pre>
Foo
Bar\n",
|
2008-01-04 20:19:04 -05:00
|
|
|
render("= preserve do\n %pre\n Foo\n Bar\n Foo\n Bar"))
|
|
|
|
end
|
|
|
|
|
2007-07-14 05:00:55 -04: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 21:33:49 -05:00
|
|
|
context.haml_tag :p, :attr => "val" do
|
2008-10-29 23:39:34 -04:00
|
|
|
context.haml_concat "Blah"
|
2007-07-14 05:00:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal("<p attr='val'>\n Blah\n</p>\n", result)
|
|
|
|
end
|
2008-04-24 13:23:01 -04:00
|
|
|
|
|
|
|
def test_non_haml
|
|
|
|
assert_equal("false\n", render("= non_haml { is_haml? }"))
|
|
|
|
end
|
2008-04-29 16:30:20 -04:00
|
|
|
|
|
|
|
class ActsLikeTag
|
2008-04-29 22:13:30 -04: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 16:30:20 -04: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 09:42:19 -04:00
|
|
|
end
|
2006-11-14 13:35:02 -05:00
|
|
|
|