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

/tools/drillbit/Resources/tests/ruby/markaby/test/test_markaby.rb

http://github.com/appcelerator/titanium_desktop
Ruby | 109 lines | 95 code | 14 blank | 0 comment | 0 complexity | 21f01735ae96b67d97547909e4bafdcb MD5 | raw file
Possible License(s): Apache-2.0
  1. require 'test/unit'
  2. require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'markaby'))
  3. module MarkabyTestHelpers
  4. def link_to(obj)
  5. %{<a href="">#{obj}</a>}
  6. end
  7. def pluralize(string)
  8. string + "s"
  9. end
  10. module_function :link_to, :pluralize
  11. end
  12. class MarkabyTest < Test::Unit::TestCase
  13. def mab(*args, &block)
  14. Markaby::Builder.new(*args, &block).to_s
  15. end
  16. def assert_exception(exclass, exmsg, *mab_args, &block)
  17. begin
  18. mab(*mab_args, &block)
  19. rescue Exception => e
  20. assert_equal exclass, e.class
  21. assert_equal exmsg, e.message
  22. end
  23. end
  24. def test_simple
  25. assert_equal "<hr/>", mab { hr }
  26. assert_equal "<p>foo</p>", mab { p 'foo' }
  27. assert_equal "<p>foo</p>", mab { p { 'foo' } }
  28. end
  29. def test_classes_and_ids
  30. assert_equal %{<div class="one"></div>}, mab { div.one '' }
  31. assert_equal %{<div class="one two"></div>}, mab { div.one.two '' }
  32. assert_equal %{<div id="three"></div>}, mab { div.three! '' }
  33. end
  34. def test_escaping
  35. assert_equal "<h1>Apples &amp; Oranges</h1>", mab { h1 'Apples & Oranges' }
  36. assert_equal "<h1>Apples & Oranges</h1>", mab { h1 { 'Apples & Oranges' } }
  37. assert_equal "<h1 class=\"fruits&amp;floots\">Apples</h1>", mab { h1 'Apples', :class => 'fruits&floots' }
  38. end
  39. def test_capture
  40. builder = Markaby::Builder.new
  41. assert builder.to_s.empty?
  42. assert_equal "<h1>TEST</h1>", builder.capture { h1 'TEST' }
  43. assert builder.to_s.empty?
  44. assert mab { capture { h1 'hello world' }; nil }.empty?
  45. assert_equal mab { div { h1 'TEST' } }, mab { div { capture { h1 'TEST' } } }
  46. end
  47. def test_ivars
  48. html = "<div><h1>Steve</h1><div><h2>Gerald</h2></div><h3>Gerald</h3></div>"
  49. assert_equal html, mab { div { @name = 'Steve'; h1 @name; div { @name = 'Gerald'; h2 @name }; h3 @name } }
  50. assert_equal html, mab { div { @name = 'Steve'; h1 @name; self << capture { div { @name = 'Gerald'; h2 @name } }; h3 @name } }
  51. assert_equal html, mab(:name => 'Steve') { div { h1 @name; self << capture { div { @name = 'Gerald'; h2 @name } }; h3 @name } }
  52. end
  53. def test_ivars_without_at_symbol
  54. assert_equal "<h1>Hello World</h1>", mab { @message = 'Hello World'; h1 message }
  55. end
  56. def test_helpers
  57. Markaby::Builder.ignored_helpers.clear
  58. assert_equal %{squirrels}, mab({}, MarkabyTestHelpers) { pluralize('squirrel') }
  59. assert_equal %{<a href="">edit</a>}, mab({}, MarkabyTestHelpers) { link_to('edit') }
  60. assert mab({}, MarkabyTestHelpers) { @output_helpers = false; link_to('edit'); nil }.empty?
  61. Markaby::Builder.ignore_helpers :pluralize
  62. assert_exception(NoMethodError, "no such method `pluralize'", {}, MarkabyTestHelpers) { pluralize('squirrel') }
  63. end
  64. def test_builder_bang_methods
  65. assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", mab { instruct! }
  66. end
  67. def test_fragments
  68. assert_equal %{<div><h1>Monkeys</h1><h2>Giraffes <small>Miniature</small> and <strong>Large</strong></h2><h3>Donkeys</h3><h4>Parakeet <b><i>Innocent IV</i></b> in Classic Chartreuse</h4></div>},
  69. mab { div { h1 "Monkeys"; h2 { "Giraffes #{small 'Miniature' } and #{strong 'Large'}" }; h3 "Donkeys"; h4 { "Parakeet #{b { i 'Innocent IV' }} in Classic Chartreuse" } } }
  70. assert_equal %{<div><h1>Monkeys</h1><h2>Giraffes <strong>Miniature</strong></h2><h3>Donkeys</h3></div>},
  71. mab { div { h1 "Monkeys"; h2 { "Giraffes #{strong 'Miniature' }" }; h3 "Donkeys" } }
  72. assert_equal %{<div><h1>Monkeys</h1><h2>Giraffes <small>Miniature</small> and <strong>Large</strong></h2><h3>Donkeys</h3><h4>Parakeet <strong>Large</strong> as well...</h4></div>},
  73. mab { div { @a = small 'Miniature'; @b = strong 'Large'; h1 "Monkeys"; h2 { "Giraffes #{@a} and #{@b}" }; h3 "Donkeys"; h4 { "Parakeet #{@b} as well..." } } }
  74. end
  75. def test_invalid_xhtml
  76. assert_exception(NoMethodError, "no such method `dav'") { dav {} }
  77. assert_exception(Markaby::InvalidXhtmlError, "no attribute `styl' on div elements") { div(:styl => 'ok') {} }
  78. assert_exception(Markaby::InvalidXhtmlError, "no attribute `class' on tbody elements") { tbody.okay {} }
  79. end
  80. def test_full_doc_transitional
  81. doc = mab { instruct!; xhtml_transitional { head { title 'OKay' } } }
  82. assert doc =~ /^<\?xml version="1.0" encoding="UTF-8"\?>/
  83. assert doc.include?(%{"-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">})
  84. assert doc.include?(%{<title>OKay</title>})
  85. end
  86. def test_full_doc_strict
  87. doc = mab { xhtml_strict { head { title 'OKay' } } }
  88. assert doc =~ /^<\?xml version="1.0" encoding="UTF-8"\?>/
  89. assert doc.include?(%{"-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">})
  90. assert doc.include?(%{<title>OKay</title>})
  91. end
  92. end