File: test_attributes.rb

package info (click to toggle)
ruby-libxml 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,040 kB
  • sloc: xml: 18,263; ansic: 8,344; ruby: 6,987; makefile: 3
file content (136 lines) | stat: -rw-r--r-- 4,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# encoding: UTF-8

require File.expand_path('../test_helper', __FILE__)

class AttributesTest < Minitest::Test
  def setup
    xp = XML::Parser.string(<<-EOS)
    <CityModel name="value"
      xmlns="http://www.opengis.net/examples"
      xmlns:city="http://www.opengis.net/examples"
      xmlns:gml="http://www.opengis.net/gml"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
      xsi:schemaLocation="http://www.opengis.net/examples city.xsd">
     <cityMember name="Cambridge" 
                 xlink:type="simple"
                 xlink:title="Trinity Lane"
                 xlink:href="http://www.foo.net/cgi-bin/wfs?FeatureID=C10239"
                 gml:remoteSchema="city.xsd#xpointer(//complexType[@name='RoadType'])"/>
    </CityModel>
    EOS
    
    @doc = xp.parse
  end
  
  def teardown
    @doc = nil
  end
  
  def city_member
    @doc.find('/city:CityModel/city:cityMember').first
  end

  def test_attributes
    attributes = city_member.attributes
    assert_instance_of(XML::Attributes, attributes)
    assert_equal(5, attributes.length)
  end

  def test_each
    attributes = city_member.attributes
    length = attributes.inject(0) do |result, attr|
      assert_instance_of(XML::Attr, attr)
      result + 1
    end
    assert_equal(5, length)
  end

  def test_get_attribute
    attributes = city_member.attributes

    attr = attributes.get_attribute('name')
    assert_instance_of(XML::Attr, attr)

    attr = attributes.get_attribute('does_not_exist')
    assert_nil(attr)

    attr = attributes.get_attribute('name')
    assert_instance_of(XML::Attr, attr)

    attr = attributes.get_attribute('href')
    assert_instance_of(XML::Attr, attr)
    assert_instance_of(XML::Namespace, attr.ns)
    assert_equal('xlink', attr.ns.prefix)
    assert_equal('http://www.w3.org/1999/xlink', attr.ns.href)

    attr = attributes.get_attribute_ns('http://www.w3.org/1999/xlink', 'href')
    assert_instance_of(XML::Attr, attr)

    attr = attributes.get_attribute_ns('http://www.opengis.net/gml', 'remoteSchema')
    assert_instance_of(XML::Attr, attr)

    attr = attributes.get_attribute_ns('http://i.dont.exist', 'nor do i')
    assert_nil(attr)
  end

  def test_get_values
    assert_equal('Cambridge', city_member[:name])
    assert_equal('http://www.foo.net/cgi-bin/wfs?FeatureID=C10239', city_member[:href])

    attributes = city_member.attributes
    assert_equal('Cambridge', attributes[:name])
    assert_equal('http://www.foo.net/cgi-bin/wfs?FeatureID=C10239', attributes[:href])
  end

  def test_get_values_gc
    # There used to be a bug caused by accessing an
    # attribute over and over and over again.
    20000.times do
      @doc.root.attributes["key"]
    end
  end

  def test_set_values
    city_member[:name] = 'London'
    assert_equal('London', city_member[:name])

    city_member[:href] = 'foo'
    assert_equal('foo', city_member[:href])

    attributes = city_member.attributes

    attributes[:name] = 'London'
    assert_equal('London', attributes[:name])

    attributes[:href] = 'foo'
    assert_equal('foo', attributes[:href])
  end

  def test_str_sym()
    attributes = city_member.attributes
    assert_equal('Cambridge', attributes[:name])
    assert_equal('Cambridge', attributes['name'])
  end

  def test_remove_first
    attributes = @doc.find_first('/city:CityModel/city:cityMember').attributes
    assert_equal(5, attributes.length)
    attr = attributes.first
    attr.remove!
    assert_equal(4, attributes.length)
  end

  def test_remove_all
    node = @doc.find_first('/city:CityModel/city:cityMember')
    assert_equal(5, node.attributes.length)

    attrs = Array.new
    node.attributes.each do |attr|
      attrs << attr
      attr.remove!
    end
    assert_equal(5, attrs.length)
    assert_equal(0, node.attributes.length)
  end
end