We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Rubygem
Fork of nex3/haml
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/chriseppstein/haml.git
nex3 (author)
Thu Jan 18 19:39:45 -0800 2007
commit  79e583d519aa1090a8709be4c1b1e1742b5ec275
tree    c7d170c15f840e991bf6f2ff9c7d87631f85b22d
parent  e4ea3b8e064812b68e83f241f6654c2db4ad064e
haml / test / helper_test.rb
100644 106 lines (83 sloc) 3.196 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
#!/usr/bin/env ruby
 
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/haml/template'
 
class HelperTest < Test::Unit::TestCase
  include Haml::Helpers
  
  def setup
    ActionView::Base.register_template_handler("haml", Haml::Template)
    @base = ActionView::Base.new
    @base.controller = ActionController::Base.new
  end
 
  def render(text, options = {})
    if options == :action_view
      @base.render :inline => text, :type => :haml
    else
      Haml::Engine.new(text, options).to_html
    end
  end
 
  def test_flatten
    assert_equal(flatten("FooBar"), "FooBar")
 
    assert_equal(flatten("Foo\rBar"), "FooBar")
 
    assert_equal(flatten("Foo\nBar"), "Foo&#x000A;Bar")
 
    assert_equal(flatten("Hello\nWorld!\nYOU ARE \rFLAT?\n\rOMGZ!"),
                         "Hello&#x000A;World!&#x000A;YOU ARE FLAT?&#x000A;OMGZ!")
  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.first"))
    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(render("foo\n- tab_up\nbar\n- tab_down\nbaz"), "foo\n bar\nbaz\n")
  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
      proper_behavior = true
    end
    assert(proper_behavior)
  end
  
  def test_action_view_included
    assert(Haml::Helpers.action_view?)
  end
  
  def test_action_view_not_included
    #This is for 100% rcov, rather than any real testing purposes.
    Kernel.module_eval do
      alias_method :old_require, :require
      def require(string)
        raise LoadError if string == "action_view"
        old_require string
      end
    end
 
    load File.dirname(__FILE__) + '/../lib/haml/helpers/action_view_mods.rb'
 
    Kernel.module_eval do
      alias_method :require, :old_require
    end
  end
  
  def test_form_tag
    # Until the next Rails is released, form_tag with a block can have one of
    # two behaviors.
    
    result = render("- form_tag 'foo' do\n %p bar\n %strong baz", :action_view)
    new_rails = "<form action=\"foo\" method=\"post\">\n <p>bar</p>\n <strong>baz</strong>\n</form>"
    old_rails = ""
    assert(result == new_rails || result == old_rails)
  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
end