File: tc_xmppstanza.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 (135 lines) | stat: -rwxr-xr-x 3,315 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
#!/usr/bin/ruby

$:.unshift '../lib'

require 'test/unit'
require 'socket'
require 'xmpp4r/rexmladdons'
require 'xmpp4r/xmppstanza'
require 'xmpp4r/iq'
require 'xmpp4r/feature_negotiation'
require 'xmpp4r/dataforms'
require 'xmpp4r/errors'
include Jabber

class XMPPStanzaTest < Test::Unit::TestCase

  ##
  # Hack: XMPPStanza derives from XMPPElement
  # which enforces element classes to be named at declaration time
  class MyXMPPStanza < XMPPStanza
    name_xmlns 'stanza', 'http://home.gna.org/xmpp4r'
  end

  class MyStanza < XMPPStanza
  end

  def test_from
    x = MyXMPPStanza.new
    assert_equal(nil, x.from)
    assert_equal(x, x.set_from("blop"))
    assert_equal("blop", x.from.to_s)
    x.from = "tada"
    assert_equal("tada", x.from.to_s)
  end

  def test_to
    x = MyXMPPStanza.new
    assert_equal(nil, x.to)
    assert_equal(x, x.set_to("blop"))
    assert_equal("blop", x.to.to_s)
    x.to = "tada"
    assert_equal("tada", x.to.to_s)
  end

  def test_id
    x = MyXMPPStanza.new
    assert_equal(nil, x.id)
    assert_equal(x, x.set_id("blop"))
    assert_equal("blop", x.id)
    x.id = "tada"
    assert_equal("tada", x.id)
  end

  def test_type
    x = MyXMPPStanza.new
    assert_equal(nil, x.type)
    assert_equal(x, x.set_type("blop"))
    assert_equal("blop", x.type)
    x.type = "tada"
    assert_equal("tada", x.type)
  end

  def test_import
    x = MyXMPPStanza.new
    x.id = "heya"
    q = x.add_element("query")
    q.add_namespace("about:blank")
    q.add_element("b").text = "I am b"
    q.add_text("I am text")
    q.add_element("a").add_attribute("href", "http://home.gna.org/xmpp4r/")
    x.add_text("yow")
    x.add_element("query")

    assert_raise(RuntimeError) { iq = Iq.import(x) }
    x.name = 'iq'
    iq = Iq.import(x)

    assert_equal(x.id, iq.id)
    assert_equal(q.to_s, iq.query.to_s)
    assert_equal(x.to_s, iq.to_s)
    assert_equal(q.namespace, iq.queryns)
  end

  def test_import2
    feature = FeatureNegotiation::IqFeature.new
    xdata = feature.add(Dataforms::XData.new(:form))
    field = xdata.add(Dataforms::XDataField.new('stream-method', :list_single))

    feature2 = FeatureNegotiation::IqFeature.new.import(feature)
    assert_equal(field.var, feature2.x.fields.first.var)
    assert_equal(field.type, feature2.x.fields.first.type)
  end

  def test_import_xml_entities
    e = REXML::Element.new('e')
    e.text = '&'
    assert_equal('<e>&amp;</e>', e.to_s)

    e2 = REXML::Element.new('e').import(e)
    assert_equal('<e>&amp;</e>', e2.to_s)
  end

  def test_error
    x = MyXMPPStanza.new
    assert_equal(nil, x.error)
    x.typed_add(REXML::Element.new('error'))
    assert_equal('<error/>', x.error.to_s)
    assert_equal(ErrorResponse, x.error.class)
  end

  def test_clone_and_dup
    x = MyXMPPStanza.new
    x.attributes['xyz'] = '123'
    x.text = 'abc'

    assert_equal(x.attributes['xyz'], '123')
    assert_equal(x.text, 'abc')

    x2 = x.clone
    assert_kind_of(MyXMPPStanza, x2)
    assert_equal('123', x2.attributes['xyz'])
    assert_nil(x2.text)

    x3 = x.dup
    assert_kind_of(MyXMPPStanza, x3)
    assert_equal('123', x3.attributes['xyz'])
    assert_equal('abc', x3.text)
  end

  def test_raise
    assert_raises(NoNameXmlnsRegistered) {
      XMPPStanza.name_xmlns_for_class(MyStanza)
    }
  end
end