XMLROCS 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ require "xmlrocs"
2
+ require "test/unit"
3
+
4
+ class TestHelper
5
+ def self.build_xml(limit)
6
+ return "" if limit <= 0
7
+ xmltext = "<X>"
8
+ ('a'..'z').to_a.each_with_index do |x,i|
9
+ xmltext += "<#{x} id=\"#{i}\">" + build_xml(limit - 1) + "</#{x}>"
10
+ end
11
+ xmltext += "</X>"
12
+ end
13
+ end
@@ -0,0 +1,300 @@
1
+ require "test/test_helper"
2
+ require "rexml/document"
3
+ require "rexml/xpath"
4
+ require "enumerator"
5
+
6
+ class Symbol
7
+ include Comparable
8
+
9
+ def to_proc
10
+ Proc.new { |*args| args.shift.__send__(self, *args) }
11
+ end
12
+
13
+ def <=>(other)
14
+ self.to_s <=> other.to_s
15
+ end
16
+ end
17
+
18
+ class TestXMLROCS < Test::Unit::TestCase
19
+ FIXTURE_PATH = "test/fixtures"
20
+ include XMLROCS
21
+
22
+ def setup
23
+ @xmlfiles = Dir.open(FIXTURE_PATH).select { |e| e =~ /\.xml/ }.map { |e| e.sub(".xml", "") }
24
+ generate_instance_variables(@xmlfiles)
25
+ end
26
+
27
+ def generate_instance_variables(xmlfiles)
28
+ xmlfiles.each do |xmlfile|
29
+ instance_variable_set("@#{xmlfile}", XMLNode.new(:text => load_fixture(xmlfile)))
30
+ end
31
+ end
32
+
33
+ def load_fixture(name)
34
+ File.new("#{FIXTURE_PATH}/#{name}.xml", "r").read
35
+ end
36
+
37
+ def with_xmlobjs
38
+ @xmlfiles.map do |name|
39
+ text = load_fixture(name)
40
+ yield(XMLNode.new(:text => text), text)
41
+ end
42
+ end
43
+
44
+ def with_xmlobjs_traverse
45
+ with_xmlobjs do |xmlobj,text|
46
+ xmlobj.each do |x|
47
+ yield(x,xmlobj,text)
48
+ end
49
+ end
50
+ end
51
+
52
+ def with_xmlobjs_cons(cons = 1)
53
+ @xmlfiles.map { |filename| XMLNode.new(:text => load_fixture(filename)) }.each_cons(cons) { |objects| yield(objects) }
54
+ end
55
+
56
+ def test_invalid_xmls
57
+ assert_raise(ArgumentError) { XMLNode.new(:text => "") }
58
+ assert_raise(ArgumentError) { XMLNode.new(:text => "sdfjgkjhfgskdljfhg") }
59
+ end
60
+
61
+ def test_new_with_text
62
+ with_xmlobjs { |xmlobj,text| assert_equal(xmlobj, XMLNode.new(:text => text)) }
63
+ end
64
+
65
+ def test_comparable
66
+ with_xmlobjs { |xmlobj,text| assert_equal(xmlobj, xmlobj) }
67
+ with_xmlobjs_cons(2) { |(e,g)| assert_not_equal(e, g) }
68
+ end
69
+
70
+ def test_attributes_and_children_present
71
+ with_xmlobjs do |xmlobj,text|
72
+ xmlobj.each do |x|
73
+ assert_respond_to(x, :attributes)
74
+ assert_respond_to(x, :children)
75
+ end
76
+ end
77
+ end
78
+
79
+ def test_parent_relationship
80
+ with_xmlobjs do |xmlobj,text|
81
+ assert_nil(xmlobj.parent)
82
+ assert_test_parent_relationship(xmlobj)
83
+ end
84
+ end
85
+
86
+ def assert_test_parent_relationship(parent)
87
+ parent.children.each do |name, obj|
88
+ obj.each do |sibling|
89
+ assert_equal(parent, sibling.parent)
90
+ assert_test_parent_relationship(sibling)
91
+ end
92
+ end
93
+ end
94
+
95
+ def test_transform_to_xml_text
96
+ with_xmlobjs_traverse do |node,xmlobj,text|
97
+ assert_equal(node, XMLNode.new(:text => node.to_xml), "'#{node.to_s}' != '#{XMLNode.new(:text => node.to_xml).to_s}'")
98
+ end
99
+ end
100
+
101
+ def test_add_attribute
102
+ with_xmlobjs_traverse do |node,xmlobj,text|
103
+ next if node[:attribute_symbol]
104
+ assert_nil(node[:attribute_symbol])
105
+ node[:attribute_symbol] = "foo"
106
+ assert_equal("foo", node[:attribute_symbol])
107
+ end
108
+ end
109
+
110
+ def test_delete_attribute
111
+ with_xmlobjs_traverse do |node,xmlobj,text|
112
+ next unless (a = node.attributes.keys.first)
113
+ assert_not_nil(node[a])
114
+ node.delete_attribute!(a)
115
+ assert_nil(node[a])
116
+ end
117
+ end
118
+
119
+ def test_add_child
120
+ with_xmlobjs_traverse do |node,xmlobj,text|
121
+ node << @friends
122
+ assert_equal(@friends, node.friends)
123
+ assert_not_nil(node.children[:friends])
124
+ end
125
+ end
126
+
127
+ def test_child_from_plaintext
128
+ xml = '<a>foo</a>'
129
+ with_xmlobjs_traverse do |node,xmlobj,text|
130
+ node << xml
131
+ assert_equal(XMLROCS::XMLNode.new(:text => xml), node.a)
132
+ end
133
+ end
134
+
135
+ def test_remove_child
136
+ with_xmlobjs_traverse do |node,xmlobj,text|
137
+ node << @friends
138
+ node >> :friends
139
+ assert(!node.respond_to?(:friends))
140
+ assert_nil(node.children[:friends])
141
+ end
142
+ end
143
+
144
+ def test_remove_child_with_closure
145
+ o = XMLROCS::XMLNode.new :text => '<a><b id="1"></b><b id="2"></b><b id="3"></b></a>'
146
+ o.>>(:b) { |child| child[:id] == "1" }
147
+ assert(o.b.all? { |x| x[:id] != "1" })
148
+ o.>> { |child| child[:id] == "3" }
149
+ assert(o.b.all? { |x| x[:id] != "3" })
150
+ end
151
+
152
+ def test_xmlnames_present
153
+ with_xmlobjs_traverse { |node,xmlobj,text| assert_not_nil(node.xmlname) }
154
+ end
155
+
156
+ def test_leafs
157
+ leaf_names = [ :reach, :url, :releasedate, :small, :medium, :large, :mbid ]
158
+ found = {}
159
+ @album.leafs.map { |leaf| leaf.xmlname }.each do |name|
160
+ assert(leaf_names.include?(name))
161
+ found[name] = 1
162
+ end
163
+ leaf_names.each { |name| assert(found.has_key?(name), name) }
164
+ end
165
+
166
+ def test_all
167
+ with_xmlobjs do |xmlobj,text|
168
+ all_node_names = xmlobj.all.map(&:xmlname)
169
+ got = assert_children_names_in(xmlobj, all_node_names)
170
+ assert_equal(all_node_names.uniq.sort, got.uniq.sort)
171
+ end
172
+ end
173
+
174
+ def assert_children_names_in(xmlobj, names)
175
+ xmlobj.children.inject([xmlobj.xmlname]) do |got,(name,value)|
176
+ assert(names.include?(name))
177
+ got += [ name ] + value.map { |child| assert_children_names_in(child, names) }.flatten
178
+ end
179
+ end
180
+
181
+ def test_dup
182
+ with_xmlobjs { |xmlobj,text| xmlobj.each { |x| assert_equal(x, x.dup) } }
183
+ end
184
+
185
+ def test_set_text
186
+ with_xmlobjs_traverse do |node,xmlobj,text|
187
+ node.set_text("foo")
188
+ assert_equal("foo", node)
189
+ end
190
+ end
191
+
192
+ def test_mappers
193
+ with_xmlobjs do |xmlobj,text|
194
+ xmlobj.map { |x| x.set_text("arbitrary_string_123"); x }.each do |node|
195
+ assert_equal("arbitrary_string_123", node)
196
+ end
197
+ xmlobj.each { |node| assert_not_equal("arbitrary_string_123", node) }
198
+ xmlobj.map! { |x| x.set_text("bar"); x }
199
+ xmlobj.each { |node| assert_equal("bar", node) }
200
+ end
201
+ end
202
+
203
+ def test_injecters
204
+ with_xmlobjs do |xmlobj,text|
205
+ xmlobj.each { |x| assert_equal(x.all.length, x.inject(0) { |cur,x| cur + 1 }) }
206
+ xmlobj.each { |x| assert_equal(x.all.map(&:xmlname).uniq.sort,
207
+ x.inject([]) { |cur,x| cur << x.xmlname }.uniq.sort) }
208
+ end
209
+ end
210
+
211
+ def test_string_comparison
212
+ with_xmlobjs do |xmlobj,text|
213
+ assert(xmlobj.inject(true) { |cur,x| cur && x == x.to_s && x.to_s == x })
214
+ end
215
+ end
216
+
217
+ def test_nil_node
218
+ assert_nothing_thrown { node = XMLROCS::XMLNode.new(:nil => true) }
219
+ end
220
+
221
+ def test_single_album
222
+ [ :reach, :releasedate, :mbid, :tracks ].each { |e| assert(@album.single?(e)) }
223
+ [ :small, :medium, :large ].each { |e| assert(@album.coverart.single?(e)) }
224
+ assert(!@album.tracks.single?(:track))
225
+
226
+ assert_equal("Metallica", @album[:artist])
227
+ assert_equal("Metallica", @album[:title])
228
+ assert_equal("195627", @album.reach)
229
+ assert_equal("29 Aug 1991, 00:00", @album.releasedate)
230
+ assert_equal("http://cdn.last.fm/coverart/130x130/1411800.jpg", @album.coverart.small)
231
+ assert_equal("http://cdn.last.fm/coverart/130x130/1411800.jpg", @album.coverart.medium)
232
+ assert_equal("http://cdn.last.fm/coverart/130x130/1411800.jpg", @album.coverart.large)
233
+ assert_equal("3750d9e2-59f5-471d-8916-463433069bd1", @album.mbid)
234
+ assert_equal("Enter Sandman", @album.tracks.track!.first[:title])
235
+ assert_equal("217037", @album.tracks.track!.first.reach)
236
+ assert_equal("http://www.last.fm/music/Metallica/_/Enter+Sandman", @album.tracks.track!.first.url)
237
+ assert_equal("The Unforgiven", @album.tracks.track![3][:title])
238
+ assert_equal("148058", @album.tracks.track![3].reach)
239
+ assert_equal("http://www.last.fm/music/Metallica/_/The+Unforgiven", @album.tracks.track![3].url)
240
+ end
241
+
242
+ def test_artists
243
+ assert(!@weeklyartists.single?(:artist))
244
+ assert_equal([:user, :from, :to].sort, @weeklyartists.attributes.keys.sort)
245
+ assert_equal([:artist], @weeklyartists.children.keys)
246
+
247
+ assert_equal("RJ", @weeklyartists[:user])
248
+ assert_equal("1114965332", @weeklyartists[:from])
249
+ assert_equal("1115570132", @weeklyartists[:to])
250
+
251
+ @weeklyartists.artist!.each do |e|
252
+ assert_not_nil(e.name)
253
+ assert_not_nil(e.chartposition)
254
+ assert_not_nil(e.playcount)
255
+ assert_not_nil(e.url)
256
+ end
257
+ end
258
+
259
+ def test_albums_tracks
260
+ assert_equal("1114965332", @weeklytracks[:from])
261
+ assert_equal("1114965332", @weeklyalbums[:from])
262
+ assert_equal("1115570132", @weeklytracks[:to])
263
+ assert_equal("1115570132", @weeklyalbums[:to])
264
+ assert_equal("RJ", @weeklytracks[:user])
265
+ assert_equal("RJ", @weeklyalbums[:user])
266
+ (@weeklytracks.track! + @weeklyalbums.album!).each do |e|
267
+ [ :artist, :name, :chartposition, :playcount, :url ].each { |s| assert(e.single?(s)) }
268
+ assert_not_nil(e.artist)
269
+ assert_not_nil(e.name)
270
+ assert_not_nil(e.chartposition)
271
+ assert_not_nil(e.playcount)
272
+ assert_not_nil(e.url)
273
+ end
274
+ end
275
+
276
+ def test_chart
277
+ assert_equal("RJ", @weeklychartlist[:user])
278
+ @weeklychartlist.chart!.each { |e| [ :from, :to ].each { |s| assert_not_nil(e[s]) } }
279
+ end
280
+
281
+ def test_name
282
+ assert_equal(:weeklyartistchart, @weeklyartists.xmlname)
283
+ assert_equal(:weeklytrackchart, @weeklytracks.xmlname)
284
+ assert_equal(:weeklyalbumchart, @weeklyalbums.xmlname)
285
+ end
286
+
287
+ def test_against_rexml
288
+ xmltext = TestHelper.build_xml(2)
289
+ rexml = REXML::Document.new(xmltext)
290
+ xmlroc = XMLROCS::XMLNode.new(:text => xmltext)
291
+
292
+ rexml_cnt = REXML::XPath.match(rexml, "//[@id < 3]").length
293
+ xmlrocs_cnt = xmlroc.select { |x| x[:id] && x[:id].to_i < 3 }.length
294
+ assert_equal(rexml_cnt, xmlrocs_cnt)
295
+
296
+ rexml_cnt = REXML::XPath.match(rexml, "//").length
297
+ xmlrocs_cnt = xmlroc.all.length
298
+ assert_equal(rexml_cnt - 1, xmlrocs_cnt)
299
+ end
300
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: XMLROCS
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2008-02-24 00:00:00 +01:00
8
+ summary: Map XML Data into Ruby objects
9
+ require_paths:
10
+ - lib
11
+ email: ameingast@gmail.com
12
+ homepage: http://yomi.at/
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: name
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Andreas Meingast
31
+ files:
32
+ - lib/xmlrocs.rb
33
+ - LICENSE
34
+ - TODO
35
+ - README
36
+ test_files:
37
+ - test/test_helper.rb
38
+ - test/test_xmlrocs.rb
39
+ - test/fixtures/album.xml
40
+ - test/fixtures/friends.xml
41
+ - test/fixtures/misc.xml
42
+ - test/fixtures/tags.xml
43
+ - test/fixtures/toptags.xml
44
+ - test/fixtures/weeklyalbums.xml
45
+ - test/fixtures/weeklyartists.xml
46
+ - test/fixtures/weeklychartlist.xml
47
+ - test/fixtures/weeklytracks.xml
48
+ rdoc_options: []
49
+
50
+ extra_rdoc_files:
51
+ - README
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ requirements: []
57
+
58
+ dependencies: []
59
+