dozuki 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,257 @@
1
+ require 'spec_helper'
2
+
3
+ module Dozuki::XML
4
+ describe Node do
5
+
6
+ describe "initialization" do
7
+ let(:nokogiri_node){mock("nokogiri_node")}
8
+ subject{Node.new(nokogiri_node)}
9
+ its(:nokogiri_node){should == nokogiri_node}
10
+ end
11
+
12
+ describe "missing methods" do
13
+ let(:nokogiri_node){mock("nokogiri node")}
14
+ subject{Node.new(nokogiri_node)}
15
+ it "should be proxied to the nokogiri node" do
16
+ nokogiri_node.should_receive(:blah)
17
+ subject.blah
18
+ end
19
+ it "should be proxied to the nokogiri node with args" do
20
+ args = [1,2,3]
21
+ nokogiri_node.should_receive(:blah).with(*args)
22
+ subject.blah(*args)
23
+ end
24
+ it "should be proxied to the nokogiri node with blocks" do
25
+ blk = Proc.new{}
26
+ nokogiri_node.should_receive(:blah).with(&blk)
27
+ subject.blah(&blk)
28
+ end
29
+ end
30
+
31
+ describe "responding to nokogiri's methods" do
32
+ let(:nokogiri_node){mock("nokogiri node")}
33
+ let(:node){Node.new(nokogiri_node)}
34
+ subject{node.respond_to?(:something_silly)}
35
+ context "where nokogiri responds to the method" do
36
+ before(:each){nokogiri_node.stub(:respond_to?).with(:something_silly).and_return(true)}
37
+ it "should return true" do
38
+ subject.should be_true
39
+ end
40
+ end
41
+ context "where nokogiri responds to the method" do
42
+ before(:each){nokogiri_node.stub(:respond_to?).with(:something_silly).and_return(false)}
43
+ it "should return true" do
44
+ subject.should be_false
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "each" do
50
+ let(:nokogiri_node) {mock("nokogiri_node")}
51
+ let(:xpath) {"/some/xpath"}
52
+ let(:node) {Node.new(nokogiri_node)}
53
+ let(:blk) {Proc.new {}}
54
+ let(:collection) {mock "node set"}
55
+ let(:node_collection) {mock "node_collection"}
56
+ before(:each) do
57
+ nokogiri_node.stub(:xpath).and_return(collection)
58
+ NodeCollection.stub(:new).and_return(node_collection)
59
+
60
+ end
61
+ subject{node.each(xpath)}
62
+
63
+ it "should get the collection from the nokogiri node" do
64
+ nokogiri_node.should_receive(:xpath).with(xpath)
65
+ subject
66
+ end
67
+
68
+ it "should create a new NodeCollection with the collection" do
69
+ NodeCollection.should_receive(:new).with(collection)
70
+ subject
71
+ end
72
+
73
+ it "should return the collection" do
74
+ subject.should == node_collection
75
+ end
76
+
77
+ context "where no block is passed" do
78
+ before(:each) do
79
+ node_collection.stub(:as_node).and_return(node_collection)
80
+ end
81
+ subject{ node.each(xpath, &blk)}
82
+ it "should call to_node on the node_collection with the passed block" do
83
+ node_collection.should_receive(:as_node).with(&blk)
84
+ subject
85
+ end
86
+ end
87
+ end
88
+
89
+ describe "string" do
90
+ let(:nokogiri_node) { mock("nokogiri_node") }
91
+ let(:xpath) { "/some/xpath" }
92
+ let(:node) { Node.new(nokogiri_node) }
93
+ let(:text_node) { mock "some node" }
94
+ let(:parsed) { mock "parsed result" }
95
+
96
+ before(:each) do
97
+ nokogiri_node.stub(:xpath).and_return([text_node])
98
+ Parser.stub(:to_string).and_return(parsed)
99
+ end
100
+
101
+ subject{ node.string(xpath) }
102
+
103
+ it "should get the collection from the nokogiri node" do
104
+ nokogiri_node.should_receive(:xpath).with(xpath)
105
+ subject
106
+ end
107
+ it "should parse the node to a string" do
108
+ Parser.should_receive(:to_string).with(text_node)
109
+ subject
110
+ end
111
+ it "should return the parsed result" do
112
+ subject.should == parsed
113
+ end
114
+ context "where the xpath returns no node" do
115
+ before(:each) do
116
+ nokogiri_node.should_receive(:xpath).and_return([])
117
+ end
118
+ it "should raise a NotFound error" do
119
+ expect{subject}.to raise_error(NotFound)
120
+ end
121
+ end
122
+ end
123
+
124
+ describe "int" do
125
+ let(:nokogiri_node) { mock("nokogiri_node") }
126
+ let(:xpath) { "/some/xpath" }
127
+ let(:node) { Node.new(nokogiri_node) }
128
+ let(:int_node) { mock "some node" }
129
+ let(:parsed) { mock "parsed result" }
130
+
131
+ before(:each) do
132
+ nokogiri_node.stub(:xpath).and_return([int_node])
133
+ Parser.stub(:to_int).and_return(parsed)
134
+ end
135
+
136
+ subject{ node.int(xpath) }
137
+
138
+ it "should get the collection from the nokogiri node" do
139
+ nokogiri_node.should_receive(:xpath).with(xpath)
140
+ subject
141
+ end
142
+ it "should parse the node to an int" do
143
+ Parser.should_receive(:to_int).with(int_node)
144
+ subject
145
+ end
146
+ it "should return the parsed result" do
147
+ subject.should == parsed
148
+ end
149
+ context "where the xpath returns no node" do
150
+ before(:each) do
151
+ nokogiri_node.should_receive(:xpath).and_return([])
152
+ end
153
+ it "should raise a NotFound error" do
154
+ expect{subject}.to raise_error(NotFound)
155
+ end
156
+ end
157
+ end
158
+
159
+ describe "float" do
160
+ let(:nokogiri_node) { mock("nokogiri_node") }
161
+ let(:xpath) { "/some/xpath" }
162
+ let(:node) { Node.new(nokogiri_node) }
163
+ let(:float_node) { mock "some node" }
164
+ let(:parsed) { mock "parsed result" }
165
+
166
+ before(:each) do
167
+ nokogiri_node.stub(:xpath).and_return([float_node])
168
+ Parser.stub(:to_float).and_return(parsed)
169
+ end
170
+
171
+ subject{ node.float(xpath) }
172
+
173
+ it "should get the collection from the nokogiri node" do
174
+ nokogiri_node.should_receive(:xpath).with(xpath)
175
+ subject
176
+ end
177
+ it "should parse the node to an int" do
178
+ Parser.should_receive(:to_float).with(float_node)
179
+ subject
180
+ end
181
+ it "should return the parsed result" do
182
+ subject.should == parsed
183
+ end
184
+ context "where the xpath returns no node" do
185
+ before(:each) do
186
+ nokogiri_node.should_receive(:xpath).and_return([])
187
+ end
188
+ it "should raise a NotFound error" do
189
+ expect{subject}.to raise_error(NotFound)
190
+ end
191
+ end
192
+ end
193
+
194
+ describe "get" do
195
+ let(:nokogiri_node) { mock("nokogiri_node") }
196
+ let(:xpath) { "/some/xpath" }
197
+ let(:node) { Node.new(nokogiri_node) }
198
+ let(:wanted_node) { mock "some node" }
199
+
200
+ before(:each) do
201
+ nokogiri_node.stub(:xpath).and_return([wanted_node])
202
+ end
203
+
204
+ subject{ node.get(xpath) }
205
+
206
+ it "should get the collection from the nokogiri node" do
207
+ nokogiri_node.should_receive(:xpath).with(xpath)
208
+ subject
209
+ end
210
+ it "should return the new dozuki node" do
211
+ subject.nokogiri_node.should == wanted_node
212
+ end
213
+
214
+ context "where the xpath returns no node" do
215
+ before(:each) do
216
+ nokogiri_node.should_receive(:xpath).and_return([])
217
+ end
218
+ it "should raise a NotFound error" do
219
+ expect{subject}.to raise_error(NotFound)
220
+ end
221
+ end
222
+ context "where a block is passed" do
223
+ subject{node.get(xpath, &blk)}
224
+ it "should call the block with the node" do
225
+ node.get(xpath) do |block_node|
226
+ block_node.nokogiri_node.should == wanted_node
227
+ end
228
+ end
229
+ end
230
+ end
231
+
232
+ describe "exists?" do
233
+ let(:nokogiri_node) { mock("nokogiri_node") }
234
+ let(:xpath) { "/some/xpath" }
235
+ let(:node) { Node.new(nokogiri_node) }
236
+ let(:xpath_result) {["something"]}
237
+ before(:each) do
238
+ nokogiri_node.stub(:xpath).and_return(xpath_result)
239
+ end
240
+ subject{ node.exists?(xpath) }
241
+ it "should get the xpath from the nokogiri_node" do
242
+ nokogiri_node.should_receive(:xpath).with(xpath)
243
+ subject
244
+ end
245
+ it "should return true for a non-empty node" do
246
+ subject.should be_true
247
+ end
248
+ context "where the results are not empty?" do
249
+ let(:xpath_result){[]}
250
+ it "should return false for an empty set" do
251
+ subject.should be_false
252
+ end
253
+ end
254
+ end
255
+
256
+ end
257
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ module Dozuki
4
+ module XML
5
+ describe Parser do
6
+ describe "to_text" do
7
+ let(:string){"something"}
8
+ let(:node){mock "node", :text => string}
9
+ subject{Parser.to_string(node)}
10
+ it "should get the text" do
11
+ subject.should == "something"
12
+ end
13
+ context "where the string has whitespace" do
14
+ let(:string){"\n\nsomething"}
15
+ it "should strip the whitespace" do
16
+ subject.should == "something"
17
+ end
18
+ end
19
+ end
20
+ describe "to_int" do
21
+ let(:string){"2"}
22
+ let(:node){mock "node", :text => string}
23
+ subject{Parser.to_int(node)}
24
+ it "should get the integer" do
25
+ subject.should == 2
26
+ end
27
+ context "where the string has whitespace" do
28
+ let(:string){"\n\n2"}
29
+ it "should strip the whitespace" do
30
+ subject.should == 2
31
+ end
32
+ end
33
+ context "where the string is badly formatted" do
34
+ context "where there is characters in the string" do
35
+ let(:string){"fjkdsk"}
36
+ it "should raise an error" do
37
+ expect{subject}.to raise_error(InvalidFormat)
38
+ end
39
+ end
40
+ context "where there are dots" do
41
+ let(:string){"0.4"}
42
+ it "should raise an error" do
43
+ expect{subject}.to raise_error(InvalidFormat)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ describe "to_float" do
49
+ let(:string){"22.50"}
50
+ let(:node){mock "node", :text => string}
51
+ subject{Parser.to_float(node)}
52
+ it "should get the float" do
53
+ subject.should == 22.50
54
+ end
55
+ context "where the string has whitespace" do
56
+ let(:string){"\n\n22.59"}
57
+ it "should strip the whitespace" do
58
+ subject.should == 22.59
59
+ end
60
+ end
61
+ context "where the string is badly formatted" do
62
+ context "where there is characters in the string" do
63
+ let(:string){"fjkdsk"}
64
+ it "should raise an error" do
65
+ expect{subject}.to raise_error(InvalidFormat)
66
+ end
67
+ end
68
+ context "where there are multiple dots" do
69
+ let(:string){"0.0.4"}
70
+ it "should raise an error" do
71
+ expect{subject}.to raise_error(InvalidFormat)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ module Dozuki
4
+ describe XML do
5
+
6
+ describe "parse" do
7
+ let(:string) {"<lall><some xml='true' /></lall>"}
8
+ let(:nokogiri_node) {mock "nokogiri node"}
9
+ let(:dozuki_xml_node) {mock "dozuki xml node"}
10
+ before(:each) do
11
+ Nokogiri::XML.stub(:parse).and_return(nokogiri_node)
12
+ XML::Node.stub(:new).and_return(dozuki_xml_node)
13
+ end
14
+ subject{XML.parse(string)}
15
+ it "should parse the xml using nokogiri" do
16
+ Nokogiri::XML.should_receive(:parse).with(string)
17
+ subject
18
+ end
19
+ it "should create a new Node with the nokogiri doc" do
20
+ XML::Node.should_receive(:new).with(nokogiri_node)
21
+ subject
22
+ end
23
+ it "should return the new node" do
24
+ subject.should == dozuki_xml_node
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require File.join(File.dirname(__FILE__),'../lib/dozuki')
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dozuki
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - James Almond
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-31 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: cucumber
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: ruby-debug19
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: autotest
74
+ prerelease: false
75
+ requirement: &id005 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ type: :development
84
+ version_requirements: *id005
85
+ description: A simple way of extracting various elements from an XML document using XPaths
86
+ email:
87
+ - james@jamesalmond.com
88
+ executables: []
89
+
90
+ extensions: []
91
+
92
+ extra_rdoc_files: []
93
+
94
+ files:
95
+ - .autotest
96
+ - .gitignore
97
+ - .rspec
98
+ - Gemfile
99
+ - Gemfile.lock
100
+ - README.rdoc
101
+ - Rakefile
102
+ - dozuki.gemspec
103
+ - features/each_accessor.feature
104
+ - features/exists_accessor.feature
105
+ - features/float_accessor.feature
106
+ - features/get_accessor.feature
107
+ - features/int_accessor.feature
108
+ - features/steps/xml_steps.rb
109
+ - features/string_accessor.feature
110
+ - features/support/env.rb
111
+ - lib/dozuki.rb
112
+ - lib/dozuki/version.rb
113
+ - lib/dozuki/xml.rb
114
+ - lib/dozuki/xml/exceptions.rb
115
+ - lib/dozuki/xml/node.rb
116
+ - lib/dozuki/xml/node_collection.rb
117
+ - lib/dozuki/xml/parser.rb
118
+ - spec/dozuki/xml/node_collection_spec.rb
119
+ - spec/dozuki/xml/node_spec.rb
120
+ - spec/dozuki/xml/parser_spec.rb
121
+ - spec/dozuki/xml_spec.rb
122
+ - spec/spec_helper.rb
123
+ has_rdoc: true
124
+ homepage: https://github.com/jamesalmond/dozuki
125
+ licenses: []
126
+
127
+ post_install_message:
128
+ rdoc_options: []
129
+
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ segments:
138
+ - 0
139
+ version: "0"
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ requirements: []
149
+
150
+ rubyforge_project: dozuki
151
+ rubygems_version: 1.3.7
152
+ signing_key:
153
+ specification_version: 3
154
+ summary: An XPath syntactic sugar wrapper for Nokogiri
155
+ test_files:
156
+ - .autotest
157
+ - features/each_accessor.feature
158
+ - features/exists_accessor.feature
159
+ - features/float_accessor.feature
160
+ - features/get_accessor.feature
161
+ - features/int_accessor.feature
162
+ - features/steps/xml_steps.rb
163
+ - features/string_accessor.feature
164
+ - features/support/env.rb
165
+ - spec/dozuki/xml/node_collection_spec.rb
166
+ - spec/dozuki/xml/node_spec.rb
167
+ - spec/dozuki/xml/parser_spec.rb
168
+ - spec/dozuki/xml_spec.rb
169
+ - spec/spec_helper.rb