public
Rubygem
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/nex3/haml.git
Search Repo:
chriseppstein (author)
Tue Apr 29 13:30:20 -0700 2008
nex3 (committer)
Tue Apr 29 18:53:29 -0700 2008
commit  9187180ae2a5c58c03a0e40b9ae32e507f1b8a0a
tree    2b43159a2b5ba68bb2a01fb0b59f655ec7e94505
parent  6f40855ed1156f4da92e6059c2ff4982b4e999e8
haml / test / haml / helper_test.rb
100644 183 lines (148 sloc) 6.137 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/test_helper'
require 'haml/template'
 
class HelperTest < Test::Unit::TestCase
  include Haml::Helpers
  Post = Struct.new('Post', :body)
  
  def setup
    @base = ActionView::Base.new
    @base.controller = ActionController::Base.new
    @base.instance_variable_set('@post', Post.new("Foo bar\nbaz"))
  end
 
  def render(text, options = {})
    if options == :action_view
      @base.render :inline => text, :type => :haml
    else
      scope = options.delete :scope_object
      Haml::Engine.new(text, options).to_html(scope ? scope : Object.new)
    end
  end
 
  def test_flatten
    assert_equal("FooBar", flatten("FooBar"))
 
    assert_equal("FooBar", flatten("Foo\rBar"))
 
    assert_equal("Foo&#x000A;Bar", flatten("Foo\nBar"))
 
    assert_equal("Hello&#x000A;World!&#x000A;YOU ARE FLAT?&#x000A;OMGZ!",
                 flatten("Hello\nWorld!\nYOU ARE \rFLAT?\n\rOMGZ!"))
  end
 
  def test_list_of_should_render_correctly
    assert_equal("<li>1</li>\n<li>2</li>\n", render("= list_of([1, 2]) do |i|\n = i"))
    assert_equal("<li>[1]</li>\n", render("= list_of([[1]]) do |i|\n = i.inspect"))
    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!"))
  end
 
  def test_buffer_access
    assert(render("= buffer") =~ /#<Haml::Buffer:0x[a-z0-9]+>/)
    assert_equal(render("= (buffer == _hamlout)"), "true\n")
  end
 
  def test_tabs
    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"))
  end
  
  def test_helpers_dont_leak
    # Haml helpers shouldn't be accessible from ERB
    render("foo")
    proper_behavior = false
 
    begin
      ActionView::Base.new.render(:inline => "<%= flatten('Foo\\nBar') %>")
    rescue NoMethodError
      proper_behavior = true
    end
    assert(proper_behavior)
 
    begin
      ActionView::Base.new.render(:inline => "<%= concat('foo') %>")
    rescue ArgumentError, NameError
      proper_behavior = true
    end
    assert(proper_behavior)
  end
  
  def test_action_view_included
    assert(Haml::Helpers.action_view?)
  end
  
  def test_form_tag
    # This is usually provided by ActionController::Base.
    def @base.protect_against_forgery?; false; end
    result = render("- form_tag 'foo' do\n %p bar\n %strong baz", :action_view)
    should_be = "<form action=\"foo\" method=\"post\">\n <p>bar</p>\n <strong>baz</strong>\n</form>\n"
    assert_equal(should_be, result)
  end
 
  def test_text_area
    assert_equal(%(<textarea id="body" name="body">Foo&#x000A;Bar&#x000A; Baz&#x000A; Boom</textarea>\n),
                 render('= text_area_tag "body", "Foo\nBar\n Baz\n Boom"', :action_view))
 
    assert_equal(%(<textarea cols="40" id="post_body" name="post[body]" rows="20">Foo bar&#x000A;baz</textarea>\n),
                 render('= text_area :post, :body', :action_view))
 
    assert_equal(%(<pre>Foo bar&#x000A; baz</pre>\n),
                 render('= content_tag "pre", "Foo bar\n baz"', :action_view))
  end
  
  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
  
  def test_haml_tag_attribute_html_escaping
    assert_equal("<p id='foo&amp;bar'>baz</p>\n", render("%p{:id => 'foo&bar'} baz", :escape_html => true))
  end
 
  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
    assert_equal("<p>\n</p>\n", render("- haml_tag :p"))
  end
 
  def test_is_haml
    assert(!ActionView::Base.new.is_haml?)
    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))
  end
 
  def test_page_class
    controller = Struct.new(:controller_name, :action_name).new('troller', 'tion')
    scope = Struct.new(:controller).new(controller)
    result = render("%div{:class => page_class} MyDiv", :scope_object => scope)
    expected = "<div class='troller tion'>MyDiv</div>\n"
    assert_equal expected, result
  end
 
  def test_indented_capture
    assert_equal(" \n Foo\n ", @base.render(:inline => " <% res = capture do %>\n Foo\n <% end %><%= res %>"))
  end
 
  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
 
  def test_find_and_preserve_with_block
    assert_equal("<pre>&#x000A; Foo&#x000A; Bar&#x000A;</pre>\nFoo\nBar\n",
                 render("= find_and_preserve do\n %pre\n Foo\n Bar\n Foo\n Bar"))
  end
 
  def test_preserve_with_block
    assert_equal("<pre>&#x000A; Foo&#x000A; Bar&#x000A;</pre>&#x000A;Foo&#x000A;Bar&#x000A;\n",
                 render("= preserve do\n %pre\n Foo\n Bar\n Foo\n Bar"))
  end
 
  def test_init_haml_helpers
    context = Object.new
    class << context
      include Haml::Helpers
    end
    context.init_haml_helpers
 
    result = context.capture_haml do
      context.haml_tag :p, :attr => "val" do
        context.puts "Blah"
      end
    end
 
    assert_equal("<p attr='val'>\n Blah\n</p>\n", result)
  end
 
  def test_non_haml
    assert_equal("false\n", render("= non_haml { is_haml? }"))
  end
  
  class ActsLikeTag
    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
end