Class: TestXmlNode

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/vendor/xml_node/test/test_parsing.rb,
lib/vendor/xml_node/test/test_generating.rb

Instance Method Summary collapse

Instance Method Details

#test_add_array_of_nodesObject


69
70
71
# File 'lib/vendor/xml_node/test/test_generating.rb', line 69

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_attributesObject


47
48
49
50
51
# File 'lib/vendor/xml_node/test/test_generating.rb', line 47

def test_attributes
  node = XmlNode.new('feed')
  node['attr'] = 1
  assert_equal '1', node['attr']
end

#test_booleanObject


73
74
75
76
# File 'lib/vendor/xml_node/test/test_generating.rb', line 73

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_cdataObject


28
29
30
31
32
33
34
35
36
37
# File 'lib/vendor/xml_node/test/test_generating.rb', line 28

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_pushingObject


82
83
84
85
86
87
88
# File 'lib/vendor/xml_node/test/test_generating.rb', line 82

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_generationObject


16
17
18
19
20
21
# File 'lib/vendor/xml_node/test/test_generating.rb', line 16

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_childrenObject


25
26
27
28
29
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 25

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_allObject


36
37
38
39
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 36

def test_find_all
  xml = XmlNode.parse('<feed><elem>1</elem><elem>2</elem><elem>3</elem></feed>')
  assert_equal %w(1 2 3), xml.find(:all, '//elem').collect(&:text)
end

#test_find_firstObject


31
32
33
34
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 31

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_xmlObject


65
66
67
# File 'lib/vendor/xml_node/test/test_generating.rb', line 65

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_sanityObject


8
9
10
11
12
13
14
# File 'lib/vendor/xml_node/test/test_generating.rb', line 8

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_namespaceObject


59
60
61
62
63
# File 'lib/vendor/xml_node/test/test_generating.rb', line 59

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_namespaceObject


53
54
55
56
57
# File 'lib/vendor/xml_node/test/test_generating.rb', line 53

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_nestingObject


23
24
25
26
# File 'lib/vendor/xml_node/test/test_generating.rb', line 23

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_nilObject


78
79
80
# File 'lib/vendor/xml_node/test/test_generating.rb', line 78

def test_nil
  assert_equal '<nil/>', XmlNode.new('nil', nil).to_s
end

#test_parse_attributesObject


13
14
15
16
17
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 13

def test_parse_attributes
  node = XmlNode.parse('<feed attr="1"/>')
  assert_equal '1', node['attr']
  assert_equal nil, node['attr2']
end

#test_parse_childrenObject


19
20
21
22
23
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 19

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

#test_parse_sanityObject


8
9
10
11
# File 'lib/vendor/xml_node/test/test_parsing.rb', line 8

def test_parse_sanity
  assert_raise(ArgumentError) { XmlNode.parse }
  assert_nothing_raised { XmlNode.parse('<feed/>') }
end

#test_textObject


39
40
41
42
43
44
45
# File 'lib/vendor/xml_node/test/test_generating.rb', line 39

def test_text
  node = XmlNode.new('feed')
  node.text = 'Hello world'

  assert_equal '<feed>Hello world</feed>', node.to_s
  assert_equal 'Hello world', node.text
end