File: tc_rexml.rb

package info (click to toggle)
ruby-xmpp4r 0.5.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,384 kB
  • sloc: ruby: 17,382; xml: 74; sh: 12; makefile: 4
file content (139 lines) | stat: -rwxr-xr-x 4,127 bytes parent folder | download | duplicates (2)
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
137
138
139
#!/usr/bin/ruby

$:.unshift File::dirname(__FILE__) + '/../lib'

require 'test/unit'
require 'xmpp4r/rexmladdons'

class REXMLTest < Test::Unit::TestCase
  def test_simple
    e = REXML::Element.new('e')
    assert_kind_of(REXML::Element, e)
    assert_nil(e.text)
    assert_nil(e.attributes['x'])
  end

  def test_normalize
    assert_equal('&amp;', REXML::Text::normalize('&'))
    assert_equal('&amp;amp;', REXML::Text::normalize('&amp;'))
    assert_equal('&amp;amp;amp;', REXML::Text::normalize('&amp;amp;'))
    assert_equal('&amp;nbsp;', REXML::Text::normalize('&nbsp;'))
  end

  def test_unnormalize
    assert_equal('&', REXML::Text::unnormalize('&amp;'))
    assert_equal('&amp;', REXML::Text::unnormalize('&amp;amp;'))
    assert_equal('&amp;amp;', REXML::Text::unnormalize('&amp;amp;amp;'))
    assert_equal('&nbsp;', REXML::Text::unnormalize('&amp;nbsp;'))
    assert_equal('&nbsp;', REXML::Text::unnormalize('&nbsp;'))  # ?
  end

  def test_text_entities
    e = REXML::Element.new('e')
    e.text = '&'
    assert_equal('<e>&amp;</e>', e.to_s)
    e.text = '&amp;'
    assert_equal('<e>&amp;amp;</e>', e.to_s)
    e.text = '&nbsp'
    assert_equal('<e>&amp;nbsp</e>', e.to_s)
    e.text = '&nbsp;'
    assert_equal('<e>&amp;nbsp;</e>', e.to_s)
    e.text = '&<;'
    assert_equal('<e>&amp;&lt;;</e>', e.to_s)
    e.text = '<>"\''
    assert_equal('<e>&lt;&gt;&quot;&apos;</e>', e.to_s)
    e.text = '<x>&amp;</x>'
    assert_equal('<e>&lt;x&gt;&amp;amp;&lt;/x&gt;</e>', e.to_s)
  end

  def test_attribute_entites
    e = REXML::Element.new('e')
    e.attributes['x'] = '&'
    assert_equal('&', e.attributes['x'])
    e.attributes['x'] = '&amp;'
    # bug in REXML 3.1.6 unescaped the ampersand
    # assert_equal('&', e.attributes['x'])
    # substituting a test that works with 3.1.5, 3.1.6, and 3.1.7
    assert_equal('&amp;amp;', e.attribute('x').to_s)
    e.attributes['x'] = '&nbsp'
    assert_equal('&nbsp', e.attributes['x'])
    e.attributes['x'] = '&nbsp;'
    assert_equal('&nbsp;', e.attributes['x'])
  end

  # test '==(o)'

  def test_passing_in_non_rexml_element_as_self
    o = Object.new
    assert_not_equal(o, REXML::Element.new('foo'))
  end

  def test_passing_in_non_rexml_element_as_comparison_object
    o = Object.new
    assert_not_equal(REXML::Element.new('foo'), o)
  end

  def test_element_equal_simple
    assert_equal(REXML::Element.new('foo'), REXML::Element.new('foo'))
  end

  def test_element_not_equal_simple
    assert_not_equal(REXML::Element.new('foo'), REXML::Element.new('bar'))
  end

  def test_element_equal_when_all_are_same
    e1 = REXML::Element.new('foo')
    e1.add_namespace('a', 'urn:test:foo')
    e1.attributes['a:bar'] = 'baz'
    e2 = REXML::Element.new('foo')
    e2.add_namespace('a', 'urn:test:foo')
    e2.attributes['a:bar'] = 'baz'

    assert_equal(e1, e2)
  end

  def test_element_not_equal_when_namespace_name_differs
    e1 = REXML::Element.new('foo')
    e1.add_namespace('a', 'urn:test:foo')
    e1.attributes['a:bar'] = 'baz'
    e2 = REXML::Element.new('foo')
    e2.add_namespace('b', 'urn:test:foo')
    e2.attributes['a:bar'] = 'baz'

    assert_not_equal(e1, e2)
  end

  def test_element_not_equal_when_namespace_value_differs
    e1 = REXML::Element.new('foo')
    e1.add_namespace('a', 'urn:test:foo')
    e1.attributes['a:bar'] = 'baz'
    e2 = REXML::Element.new('foo')
    e2.add_namespace('a', 'urn:test:bar')
    e2.attributes['a:bar'] = 'baz'

    assert_not_equal(e1, e2)
  end

  def test_element_not_equal_when_attribute_name_differs
    e1 = REXML::Element.new('foo')
    e1.add_namespace('a', 'urn:test:foo')
    e1.attributes['a:bar'] = 'baz'
    e2 = REXML::Element.new('foo')
    e1.add_namespace('a', 'urn:test:foo')
    e2.attributes['b:bar'] = 'baz'

    assert_not_equal(e1, e2)
  end

  def test_element_not_equal_when_attribute_value_differs
    e1 = REXML::Element.new('foo')
    e1.add_namespace('a', 'urn:test:foo')
    e1.attributes['a:bar'] = 'baz'
    e2 = REXML::Element.new('foo')
    e1.add_namespace('a', 'urn:test:foo')
    e2.attributes['a:bar'] = 'bar'

    assert_not_equal(e1, e2)
  end

end