representable 3.2.0 → 3.3.0
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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +17 -10
- data/.github/workflows/ci_jruby.yml +21 -0
- data/.github/workflows/ci_truffleruby.yml +19 -0
- data/CHANGES.md +5 -0
- data/Gemfile +1 -1
- data/README.md +0 -1
- data/lib/representable/hash/binding.rb +1 -0
- data/lib/representable/json.rb +14 -2
- data/lib/representable/version.rb +1 -1
- data/lib/representable/xml/binding.rb +0 -7
- data/lib/representable/xml.rb +7 -0
- data/representable.gemspec +1 -3
- data/test/as_test.rb +8 -13
- data/test/benchmarking.rb +20 -25
- data/test/binding_test.rb +13 -5
- data/test/cached_test.rb +55 -35
- data/test/class_test.rb +46 -33
- data/test/coercion_test.rb +29 -19
- data/test/config/inherit_test.rb +12 -17
- data/test/config_test.rb +5 -7
- data/test/decorator_scope_test.rb +13 -11
- data/test/decorator_test.rb +11 -11
- data/test/default_test.rb +2 -2
- data/test/defaults_options_test.rb +37 -9
- data/test/definition_test.rb +10 -12
- data/test/examples/example.rb +43 -38
- data/test/examples/object.rb +7 -6
- data/test/exec_context_test.rb +18 -23
- data/test/features_test.rb +29 -10
- data/test/filter_test.rb +29 -20
- data/test/for_collection_test.rb +12 -15
- data/test/generic_test.rb +15 -22
- data/test/getter_setter_test.rb +11 -6
- data/test/hash_bindings_test.rb +21 -12
- data/test/hash_test.rb +53 -38
- data/test/heritage_test.rb +0 -1
- data/test/if_test.rb +25 -21
- data/test/include_exclude_test.rb +24 -16
- data/test/inherit_test.rb +114 -27
- data/test/inline_test.rb +42 -33
- data/test/instance_test.rb +151 -109
- data/test/is_representable_test.rb +19 -17
- data/test/json_test.rb +48 -46
- data/test/lonely_test.rb +35 -33
- data/test/nested_test.rb +16 -19
- data/test/object_test.rb +6 -6
- data/test/option_test.rb +10 -8
- data/test/parse_pipeline_test.rb +21 -15
- data/test/pipeline_test.rb +144 -108
- data/test/populator_test.rb +18 -15
- data/test/prepare_test.rb +15 -17
- data/test/private_options_test.rb +3 -2
- data/test/reader_writer_test.rb +4 -4
- data/test/realistic_benchmark.rb +21 -27
- data/test/render_nil_test.rb +1 -1
- data/test/represent_test.rb +14 -18
- data/test/representable_test.rb +86 -53
- data/test/schema_test.rb +51 -21
- data/test/serialize_deserialize_test.rb +14 -14
- data/test/skip_test.rb +39 -28
- data/test/stringify_hash_test.rb +29 -31
- data/test/test_helper.rb +31 -25
- data/test/test_helper_test.rb +7 -7
- data/test/uncategorized_test.rb +20 -15
- data/test/user_options_test.rb +12 -3
- data/test/wrap_test.rb +27 -17
- data/test/xml_assertions.rb +29 -0
- data/test/xml_bindings_test.rb +5 -8
- data/test/xml_namespace_test.rb +46 -39
- data/test/xml_test.rb +109 -88
- data/test/yaml_test.rb +73 -51
- metadata +7 -32
data/test/xml_test.rb
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
require
|
|
2
|
-
|
|
1
|
+
require "test_helper"
|
|
3
2
|
|
|
4
3
|
class XmlPublicMethodsTest < Minitest::Spec
|
|
5
4
|
#---
|
|
@@ -10,32 +9,30 @@ class XmlPublicMethodsTest < Minitest::Spec
|
|
|
10
9
|
property :name
|
|
11
10
|
end
|
|
12
11
|
|
|
13
|
-
let(:data) { %{<
|
|
12
|
+
let(:data) { %{<band><id>1</id><name>Rancid</name></band>} }
|
|
14
13
|
|
|
15
|
-
it { _(BandRepresenter.new(Band.new).from_xml(data)[:id, :name]).must_equal [
|
|
16
|
-
it { _(BandRepresenter.new(Band.new).parse(data)[:id, :name]).must_equal [
|
|
14
|
+
it { _(BandRepresenter.new(Band.new).from_xml(data)[:id, :name]).must_equal %w[1 Rancid] }
|
|
15
|
+
it { _(BandRepresenter.new(Band.new).parse(data)[:id, :name]).must_equal %w[1 Rancid] }
|
|
17
16
|
|
|
18
17
|
#---
|
|
19
18
|
# to_hash
|
|
20
19
|
let(:band) { Band.new("1", "Rancid") }
|
|
21
20
|
|
|
22
|
-
it { BandRepresenter.new(band).to_xml
|
|
23
|
-
it { BandRepresenter.new(band).render
|
|
21
|
+
it { assert_xml_equal(BandRepresenter.new(band).to_xml, data) }
|
|
22
|
+
it { assert_xml_equal(BandRepresenter.new(band).render, data) }
|
|
24
23
|
end
|
|
25
24
|
|
|
26
|
-
class XmlTest <
|
|
27
|
-
|
|
25
|
+
class XmlTest < Minitest::Spec
|
|
28
26
|
class Band
|
|
29
27
|
include Representable::XML
|
|
30
28
|
property :name
|
|
31
29
|
attr_accessor :name
|
|
32
30
|
|
|
33
|
-
def initialize(name=nil)
|
|
31
|
+
def initialize(name = nil)
|
|
34
32
|
name and self.name = name
|
|
35
33
|
end
|
|
36
34
|
end
|
|
37
35
|
|
|
38
|
-
|
|
39
36
|
XML = Representable::XML
|
|
40
37
|
Def = Representable::Definition
|
|
41
38
|
|
|
@@ -52,7 +49,6 @@ class XmlTest < MiniTest::Spec
|
|
|
52
49
|
@band = @Band.new
|
|
53
50
|
end
|
|
54
51
|
|
|
55
|
-
|
|
56
52
|
describe "#from_xml" do
|
|
57
53
|
before do
|
|
58
54
|
@band = @Band.new
|
|
@@ -61,7 +57,7 @@ class XmlTest < MiniTest::Spec
|
|
|
61
57
|
|
|
62
58
|
it "parses XML and assigns properties" do
|
|
63
59
|
@band.from_xml(@xml)
|
|
64
|
-
assert_equal [
|
|
60
|
+
assert_equal %w[Nofx NOFX], [@band.name, @band.label]
|
|
65
61
|
end
|
|
66
62
|
end
|
|
67
63
|
|
|
@@ -74,7 +70,7 @@ class XmlTest < MiniTest::Spec
|
|
|
74
70
|
|
|
75
71
|
it "parses with xmlns present" do
|
|
76
72
|
@band.from_xml(@xml)
|
|
77
|
-
assert_equal [
|
|
73
|
+
assert_equal %w[Nofx NOFX], [@band.name, @band.label]
|
|
78
74
|
end
|
|
79
75
|
end
|
|
80
76
|
|
|
@@ -86,18 +82,16 @@ class XmlTest < MiniTest::Spec
|
|
|
86
82
|
|
|
87
83
|
it "receives Nokogiri node and assigns properties" do
|
|
88
84
|
@band.from_node(@xml)
|
|
89
|
-
assert_equal [
|
|
85
|
+
assert_equal %w[Nofx NOFX], [@band.name, @band.label]
|
|
90
86
|
end
|
|
91
87
|
end
|
|
92
88
|
|
|
93
|
-
|
|
94
89
|
describe "#to_xml" do
|
|
95
90
|
it "delegates to #to_node and returns string" do
|
|
96
|
-
assert_xml_equal "<band><name>Rise Against</name></band>"
|
|
91
|
+
assert_xml_equal(Band.new("Rise Against").to_xml, "<band><name>Rise Against</name></band>")
|
|
97
92
|
end
|
|
98
93
|
end
|
|
99
94
|
|
|
100
|
-
|
|
101
95
|
describe "#to_node" do
|
|
102
96
|
it "returns Nokogiri node" do
|
|
103
97
|
node = Band.new("Rise Against").to_node
|
|
@@ -106,7 +100,7 @@ class XmlTest < MiniTest::Spec
|
|
|
106
100
|
|
|
107
101
|
it "wraps with infered class name per default" do
|
|
108
102
|
node = Band.new("Rise Against").to_node
|
|
109
|
-
assert_xml_equal "<band><name>Rise Against</name></band>"
|
|
103
|
+
assert_xml_equal(node.to_s, "<band><name>Rise Against</name></band>")
|
|
110
104
|
end
|
|
111
105
|
|
|
112
106
|
it "respects #representation_wrap=" do
|
|
@@ -116,11 +110,10 @@ class XmlTest < MiniTest::Spec
|
|
|
116
110
|
end
|
|
117
111
|
|
|
118
112
|
klass.representation_wrap = :group
|
|
119
|
-
assert_xml_equal "<group><name>Rise Against</name></group>"
|
|
113
|
+
assert_xml_equal(klass.new("Rise Against").to_node.to_s, "<group><name>Rise Against</name></group>")
|
|
120
114
|
end
|
|
121
115
|
end
|
|
122
116
|
|
|
123
|
-
|
|
124
117
|
describe "XML::Binding#build_for" do
|
|
125
118
|
it "returns AttributeBinding" do
|
|
126
119
|
assert_kind_of XML::Binding::Attribute, XML::Binding.build_for(Def.new(:band, :as => "band", :attribute => true))
|
|
@@ -132,15 +125,14 @@ class XmlTest < MiniTest::Spec
|
|
|
132
125
|
end
|
|
133
126
|
|
|
134
127
|
it "returns CollectionBinding" do
|
|
135
|
-
assert_kind_of XML::Binding::Collection, XML::Binding.build_for(Def.new(:band, :collection =>
|
|
128
|
+
assert_kind_of XML::Binding::Collection, XML::Binding.build_for(Def.new(:band, :collection => true))
|
|
136
129
|
end
|
|
137
130
|
|
|
138
131
|
it "returns HashBinding" do
|
|
139
|
-
assert_kind_of XML::Binding::Hash, XML::Binding.build_for(Def.new(:band, :hash =>
|
|
132
|
+
assert_kind_of XML::Binding::Hash, XML::Binding.build_for(Def.new(:band, :hash => true))
|
|
140
133
|
end
|
|
141
134
|
end
|
|
142
135
|
|
|
143
|
-
|
|
144
136
|
describe "DCI" do
|
|
145
137
|
module SongRepresenter
|
|
146
138
|
include Representable::XML
|
|
@@ -153,7 +145,6 @@ class XmlTest < MiniTest::Spec
|
|
|
153
145
|
collection :songs, :class => Song, :as => :song, :extend => SongRepresenter
|
|
154
146
|
end
|
|
155
147
|
|
|
156
|
-
|
|
157
148
|
it "allows adding the representer by using #extend" do
|
|
158
149
|
module BandRepresenter
|
|
159
150
|
include Representable::XML
|
|
@@ -163,25 +154,27 @@ class XmlTest < MiniTest::Spec
|
|
|
163
154
|
civ = Object.new
|
|
164
155
|
civ.instance_eval do
|
|
165
156
|
def name; "CIV"; end
|
|
157
|
+
|
|
166
158
|
def name=(v)
|
|
167
159
|
@name = v
|
|
168
160
|
end
|
|
169
161
|
end
|
|
170
162
|
|
|
171
163
|
civ.extend(BandRepresenter)
|
|
172
|
-
assert_xml_equal "<object><name>CIV</name></object>"
|
|
164
|
+
assert_xml_equal(civ.to_xml, "<object><name>CIV</name></object>")
|
|
173
165
|
end
|
|
174
166
|
|
|
175
167
|
it "extends contained models when serializing" do
|
|
176
168
|
@album = Album.new(
|
|
177
|
-
[Song.new("I Hate My Brain"), mr=Song.new("Mr. Charisma")], mr
|
|
169
|
+
[Song.new("I Hate My Brain"), mr = Song.new("Mr. Charisma")], mr
|
|
170
|
+
)
|
|
178
171
|
@album.extend(AlbumRepresenter)
|
|
179
172
|
|
|
180
|
-
@album.to_xml
|
|
173
|
+
assert_xml_equal(@album.to_xml, "<album>
|
|
181
174
|
<song><name>Mr. Charisma</name></song>
|
|
182
175
|
<song><name>I Hate My Brain</name></song>
|
|
183
176
|
<song><name>Mr. Charisma</name></song>
|
|
184
|
-
</album>"
|
|
177
|
+
</album>")
|
|
185
178
|
end
|
|
186
179
|
|
|
187
180
|
it "extends contained models when deserializing" do
|
|
@@ -195,8 +188,7 @@ class XmlTest < MiniTest::Spec
|
|
|
195
188
|
end
|
|
196
189
|
end
|
|
197
190
|
|
|
198
|
-
|
|
199
|
-
class AttributesTest < MiniTest::Spec
|
|
191
|
+
class AttributesTest < Minitest::Spec
|
|
200
192
|
describe ":as => rel, :attribute => true" do
|
|
201
193
|
class Link
|
|
202
194
|
include Representable::XML
|
|
@@ -206,9 +198,11 @@ class AttributesTest < MiniTest::Spec
|
|
|
206
198
|
end
|
|
207
199
|
|
|
208
200
|
it "#from_xml creates correct accessors" do
|
|
209
|
-
link = Link.new.from_xml(
|
|
201
|
+
link = Link.new.from_xml(
|
|
202
|
+
%{
|
|
210
203
|
<a href="http://apotomo.de" title="Home, sweet home" />
|
|
211
|
-
}
|
|
204
|
+
}
|
|
205
|
+
)
|
|
212
206
|
assert_equal "http://apotomo.de", link.href
|
|
213
207
|
assert_equal "Home, sweet home", link.title
|
|
214
208
|
end
|
|
@@ -217,35 +211,36 @@ class AttributesTest < MiniTest::Spec
|
|
|
217
211
|
link = Link.new
|
|
218
212
|
link.href = "http://apotomo.de/"
|
|
219
213
|
|
|
220
|
-
assert_xml_equal %{<link href="http://apotomo.de/">}
|
|
214
|
+
assert_xml_equal(link.to_xml, %{<link href="http://apotomo.de/">})
|
|
221
215
|
end
|
|
222
216
|
end
|
|
223
217
|
end
|
|
224
218
|
|
|
225
|
-
|
|
226
219
|
class CDataBand
|
|
227
220
|
class CData < Representable::XML::Binding
|
|
228
|
-
def serialize_node(parent,
|
|
221
|
+
def serialize_node(parent, _value)
|
|
229
222
|
parent << Nokogiri::XML::CDATA.new(parent, represented.name)
|
|
230
223
|
end
|
|
231
224
|
end
|
|
232
225
|
|
|
233
226
|
include Representable::XML
|
|
234
|
-
property :name, :binding =>
|
|
227
|
+
property :name, :binding => ->(*args) {
|
|
228
|
+
CData.new(*args)
|
|
229
|
+
} # getter: lambda { |opt| Nokogiri::XML::CDATA.new(opt[:doc], name) }
|
|
235
230
|
attr_accessor :name
|
|
236
231
|
|
|
237
|
-
def initialize(name=nil)
|
|
232
|
+
def initialize(name = nil)
|
|
238
233
|
name and self.name = name
|
|
239
234
|
end
|
|
240
235
|
end
|
|
241
236
|
|
|
242
|
-
class TypedPropertyTest <
|
|
237
|
+
class TypedPropertyTest < Minitest::Spec
|
|
243
238
|
class Band
|
|
244
239
|
include Representable::XML
|
|
245
240
|
property :name
|
|
246
241
|
attr_accessor :name
|
|
247
242
|
|
|
248
|
-
def initialize(name=nil)
|
|
243
|
+
def initialize(name = nil)
|
|
249
244
|
name and self.name = name
|
|
250
245
|
end
|
|
251
246
|
end
|
|
@@ -257,21 +252,24 @@ class TypedPropertyTest < MiniTest::Spec
|
|
|
257
252
|
|
|
258
253
|
class Album
|
|
259
254
|
attr_accessor :band
|
|
260
|
-
|
|
255
|
+
|
|
256
|
+
def initialize(band = nil)
|
|
261
257
|
@band = band
|
|
262
258
|
end
|
|
263
259
|
end
|
|
264
260
|
|
|
265
|
-
# TODO:property :group, :class => Band
|
|
261
|
+
# TODO: property :group, :class => Band
|
|
266
262
|
# :class
|
|
267
263
|
# where to mixin DCI?
|
|
268
264
|
describe ":class => Item" do
|
|
269
265
|
it "#from_xml creates one Item instance" do
|
|
270
|
-
album = Album.new.extend(AlbumRepresenter).from_xml(
|
|
266
|
+
album = Album.new.extend(AlbumRepresenter).from_xml(
|
|
267
|
+
%{
|
|
271
268
|
<album>
|
|
272
269
|
<band><name>Bad Religion</name></band>
|
|
273
270
|
</album>
|
|
274
|
-
}
|
|
271
|
+
}
|
|
272
|
+
)
|
|
275
273
|
assert_equal "Bad Religion", album.band.name
|
|
276
274
|
end
|
|
277
275
|
|
|
@@ -280,11 +278,11 @@ class TypedPropertyTest < MiniTest::Spec
|
|
|
280
278
|
band = Band.new("Bad Religion")
|
|
281
279
|
album = Album.new(band).extend(AlbumRepresenter)
|
|
282
280
|
|
|
283
|
-
assert_xml_equal %{<album>
|
|
281
|
+
assert_xml_equal(album.to_xml, %{<album>
|
|
284
282
|
<band>
|
|
285
283
|
<name>Bad Religion</name>
|
|
286
284
|
</band>
|
|
287
|
-
</album>}
|
|
285
|
+
</album>})
|
|
288
286
|
end
|
|
289
287
|
|
|
290
288
|
it "doesn't escape and wrap string from Band#to_node" do
|
|
@@ -295,7 +293,7 @@ class TypedPropertyTest < MiniTest::Spec
|
|
|
295
293
|
end
|
|
296
294
|
end
|
|
297
295
|
|
|
298
|
-
assert_xml_equal %{<album><band>Baaaad Religion</band></album>}
|
|
296
|
+
assert_xml_equal(Album.new(band).extend(AlbumRepresenter).to_xml, %{<album><band>Baaaad Religion</band></album>})
|
|
299
297
|
end
|
|
300
298
|
end
|
|
301
299
|
|
|
@@ -304,11 +302,11 @@ class TypedPropertyTest < MiniTest::Spec
|
|
|
304
302
|
band = CDataBand.new("Bad Religion")
|
|
305
303
|
album = Album.new(band).extend(AlbumRepresenter)
|
|
306
304
|
|
|
307
|
-
assert_xml_equal %{<album>
|
|
305
|
+
assert_xml_equal(album.to_xml, %{<album>
|
|
308
306
|
<c_data_band>
|
|
309
307
|
<name><![CDATA[Bad Religion]]></name>
|
|
310
308
|
</c_data_band>
|
|
311
|
-
</album>}
|
|
309
|
+
</album>})
|
|
312
310
|
end
|
|
313
311
|
end
|
|
314
312
|
end
|
|
@@ -327,7 +325,7 @@ class XMLPropertyTest < Minitest::Spec
|
|
|
327
325
|
property :genre, attribute: true
|
|
328
326
|
end
|
|
329
327
|
|
|
330
|
-
it { BandRepresenter.new(Band.new("Mute")).to_xml
|
|
328
|
+
it { assert_xml_equal(BandRepresenter.new(Band.new("Mute")).to_xml, %{<band><theyCallUs>Mute</theyCallUs></band>}) }
|
|
331
329
|
|
|
332
330
|
class ManagerRepresenter < Representable::Decorator
|
|
333
331
|
include Representable::XML
|
|
@@ -335,11 +333,19 @@ class XMLPropertyTest < Minitest::Spec
|
|
|
335
333
|
end
|
|
336
334
|
|
|
337
335
|
#- :as with nested property
|
|
338
|
-
it {
|
|
336
|
+
it {
|
|
337
|
+
assert_xml_equal(ManagerRepresenter.new(
|
|
338
|
+
Manager.new(
|
|
339
|
+
Band.new(
|
|
340
|
+
"Mute",
|
|
341
|
+
"Punkrock"
|
|
342
|
+
)
|
|
343
|
+
)
|
|
344
|
+
).to_xml, %{<manager><band genre="Punkrock"><theyCallUs>Mute</theyCallUs></band></manager>})
|
|
345
|
+
}
|
|
339
346
|
end
|
|
340
347
|
|
|
341
|
-
|
|
342
|
-
class XMLCollectionTest < MiniTest::Spec
|
|
348
|
+
class XMLCollectionTest < Minitest::Spec
|
|
343
349
|
Band = Struct.new(:name)
|
|
344
350
|
Compilation = Struct.new(:bands)
|
|
345
351
|
|
|
@@ -358,12 +364,14 @@ class XMLCollectionTest < MiniTest::Spec
|
|
|
358
364
|
|
|
359
365
|
describe "#from_xml" do
|
|
360
366
|
it "pushes collection items to array" do
|
|
361
|
-
cd = CompilationRepresenter.new(Compilation.new).from_xml(
|
|
367
|
+
cd = CompilationRepresenter.new(Compilation.new).from_xml(
|
|
368
|
+
%{
|
|
362
369
|
<compilation>
|
|
363
370
|
<group><name>Diesel Boy</name></group>
|
|
364
371
|
<group><name>Cobra Skulls</name></group>
|
|
365
372
|
</compilation>
|
|
366
|
-
}
|
|
373
|
+
}
|
|
374
|
+
)
|
|
367
375
|
assert_equal ["Cobra Skulls", "Diesel Boy"], cd.bands.map(&:name).sort
|
|
368
376
|
end
|
|
369
377
|
end
|
|
@@ -371,45 +379,48 @@ class XMLCollectionTest < MiniTest::Spec
|
|
|
371
379
|
it "responds to #to_xml" do
|
|
372
380
|
cd = Compilation.new([Band.new("Diesel Boy"), Band.new("Bad Religion")])
|
|
373
381
|
|
|
374
|
-
CompilationRepresenter.new(cd).to_xml
|
|
382
|
+
assert_xml_equal(CompilationRepresenter.new(cd).to_xml, %{<compilation>
|
|
375
383
|
<group><name>Diesel Boy</name></group>
|
|
376
384
|
<group><name>Bad Religion</name></group>
|
|
377
|
-
</compilation>}
|
|
385
|
+
</compilation>})
|
|
378
386
|
end
|
|
379
387
|
end
|
|
380
388
|
|
|
381
|
-
|
|
382
389
|
describe ":as" do
|
|
383
|
-
let(:xml_doc)
|
|
390
|
+
let(:xml_doc) do
|
|
384
391
|
Module.new do
|
|
385
392
|
include Representable::XML
|
|
386
393
|
collection :songs, :as => :song
|
|
387
|
-
end
|
|
394
|
+
end
|
|
395
|
+
end
|
|
388
396
|
|
|
389
397
|
it "collects untyped items" do
|
|
390
|
-
album = Album.new.extend(xml_doc).from_xml(
|
|
398
|
+
album = Album.new.extend(xml_doc).from_xml(
|
|
399
|
+
%{
|
|
391
400
|
<album>
|
|
392
401
|
<song>Two Kevins</song>
|
|
393
402
|
<song>Wright and Rong</song>
|
|
394
403
|
<song>Laundry Basket</song>
|
|
395
404
|
</album>
|
|
396
|
-
}
|
|
405
|
+
}
|
|
406
|
+
)
|
|
397
407
|
assert_equal ["Laundry Basket", "Two Kevins", "Wright and Rong"].sort, album.songs.sort
|
|
398
408
|
end
|
|
399
409
|
end
|
|
400
410
|
|
|
401
|
-
|
|
402
411
|
describe ":wrap" do
|
|
403
412
|
let(:album) { Album.new.extend(xml_doc) }
|
|
404
|
-
let(:xml_doc)
|
|
413
|
+
let(:xml_doc) do
|
|
405
414
|
Module.new do
|
|
406
415
|
include Representable::XML
|
|
407
416
|
collection :songs, :as => :song, :wrap => :songs
|
|
408
|
-
end
|
|
417
|
+
end
|
|
418
|
+
end
|
|
409
419
|
|
|
410
420
|
describe "#from_xml" do
|
|
411
421
|
it "finds items in wrapped collection" do
|
|
412
|
-
album.from_xml(
|
|
422
|
+
album.from_xml(
|
|
423
|
+
%{
|
|
413
424
|
<album>
|
|
414
425
|
<songs>
|
|
415
426
|
<song>Two Kevins</song>
|
|
@@ -417,7 +428,8 @@ class XMLCollectionTest < MiniTest::Spec
|
|
|
417
428
|
<song>Laundry Basket</song>
|
|
418
429
|
</songs>
|
|
419
430
|
</album>
|
|
420
|
-
}
|
|
431
|
+
}
|
|
432
|
+
)
|
|
421
433
|
assert_equal ["Laundry Basket", "Two Kevins", "Wright and Rong"].sort, album.songs.sort
|
|
422
434
|
end
|
|
423
435
|
end
|
|
@@ -425,7 +437,7 @@ class XMLCollectionTest < MiniTest::Spec
|
|
|
425
437
|
describe "#to_xml" do
|
|
426
438
|
it "wraps items" do
|
|
427
439
|
album.songs = ["Laundry Basket", "Two Kevins", "Wright and Rong"]
|
|
428
|
-
assert_xml_equal %{
|
|
440
|
+
assert_xml_equal(album.to_xml, %{
|
|
429
441
|
<album>
|
|
430
442
|
<songs>
|
|
431
443
|
<song>Laundry Basket</song>
|
|
@@ -433,13 +445,13 @@ class XMLCollectionTest < MiniTest::Spec
|
|
|
433
445
|
<song>Wright and Rong</song>
|
|
434
446
|
</songs>
|
|
435
447
|
</album>
|
|
436
|
-
}
|
|
448
|
+
})
|
|
437
449
|
end
|
|
438
450
|
end
|
|
439
451
|
end
|
|
440
452
|
|
|
441
|
-
require
|
|
442
|
-
class LonelyRepresenterTest <
|
|
453
|
+
require "representable/xml/hash"
|
|
454
|
+
class LonelyRepresenterTest < Minitest::Spec
|
|
443
455
|
# TODO: where is the XML::Hash test?
|
|
444
456
|
module SongRepresenter
|
|
445
457
|
include Representable::XML
|
|
@@ -451,20 +463,20 @@ class XMLCollectionTest < MiniTest::Spec
|
|
|
451
463
|
|
|
452
464
|
describe "XML::Collection" do
|
|
453
465
|
describe "with contained objects" do
|
|
454
|
-
representer!(:module => Representable::XML::Collection)
|
|
466
|
+
representer!(:module => Representable::XML::Collection) do
|
|
455
467
|
items :class => Song, :extend => SongRepresenter
|
|
456
|
-
self.representation_wrap= :songs
|
|
468
|
+
self.representation_wrap = :songs
|
|
457
469
|
end
|
|
458
470
|
|
|
459
471
|
let(:songs) { [Song.new("Days Go By"), Song.new("Can't Take Them All")] }
|
|
460
|
-
let(:xml_doc)
|
|
472
|
+
let(:xml_doc) { "<songs><song><name>Days Go By</name></song><song><name>Can't Take Them All</name></song></songs>" }
|
|
461
473
|
|
|
462
474
|
it "renders array" do
|
|
463
|
-
songs.extend(representer).to_xml
|
|
475
|
+
assert_xml_equal(songs.extend(representer).to_xml, xml_doc)
|
|
464
476
|
end
|
|
465
477
|
|
|
466
478
|
it "renders array with decorator" do
|
|
467
|
-
decorator.new(songs).to_xml
|
|
479
|
+
assert_xml_equal(decorator.new(songs).to_xml, xml_doc)
|
|
468
480
|
end
|
|
469
481
|
|
|
470
482
|
it "parses array" do
|
|
@@ -477,29 +489,29 @@ class XMLCollectionTest < MiniTest::Spec
|
|
|
477
489
|
end
|
|
478
490
|
end
|
|
479
491
|
|
|
480
|
-
describe "XML::AttributeHash" do
|
|
492
|
+
describe "XML::AttributeHash" do # TODO: move to HashTest.
|
|
481
493
|
representer!(:module => Representable::XML::AttributeHash) do
|
|
482
|
-
self.representation_wrap= :songs
|
|
494
|
+
self.representation_wrap = :songs
|
|
483
495
|
end
|
|
484
496
|
|
|
485
497
|
let(:songs) { {"one" => "Graveyards", "two" => "Can't Take Them All"} }
|
|
486
|
-
let(:xml_doc)
|
|
498
|
+
let(:xml_doc) { "<songs one=\"Graveyards\" two=\"Can't Take Them All\" />" }
|
|
487
499
|
|
|
488
500
|
describe "#to_xml" do
|
|
489
501
|
it "renders hash" do
|
|
490
|
-
songs.extend(representer).to_xml
|
|
502
|
+
assert_xml_equal(songs.extend(representer).to_xml, xml_doc)
|
|
491
503
|
end
|
|
492
504
|
|
|
493
505
|
it "respects :exclude" do
|
|
494
|
-
assert_xml_equal "<
|
|
506
|
+
assert_xml_equal(songs.extend(representer).to_xml(:exclude => [:one]), "<songs two=\"Can't Take Them All\" />")
|
|
495
507
|
end
|
|
496
508
|
|
|
497
509
|
it "respects :include" do
|
|
498
|
-
assert_xml_equal "<
|
|
510
|
+
assert_xml_equal(songs.extend(representer).to_xml(:include => [:two]), "<songs two=\"Can't Take Them All\" />")
|
|
499
511
|
end
|
|
500
512
|
|
|
501
513
|
it "renders hash with decorator" do
|
|
502
|
-
decorator.new(songs).to_xml
|
|
514
|
+
assert_xml_equal(decorator.new(songs).to_xml, xml_doc)
|
|
503
515
|
end
|
|
504
516
|
end
|
|
505
517
|
|
|
@@ -524,7 +536,7 @@ class XMLCollectionTest < MiniTest::Spec
|
|
|
524
536
|
end
|
|
525
537
|
end
|
|
526
538
|
|
|
527
|
-
class XmlHashTest <
|
|
539
|
+
class XmlHashTest < Minitest::Spec
|
|
528
540
|
# scalar, no object
|
|
529
541
|
describe "plain text" do
|
|
530
542
|
representer!(module: Representable::XML) do
|
|
@@ -534,7 +546,7 @@ class XmlHashTest < MiniTest::Spec
|
|
|
534
546
|
let(:doc) { "<open_struct><first>The Gargoyle</first><second>Bronx</second></open_struct>" }
|
|
535
547
|
|
|
536
548
|
# to_xml
|
|
537
|
-
it { OpenStruct.new(songs: {"first" => "The Gargoyle", "second" => "Bronx"}).extend(representer).to_xml
|
|
549
|
+
it { assert_xml_equal(OpenStruct.new(songs: {"first" => "The Gargoyle", "second" => "Bronx"}).extend(representer).to_xml, doc) }
|
|
538
550
|
# FIXME: this NEVER worked!
|
|
539
551
|
# it { OpenStruct.new.extend(representer).from_xml(doc).songs.must_equal({"first" => "The Gargoyle", "second" => "Bronx"}) }
|
|
540
552
|
end
|
|
@@ -546,17 +558,26 @@ class XmlHashTest < MiniTest::Spec
|
|
|
546
558
|
end
|
|
547
559
|
end
|
|
548
560
|
|
|
549
|
-
let(:doc)
|
|
561
|
+
let(:doc) do
|
|
562
|
+
"<open_struct>
|
|
550
563
|
<open_struct>
|
|
551
564
|
<title>The Gargoyle</title>
|
|
552
565
|
</open_struct>
|
|
553
566
|
<open_struct>
|
|
554
567
|
<title>Bronx</title>
|
|
555
568
|
</open_struct>
|
|
556
|
-
</open_struct>"
|
|
569
|
+
</open_struct>"
|
|
570
|
+
end
|
|
557
571
|
|
|
558
572
|
# to_xml
|
|
559
|
-
it {
|
|
573
|
+
it {
|
|
574
|
+
assert_xml_equal(OpenStruct.new(
|
|
575
|
+
songs: {
|
|
576
|
+
"first" => OpenStruct.new(title: "The Gargoyle"),
|
|
577
|
+
"second" => OpenStruct.new(title: "Bronx")
|
|
578
|
+
}
|
|
579
|
+
).extend(representer).to_xml, doc)
|
|
580
|
+
}
|
|
560
581
|
# FIXME: this NEVER worked!
|
|
561
582
|
# it { OpenStruct.new.extend(representer).from_xml(doc).songs.must_equal({"first" => "The Gargoyle", "second" => "Bronx"}) }
|
|
562
583
|
end
|