forked from Empact/roxml
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathas_test.rb
67 lines (55 loc) · 2.08 KB
/
as_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true
require "test_helper"
class AsTest < MiniTest::Spec
for_formats(
hash: [Representable::Hash, {"title" => "Wie Es Geht"}, {"title" => "Revolution"}]
# :xml => [Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>", "<open_struct><song><name>You've Taken Everything</name></song>/open_struct>"],
# :yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"],
) do |format, mod, input, output|
let(:song) { representer.prepare(Song.new("Revolution")) }
let(:format) { format }
describe "as: with :symbol" do
representer!(module: mod) do
property :name, as: :title
end
it { assert_equal_document render(song), output }
it { _(parse(song, input).name).must_equal "Wie Es Geht" }
end
describe "as: with lambda" do
representer!(module: mod) do
property :name, as: ->(*) { self.class.to_s }
end
it { assert_equal_document(render(song), {"Song" => "Revolution"}) }
it { _(parse(song, {"Song" => "Wie Es Geht"}).name).must_equal "Wie Es Geht" }
end
describe "lambda arguments" do
representer! do
property :name, as: ->(options) { options[:user_options].inspect }
end
it { assert_equal_document(render(song, user_options: {volume: 1}), {"{:volume=>1}" => "Revolution"}) }
it {
_(parse(song, {"{:volume=>1}" => "Wie Es Geht"}, user_options: {volume: 1}).name).must_equal "Wie Es Geht"
}
end
end
end
# hash: to_hash(wrap: ) is representation_wrap
class AsXmlTest < MiniTest::Spec
Band = Struct.new(:name, :label)
Album = Struct.new(:band)
Label = Struct.new(:name)
representer!(module: Representable::XML, decorator: true) do
self.representation_wrap = :album
property :band, as: :combo do
self.representation_wrap = :band
property :name
end
end
it do
assert_xml_equal "<album>
<combo>
<name>Offspring</name>
</combo>
</album>", representer.new(Album.new(Band.new("Offspring"))).to_xml
end
end