/factory_girl/ruby/1.8/gems/nokogiri-1.5.0/test/xml/test_builder.rb

https://github.com/somasekar/My_First_Application · Ruby · 227 lines · 198 code · 29 blank · 0 comment · 0 complexity · 05d64c8777478d19a60da8f4fd909bd2 MD5 · raw file

  1. require "helper"
  2. module Nokogiri
  3. module XML
  4. class TestBuilder < Nokogiri::TestCase
  5. def test_attribute_sensitivity
  6. xml = Nokogiri::XML::Builder.new { |x|
  7. x.tag "hello", "abcDef" => "world"
  8. }.to_xml
  9. doc = Nokogiri.XML xml
  10. assert_equal 'world', doc.root['abcDef']
  11. end
  12. def test_builder_escape
  13. xml = Nokogiri::XML::Builder.new { |x|
  14. x.condition "value < 1", :attr => "value < 1"
  15. }.to_xml
  16. doc = Nokogiri.XML xml
  17. assert_equal 'value < 1', doc.root['attr']
  18. assert_equal 'value < 1', doc.root.content
  19. end
  20. def test_builder_namespace
  21. doc = Nokogiri::XML::Builder.new { |xml|
  22. xml.a("xmlns:a" => "x") do
  23. xml.b("xmlns:a" => "x", "xmlns:b" => "y")
  24. end
  25. }.doc
  26. b = doc.at('b')
  27. assert b
  28. assert_equal({"xmlns:a"=>"x", "xmlns:b"=>"y"}, b.namespaces)
  29. assert_equal({"xmlns:b"=>"y"}, namespaces_defined_on(b))
  30. end
  31. def test_builder_namespace_part_deux
  32. doc = Nokogiri::XML::Builder.new { |xml|
  33. xml.a("xmlns:b" => "y") do
  34. xml.b("xmlns:a" => "x", "xmlns:b" => "y", "xmlns:c" => "z")
  35. end
  36. }.doc
  37. b = doc.at('b')
  38. assert b
  39. assert_equal({"xmlns:a"=>"x", "xmlns:b"=>"y", "xmlns:c"=>"z"}, b.namespaces)
  40. assert_equal({"xmlns:a"=>"x", "xmlns:c"=>"z"}, namespaces_defined_on(b))
  41. end
  42. def test_builder_with_unlink
  43. assert_nothing_raised do
  44. Nokogiri::XML::Builder.new do |xml|
  45. xml.foo do
  46. xml.bar { xml.parent.unlink }
  47. xml.bar2
  48. end
  49. end
  50. end
  51. end
  52. def test_with_root
  53. doc = Nokogiri::XML(File.read(XML_FILE))
  54. Nokogiri::XML::Builder.with(doc.at('employee')) do |xml|
  55. xml.foo
  56. end
  57. assert_equal 1, doc.xpath('//employee/foo').length
  58. end
  59. def test_root_namespace_default_decl
  60. b = Nokogiri::XML::Builder.new { |xml| xml.root(:xmlns => 'one:two') }
  61. doc = b.doc
  62. assert_equal 'one:two', doc.root.namespace.href
  63. assert_equal({ 'xmlns' => 'one:two' }, doc.root.namespaces)
  64. end
  65. def test_root_namespace_multi_decl
  66. b = Nokogiri::XML::Builder.new { |xml|
  67. xml.root(:xmlns => 'one:two', 'xmlns:foo' => 'bar') do
  68. xml.hello
  69. end
  70. }
  71. doc = b.doc
  72. assert_equal 'one:two', doc.root.namespace.href
  73. assert_equal({ 'xmlns' => 'one:two', 'xmlns:foo' => 'bar' }, doc.root.namespaces)
  74. assert_equal 'one:two', doc.at('hello').namespace.href
  75. end
  76. def test_non_root_namespace
  77. b = Nokogiri::XML::Builder.new { |xml|
  78. xml.root { xml.hello(:xmlns => 'one') }
  79. }
  80. assert_equal 'one', b.doc.at('hello', 'xmlns' => 'one').namespace.href
  81. end
  82. def test_specify_namespace
  83. b = Nokogiri::XML::Builder.new { |xml|
  84. xml.root('xmlns:foo' => 'bar') do
  85. xml[:foo].bar
  86. xml['foo'].baz
  87. end
  88. }
  89. doc = b.doc
  90. assert_equal 'bar', doc.at('foo|bar', 'foo' => 'bar').namespace.href
  91. assert_equal 'bar', doc.at('foo|baz', 'foo' => 'bar').namespace.href
  92. end
  93. def test_specify_namespace_nested
  94. b = Nokogiri::XML::Builder.new { |xml|
  95. xml.root('xmlns:foo' => 'bar') do
  96. xml.yay do
  97. xml[:foo].bar
  98. xml.yikes do
  99. xml['foo'].baz
  100. end
  101. end
  102. end
  103. }
  104. doc = b.doc
  105. assert_equal 'bar', doc.at('foo|bar', 'foo' => 'bar').namespace.href
  106. assert_equal 'bar', doc.at('foo|baz', 'foo' => 'bar').namespace.href
  107. end
  108. def test_specified_namespace_undeclared
  109. Nokogiri::XML::Builder.new { |xml|
  110. xml.root do
  111. assert_raises(ArgumentError) do
  112. xml[:foo]
  113. end
  114. end
  115. }
  116. end
  117. def test_set_encoding
  118. builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
  119. xml.root do
  120. xml.bar 'blah'
  121. end
  122. end
  123. assert_match 'UTF-8', builder.to_xml
  124. end
  125. def test_bang_and_underscore_is_escaped
  126. builder = Nokogiri::XML::Builder.new do |xml|
  127. xml.root do
  128. xml.p_('adsfadsf')
  129. xml.p!('adsfadsf')
  130. end
  131. end
  132. assert_equal 2, builder.doc.xpath('//p').length
  133. end
  134. def test_square_brackets_set_attributes
  135. builder = Nokogiri::XML::Builder.new do |xml|
  136. xml.root do
  137. foo = xml.foo
  138. foo['id'] = 'hello'
  139. assert_equal 'hello', foo['id']
  140. end
  141. end
  142. assert_equal 1, builder.doc.xpath('//foo[@id = "hello"]').length
  143. end
  144. def test_nested_local_variable
  145. @ivar = 'hello'
  146. local_var = 'hello world'
  147. builder = Nokogiri::XML::Builder.new do |xml|
  148. xml.root do
  149. xml.foo local_var
  150. xml.bar @ivar
  151. xml.baz {
  152. xml.text @ivar
  153. }
  154. end
  155. end
  156. assert_equal 'hello world', builder.doc.at('//root/foo').content
  157. assert_equal 'hello', builder.doc.at('//root/bar').content
  158. assert_equal 'hello', builder.doc.at('baz').content
  159. end
  160. def test_raw_append
  161. builder = Nokogiri::XML::Builder.new do |xml|
  162. xml.root do
  163. xml << 'hello'
  164. end
  165. end
  166. assert_equal 'hello', builder.doc.at('/root').content
  167. end
  168. def test_raw_append_with_instance_eval
  169. builder = Nokogiri::XML::Builder.new do
  170. root do
  171. self << 'hello'
  172. end
  173. end
  174. assert_equal 'hello', builder.doc.at('/root').content
  175. end
  176. def test_cdata
  177. builder = Nokogiri::XML::Builder.new do
  178. root {
  179. cdata "hello world"
  180. }
  181. end
  182. assert_equal("<?xml version=\"1.0\"?><root><![CDATA[hello world]]></root>", builder.to_xml.gsub(/\n/, ''))
  183. end
  184. def test_builder_no_block
  185. string = "hello world"
  186. builder = Nokogiri::XML::Builder.new
  187. builder.root {
  188. cdata string
  189. }
  190. assert_equal("<?xml version=\"1.0\"?><root><![CDATA[hello world]]></root>", builder.to_xml.gsub(/\n/, ''))
  191. end
  192. private
  193. def namespaces_defined_on(node)
  194. Hash[*node.namespace_definitions.collect{|n| ["xmlns:" + n.prefix, n.href]}.flatten]
  195. end
  196. end
  197. end
  198. end