test_eventbuilder.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env ruby
  2. #--
  3. # Portions copyright 2004 by Jim Weirich (jim@weirichhouse.org).
  4. # Portions copyright 2005 by Sam Ruby (rubys@intertwingly.net).
  5. # All rights reserved.
  6. # Permission is granted for use, copying, modification, distribution,
  7. # and distribution of modified versions of this work as long as the
  8. # above copyright notice is included.
  9. #++
  10. require 'test/unit'
  11. require 'test/preload'
  12. require 'builder'
  13. require 'builder/xmlevents'
  14. class TestEvents < Test::Unit::TestCase
  15. class Target
  16. attr_reader :events
  17. def initialize
  18. @events = []
  19. end
  20. def start_tag(tag, attrs)
  21. @events << [:start_tag, tag, attrs]
  22. end
  23. def end_tag(tag)
  24. @events << [:end_tag, tag]
  25. end
  26. def text(string)
  27. @events << [:text, string]
  28. end
  29. end
  30. def setup
  31. @target = Target.new
  32. @xml = Builder::XmlEvents.new(:target=>@target)
  33. end
  34. def test_simple
  35. @xml.one
  36. expect [:start_tag, :one, nil]
  37. expect [:end_tag, :one]
  38. expect_done
  39. end
  40. def test_nested
  41. @xml.one { @xml.two }
  42. expect [:start_tag, :one, nil]
  43. expect [:start_tag, :two, nil]
  44. expect [:end_tag, :two]
  45. expect [:end_tag, :one]
  46. expect_done
  47. end
  48. def test_text
  49. @xml.one("a")
  50. expect [:start_tag, :one, nil]
  51. expect [:text, "a"]
  52. expect [:end_tag, :one]
  53. expect_done
  54. end
  55. def test_special_text
  56. @xml.one("H&R")
  57. expect [:start_tag, :one, nil]
  58. expect [:text, "H&R"]
  59. expect [:end_tag, :one]
  60. expect_done
  61. end
  62. def test_text_with_entity
  63. @xml.one("H&amp;R")
  64. expect [:start_tag, :one, nil]
  65. expect [:text, "H&amp;R"]
  66. expect [:end_tag, :one]
  67. expect_done
  68. end
  69. def test_attributes
  70. @xml.a(:b=>"c", :x=>"y")
  71. expect [:start_tag, :a, {:x => "y", :b => "c"}]
  72. expect [:end_tag, :a]
  73. expect_done
  74. end
  75. def test_moderately_complex
  76. @xml.tag! "address-book" do |x|
  77. x.entry :id=>"1" do
  78. x.name {
  79. x.first "Bill"
  80. x.last "Smith"
  81. }
  82. x.address "Cincinnati"
  83. end
  84. x.entry :id=>"2" do
  85. x.name {
  86. x.first "John"
  87. x.last "Doe"
  88. }
  89. x.address "Columbus"
  90. end
  91. end
  92. expect [:start_tag, "address-book".intern, nil]
  93. expect [:start_tag, :entry, {:id => "1"}]
  94. expect [:start_tag, :name, nil]
  95. expect [:start_tag, :first, nil]
  96. expect [:text, "Bill"]
  97. expect [:end_tag, :first]
  98. expect [:start_tag, :last, nil]
  99. expect [:text, "Smith"]
  100. expect [:end_tag, :last]
  101. expect [:end_tag, :name]
  102. expect [:start_tag, :address, nil]
  103. expect [:text, "Cincinnati"]
  104. expect [:end_tag, :address]
  105. expect [:end_tag, :entry]
  106. expect [:start_tag, :entry, {:id => "2"}]
  107. expect [:start_tag, :name, nil]
  108. expect [:start_tag, :first, nil]
  109. expect [:text, "John"]
  110. expect [:end_tag, :first]
  111. expect [:start_tag, :last, nil]
  112. expect [:text, "Doe"]
  113. expect [:end_tag, :last]
  114. expect [:end_tag, :name]
  115. expect [:start_tag, :address, nil]
  116. expect [:text, "Columbus"]
  117. expect [:end_tag, :address]
  118. expect [:end_tag, :entry]
  119. expect [:end_tag, "address-book".intern]
  120. expect_done
  121. end
  122. def expect(value)
  123. assert_equal value, @target.events.shift
  124. end
  125. def expect_done
  126. assert_nil @target.events.shift
  127. end
  128. end