Class: TestXmlNode
- Defined in:
- lib/vendor/xml_node/test/test_parsing.rb,
lib/vendor/xml_node/test/test_generating.rb
Instance Method Summary collapse
- #test_add_array_of_nodes ⇒ Object
- #test_attributes ⇒ Object
- #test_boolean ⇒ Object
- #test_cdata ⇒ Object
- #test_dont_choke_on_nil_pushing ⇒ Object
- #test_element_generation ⇒ Object
- #test_enumerate_children ⇒ Object
- #test_find_all ⇒ Object
- #test_find_first ⇒ Object
- #test_generate_nice_xml ⇒ Object
- #test_init_sanity ⇒ Object
- #test_named_namespace ⇒ Object
- #test_namespace ⇒ Object
- #test_nesting ⇒ Object
- #test_nil ⇒ Object
- #test_parse_attributes ⇒ Object
- #test_parse_children ⇒ Object
- #test_parse_sanity ⇒ Object
- #test_text ⇒ Object
Instance Method Details
#test_add_array_of_nodes ⇒ Object
70 71 72 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 70 def test_add_array_of_nodes assert_equal '<feed><e>1</e><e>2</e><e>3</e></feed>', XmlNode.new('feed') { |n| n << [1,2,3].collect{ |i| XmlNode.new('e', i) }}.to_s end |
#test_attributes ⇒ Object
48 49 50 51 52 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 48 def test_attributes node = XmlNode.new('feed') node['attr'] = 1 assert_equal '1', node['attr'] end |
#test_boolean ⇒ Object
74 75 76 77 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 74 def test_boolean assert_equal '<boolean>true</boolean>', XmlNode.new('boolean', true).to_s assert_equal '<boolean>false</boolean>', XmlNode.new('boolean', false).to_s end |
#test_cdata ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 29 def test_cdata node = XmlNode.new('feed') node.text = '...' node.cdata = 'Goodbye world' node.cdata = 'Hello world' assert_equal '<feed>...<![CDATA[Hello world]]></feed>', node.to_s assert_equal 'Hello world', node.cdata assert_equal '...', node.text end |
#test_dont_choke_on_nil_pushing ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 84 def test_dont_choke_on_nil_pushing feed = XmlNode.new 'feed' assert_nothing_raised do feed << nil end assert_equal '<feed/>', feed.to_s end |
#test_element_generation ⇒ Object
17 18 19 20 21 22 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 17 def test_element_generation assert_equal '<feed/>', XmlNode.new('feed').to_s assert_equal '<feed>content</feed>', XmlNode.new('feed', 'content').to_s assert_equal "<feed attr='true'>content</feed>", XmlNode.new('feed', 'content', :attr => true).to_s assert_equal "<feed attr='true'/>", XmlNode.new('feed', :attr => true).to_s end |
#test_enumerate_children ⇒ Object
27 28 29 30 31 |
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 27 def test_enumerate_children count = 0 XmlNode.parse('<feed><element>text</element><element>text</element></feed>').children.each { count += 1 } assert_equal 2, count end |
#test_find_all ⇒ Object
38 39 40 41 |
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 38 def test_find_all xml = XmlNode.parse('<feed><elem>1</elem><elem>2</elem><elem>3</elem></feed>') assert_equal ['1', '2', '3'], xml.find(:all, '//elem').collect(&:text) end |
#test_find_first ⇒ Object
33 34 35 36 |
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 33 def test_find_first xml = XmlNode.parse('<feed><elem>1</elem><elem>2</elem><elem>3</elem></feed>') assert_equal '1', xml.find(:first, '//elem').text end |
#test_generate_nice_xml ⇒ Object
66 67 68 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 66 def test_generate_nice_xml assert_equal "<?xml version='1.0'?>\n<feed>\n <element>test</element>\n <element/>\n</feed>", XmlNode.new('feed') { |n| n << XmlNode.new('element', 'test'); n << XmlNode.new('element') }.to_xml end |
#test_init_sanity ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 9 def test_init_sanity assert_raise(ArgumentError) { XmlNode.new } assert_nothing_raised { XmlNode.new('feed')} assert_nothing_raised { XmlNode.new('feed', 'content') } assert_nothing_raised { XmlNode.new('feed', :attribute => true) } assert_nothing_raised { XmlNode.new('feed', 'content', :attribute => true) } end |
#test_named_namespace ⇒ Object
60 61 62 63 64 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 60 def test_named_namespace node = XmlNode.new('feed') node.namespace :opensearch, 'http://a9.com/-/spec/opensearch/1.1/' assert_equal "<feed xmlns:opensearch='http://a9.com/-/spec/opensearch/1.1/'/>", node.to_s end |
#test_namespace ⇒ Object
54 55 56 57 58 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 54 def test_namespace node = XmlNode.new('feed') node.namespace 'http://www.w3.org/2005/Atom' assert_equal "<feed xmlns='http://www.w3.org/2005/Atom'/>", node.to_s end |
#test_nesting ⇒ Object
24 25 26 27 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 24 def test_nesting assert_equal '<feed><element/></feed>', XmlNode.new('feed') { |n| n << XmlNode.new('element') }.to_s assert_equal '<feed><element><id>1</id></element></feed>', XmlNode.new('feed') { |n| n << XmlNode.new('element') { |n| n << XmlNode.new('id', '1')} }.to_s end |
#test_nil ⇒ Object
79 80 81 |
# File 'lib/vendor/xml_node/test/test_generating.rb', line 79 def test_nil assert_equal '<nil/>', XmlNode.new('nil', nil).to_s end |
#test_parse_attributes ⇒ Object
15 16 17 18 19 |
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 15 def test_parse_attributes node = XmlNode.parse('<feed attr="1"/>') assert_equal '1', node['attr'] assert_equal nil, node['attr2'] end |
#test_parse_children ⇒ Object
21 22 23 24 25 |
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 21 def test_parse_children node = XmlNode.parse('<feed><element>text</element></feed>') assert_equal XmlNode, node.children['element'].class assert_equal 'text', node.children['element'].text end |