PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/blather/stanza/discos/disco_info_spec.rb

http://github.com/sprsquish/blather
Ruby | 247 lines | 206 code | 41 blank | 0 comment | 52 complexity | 8d65392abf8894071915fd806f24bcd9 MD5 | raw file
  1. require 'spec_helper'
  2. def disco_info_xml
  3. <<-XML
  4. <iq type='result'
  5. from='romeo@montague.net/orchard'
  6. to='juliet@capulet.com/balcony'
  7. id='info4'>
  8. <query xmlns='http://jabber.org/protocol/disco#info'>
  9. <identity
  10. category='client'
  11. type='pc'
  12. name='Gabber'
  13. xml:lang='en'/>
  14. <feature var='jabber:iq:time'/>
  15. <feature var='jabber:iq:version'/>
  16. </query>
  17. </iq>
  18. XML
  19. end
  20. describe Blather::Stanza::Iq::DiscoInfo do
  21. it 'registers itself' do
  22. Blather::XMPPNode.class_from_registration(:query, 'http://jabber.org/protocol/disco#info').should == Blather::Stanza::Iq::DiscoInfo
  23. end
  24. it 'must be importable' do
  25. Blather::XMPPNode.parse(disco_info_xml).should be_instance_of Blather::Stanza::Iq::DiscoInfo
  26. end
  27. it 'has a node attribute' do
  28. n = Blather::Stanza::Iq::DiscoInfo.new nil, 'music', [], []
  29. n.node.should == 'music'
  30. n.node = :foo
  31. n.node.should == 'foo'
  32. end
  33. it 'inherits a list of identities' do
  34. n = parse_stanza disco_info_xml
  35. r = Blather::Stanza::Iq::DiscoInfo.new.inherit n.root
  36. r.identities.size.should == 1
  37. r.identities.map { |i| i.class }.uniq.should == [Blather::Stanza::Iq::DiscoInfo::Identity]
  38. end
  39. it 'inherits a list of features' do
  40. n = parse_stanza disco_info_xml
  41. r = Blather::Stanza::Iq::DiscoInfo.new.inherit n.root
  42. r.features.size.should == 2
  43. r.features.map { |i| i.class }.uniq.should == [Blather::Stanza::Iq::DiscoInfo::Feature]
  44. end
  45. it 'is constructed properly' do
  46. n = Blather::Stanza::Iq::DiscoInfo.new :get, '/path/to/node'
  47. n.to = 'to@jid.com'
  48. n.find("/iq[@to='to@jid.com' and @type='get' and @id='#{n.id}']/ns:query[@node='/path/to/node']", :ns => Blather::Stanza::Iq::DiscoInfo.registered_ns).should_not be_empty
  49. end
  50. it 'allows adding of identities' do
  51. di = Blather::Stanza::Iq::DiscoInfo.new
  52. di.identities.size.should == 0
  53. di.identities = [{:name => 'name', :type => 'type', :category => 'category'}]
  54. di.identities.size.should == 1
  55. di.identities += [Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type category])]
  56. di.identities.size.should == 2
  57. di.identities = nil
  58. di.identities.size.should == 0
  59. end
  60. it 'allows adding of features' do
  61. di = Blather::Stanza::Iq::DiscoInfo.new
  62. di.features.size.should == 0
  63. di.features = ["feature1"]
  64. di.features.size.should == 1
  65. di.features += [Blather::Stanza::Iq::DiscoInfo::Feature.new("feature2")]
  66. di.features.size.should == 2
  67. di.features = nil
  68. di.features.size.should == 0
  69. end
  70. end
  71. describe 'Blather::Stanza::Iq::DiscoInfo identities' do
  72. it 'takes a list of hashes for identities' do
  73. ids = [
  74. {:name => 'name', :type => 'type', :category => 'category'},
  75. {:name => 'name1', :type => 'type1', :category => 'category1'},
  76. ]
  77. control = [ Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type category]),
  78. Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name1 type1 category1])]
  79. di = Blather::Stanza::Iq::DiscoInfo.new nil, nil, ids
  80. di.identities.size.should == 2
  81. di.identities.each { |i| control.include?(i).should == true }
  82. end
  83. it 'takes a list of Identity objects as identities' do
  84. control = [ Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type category]),
  85. Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name1 type1 category1])]
  86. di = Blather::Stanza::Iq::DiscoInfo.new nil, nil, control
  87. di.identities.size.should == 2
  88. di.identities.each { |i| control.include?(i).should == true }
  89. end
  90. it 'takes a single hash as identity' do
  91. control = [Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type category])]
  92. di = Blather::Stanza::Iq::DiscoInfo.new nil, nil, {:name => 'name', :type => 'type', :category => 'category'}
  93. di.identities.size.should == 1
  94. di.identities.each { |i| control.include?(i).should == true }
  95. end
  96. it 'takes a single identity object as identity' do
  97. control = [Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type category])]
  98. di = Blather::Stanza::Iq::DiscoInfo.new nil, nil, control.first
  99. di.identities.size.should == 1
  100. di.identities.each { |i| control.include?(i).should == true }
  101. end
  102. it 'takes a mix of hashes and identity objects as identities' do
  103. ids = [
  104. {:name => 'name', :type => 'type', :category => 'category'},
  105. Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name1 type1 category1]),
  106. ]
  107. control = [ Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type category]),
  108. Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name1 type1 category1])]
  109. di = Blather::Stanza::Iq::DiscoInfo.new nil, nil, ids
  110. di.identities.size.should == 2
  111. di.identities.each { |i| control.include?(i).should == true }
  112. end
  113. end
  114. describe 'Blather::Stanza::Iq::DiscoInfo features' do
  115. it 'takes a list of features as strings' do
  116. features = %w[feature1 feature2 feature3]
  117. control = features.map { |f| Blather::Stanza::Iq::DiscoInfo::Feature.new f }
  118. di = Blather::Stanza::Iq::DiscoInfo.new nil, nil, [], features
  119. di.features.size.should == 3
  120. di.features.each { |f| control.include?(f).should == true }
  121. end
  122. it 'takes a list of features as Feature objects' do
  123. features = %w[feature1 feature2 feature3]
  124. control = features.map { |f| Blather::Stanza::Iq::DiscoInfo::Feature.new f }
  125. di = Blather::Stanza::Iq::DiscoInfo.new nil, nil, [], control
  126. di.features.size.should == 3
  127. di.features.each { |f| control.include?(f).should == true }
  128. end
  129. it 'takes a single string' do
  130. control = [Blather::Stanza::Iq::DiscoInfo::Feature.new('feature1')]
  131. di = Blather::Stanza::Iq::DiscoInfo.new nil, nil, [], 'feature1'
  132. di.features.size.should == 1
  133. di.features.each { |f| control.include?(f).should == true }
  134. end
  135. it 'takes a single Feature object' do
  136. control = [Blather::Stanza::Iq::DiscoInfo::Feature.new('feature1')]
  137. di = Blather::Stanza::Iq::DiscoInfo.new nil, nil, [], control.first
  138. di.features.size.should == 1
  139. di.features.each { |f| control.include?(f).should == true }
  140. end
  141. it 'takes a mixed list of features as Feature objects and strings' do
  142. features = %w[feature1 feature2 feature3]
  143. control = features.map { |f| Blather::Stanza::Iq::DiscoInfo::Feature.new f }
  144. features[1] = control[1]
  145. di = Blather::Stanza::Iq::DiscoInfo.new nil, nil, [], features
  146. di.features.size.should == 3
  147. di.features.each { |f| control.include?(f).should == true }
  148. end
  149. end
  150. describe Blather::Stanza::Iq::DiscoInfo::Identity do
  151. it 'will auto-inherit nodes' do
  152. n = parse_stanza "<identity name='Personal Events' type='pep' category='pubsub' node='publish' xml:lang='en' />"
  153. i = Blather::Stanza::Iq::DiscoInfo::Identity.new n.root
  154. i.name.should == 'Personal Events'
  155. i.type.should == :pep
  156. i.category.should == :pubsub
  157. i.xml_lang.should == 'en'
  158. end
  159. it 'has a category attribute' do
  160. n = Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type cat])
  161. n.category.should == :cat
  162. n.category = :foo
  163. n.category.should == :foo
  164. end
  165. it 'has a type attribute' do
  166. n = Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type cat])
  167. n.type.should == :type
  168. n.type = :foo
  169. n.type.should == :foo
  170. end
  171. it 'has a name attribute' do
  172. n = Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type cat])
  173. n.name.should == 'name'
  174. n.name = :foo
  175. n.name.should == 'foo'
  176. end
  177. it 'has an xml:lang attribute' do
  178. n = Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type cat en])
  179. n.xml_lang.should == 'en'
  180. n.xml_lang = 'de'
  181. n.xml_lang.should == 'de'
  182. end
  183. it 'can determine equality' do
  184. a = Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type cat])
  185. a.should == Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[name type cat])
  186. a.should_not equal Blather::Stanza::Iq::DiscoInfo::Identity.new(*%w[not-name not-type not-cat])
  187. end
  188. end
  189. describe Blather::Stanza::Iq::DiscoInfo::Feature do
  190. it 'will auto-inherit nodes' do
  191. n = parse_stanza "<feature var='ipv6' />"
  192. i = Blather::Stanza::Iq::DiscoInfo::Feature.new n.root
  193. i.var.should == 'ipv6'
  194. end
  195. it 'has a var attribute' do
  196. n = Blather::Stanza::Iq::DiscoInfo::Feature.new 'var'
  197. n.var.should == 'var'
  198. n.var = :foo
  199. n.var.should == 'foo'
  200. end
  201. it 'can determine equality' do
  202. a = Blather::Stanza::Iq::DiscoInfo::Feature.new('var')
  203. a.should == Blather::Stanza::Iq::DiscoInfo::Feature.new('var')
  204. a.should_not equal Blather::Stanza::Iq::DiscoInfo::Feature.new('not-var')
  205. end
  206. end