representable 3.0.4 → 3.2.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 +5 -5
- data/.github/workflows/ci.yml +23 -0
- data/CHANGES.md +14 -0
- data/Gemfile +4 -7
- data/LICENSE +1 -1
- data/README.md +12 -12
- data/Rakefile +1 -6
- data/lib/representable/binding.rb +32 -12
- data/lib/representable/cached.rb +1 -1
- data/lib/representable/coercion.rb +8 -6
- data/lib/representable/config.rb +8 -3
- data/lib/representable/debug.rb +23 -15
- data/lib/representable/declarative.rb +8 -3
- data/lib/representable/decorator.rb +1 -1
- data/lib/representable/definition.rb +7 -2
- data/lib/representable/deserializer.rb +4 -3
- data/lib/representable/for_collection.rb +1 -1
- data/lib/representable/hash/allow_symbols.rb +9 -11
- data/lib/representable/hash/binding.rb +1 -0
- data/lib/representable/hash/collection.rb +4 -2
- data/lib/representable/hash.rb +6 -2
- data/lib/representable/hash_methods.rb +3 -2
- data/lib/representable/insert.rb +1 -1
- data/lib/representable/json/collection.rb +3 -0
- data/lib/representable/json/hash.rb +1 -0
- data/lib/representable/json.rb +5 -7
- data/lib/representable/object/binding.rb +5 -1
- data/lib/representable/object.rb +1 -1
- data/lib/representable/option.rb +19 -0
- data/lib/representable/pipeline.rb +3 -2
- data/lib/representable/pipeline_factories.rb +4 -2
- data/lib/representable/populator.rb +1 -1
- data/lib/representable/represent.rb +1 -0
- data/lib/representable/serializer.rb +2 -1
- data/lib/representable/version.rb +1 -1
- data/lib/representable/xml/binding.rb +5 -6
- data/lib/representable/xml.rb +7 -10
- data/lib/representable/yaml/binding.rb +1 -0
- data/lib/representable/yaml.rb +3 -3
- data/lib/representable.rb +18 -25
- data/representable.gemspec +3 -3
- data/test/as_test.rb +4 -4
- data/test/binding_test.rb +10 -10
- data/test/cached_test.rb +19 -19
- data/test/class_test.rb +7 -7
- data/test/coercion_test.rb +33 -22
- data/test/config/inherit_test.rb +14 -14
- data/test/config_test.rb +18 -18
- data/test/decorator_scope_test.rb +3 -3
- data/test/decorator_test.rb +17 -17
- data/test/default_test.rb +7 -7
- data/test/definition_test.rb +32 -32
- data/test/{example.rb → examples/example.rb} +0 -0
- data/test/exec_context_test.rb +6 -6
- data/test/features_test.rb +3 -3
- data/test/filter_test.rb +6 -6
- data/test/for_collection_test.rb +2 -2
- data/test/generic_test.rb +3 -3
- data/test/getter_setter_test.rb +5 -5
- data/test/hash_test.rb +19 -19
- data/test/heritage_test.rb +4 -4
- data/test/if_test.rb +6 -6
- data/test/include_exclude_test.rb +12 -12
- data/test/inherit_test.rb +15 -15
- data/test/inline_test.rb +11 -11
- data/test/instance_test.rb +29 -29
- data/test/is_representable_test.rb +10 -10
- data/test/json_test.rb +7 -7
- data/test/lonely_test.rb +16 -16
- data/test/nested_test.rb +7 -7
- data/test/object_test.rb +7 -7
- data/test/option_test.rb +36 -0
- data/test/parse_pipeline_test.rb +3 -3
- data/test/pipeline_test.rb +43 -43
- data/test/populator_test.rb +15 -15
- data/test/prepare_test.rb +2 -2
- data/test/private_options_test.rb +2 -2
- data/test/reader_writer_test.rb +2 -2
- data/test/render_nil_test.rb +2 -2
- data/test/represent_test.rb +4 -4
- data/test/representable_test.rb +27 -27
- data/test/schema_test.rb +5 -5
- data/test/serialize_deserialize_test.rb +2 -2
- data/test/skip_test.rb +10 -10
- data/test/stringify_hash_test.rb +3 -3
- data/test/test_helper.rb +4 -2
- data/test/uncategorized_test.rb +10 -10
- data/test/user_options_test.rb +4 -4
- data/test/wrap_test.rb +11 -11
- data/test/xml_namespace_test.rb +1 -1
- data/test/xml_test.rb +6 -6
- data/test/yaml_test.rb +20 -20
- metadata +16 -11
- data/.travis.yml +0 -16
- data/lib/representable/autoload.rb +0 -14
- data/test/mongoid_test.rb +0 -31
data/test/generic_test.rb
CHANGED
|
@@ -14,13 +14,13 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
|
14
14
|
|
|
15
15
|
it "doesn't initialize property" do
|
|
16
16
|
new_album.from_hash({})
|
|
17
|
-
new_album.songs.must_be_nil
|
|
17
|
+
_(new_album.songs).must_be_nil
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
it "leaves properties untouched" do
|
|
21
21
|
album.from_hash({})
|
|
22
22
|
# TODO: test property.
|
|
23
|
-
album.songs.must_equal ["Fuck Armageddon"] # when the collection is not present in the incoming hash, this propery stays untouched.
|
|
23
|
+
_(album.songs).must_equal ["Fuck Armageddon"] # when the collection is not present in the incoming hash, this propery stays untouched.
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
|
|
@@ -109,7 +109,7 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
|
109
109
|
end
|
|
110
110
|
|
|
111
111
|
it "doesn't change represented object" do
|
|
112
|
-
song.extend(representer).send("from_#{format}", input).title.must_equal "Resist Stance"
|
|
112
|
+
_(song.extend(representer).send("from_#{format}", input).title).must_equal "Resist Stance"
|
|
113
113
|
end
|
|
114
114
|
end
|
|
115
115
|
end
|
data/test/getter_setter_test.rb
CHANGED
|
@@ -3,19 +3,19 @@ require 'test_helper'
|
|
|
3
3
|
class GetterSetterTest < BaseTest
|
|
4
4
|
representer! do
|
|
5
5
|
property :name, # key under :name.
|
|
6
|
-
:getter => lambda { |
|
|
7
|
-
:setter => lambda { |
|
|
6
|
+
:getter => lambda { |user_options:, **| "#{user_options[:welcome]} #{song_name}" },
|
|
7
|
+
:setter => lambda { |user_options:, input:, **| self.song_name = "#{user_options[:welcome]} #{input}" }
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
subject { Struct.new(:song_name).new("Mony Mony").extend(representer) }
|
|
11
11
|
|
|
12
12
|
it "uses :getter when rendering" do
|
|
13
13
|
subject.instance_eval { def name; raise; end }
|
|
14
|
-
subject.to_hash(user_options: {welcome: "Hi"}).must_equal({"name" => "Hi Mony Mony"})
|
|
14
|
+
_(subject.to_hash(user_options: {welcome: "Hi"})).must_equal({"name" => "Hi Mony Mony"})
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it "uses :setter when parsing" do
|
|
18
18
|
subject.instance_eval { def name=(*); raise; end; self }
|
|
19
|
-
subject.from_hash({"name" => "Eyes Without A Face"}, user_options: {welcome: "Hello"}).song_name.must_equal "Hello Eyes Without A Face"
|
|
19
|
+
_(subject.from_hash({"name" => "Eyes Without A Face"}, user_options: {welcome: "Hello"}).song_name).must_equal "Hello Eyes Without A Face"
|
|
20
20
|
end
|
|
21
|
-
end
|
|
21
|
+
end
|
data/test/hash_test.rb
CHANGED
|
@@ -11,15 +11,15 @@ class HashPublicMethodsTest < Minitest::Spec
|
|
|
11
11
|
|
|
12
12
|
let(:data) { {"id"=>1,"name"=>"Rancid"} }
|
|
13
13
|
|
|
14
|
-
it { BandRepresenter.new(Band.new).from_hash(data)[:id, :name].must_equal [1, "Rancid"] }
|
|
15
|
-
it { BandRepresenter.new(Band.new).parse(data)[:id, :name].must_equal [1, "Rancid"] }
|
|
14
|
+
it { _(BandRepresenter.new(Band.new).from_hash(data)[:id, :name]).must_equal [1, "Rancid"] }
|
|
15
|
+
it { _(BandRepresenter.new(Band.new).parse(data)[:id, :name]).must_equal [1, "Rancid"] }
|
|
16
16
|
|
|
17
17
|
#---
|
|
18
18
|
# to_hash
|
|
19
19
|
let(:band) { Band.new(1, "Rancid") }
|
|
20
20
|
|
|
21
|
-
it { BandRepresenter.new(band).to_hash.must_equal data }
|
|
22
|
-
it { BandRepresenter.new(band).render.must_equal data }
|
|
21
|
+
it { _(BandRepresenter.new(band).to_hash).must_equal data }
|
|
22
|
+
it { _(BandRepresenter.new(band).render).must_equal data }
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
class HashWithScalarPropertyTest < MiniTest::Spec
|
|
@@ -33,20 +33,20 @@ class HashWithScalarPropertyTest < MiniTest::Spec
|
|
|
33
33
|
|
|
34
34
|
describe "#to_hash" do
|
|
35
35
|
it "renders plain property" do
|
|
36
|
-
album.extend(representer).to_hash.must_equal("title" => "Liar")
|
|
36
|
+
_(album.extend(representer).to_hash).must_equal("title" => "Liar")
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
|
|
41
41
|
describe "#from_hash" do
|
|
42
42
|
it "parses plain property" do
|
|
43
|
-
album.extend(representer).from_hash("title" => "This Song Is Recycled").title.must_equal "This Song Is Recycled"
|
|
43
|
+
_(album.extend(representer).from_hash("title" => "This Song Is Recycled").title).must_equal "This Song Is Recycled"
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
# Fixes issue #115
|
|
47
47
|
it "allows nil value in the incoming document and corresponding nil value for the represented" do
|
|
48
48
|
album = Album.new
|
|
49
|
-
album.title.must_be_nil
|
|
49
|
+
_(album.title).must_be_nil
|
|
50
50
|
album.extend(representer).from_hash("title" => nil)
|
|
51
51
|
end
|
|
52
52
|
end
|
|
@@ -66,35 +66,35 @@ class HashWithTypedPropertyTest < MiniTest::Spec
|
|
|
66
66
|
|
|
67
67
|
describe "#to_hash" do
|
|
68
68
|
it "renders embedded typed property" do
|
|
69
|
-
album.extend(representer).to_hash.must_equal("best_song" => {"name" => "Liar"})
|
|
69
|
+
_(album.extend(representer).to_hash).must_equal("best_song" => {"name" => "Liar"})
|
|
70
70
|
end
|
|
71
71
|
end
|
|
72
72
|
|
|
73
73
|
describe "#from_hash" do
|
|
74
74
|
it "parses embedded typed property" do
|
|
75
75
|
album.extend(representer).from_hash("best_song" => {"name" => "Go With Me"})
|
|
76
|
-
album.best_song.name.must_equal "Go With Me"
|
|
76
|
+
_(album.best_song.name).must_equal "Go With Me"
|
|
77
77
|
end
|
|
78
78
|
|
|
79
79
|
# nested nil removes nested object.
|
|
80
80
|
it do
|
|
81
81
|
album = Album.new(Song.new("Pre-medicated Murder"))
|
|
82
82
|
album.extend(representer).from_hash("best_song" => nil)
|
|
83
|
-
album.best_song.must_be_nil
|
|
83
|
+
_(album.best_song).must_be_nil
|
|
84
84
|
end
|
|
85
85
|
|
|
86
86
|
# nested blank hash creates blank object when not populated.
|
|
87
87
|
it do
|
|
88
88
|
album = Album.new#(Song.new("Pre-medicated Murder"))
|
|
89
89
|
album.extend(representer).from_hash("best_song" => {})
|
|
90
|
-
album.best_song.name.must_be_nil
|
|
90
|
+
_(album.best_song.name).must_be_nil
|
|
91
91
|
end
|
|
92
92
|
|
|
93
93
|
# Fixes issue #115
|
|
94
94
|
it "allows nil value in the incoming document and corresponding nil value for the represented" do
|
|
95
95
|
album = Album.new
|
|
96
96
|
album.extend(representer).from_hash("best_song" => nil)
|
|
97
|
-
album.best_song.must_be_nil
|
|
97
|
+
_(album.best_song).must_be_nil
|
|
98
98
|
end
|
|
99
99
|
end
|
|
100
100
|
end
|
|
@@ -109,8 +109,8 @@ class HashWithTypedPropertyAndAs < MiniTest::Spec
|
|
|
109
109
|
|
|
110
110
|
let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(representer) }
|
|
111
111
|
|
|
112
|
-
it { album.to_hash.must_equal("hit" => {"name" => "Liar"}) }
|
|
113
|
-
it { album.from_hash("hit" => {"name" => "Go With Me"}).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
|
|
112
|
+
it { _(album.to_hash).must_equal("hit" => {"name" => "Liar"}) }
|
|
113
|
+
it { _(album.from_hash("hit" => {"name" => "Go With Me"})).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
|
|
114
114
|
end
|
|
115
115
|
# # describe "FIXME COMBINE WITH ABOVE with :extend and :as" do
|
|
116
116
|
# # hash_song = Module.new do
|
|
@@ -148,14 +148,14 @@ class HashWithTypedCollectionTest < MiniTest::Spec
|
|
|
148
148
|
|
|
149
149
|
describe "#to_hash" do
|
|
150
150
|
it "renders collection of typed property" do
|
|
151
|
-
album.extend(representer).to_hash.must_equal("songs" => [{"name" => "Liar", "track" => 1}, {"name" => "What I Know", "track" => 2}])
|
|
151
|
+
_(album.extend(representer).to_hash).must_equal("songs" => [{"name" => "Liar", "track" => 1}, {"name" => "What I Know", "track" => 2}])
|
|
152
152
|
end
|
|
153
153
|
end
|
|
154
154
|
|
|
155
155
|
describe "#from_hash" do
|
|
156
156
|
it "parses collection of typed property" do
|
|
157
|
-
album.extend(representer).from_hash("songs" => [{"name" => "One Shot Deal", "track" => 4},
|
|
158
|
-
{"name" => "Three Way Dance", "track" => 5}]).must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
|
|
157
|
+
_(album.extend(representer).from_hash("songs" => [{"name" => "One Shot Deal", "track" => 4},
|
|
158
|
+
{"name" => "Three Way Dance", "track" => 5}])).must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
|
|
159
159
|
end
|
|
160
160
|
end
|
|
161
161
|
end
|
|
@@ -169,14 +169,14 @@ class HashWithScalarCollectionTest < MiniTest::Spec
|
|
|
169
169
|
|
|
170
170
|
describe "#to_hash" do
|
|
171
171
|
it "renders a block style list per default" do
|
|
172
|
-
album.extend(representer).to_hash.must_equal("songs" => ["Jackhammer", "Terrible Man"])
|
|
172
|
+
_(album.extend(representer).to_hash).must_equal("songs" => ["Jackhammer", "Terrible Man"])
|
|
173
173
|
end
|
|
174
174
|
end
|
|
175
175
|
|
|
176
176
|
|
|
177
177
|
describe "#from_hash" do
|
|
178
178
|
it "parses a block style list" do
|
|
179
|
-
album.extend(representer).from_hash("songs" => ["Off Key Melody", "Sinking"]).must_equal Album.new(["Off Key Melody", "Sinking"])
|
|
179
|
+
_(album.extend(representer).from_hash("songs" => ["Off Key Melody", "Sinking"])).must_equal Album.new(["Off Key Melody", "Sinking"])
|
|
180
180
|
end
|
|
181
181
|
end
|
|
182
182
|
end
|
data/test/heritage_test.rb
CHANGED
|
@@ -35,15 +35,15 @@ class HeritageTest < Minitest::Spec
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
it "B must inherit Hello! feature from A" do
|
|
38
|
-
B.representable_attrs.get(:id)[:extend].(nil).new(nil).hello.must_equal "Hello!"
|
|
38
|
+
_(B.representable_attrs.get(:id)[:extend].(nil).new(nil).hello).must_equal "Hello!"
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
it "B must have Ciao from module (feauture) Ciao" do
|
|
42
|
-
B.representable_attrs.get(:id)[:extend].(nil).new(nil).ciao.must_equal "Ciao!"
|
|
42
|
+
_(B.representable_attrs.get(:id)[:extend].(nil).new(nil).ciao).must_equal "Ciao!"
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
it "C must inherit Hello! feature from A" do
|
|
46
|
-
C.representable_attrs.get(:id)[:extend].(nil).new(nil).hello.must_equal "Hello!"
|
|
46
|
+
_(C.representable_attrs.get(:id)[:extend].(nil).new(nil).hello).must_equal "Hello!"
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
module M
|
|
@@ -60,6 +60,6 @@ class HeritageTest < Minitest::Spec
|
|
|
60
60
|
let(:obj_extending_N) { Object.new.extend(N) }
|
|
61
61
|
|
|
62
62
|
it "obj should inherit from N, and N from M" do
|
|
63
|
-
obj_extending_N.hello.must_equal "Hello!"
|
|
63
|
+
_(obj_extending_N.hello).must_equal "Hello!"
|
|
64
64
|
end
|
|
65
65
|
end
|
data/test/if_test.rb
CHANGED
|
@@ -44,20 +44,20 @@ class IfTest < MiniTest::Spec
|
|
|
44
44
|
subject { OpenStruct.new(:signed_contract => false, :label => "Fat") }
|
|
45
45
|
|
|
46
46
|
it "skips when false" do
|
|
47
|
-
subject.extend(representer).to_hash.must_equal({})
|
|
47
|
+
_(subject.extend(representer).to_hash).must_equal({})
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
it "represents when true" do
|
|
51
51
|
subject.signed_contract= true
|
|
52
|
-
subject.extend(representer).to_hash.must_equal({"label"=>"Fat"})
|
|
52
|
+
_(subject.extend(representer).to_hash).must_equal({"label"=>"Fat"})
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
it "works with decorator" do
|
|
56
56
|
rpr = representer
|
|
57
|
-
Class.new(Representable::Decorator) do
|
|
57
|
+
_(Class.new(Representable::Decorator) do
|
|
58
58
|
include Representable::Hash
|
|
59
59
|
include rpr
|
|
60
|
-
end.new(subject).to_hash.must_equal({})
|
|
60
|
+
end.new(subject).to_hash).must_equal({})
|
|
61
61
|
end
|
|
62
62
|
end
|
|
63
63
|
|
|
@@ -69,11 +69,11 @@ class IfTest < MiniTest::Spec
|
|
|
69
69
|
subject { OpenStruct.new(:name => "Outbound").extend(representer) }
|
|
70
70
|
|
|
71
71
|
it "works without specifying options" do
|
|
72
|
-
subject.to_hash.must_equal({})
|
|
72
|
+
_(subject.to_hash).must_equal({})
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
it "passes user options to block" do
|
|
76
|
-
subject.to_hash(user_options: { include_name: true }).must_equal({"name" => "Outbound"})
|
|
76
|
+
_(subject.to_hash(user_options: { include_name: true })).must_equal({"name" => "Outbound"})
|
|
77
77
|
end
|
|
78
78
|
end
|
|
79
79
|
end
|
|
@@ -24,15 +24,15 @@ class IncludeExcludeTest < Minitest::Spec
|
|
|
24
24
|
it "accepts :exclude option" do
|
|
25
25
|
decorator.from_hash({"title"=>"Don't Smile In Trouble", "artist"=>{"id"=>2}}, exclude: [:title])
|
|
26
26
|
|
|
27
|
-
song.title.must_equal "Listless"
|
|
28
|
-
song.artist.must_equal Artist.new(nil, 2)
|
|
27
|
+
_(song.title).must_equal "Listless"
|
|
28
|
+
_(song.artist).must_equal Artist.new(nil, 2)
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it "accepts :include option" do
|
|
32
32
|
decorator.from_hash({"title"=>"Don't Smile In Trouble", "artist"=>{"id"=>2}}, include: [:title])
|
|
33
33
|
|
|
34
|
-
song.title.must_equal "Don't Smile In Trouble"
|
|
35
|
-
song.artist.must_equal Artist.new("7yearsbadluck", 1)
|
|
34
|
+
_(song.title).must_equal "Don't Smile In Trouble"
|
|
35
|
+
_(song.artist).must_equal Artist.new("7yearsbadluck", 1)
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
it "accepts nested :exclude/:include option" do
|
|
@@ -44,30 +44,30 @@ class IncludeExcludeTest < Minitest::Spec
|
|
|
44
44
|
}
|
|
45
45
|
)
|
|
46
46
|
|
|
47
|
-
song.title.must_equal "Listless"
|
|
48
|
-
song.artist.must_equal Artist.new("Foo", nil, [Song.new("Listless", nil, nil)])
|
|
47
|
+
_(song.title).must_equal "Listless"
|
|
48
|
+
_(song.artist).must_equal Artist.new("Foo", nil, [Song.new("Listless", nil, nil)])
|
|
49
49
|
end
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
describe "#to_hash" do
|
|
53
53
|
it "accepts :exclude option" do
|
|
54
|
-
decorator.to_hash(exclude: [:title]).must_equal({"artist"=>{"name"=>"7yearsbadluck", "id"=>1}})
|
|
54
|
+
_(decorator.to_hash(exclude: [:title])).must_equal({"artist"=>{"name"=>"7yearsbadluck", "id"=>1}})
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
it "accepts :include option" do
|
|
58
|
-
decorator.to_hash(include: [:title]).must_equal({"title"=>"Listless"})
|
|
58
|
+
_(decorator.to_hash(include: [:title])).must_equal({"title"=>"Listless"})
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
it "accepts nested :exclude/:include option" do
|
|
62
62
|
decorator = representer.new(Song.new("Listless", Artist.new("7yearsbadluck", 1, [Song.new("C.O.A.B.I.E.T.L.")])))
|
|
63
63
|
|
|
64
|
-
decorator.to_hash(
|
|
64
|
+
_(decorator.to_hash(
|
|
65
65
|
exclude: [:title],
|
|
66
66
|
artist: {
|
|
67
67
|
exclude: [:id],
|
|
68
68
|
songs: { include: [:title] }
|
|
69
69
|
}
|
|
70
|
-
).must_equal({"artist"=>{"name"=>"7yearsbadluck", "songs"=>[{"title"=>"C.O.A.B.I.E.T.L."}]}})
|
|
70
|
+
)).must_equal({"artist"=>{"name"=>"7yearsbadluck", "songs"=>[{"title"=>"C.O.A.B.I.E.T.L."}]}})
|
|
71
71
|
end
|
|
72
72
|
end
|
|
73
73
|
|
|
@@ -82,7 +82,7 @@ class IncludeExcludeTest < Minitest::Spec
|
|
|
82
82
|
|
|
83
83
|
# FIXME: we should test all representable-options (:include, :exclude, ?)
|
|
84
84
|
|
|
85
|
-
Cover.new("Roxanne", Cover.new("Roxanne (Don't Put On The Red Light)")).extend(cover_rpr).
|
|
86
|
-
to_hash(:include => [:original]).must_equal({"original"=>{"title"=>"Roxanne (Don't Put On The Red Light)"}})
|
|
85
|
+
_(Cover.new("Roxanne", Cover.new("Roxanne (Don't Put On The Red Light)")).extend(cover_rpr).
|
|
86
|
+
to_hash(:include => [:original])).must_equal({"original"=>{"title"=>"Roxanne (Don't Put On The Red Light)"}})
|
|
87
87
|
end
|
|
88
88
|
end
|
data/test/inherit_test.rb
CHANGED
|
@@ -19,8 +19,8 @@ class InheritTest < MiniTest::Spec
|
|
|
19
19
|
property :track, :inherit => true, :getter => lambda { |*| "n/a" }
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
it { SongRepresenter.prepare(song).to_hash.must_equal({"title"=>{"str"=>"Roxanne"}, "no"=>1}) }
|
|
23
|
-
it { representer.prepare(song).to_hash.must_equal({"title"=>{"str"=>"Roxanne"}, "no"=>"n/a"}) } # as: inherited.
|
|
22
|
+
it { _(SongRepresenter.prepare(song).to_hash).must_equal({"title"=>{"str"=>"Roxanne"}, "no"=>1}) }
|
|
23
|
+
it { _(representer.prepare(song).to_hash).must_equal({"title"=>{"str"=>"Roxanne"}, "no"=>"n/a"}) } # as: inherited.
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
describe ":inherit with empty inline representer" do
|
|
@@ -32,9 +32,9 @@ class InheritTest < MiniTest::Spec
|
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
it { SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
35
|
+
it { _(SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
36
36
|
# the block doesn't override the inline representer.
|
|
37
|
-
it { representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
37
|
+
it { _(representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
describe ":inherit with overriding inline representer" do
|
|
@@ -48,7 +48,7 @@ class InheritTest < MiniTest::Spec
|
|
|
48
48
|
end
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
-
it { representer.prepare( Song.new(Struct.new(:string, :length).new("Believe It", 10), 1)).to_hash.must_equal({"title"=>{"s"=>"Believe It","length"=>10}, "no"=>1}) }
|
|
51
|
+
it { _(representer.prepare( Song.new(Struct.new(:string, :length).new("Believe It", 10), 1)).to_hash).must_equal({"title"=>{"s"=>"Believe It","length"=>10}, "no"=>1}) }
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
describe ":inherit with empty inline and options" do
|
|
@@ -60,8 +60,8 @@ class InheritTest < MiniTest::Spec
|
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
-
it { SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
64
|
-
it { representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"name"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
63
|
+
it { _(SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
64
|
+
it { _(representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"name"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
65
65
|
end
|
|
66
66
|
|
|
67
67
|
describe ":inherit with inline without block but options" do
|
|
@@ -71,8 +71,8 @@ class InheritTest < MiniTest::Spec
|
|
|
71
71
|
property :name, :inherit => true, :as => :name # FIXME: add :getter or something else dynamic since this is double-wrapped.
|
|
72
72
|
end
|
|
73
73
|
|
|
74
|
-
it { SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
75
|
-
it { representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"name"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
74
|
+
it { _(SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
75
|
+
it { _(representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"name"=>{"str"=>"Believe It"}, "no"=>1}) }
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
|
|
@@ -86,12 +86,12 @@ class InheritTest < MiniTest::Spec
|
|
|
86
86
|
end
|
|
87
87
|
|
|
88
88
|
it "replaces inherited property" do
|
|
89
|
-
representer.representable_attrs.size.must_equal 2
|
|
89
|
+
_(representer.representable_attrs.size).must_equal 2
|
|
90
90
|
|
|
91
91
|
definition = representer.representable_attrs.get(:track) # TODO: find a better way to assert Definition identity.
|
|
92
92
|
# definition.keys.size.must_equal 2
|
|
93
|
-
definition[:representable]. must_equal true
|
|
94
|
-
definition.name.must_equal "track" # was "no".
|
|
93
|
+
_(definition[:representable]). must_equal true
|
|
94
|
+
_(definition.name).must_equal "track" # was "no".
|
|
95
95
|
end
|
|
96
96
|
end
|
|
97
97
|
|
|
@@ -119,12 +119,12 @@ class InheritTest < MiniTest::Spec
|
|
|
119
119
|
end
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
it { representer.new(OpenStruct.new(hit: OpenStruct.new(title: "I WILL BE OVERRIDDEN", :length => "2:59"))).to_hash.must_equal(
|
|
122
|
+
it { _(representer.new(OpenStruct.new(hit: OpenStruct.new(title: "I WILL BE OVERRIDDEN", :length => "2:59"))).to_hash).must_equal(
|
|
123
123
|
{"hit"=>{"title"=>"Cheap Transistor Radio"}}) }
|
|
124
124
|
|
|
125
125
|
# inheriting decorator inherits inline representer class (InlineRepresenter#title).
|
|
126
126
|
# inheriting decorator adds :length.
|
|
127
|
-
it { inheriting.new(OpenStruct.new(:hit => OpenStruct.new(:title => "Hole In Your Soul", :length => "2:59"))).to_hash.must_equal(
|
|
127
|
+
it { _(inheriting.new(OpenStruct.new(:hit => OpenStruct.new(:title => "Hole In Your Soul", :length => "2:59"))).to_hash).must_equal(
|
|
128
128
|
{"hit"=>{"title"=>"Cheap Transistor Radio", "length"=>"2:59"}}) }
|
|
129
129
|
end
|
|
130
130
|
|
|
@@ -135,7 +135,7 @@ class InheritTest < MiniTest::Spec
|
|
|
135
135
|
property :name, :inherit => true
|
|
136
136
|
end
|
|
137
137
|
|
|
138
|
-
it { representer.prepare(Song.new("The Beginning")).to_hash.must_equal({"name"=>"The Beginning"})}
|
|
138
|
+
it { _(representer.prepare(Song.new("The Beginning")).to_hash).must_equal({"name"=>"The Beginning"})}
|
|
139
139
|
end
|
|
140
140
|
end
|
|
141
141
|
|
data/test/inline_test.rb
CHANGED
|
@@ -22,7 +22,7 @@ class InlineTest < MiniTest::Spec
|
|
|
22
22
|
let(:format) { format }
|
|
23
23
|
|
|
24
24
|
it { render(request).must_equal_document output }
|
|
25
|
-
it { parse(request, input).song.name.must_equal "You've Taken Everything"}
|
|
25
|
+
it { _(parse(request, input).song.name).must_equal "You've Taken Everything"}
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
28
|
|
|
@@ -47,7 +47,7 @@ class InlineTest < MiniTest::Spec
|
|
|
47
47
|
let(:format) { format } # FIXME: why do we have to define this?
|
|
48
48
|
|
|
49
49
|
it { render(request).must_equal_document output }
|
|
50
|
-
it { parse(request, input).songs.first.name.must_equal "You've Taken Everything"}
|
|
50
|
+
it { _(parse(request, input).songs.first.name).must_equal "You've Taken Everything"}
|
|
51
51
|
end
|
|
52
52
|
end
|
|
53
53
|
|
|
@@ -58,7 +58,7 @@ class InlineTest < MiniTest::Spec
|
|
|
58
58
|
end
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
-
it { request.to_hash.must_equal({"song"=>{"name"=>"Alive"}}) }
|
|
61
|
+
it { _(request.to_hash).must_equal({"song"=>{"name"=>"Alive"}}) }
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
|
|
@@ -76,7 +76,7 @@ class InlineTest < MiniTest::Spec
|
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
it "doesn't change represented object" do
|
|
79
|
-
request.send("from_#{format}", input).song.must_equal song
|
|
79
|
+
_(request.send("from_#{format}", input).song).must_equal song
|
|
80
80
|
end
|
|
81
81
|
end
|
|
82
82
|
end
|
|
@@ -96,8 +96,8 @@ class InlineTest < MiniTest::Spec
|
|
|
96
96
|
|
|
97
97
|
let(:decorator) { representer.prepare(request) }
|
|
98
98
|
|
|
99
|
-
it { decorator.to_hash.must_equal({"requester"=>"Josephine", "song"=>{"name"=>"Alive"}}) }
|
|
100
|
-
it { decorator.from_hash({"song"=>{"name"=>"You've Taken Everything"}}).song.name.must_equal "You've Taken Everything"}
|
|
99
|
+
it { _(decorator.to_hash).must_equal({"requester"=>"Josephine", "song"=>{"name"=>"Alive"}}) }
|
|
100
|
+
it { _(decorator.from_hash({"song"=>{"name"=>"You've Taken Everything"}}).song.name).must_equal "You've Taken Everything"}
|
|
101
101
|
end
|
|
102
102
|
end
|
|
103
103
|
|
|
@@ -110,7 +110,7 @@ class InlineTest < MiniTest::Spec
|
|
|
110
110
|
|
|
111
111
|
it "uses an inline decorator and doesn't alter represented" do
|
|
112
112
|
representer.prepare(Struct.new(:song).new(song)).to_hash
|
|
113
|
-
song.wont_be_kind_of Representable
|
|
113
|
+
_(song).wont_be_kind_of Representable
|
|
114
114
|
end
|
|
115
115
|
end
|
|
116
116
|
|
|
@@ -221,8 +221,8 @@ class InlineTest < MiniTest::Spec
|
|
|
221
221
|
end
|
|
222
222
|
end
|
|
223
223
|
|
|
224
|
-
it do OpenStruct.new(:song => OpenStruct.new(:title => "The Fever And The Sound", :artist => "Strung Out")).extend(representer).
|
|
225
|
-
to_hash.
|
|
224
|
+
it do _(OpenStruct.new(:song => OpenStruct.new(:title => "The Fever And The Sound", :artist => "Strung Out")).extend(representer).
|
|
225
|
+
to_hash).
|
|
226
226
|
must_equal({"song"=>{"artist"=>"Strung Out", "title"=>"The Fever And The Sound"}})
|
|
227
227
|
end
|
|
228
228
|
end
|
|
@@ -246,7 +246,7 @@ class InlineTest < MiniTest::Spec
|
|
|
246
246
|
end
|
|
247
247
|
end
|
|
248
248
|
|
|
249
|
-
it { Object.new.extend(Mod).to_hash.must_equal("song"=>{"duration"=>"6:53"}) }
|
|
249
|
+
it { _(Object.new.extend(Mod).to_hash).must_equal("song"=>{"duration"=>"6:53"}) }
|
|
250
250
|
end
|
|
251
251
|
|
|
252
252
|
# define method inline with Decorator
|
|
@@ -267,6 +267,6 @@ class InlineTest < MiniTest::Spec
|
|
|
267
267
|
end
|
|
268
268
|
end
|
|
269
269
|
|
|
270
|
-
it { dec.new(Object.new).to_hash.must_equal("song"=>{"duration"=>"6:53"}) }
|
|
270
|
+
it { _(dec.new(Object.new).to_hash).must_equal("song"=>{"duration"=>"6:53"}) }
|
|
271
271
|
end
|
|
272
272
|
end
|
data/test/instance_test.rb
CHANGED
|
@@ -22,7 +22,7 @@ class InstanceTest < BaseTest
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
song = OpenStruct.new.extend(representer).from_hash({"title" => object})
|
|
25
|
-
song.title.must_equal object
|
|
25
|
+
_(song.title).must_equal object
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
28
|
|
|
@@ -36,11 +36,11 @@ class InstanceTest < BaseTest
|
|
|
36
36
|
:extend => song_representer
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
it( "xxx") { OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
|
40
|
-
from_hash("song" => {"id" => 1}).song.must_equal Song.new(1, "The Answer Is Still No") }
|
|
39
|
+
it( "xxx") { _(OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
|
40
|
+
from_hash("song" => {"id" => 1}).song).must_equal Song.new(1, "The Answer Is Still No") }
|
|
41
41
|
|
|
42
|
-
it { OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
|
43
|
-
from_hash("song" => {"id" => 2}).song.must_equal Song.new(2, "Invincible") }
|
|
42
|
+
it { _(OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
|
43
|
+
from_hash("song" => {"id" => 2}).song).must_equal Song.new(2, "Invincible") }
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
|
|
@@ -59,9 +59,9 @@ class InstanceTest < BaseTest
|
|
|
59
59
|
Song.new(1, "The Answer Is Still No"),
|
|
60
60
|
Song.new(2, "")])
|
|
61
61
|
|
|
62
|
-
album.
|
|
62
|
+
_(album.
|
|
63
63
|
extend(representer).
|
|
64
|
-
from_hash("songs" => [{"id" => 2},{"id" => 2, "title"=>"The Answer Is Still No"}]).songs.must_equal [
|
|
64
|
+
from_hash("songs" => [{"id" => 2},{"id" => 2, "title"=>"The Answer Is Still No"}]).songs).must_equal [
|
|
65
65
|
Song.new(2, "Invincible"), Song.new(2, "The Answer Is Still No")]
|
|
66
66
|
}
|
|
67
67
|
end
|
|
@@ -71,8 +71,8 @@ class InstanceTest < BaseTest
|
|
|
71
71
|
property :song, :instance => lambda { |options| Struct.new(:args, :id).new([options[:fragment], options[:user_options]]) }, :extend => song_representer
|
|
72
72
|
end
|
|
73
73
|
|
|
74
|
-
it { OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
|
75
|
-
from_hash({"song" => {"id" => 1}}, user_options: { volume: 1 }).song.args.must_equal([{"id"=>1}, {:volume=>1}]) }
|
|
74
|
+
it { _(OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
|
75
|
+
from_hash({"song" => {"id" => 1}}, user_options: { volume: 1 }).song.args).must_equal([{"id"=>1}, {:volume=>1}]) }
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
# TODO: raise and test instance:{nil}
|
|
@@ -117,13 +117,13 @@ class InstanceTest < BaseTest
|
|
|
117
117
|
extend(representer).
|
|
118
118
|
from_hash("songs" => [{"title" => "The Answer Is Still No"}, {"title" => "Invincible"}])
|
|
119
119
|
|
|
120
|
-
album.songs.must_equal [
|
|
120
|
+
_(album.songs).must_equal [
|
|
121
121
|
Song.new(1, "The Answer Is Still No"),
|
|
122
122
|
Song.new(2, "Invincible")]
|
|
123
123
|
|
|
124
|
-
songs.object_id.must_equal album.songs.object_id
|
|
125
|
-
songs[0].object_id.must_equal album.songs[0].object_id
|
|
126
|
-
songs[1].object_id.must_equal album.songs[1].object_id
|
|
124
|
+
_(songs.object_id).must_equal album.songs.object_id
|
|
125
|
+
_(songs[0].object_id).must_equal album.songs[0].object_id
|
|
126
|
+
_(songs[1].object_id).must_equal album.songs[1].object_id
|
|
127
127
|
}
|
|
128
128
|
end
|
|
129
129
|
|
|
@@ -146,17 +146,17 @@ class InstanceTest < BaseTest
|
|
|
146
146
|
Song.new(1, "The Answer Is Still No"),
|
|
147
147
|
Song.new(2, "Invncble")])
|
|
148
148
|
|
|
149
|
-
album.
|
|
149
|
+
_(album.
|
|
150
150
|
extend(representer).
|
|
151
151
|
from_hash("songs" => [{"id" => 2, "title" => "Invincible"}]).
|
|
152
|
-
songs.must_equal [
|
|
152
|
+
songs).must_equal [
|
|
153
153
|
Song.new(1, "The Answer Is Still No"),
|
|
154
154
|
Song.new(2, "Invincible")]
|
|
155
155
|
# TODO: check elements object_id!
|
|
156
156
|
|
|
157
|
-
songs.object_id.must_equal album.songs.object_id
|
|
158
|
-
songs[0].object_id.must_equal album.songs[0].object_id
|
|
159
|
-
songs[1].object_id.must_equal album.songs[1].object_id
|
|
157
|
+
_(songs.object_id).must_equal album.songs.object_id
|
|
158
|
+
_(songs[0].object_id).must_equal album.songs[0].object_id
|
|
159
|
+
_(songs[1].object_id).must_equal album.songs[1].object_id
|
|
160
160
|
}
|
|
161
161
|
end
|
|
162
162
|
|
|
@@ -178,15 +178,15 @@ class InstanceTest < BaseTest
|
|
|
178
178
|
album= Struct.new(:songs).new(songs = [
|
|
179
179
|
Song.new(1, "The Answer Is Still No")])
|
|
180
180
|
|
|
181
|
-
album.
|
|
181
|
+
_(album.
|
|
182
182
|
extend(representer).
|
|
183
183
|
from_hash("songs" => [{"title" => "Invincible"}]).
|
|
184
|
-
songs.must_equal [
|
|
184
|
+
songs).must_equal [
|
|
185
185
|
Song.new(1, "The Answer Is Still No"),
|
|
186
186
|
Song.new(2, "Invincible")]
|
|
187
187
|
|
|
188
|
-
songs.object_id.must_equal album.songs.object_id
|
|
189
|
-
songs[0].object_id.must_equal album.songs[0].object_id
|
|
188
|
+
_(songs.object_id).must_equal album.songs.object_id
|
|
189
|
+
_(songs[0].object_id).must_equal album.songs[0].object_id
|
|
190
190
|
}
|
|
191
191
|
end
|
|
192
192
|
|
|
@@ -212,15 +212,15 @@ class InstanceTest < BaseTest
|
|
|
212
212
|
Song.new(1, "The Answer Is Still No"),
|
|
213
213
|
Song.new(2, "Invincible")])
|
|
214
214
|
|
|
215
|
-
album.
|
|
215
|
+
_(album.
|
|
216
216
|
extend(representer).
|
|
217
217
|
from_hash("songs" => [{"replace_id"=>2, "id" => 3, "title" => "Soulmate"}]).
|
|
218
|
-
songs.must_equal [
|
|
218
|
+
songs).must_equal [
|
|
219
219
|
Song.new(1, "The Answer Is Still No"),
|
|
220
220
|
Song.new(3, "Soulmate")]
|
|
221
221
|
|
|
222
|
-
songs.object_id.must_equal album.songs.object_id
|
|
223
|
-
songs[0].object_id.must_equal album.songs[0].object_id
|
|
222
|
+
_(songs.object_id).must_equal album.songs.object_id
|
|
223
|
+
_(songs[0].object_id).must_equal album.songs[0].object_id
|
|
224
224
|
}
|
|
225
225
|
end
|
|
226
226
|
|
|
@@ -235,13 +235,13 @@ class InstanceTest < BaseTest
|
|
|
235
235
|
album= Struct.new(:songs).new(songs = [
|
|
236
236
|
Song.new(1, "The Answer Is Still No")])
|
|
237
237
|
|
|
238
|
-
album.
|
|
238
|
+
_(album.
|
|
239
239
|
extend(representer).
|
|
240
240
|
from_hash("songs" => [{"title" => "Invincible"}]).
|
|
241
|
-
songs.must_equal [
|
|
241
|
+
songs).must_equal [
|
|
242
242
|
Song.new(nil, "Invincible")]
|
|
243
243
|
|
|
244
|
-
songs.object_id.wont_equal album.songs.object_id
|
|
244
|
+
_(songs.object_id).wont_equal album.songs.object_id
|
|
245
245
|
}
|
|
246
246
|
end
|
|
247
247
|
end
|