radamanthus-skates 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,283 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Skates::XmppParser do
4
+
5
+ before(:each) do
6
+ @last_stanza = ""
7
+ @proc = mock(Proc, :call => true)
8
+ @parser = Skates::XmppParser.new(@proc)
9
+ end
10
+
11
+ describe ".reset" do
12
+ it "should reset the document to nil" do
13
+ @parser.reset
14
+ @parser.elem.should be_nil
15
+ end
16
+
17
+ it "should reset the element to nil (no element is being parsed)" do
18
+ @parser.reset
19
+ @parser.elem.should be_nil
20
+ end
21
+
22
+ it "should reset the parser to a parser" do
23
+ new_parser = Nokogiri::XML::SAX::PushParser.new(@parser, "UTF-8")
24
+ Nokogiri::XML::SAX::PushParser.should_receive(:new).with(@parser, "UTF-8").and_return(new_parser)
25
+ @parser.reset
26
+ @parser.parser.should == new_parser
27
+ end
28
+ end
29
+
30
+ describe ".push" do
31
+ it "should send the data to the parser" do
32
+ data = "<name>me</name>"
33
+ @parser.parser.should_receive(:<<).with(data)
34
+ @parser.push(data)
35
+ end
36
+ end
37
+
38
+ describe ".characters" do
39
+ before(:each) do
40
+ @parser.doc = Nokogiri::XML::Document.new
41
+ @parser.elem = Nokogiri::XML::Element.new("element", @parser.doc)
42
+ end
43
+
44
+ it "should add the characters to the buffer" do
45
+ chars = "hello my name is julien"
46
+ @parser.characters(chars)
47
+ @parser.instance_variable_get("@buffer").should == chars
48
+ end
49
+
50
+ it "should concatenate the text if we receive in both pieces" do
51
+ chars = "hello my name is julien"
52
+ @parser.characters(chars)
53
+ @parser.instance_variable_get("@buffer").should == chars
54
+ chars2 = "and I'm french!"
55
+ @parser.characters(chars2)
56
+ @parser.instance_variable_get("@buffer").should == chars + chars2
57
+ end
58
+
59
+ it "should not parse text content as XML" do
60
+ stanza = "<message><body>&amp;#187;</body></message>"
61
+ @parser.push stanza
62
+ # Not "\273":
63
+ (@parser.elem / 'message/body')[0].content.should == '&#187;'
64
+ (@parser.elem / 'message/body').to_xml.should == "<body>&amp;#187;</body>"
65
+ end
66
+ end
67
+
68
+ describe ".start_element" do
69
+
70
+ before(:each) do
71
+ @new_elem_name = "new"
72
+ @new_elem_attributes = ["to", "you@yourserver.com/home", "xmlns", "http://ns.com"]
73
+ end
74
+
75
+ it "should create a new doc if we don't have one" do
76
+ new_doc = Nokogiri::XML::Document.new
77
+ @parser.doc = nil
78
+ Nokogiri::XML::Document.should_receive(:new).and_return(new_doc)
79
+ @parser.start_element(@new_elem_name, @new_elem_attributes)
80
+ @parser.doc.should == new_doc
81
+ end
82
+
83
+ it "should not create a new doc we already have one" do
84
+ @parser.doc = Nokogiri::XML::Document.new
85
+ Nokogiri::XML::Document.should_not_receive(:new)
86
+ @parser.start_element(@new_elem_name, @new_elem_attributes)
87
+ end
88
+
89
+ it "should create a new element" do
90
+ @doc = Nokogiri::XML::Document.new
91
+ @parser.doc = @doc
92
+ @new_elem = Nokogiri::XML::Element.new(@new_elem_name, @parser.doc)
93
+ Nokogiri::XML::Element.should_receive(:new).and_return(@new_elem)
94
+ @parser.start_element(@new_elem_name, @new_elem_attributes)
95
+ end
96
+
97
+ it "should add the new element as a child to the current element if there is one" do
98
+ @doc = Nokogiri::XML::Document.new
99
+ @parser.doc = @doc
100
+ @current = Nokogiri::XML::Element.new("element", @parser.doc)
101
+ @parser.elem = @current
102
+ @new_elem = Nokogiri::XML::Element.new(@new_elem_name, @parser.doc)
103
+ Nokogiri::XML::Element.stub!(:new).and_return(@new_elem)
104
+ @parser.start_element(@new_elem_name, @new_elem_attributes)
105
+ @new_elem.parent.should == @current
106
+ end
107
+
108
+ it "should add the new element as the child of the doc if there is none" do
109
+ @doc = Nokogiri::XML::Document.new
110
+ @parser.doc = @doc
111
+ @new_elem = Nokogiri::XML::Element.new(@new_elem_name, @parser.doc)
112
+ Nokogiri::XML::Element.stub!(:new).and_return(@new_elem)
113
+ @parser.start_element(@new_elem_name, @new_elem_attributes)
114
+ @new_elem.parent.should == @doc
115
+ end
116
+
117
+ it "should add the right attributes and namespaces to the newly created element" do
118
+ @parser.start_element(@new_elem_name, @new_elem_attributes)
119
+ @parser.elem["to"].should == "you@yourserver.com/home"
120
+ # TODO : FIX NAMESPACES : @parser.elem.namespaces.should == {"xmlns"=>"http://ns.com"}
121
+ @parser.elem.namespaces.should == {}
122
+ end
123
+
124
+ describe "when the new element is of name stream:stream" do
125
+ it "should callback" do
126
+ @proc.should_receive(:call)
127
+ @parser.start_element("stream:stream", [])
128
+ end
129
+
130
+ it "should reinit to nil the doc and the elem" do
131
+ @parser.start_element("stream:stream", [])
132
+ @parser.doc.should == nil
133
+ @parser.elem.should == nil
134
+ end
135
+ end
136
+ end
137
+
138
+ describe ".end_element" do
139
+ before(:each) do
140
+ @doc = Nokogiri::XML::Document.new
141
+ @parser.doc = @doc
142
+ @current = Nokogiri::XML::Element.new("element", @parser.doc)
143
+ @parser.elem = @current
144
+ end
145
+
146
+ it "should add the content of the buffer to the @elem" do
147
+ @elem = Nokogiri::XML::Element.new("element", @parser.doc)
148
+ chars = "hello world"
149
+ @parser.instance_variable_set("@buffer", chars)
150
+ @parser.elem = @elem
151
+ @parser.end_element("element")
152
+ @elem.content.should == chars
153
+ end
154
+
155
+ describe "when we're finishing the doc's root" do
156
+ before(:each) do
157
+ @parser.doc.root = @current
158
+ end
159
+
160
+ it "should callback" do
161
+ @proc.should_receive(:call)
162
+ @parser.end_element("element")
163
+ end
164
+
165
+ it "should reinit to nil the doc and the elem" do
166
+ @parser.end_element("element")
167
+ @parser.doc.should == nil
168
+ @parser.elem.should == nil
169
+ end
170
+ end
171
+
172
+ describe "when we're finishing another element" do
173
+ before(:each) do
174
+ @parser.doc.root = Nokogiri::XML::Element.new("root", @parser.doc)
175
+ @current.parent = @parser.doc.root
176
+ end
177
+
178
+ it "should go back up one level" do
179
+ @parser.end_element("element")
180
+ @parser.elem = @current.parent
181
+ end
182
+ end
183
+ end
184
+
185
+ describe ".add_namespaces_and_attributes_to_node" do
186
+ before(:each) do
187
+ @doc = Nokogiri::XML::Document.new
188
+ @parser.doc = @doc
189
+ @element = Nokogiri::XML::Element.new("element", @parser.doc)
190
+ @attrs = ["from", "me", "xmlns:atom", "http://www.w3.org/2005/Atom" ,"to", "you", "xmlns", "http://namespace.com"]
191
+ @parser.elem = @element
192
+ end
193
+
194
+ it "should assign even elements to attributes value or namespaces urls" do
195
+ @parser.__send__(:add_namespaces_and_attributes_to_current_node, @attrs)
196
+ even = []
197
+ @attrs.size.times do |i|
198
+ even << @attrs[i*2]
199
+ end
200
+ even.compact!
201
+ @element.attributes.keys.each do |k|
202
+ even.should include(k)
203
+ end
204
+ end
205
+
206
+ it "should assign odd elements to attributes names of namespaces prefixes" do
207
+ @parser.__send__(:add_namespaces_and_attributes_to_current_node, @attrs)
208
+ even = []
209
+ @attrs.size.times do |i|
210
+ even << @attrs[i*2+1]
211
+ end
212
+ even.compact!
213
+ @element.attributes.values.each do |v|
214
+ even.should include("#{v}")
215
+ end
216
+ end
217
+
218
+ it "should add namespace for each attribute name that starts with xmlns" do
219
+ @parser.__send__(:add_namespaces_and_attributes_to_current_node, @attrs)
220
+ # TODO: FIX NAMESPACES @element.namespaces.values.should == ["http://www.w3.org/2005/Atom", "http://namespace.com"]
221
+ @element.namespaces.values.should == []
222
+ end
223
+
224
+ it "should escape characters correctly" do
225
+ @attrs = ["url", "http://api.flickr.com/services/feeds/photos_public.gne?id=49724566@N00&amp;lang=en-us&amp;format=atom"]
226
+ @parser.__send__(:add_namespaces_and_attributes_to_current_node, @attrs)
227
+ @element["url"].should == "http://api.flickr.com/services/feeds/photos_public.gne?id=49724566@N00&lang=en-us&format=atom"
228
+ end
229
+
230
+ end
231
+
232
+ describe "a communication with an XMPP Client" do
233
+
234
+ before(:each) do
235
+ @stanzas = []
236
+ @proc = Proc.new { |stanza|
237
+ @stanzas << stanza
238
+ }
239
+ @parser = Skates::XmppParser.new(@proc)
240
+ end
241
+
242
+ it "should parse the right information" do
243
+ string = "<stream:stream
244
+ xmlns:stream='http://etherx.jabber.org/streams'
245
+ xmlns='jabber:component:accept'
246
+ from='plays.shakespeare.lit'
247
+ id='3BF96D32'>
248
+ <handshake/>
249
+ <message from='juliet@example.com'
250
+ to='romeo@example.net'
251
+ xml:lang='en'>
252
+ <body>Art thou not Romeo, and a Montague?</body>
253
+ <link href='http://sfbay.craigslist.org/search/sss?query=%2522mac+mini%2522+Intel+Core+Duo&amp;minAsk=min&amp;maxAsk=max&amp;format=rss&amp;format=rss' />
254
+ </message>"
255
+ pieces = rand(string.size/30)
256
+ # So we have to pick 'pieces' number between 0 and string.size
257
+ indices = []
258
+ pieces.times do |i|
259
+ indices[i] = rand(string.size)
260
+ end
261
+ # Now let's sort indices
262
+ indices.sort!
263
+ substrings = []
264
+ prev_index = 0
265
+ indices.each do |index|
266
+ substrings << string[prev_index, (index-prev_index)]
267
+ prev_index = index
268
+ end
269
+ substrings << string[prev_index, string.size]
270
+ #Just to make sure we split the string the right way!
271
+ substrings.join("").should == string
272
+
273
+ substrings.each do |s|
274
+ @parser.push(s)
275
+ end
276
+
277
+ @stanzas.join("").should == "<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" xmlns=\"jabber:component:accept\" from=\"plays.shakespeare.lit\" id=\"3BF96D32\"/><handshake/><message from=\"juliet@example.com\" to=\"romeo@example.net\" xml:lang=\"en\">\n <body>Art thou not Romeo, and a Montague?</body>\n <link href=\"http://sfbay.craigslist.org/search/sss?query=%2522mac+mini%2522+Intel+Core+Duo&amp;minAsk=min&amp;maxAsk=max&amp;format=rss&amp;format=rss\"/>\n</message>"
278
+ @stanzas.last.at("link")["href"].should == "http://sfbay.craigslist.org/search/sss?query=%2522mac+mini%2522+Intel+Core+Duo&minAsk=min&maxAsk=max&format=rss&format=rss"
279
+ end
280
+
281
+ end
282
+
283
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --diff
2
+ --color
@@ -0,0 +1,37 @@
1
+ require 'templater'
2
+
3
+ require File.dirname(__FILE__) + "/../lib/skates"
4
+ require File.dirname(__FILE__) + "/../lib/skates/generator"
5
+
6
+ # #
7
+ # Deactivate the logging
8
+ Skates.logger.level = Log4r::FATAL
9
+
10
+ Skates.environment = "test"
11
+
12
+ if !defined? SkatesSpecHelper
13
+ module SkatesSpecHelper
14
+ ##
15
+ # Load configuration from a local config file
16
+ def skates_config
17
+ @config ||= YAML.load(File.read(File.join(File.dirname(__FILE__), "config.yaml")))
18
+ end
19
+
20
+ ##
21
+ # Mock for Handler
22
+ def handler_mock
23
+ @handler_mock ||=
24
+ begin
25
+ mock(Object, { :on_connected => Proc.new { |conn|
26
+ },
27
+ :on_disconnected => Proc.new {
28
+ # Disconnected
29
+ },
30
+ :on_stanza => Proc.new { |stanza|
31
+ # Stanza!
32
+ }
33
+ })
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class SkatesTest < Test::Unit::TestCase
4
+ # Tests are done through Rspec... but feel free to add any Unit tests you think might be helpful to understand and fix code more easily
5
+ should "probably rename this file and start testing for real" do
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'skates'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: radamanthus-skates
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 5
9
+ version: 0.3.5
10
+ platform: ruby
11
+ authors:
12
+ - Julien Genestoux
13
+ - Radamanthus Batnag
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-03 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rake
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rspec
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 2
44
+ - 0
45
+ - 0
46
+ version: 2.0.0
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: Modified to add the ability to specify log_dir
50
+ email: rad@batnag.org
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - Rakefile
59
+ - bin/skates
60
+ - lib/skates/base/controller.rb
61
+ - lib/skates/base/stanza.rb
62
+ - lib/skates/base/view.rb
63
+ - lib/skates/client_connection.rb
64
+ - lib/skates/component_connection.rb
65
+ - lib/skates/ext/array.rb
66
+ - lib/skates/generator.rb
67
+ - lib/skates/router/dsl.rb
68
+ - lib/skates/router.rb
69
+ - lib/skates/runner.rb
70
+ - lib/skates/xmpp_connection.rb
71
+ - lib/skates/xmpp_parser.rb
72
+ - lib/skates.rb
73
+ - test/skates_test.rb
74
+ - test/test_helper.rb
75
+ - spec/bin/skates_spec.rb
76
+ - spec/em_mock.rb
77
+ - spec/lib/skates/base/controller_spec.rb
78
+ - spec/lib/skates/base/stanza_spec.rb
79
+ - spec/lib/skates/base/view_spec.rb
80
+ - spec/lib/skates/client_connection_spec.rb
81
+ - spec/lib/skates/component_connection_spec.rb
82
+ - spec/lib/skates/generator_spec.rb
83
+ - spec/lib/skates/router/dsl_spec.rb
84
+ - spec/lib/skates/router_spec.rb
85
+ - spec/lib/skates/runner_spec.rb
86
+ - spec/lib/skates/xmpp_connection_spec.rb
87
+ - spec/lib/skates/xmpp_parser_spec.rb
88
+ - spec/spec.opts
89
+ - spec/spec_helper.rb
90
+ - README.rdoc
91
+ - LICENSE
92
+ has_rdoc: true
93
+ homepage: http://github.com/radamanthus/skates
94
+ licenses: []
95
+
96
+ post_install_message:
97
+ rdoc_options: []
98
+
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.3.7
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: A Skates fork
124
+ test_files: []
125
+