PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/gems/haml-1.8.1/test/haml/engine_test.rb

http://huisrekening.googlecode.com/
Ruby | 370 lines | 314 code | 53 blank | 3 comment | 15 complexity | d6fc200078fabb879cb150aee3d47177 MD5 | raw file
  1. #!/usr/bin/env ruby
  2. require 'rubygems'
  3. require 'active_support'
  4. require 'action_controller'
  5. require 'action_view'
  6. require 'test/unit'
  7. require File.dirname(__FILE__) + '/../../lib/haml'
  8. require 'haml/engine'
  9. class EngineTest < Test::Unit::TestCase
  10. def render(text, options = {}, &block)
  11. scope = options.delete(:scope) || Object.new
  12. locals = options.delete(:locals) || {}
  13. Haml::Engine.new(text, options).to_html(scope, locals, &block)
  14. end
  15. def test_empty_render_should_remain_empty
  16. assert_equal('', render(''))
  17. end
  18. def test_attributes_should_render_correctly
  19. assert_equal("<div class='atlantis' style='ugly'>\n</div>", render(".atlantis{:style => 'ugly'}").chomp)
  20. end
  21. def test_ruby_code_should_work_inside_attributes
  22. author = 'hcatlin'
  23. assert_equal("<p class='3'>foo</p>", render("%p{:class => 1+2} foo").chomp)
  24. end
  25. def test_nil_should_render_empty_tag
  26. assert_equal("<div class='no_attributes'>\n</div>",
  27. render(".no_attributes{:nil => nil}").chomp)
  28. end
  29. def test_strings_should_get_stripped_inside_tags
  30. assert_equal("<div class='stripped'>This should have no spaces in front of it</div>",
  31. render(".stripped This should have no spaces in front of it").chomp)
  32. end
  33. def test_one_liner_should_be_one_line
  34. assert_equal("<p>Hello</p>", render('%p Hello').chomp)
  35. end
  36. def test_long_liner_should_not_print_on_one_line
  37. assert_equal("<div>\n #{'x' * 51}\n</div>", render("%div #{'x' * 51}").chomp)
  38. end
  39. def test_non_prerendered_one_liner
  40. assert_equal("<p class='awesome'>One line</p>\n", render("%p{:class => c} One line", :locals => {:c => 'awesome'}))
  41. end
  42. def test_non_prerendered_script_one_liner
  43. assert_equal("<p class='awesome'>One line</p>\n", render("%p{:class => c}= 'One line'", :locals => {:c => 'awesome'}))
  44. end
  45. def test_non_prerendered_long_script_one_liner
  46. assert_equal("<p class='awesome'>\n #{'x' * 60}\n</p>\n", render("%p{:class => c}= 'x' * 60", :locals => {:c => 'awesome'}))
  47. end
  48. def test_multi_render
  49. engine = Haml::Engine.new("%strong Hi there!")
  50. assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
  51. assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
  52. assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
  53. end
  54. def test_double_equals
  55. assert_equal("<p>Hello World</p>\n", render('%p== Hello #{who}', :locals => {:who => 'World'}))
  56. assert_equal("<p>\n Hello World\n</p>\n", render("%p\n == Hello \#{who}", :locals => {:who => 'World'}))
  57. end
  58. def test_double_equals_in_the_middle_of_a_string
  59. assert_equal("\"title 'Title'. \"\n",
  60. render("== \"title '\#{\"Title\"}'. \""))
  61. end
  62. def test_nil_tag_value_should_render_as_empty
  63. assert_equal("<p></p>\n", render("%p= nil"))
  64. end
  65. def test_tag_with_failed_if_should_render_as_empty
  66. assert_equal("<p></p>\n", render("%p= 'Hello' if false"))
  67. end
  68. def test_static_attributes_with_empty_attr
  69. assert_equal("<img alt='' src='/foo.png' />\n", render("%img{:src => '/foo.png', :alt => ''}"))
  70. end
  71. def test_dynamic_attributes_with_empty_attr
  72. assert_equal("<img alt='' src='/foo.png' />\n", render("%img{:width => nil, :src => '/foo.png', :alt => String.new}"))
  73. end
  74. def test_end_of_file_multiline
  75. assert_equal("<p>0</p>\n<p>1</p>\n<p>2</p>\n", render("- for i in (0...3)\n %p= |\n i |"))
  76. end
  77. # Options tests
  78. def test_stop_eval
  79. assert_equal("", render("= 'Hello'", :suppress_eval => true))
  80. assert_equal("", render("- puts 'foo'", :suppress_eval => true))
  81. assert_equal("<div id='foo' yes='no' />\n", render("#foo{:yes => 'no'}/", :suppress_eval => true))
  82. assert_equal("<div id='foo' />\n", render("#foo{:yes => 'no', :call => a_function() }/", :suppress_eval => true))
  83. assert_equal("<div />\n", render("%div[1]/", :suppress_eval => true))
  84. begin
  85. assert_equal("", render(":ruby\n puts 'hello'", :suppress_eval => true))
  86. rescue Haml::HamlError => err
  87. caught = true
  88. assert_equal('"ruby" filter is not defined!', err.message)
  89. end
  90. assert(caught, "Rendering a ruby filter without evaluating didn't throw an error!")
  91. end
  92. def test_attr_wrapper
  93. assert_equal("<p strange=*attrs*>\n</p>\n", render("%p{ :strange => 'attrs'}", :attr_wrapper => '*'))
  94. assert_equal("<p escaped='quo\"te'>\n</p>\n", render("%p{ :escaped => 'quo\"te'}", :attr_wrapper => '"'))
  95. assert_equal("<p escaped=\"q'uo&quot;te\">\n</p>\n", render("%p{ :escaped => 'q\\'uo\"te'}", :attr_wrapper => '"'))
  96. assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n", render("!!! XML", :attr_wrapper => '"'))
  97. end
  98. def test_attrs_parsed_correctly
  99. assert_equal("<p boom=>biddly='bar => baz'>\n</p>\n", render("%p{'boom=>biddly' => 'bar => baz'}"))
  100. assert_equal("<p foo,bar='baz, qux'>\n</p>\n", render("%p{'foo,bar' => 'baz, qux'}"))
  101. assert_equal("<p escaped='quo\nte'>\n</p>\n", render("%p{ :escaped => \"quo\\nte\"}"))
  102. assert_equal("<p escaped='quo4te'>\n</p>\n", render("%p{ :escaped => \"quo\#{2 + 2}te\"}"))
  103. end
  104. def test_empty_attrs
  105. assert_equal("<p attr=''>empty</p>\n", render("%p{ :attr => '' } empty"))
  106. assert_equal("<p attr=''>empty</p>\n", render("%p{ :attr => x } empty", :locals => {:x => ''}))
  107. end
  108. def test_nil_attrs
  109. assert_equal("<p>nil</p>\n", render("%p{ :attr => nil } nil"))
  110. assert_equal("<p>nil</p>\n", render("%p{ :attr => x } nil", :locals => {:x => nil}))
  111. end
  112. def test_nil_id_with_syntactic_id
  113. assert_equal("<p id='foo'>nil</p>\n", render("%p#foo{:id => nil} nil"))
  114. assert_equal("<p id='foo_bar'>nil</p>\n", render("%p#foo{{:id => 'bar'}, :id => nil} nil"))
  115. assert_equal("<p id='foo_bar'>nil</p>\n", render("%p#foo{{:id => nil}, :id => 'bar'} nil"))
  116. end
  117. def test_nil_class_with_syntactic_class
  118. assert_equal("<p class='foo'>nil</p>\n", render("%p.foo{:class => nil} nil"))
  119. assert_equal("<p class='bar foo'>nil</p>\n", render("%p.bar.foo{:class => nil} nil"))
  120. assert_equal("<p class='bar foo'>nil</p>\n", render("%p.foo{{:class => 'bar'}, :class => nil} nil"))
  121. assert_equal("<p class='bar foo'>nil</p>\n", render("%p.foo{{:class => nil}, :class => 'bar'} nil"))
  122. end
  123. def test_locals
  124. assert_equal("<p>Paragraph!</p>\n", render("%p= text", :locals => { :text => "Paragraph!" }))
  125. end
  126. def test_deprecated_locals_option
  127. Kernel.module_eval do
  128. def warn_with_stub(msg); end
  129. alias_method :warn_without_stub, :warn
  130. alias_method :warn, :warn_with_stub
  131. end
  132. assert_equal("<p>Paragraph!</p>\n", Haml::Engine.new("%p= text", :locals => { :text => "Paragraph!" }).render)
  133. Kernel.module_eval { alias_method :warn, :warn_without_stub }
  134. end
  135. def test_dynamic_attrs_shouldnt_register_as_literal_values
  136. assert_equal("<p a='b2c'>\n</p>\n", render('%p{:a => "b#{1 + 1}c"}'))
  137. assert_equal("<p a='b2c'>\n</p>\n", render("%p{:a => 'b' + (1 + 1).to_s + 'c'}"))
  138. end
  139. def test_dynamic_attrs_with_self_closed_tag
  140. assert_equal("<a b='2' />\nc\n", render("%a{'b' => 1 + 1}/\n= 'c'\n"))
  141. end
  142. def test_rec_merge
  143. hash1 = {1=>2, 3=>{5=>7, 8=>9}}
  144. hash2 = {4=>5, 3=>{5=>2, 16=>12}}
  145. hash3 = {1=>2, 4=>5, 3=>{5=>2, 8=>9, 16=>12}}
  146. hash1.rec_merge!(hash2)
  147. assert_equal(hash3, hash1)
  148. end
  149. def test_syntax_errors
  150. errs = [ "!!!\n a", "a\n b", "a\n:foo\nb", "/ a\n b",
  151. "% a", "%p a\n b", "a\n%p=\nb", "%p=\n a",
  152. "a\n%p~\nb", "a\n~\nb", "a\n~\n b", "%p~\n b", "%p/\n a",
  153. "%p\n \t%a b", "%a\n b\nc", "%a\n b\nc",
  154. ":notafilter\n This isn't\n a filter!",
  155. ".{} a", "\#{} a", ".= 'foo'", "%a/ b", "%p..class", "%p..#." ]
  156. errs.each do |err|
  157. begin
  158. render(err)
  159. rescue Exception => e
  160. assert(e.is_a?(Haml::Error), "#{err.dump} doesn't produce Haml::SyntaxError")
  161. else
  162. assert(false, "#{err.dump} doesn't produce an exception")
  163. end
  164. end
  165. end
  166. def test_syntax_error
  167. render("a\nb\n!!!\n c\nd")
  168. rescue Haml::SyntaxError => e
  169. assert_equal(e.message, "Illegal Nesting: Nesting within a header command is illegal.")
  170. assert_equal("(haml):3", e.backtrace[0])
  171. rescue Exception => e
  172. assert(false, '"a\nb\n!!!\n c\nd" doesn\'t produce a Haml::SyntaxError')
  173. else
  174. assert(false, '"a\nb\n!!!\n c\nd" doesn\'t produce an exception')
  175. end
  176. def test_exception
  177. render("%p\n hi\n %a= undefined\n= 12")
  178. rescue Exception => e
  179. assert_match("(haml):3", e.backtrace[0])
  180. else
  181. # Test failed... should have raised an exception
  182. assert(false)
  183. end
  184. def test_compile_error
  185. render("a\nb\n- fee)\nc")
  186. rescue Exception => e
  187. assert_match(/^compile error\n\(haml\):3: syntax error/i, e.message)
  188. else
  189. assert(false,
  190. '"a\nb\n- fee)\nc" doesn\'t produce an exception!')
  191. end
  192. def test_unbalanced_brackets
  193. render('== #{1 + 5} foo #{6 + 7 bar #{8 + 9}')
  194. rescue Haml::SyntaxError => e
  195. assert_equal("Unbalanced brackets.", e.message)
  196. end
  197. def test_no_bluecloth
  198. Kernel.module_eval do
  199. def gem_original_require_with_bluecloth(file)
  200. raise LoadError if file == 'bluecloth'
  201. gem_original_require_without_bluecloth(file)
  202. end
  203. alias_method :gem_original_require_without_bluecloth, :gem_original_require
  204. alias_method :gem_original_require, :gem_original_require_with_bluecloth
  205. end
  206. begin
  207. assert_equal("<h1>Foo</h1>\t<p>- a\n- b</p>\n",
  208. Haml::Engine.new(":markdown\n Foo\n ===\n - a\n - b").to_html)
  209. rescue Haml::HamlError => e
  210. if e.message == "Can't run Markdown filter; required 'bluecloth' or 'redcloth', but none were found"
  211. puts "\nCouldn't require 'bluecloth' or 'redcloth'; skipping a test."
  212. else
  213. raise e
  214. end
  215. end
  216. Kernel.module_eval do
  217. alias_method :gem_original_require, :gem_original_require_without_bluecloth
  218. end
  219. end
  220. def test_no_redcloth
  221. Kernel.module_eval do
  222. def gem_original_require_with_redcloth(file)
  223. raise LoadError if file == 'redcloth'
  224. gem_original_require_without_redcloth(file)
  225. end
  226. alias_method :gem_original_require_without_redcloth, :gem_original_require
  227. alias_method :gem_original_require, :gem_original_require_with_redcloth
  228. end
  229. begin
  230. Haml::Engine.new(":redcloth\n _foo_").to_html
  231. rescue Haml::HamlError
  232. else
  233. assert(false, "No exception raised!")
  234. end
  235. Kernel.module_eval do
  236. alias_method :gem_original_require, :gem_original_require_without_redcloth
  237. end
  238. end
  239. def test_no_redcloth_or_bluecloth
  240. Kernel.module_eval do
  241. def gem_original_require_with_redcloth_and_bluecloth(file)
  242. raise LoadError if file == 'redcloth' || file == 'bluecloth'
  243. gem_original_require_without_redcloth_and_bluecloth(file)
  244. end
  245. alias_method :gem_original_require_without_redcloth_and_bluecloth, :gem_original_require
  246. alias_method :gem_original_require, :gem_original_require_with_redcloth_and_bluecloth
  247. end
  248. begin
  249. Haml::Engine.new(":markdown\n _foo_").to_html
  250. rescue Haml::HamlError
  251. else
  252. assert(false, "No exception raised!")
  253. end
  254. Kernel.module_eval do
  255. alias_method :gem_original_require, :gem_original_require_without_redcloth_and_bluecloth
  256. end
  257. end
  258. def test_local_assigns_dont_modify_class
  259. assert_equal("bar\n", render("= foo", :locals => {:foo => 'bar'}))
  260. assert_equal(nil, defined?(foo))
  261. end
  262. def test_object_ref_with_nil_id
  263. user = Struct.new('User', :id).new
  264. assert_equal("<p class='struct_user' id='struct_user_new'>New User</p>\n",
  265. render("%p[user] New User", :locals => {:user => user}))
  266. end
  267. def test_non_literal_attributes
  268. assert_equal("<p a1='foo' a2='bar' a3='baz' />\n",
  269. render("%p{a2, a1, :a3 => 'baz'}/",
  270. :locals => {:a1 => {:a1 => 'foo'}, :a2 => {:a2 => 'bar'}}))
  271. end
  272. def test_render_should_accept_a_binding_as_scope
  273. string = "This is a string!"
  274. string.instance_variable_set("@var", "Instance variable")
  275. b = string.instance_eval do
  276. var = "Local variable"
  277. binding
  278. end
  279. assert_equal("<p>THIS IS A STRING!</p>\n<p>Instance variable</p>\n<p>Local variable</p>\n",
  280. render("%p= upcase\n%p= @var\n%p= var", :scope => b))
  281. end
  282. def test_yield_should_work_with_binding
  283. assert_equal("12\nFOO\n", render("= yield\n= upcase", :scope => "foo".instance_eval{binding}) { 12 })
  284. end
  285. def test_yield_should_work_with_def_method
  286. s = "foo"
  287. Haml::Engine.new("= yield\n= upcase").def_method(s, :render)
  288. assert_equal("12\nFOO\n", s.render { 12 })
  289. end
  290. def test_def_method_with_module
  291. Haml::Engine.new("= yield\n= upcase").def_method(String, :render_haml)
  292. assert_equal("12\nFOO\n", "foo".render_haml { 12 })
  293. end
  294. def test_def_method_locals
  295. obj = Object.new
  296. Haml::Engine.new("%p= foo\n.bar{:baz => baz}= boom").def_method(obj, :render, :foo, :baz, :boom)
  297. assert_equal("<p>1</p>\n<div baz='2' class='bar'>3</div>\n", obj.render(:foo => 1, :baz => 2, :boom => 3))
  298. end
  299. def test_render_proc_locals
  300. proc = Haml::Engine.new("%p= foo\n.bar{:baz => baz}= boom").render_proc(Object.new, :foo, :baz, :boom)
  301. assert_equal("<p>1</p>\n<div baz='2' class='bar'>3</div>\n", proc[:foo => 1, :baz => 2, :boom => 3])
  302. end
  303. def test_render_proc_with_binding
  304. assert_equal("FOO\n", Haml::Engine.new("= upcase").render_proc("foo".instance_eval{binding}).call)
  305. end
  306. end