File: tc_presence.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 (150 lines) | stat: -rwxr-xr-x 4,918 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
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/ruby

$:.unshift '../lib'

require 'test/unit'
require 'socket'
require 'xmpp4r/rexmladdons'
require 'xmpp4r/presence'
require 'xmpp4r/errors'
include Jabber

class PresenceTest < Test::Unit::TestCase
  def test_create
    x = Presence.new()
    assert_equal("presence", x.name)
    assert_equal("jabber:client", x.namespace)
    assert_equal(nil, x.to)
    assert_equal(nil, x.show)
    assert_equal(nil, x.status)
    assert_equal(nil, x.priority)

    x = Presence.new(:away, "I am away", 23)
    assert_equal("presence", x.name)
    assert_equal(:away, x.show)
    assert_equal("away", x.show.to_s)
    assert_equal("I am away", x.status)
    assert_equal(23, x.priority)
  end

  def test_show
    x = Presence.new()
    assert_equal(nil, x.show)
    assert_raise(RuntimeError) { x.show = "a" }
    assert_equal(nil, x.show)
    assert_raise(RuntimeError) { x.show = 'away' }
    assert_equal(nil, x.show)
    x.show = :away
    assert_equal(:away, x.show)
    x.each_element('show') { |e| assert(e.class == REXML::Element, "<show/> is not REXML::Element") }
    x.show = nil
    assert_equal(nil, x.show)
    x.each_element('show') { |e| assert(true, "<show/> exists after 'show=nil'") }
    x.show = nil
    assert_equal(nil, x.show)

    showelement = REXML::Element.new('show')
    showelement.text = 'chat'
    x.add(showelement)
    assert_equal(:chat, x.show)
  end

  def test_status
    x = Presence.new()
    assert_equal(nil, x.status)
    x.status = "b"
    assert_equal("b", x.status)
    x.each_element('status') { |e| assert(e.class == REXML::Element, "<status/> is not REXML::Element") }
    x.status = nil
    assert_equal(nil, x.status)
    x.each_element('status') { |e| assert(true, "<status/> exists after 'status=nil'") }
    x.status = nil
    assert_equal(nil, x.status)
  end

  def test_priority
    x = Presence.new()
    assert_equal(nil, x.priority)
    x.priority = 5
    assert_equal(5, x.priority)
    x.each_element('priority') { |e| assert(e.class == REXML::Element, "<priority/> is not REXML::Element") }
    x.priority = "5"
    assert_equal(5, x.priority)
    x.priority = nil
    assert_equal(nil, x.priority)
    x.each_element('priority') { |e| assert(true, "<priority/> exists after 'priority=nil'") }
  end

  def test_type
    x = Presence.new()
    assert_equal(nil, x.type)
    x.type = :delete
    assert_equal(nil, x.type)
    x.type = nil
    assert_equal(nil, x.type)
    x.type = nil
    assert_equal(nil, x.type)
    [:error, :probe, :subscribe, :subscribed, :unavailable, :unsubscribe, :unsubscribed, nil].each { |type|
      x.type = type
      assert_equal(type, x.type)
    }
  end

  def test_chaining
    x = Presence.new()
    x.set_show(:xa).set_status("Plundering the fridge.").set_priority(0)
    assert_equal(:xa, x.show)
    assert_equal("Plundering the fridge.", x.status)
    assert_equal(0, x.priority)
  end

  def test_error
    x = Presence.new()
    e = REXML::Element.new('error')
    x.add(e)
    x2 = Presence.new.import(x)
    # test if, after an import, the error element is successfully changed
    # into an ErrorResponse object.
    assert_equal(ErrorResponse, x2.first_element('error').class)
  end

  def test_sample
    x = Presence.new
    require 'rexml/document'
    d = REXML::Document.new("<presence from='astro@spaceboyz.net/versionbot' to='astro@spaceboyz.net/edgarr' xmlns='jabber:client'>\n    <x from='astro@spaceboyz.net/versionbot' stamp='20050823T02:18:42' xmlns='jabber:x:delay'/><show>xa</show>\n    <status>I am the evil fingerprinting robot</status>\n  </presence>")
    x.import(d.root)
    num = 0
    x.each_element('show') { num += 1 }
    assert_equal(1, num)
    assert_equal(:xa, x.show)
    assert_equal('I am the evil fingerprinting robot', x.status)
  end

  def test_xpathbug
    require 'rexml/document'
    d = REXML::Document.new("<tag1 xmlns='ns1'><tag2 xmlns='ns2'/><tada>xa</tada></tag1>")
    x = d.root
    num = 0
    x.each_element('tada') {  num += 1 }
    assert_equal(1, num)
  end

  def test_compare_prio
    assert_equal(0, Presence.new(:chat, '', 5) <=> Presence.new(:chat, '', 5))
    assert_equal(-1, Presence.new(:chat, '', 4) <=> Presence.new(:chat, '', 5))
    assert_equal(1, Presence.new(:chat, '', 4) <=> Presence.new(:chat, '', 3))
    assert_equal(-1, Presence.new(:chat, '', nil) <=> Presence.new(:chat, '', 3))
    assert_equal(1, Presence.new(:chat, '', 10) <=> Presence.new(:chat, '', nil))
    assert_equal(0, Presence.new(:chat, '', nil) <=> Presence.new(:chat, '', nil))
  end

  def test_compare_interest
    unav = Presence.new.set_type(:unavailable)
    assert_equal(0, unav.cmp_interest(unav))
    assert_equal(1, unav.cmp_interest(Presence.new))
    assert_equal(-1, Presence.new.cmp_interest(unav))
    assert_equal(1, Presence.new(:chat).cmp_interest(Presence.new))
    assert_equal(-1, Presence.new(:away).cmp_interest(Presence.new(:dnd)))
  end

end