representable 3.3.0 → 3.4.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.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +10 -0
  3. data/Gemfile +0 -1
  4. data/README.md +1 -7
  5. data/lib/representable/json.rb +11 -14
  6. data/lib/representable/version.rb +1 -1
  7. data/representable.gemspec +1 -2
  8. metadata +3 -65
  9. data/test/as_test.rb +0 -60
  10. data/test/benchmarking.rb +0 -78
  11. data/test/binding_test.rb +0 -54
  12. data/test/cached_test.rb +0 -177
  13. data/test/class_test.rb +0 -132
  14. data/test/coercion_test.rb +0 -73
  15. data/test/config/inherit_test.rb +0 -130
  16. data/test/config_test.rb +0 -120
  17. data/test/decorator_scope_test.rb +0 -30
  18. data/test/decorator_test.rb +0 -109
  19. data/test/default_test.rb +0 -34
  20. data/test/defaults_options_test.rb +0 -121
  21. data/test/definition_test.rb +0 -253
  22. data/test/examples/example.rb +0 -314
  23. data/test/examples/object.rb +0 -28
  24. data/test/exec_context_test.rb +0 -88
  25. data/test/features_test.rb +0 -89
  26. data/test/filter_test.rb +0 -66
  27. data/test/for_collection_test.rb +0 -71
  28. data/test/generic_test.rb +0 -109
  29. data/test/getter_setter_test.rb +0 -26
  30. data/test/hash_bindings_test.rb +0 -96
  31. data/test/hash_test.rb +0 -197
  32. data/test/heritage_test.rb +0 -64
  33. data/test/if_test.rb +0 -83
  34. data/test/include_exclude_test.rb +0 -96
  35. data/test/inherit_test.rb +0 -246
  36. data/test/inline_test.rb +0 -281
  37. data/test/instance_test.rb +0 -289
  38. data/test/is_representable_test.rb +0 -79
  39. data/test/json_test.rb +0 -379
  40. data/test/lonely_test.rb +0 -241
  41. data/test/nested_test.rb +0 -112
  42. data/test/object_test.rb +0 -60
  43. data/test/option_test.rb +0 -38
  44. data/test/parse_pipeline_test.rb +0 -68
  45. data/test/pipeline_test.rb +0 -309
  46. data/test/populator_test.rb +0 -108
  47. data/test/prepare_test.rb +0 -64
  48. data/test/private_options_test.rb +0 -19
  49. data/test/reader_writer_test.rb +0 -19
  50. data/test/realistic_benchmark.rb +0 -96
  51. data/test/render_nil_test.rb +0 -21
  52. data/test/represent_test.rb +0 -84
  53. data/test/representable_test.rb +0 -526
  54. data/test/schema_test.rb +0 -175
  55. data/test/serialize_deserialize_test.rb +0 -33
  56. data/test/skip_test.rb +0 -92
  57. data/test/stringify_hash_test.rb +0 -39
  58. data/test/test_helper.rb +0 -153
  59. data/test/test_helper_test.rb +0 -25
  60. data/test/uncategorized_test.rb +0 -72
  61. data/test/user_options_test.rb +0 -24
  62. data/test/wrap_test.rb +0 -162
  63. data/test/xml_assertions.rb +0 -29
  64. data/test/xml_bindings_test.rb +0 -55
  65. data/test/xml_namespace_test.rb +0 -193
  66. data/test/xml_test.rb +0 -584
  67. data/test/yaml_test.rb +0 -209
@@ -1,33 +0,0 @@
1
- require "test_helper"
2
-
3
- class SerializeDeserializeTest < BaseTest
4
- subject { Struct.new(:song).new.extend(representer) }
5
-
6
- describe "deserialize" do
7
- representer! do
8
- property :song,
9
- :instance => ->(options) { options[:input].to_s.upcase },
10
- :prepare => ->(options) { options[:input] },
11
- :deserialize => ->(options) {
12
- "#{options[:input]} #{options[:fragment]} #{options[:user_options].inspect}"
13
- }
14
- end
15
-
16
- it { _(subject.from_hash({"song" => Object}, user_options: {volume: 9}).song).must_equal "OBJECT Object #{{volume: 9}.inspect}" }
17
- end
18
-
19
- describe "serialize" do
20
- representer! do
21
- property :song,
22
- :representable => true,
23
- :prepare => ->(options) { options[:fragment] },
24
- :serialize => ->(options) {
25
- "#{options[:input]} #{options[:user_options].inspect}"
26
- }
27
- end
28
-
29
- before { subject.song = "Arrested In Shanghai" }
30
-
31
- it { _(subject.to_hash(user_options: {volume: 9})).must_equal({"song" => "Arrested In Shanghai #{{volume: 9}.inspect}"}) }
32
- end
33
- end
data/test/skip_test.rb DELETED
@@ -1,92 +0,0 @@
1
- require "test_helper"
2
-
3
- class SkipParseTest < Minitest::Spec
4
- representer! do
5
- property :title, skip_parse: ->(options) { options[:user_options][:skip?] and options[:fragment] == "skip me" }
6
- property :band,
7
- skip_parse: ->(options) { options[:user_options][:skip?] and options[:fragment]["name"].nil? }, class: OpenStruct do
8
- property :name
9
- end
10
-
11
- collection :airplays,
12
- skip_parse: ->(options) {
13
- options[:user_options][:skip?] and options[:fragment]["station"].nil?
14
- }, class: OpenStruct do
15
- property :station
16
- end
17
- end
18
-
19
- let(:song) { OpenStruct.new.extend(representer) }
20
-
21
- # do parse.
22
- it do
23
- song.from_hash(
24
- {
25
- "title" => "Victim Of Fate",
26
- "band" => {"name" => "Mute 98"},
27
- "airplays" => [{"station" => "JJJ"}]
28
- }, user_options: {skip?: true}
29
- )
30
-
31
- _(song.title).must_equal "Victim Of Fate"
32
- _(song.band.name).must_equal "Mute 98"
33
- _(song.airplays[0].station).must_equal "JJJ"
34
- end
35
-
36
- # skip parsing.
37
- let(:airplay) { OpenStruct.new(station: "JJJ") }
38
-
39
- it do
40
- song.from_hash(
41
- {
42
- "title" => "skip me",
43
- "band" => {},
44
- "airplays" => [{}, {"station" => "JJJ"}, {}]
45
- }, user_options: {skip?: true}
46
- )
47
-
48
- _(song.title).must_be_nil
49
- _(song.band).must_be_nil
50
- _(song.airplays).must_equal [airplay]
51
- end
52
- end
53
-
54
- class SkipRenderTest < Minitest::Spec
55
- representer! do
56
- property :title
57
- property :band,
58
- skip_render: ->(options) { options[:user_options][:skip?] and options[:input].name == "Rancid" } do
59
- property :name
60
- end
61
-
62
- collection :airplays,
63
- skip_render: ->(options) { options[:user_options][:skip?] and options[:input].station == "Radio Dreyeckland" } do
64
- property :station
65
- end
66
- end
67
-
68
- let(:song) { OpenStruct.new(title: "Black Night", band: OpenStruct.new(name: "Time Again")).extend(representer) }
69
- let(:skip_song) { OpenStruct.new(title: "Time Bomb", band: OpenStruct.new(name: "Rancid")).extend(representer) }
70
-
71
- # do render.
72
- it { _(song.to_hash(user_options: {skip?: true})).must_equal({"title" => "Black Night", "band" => {"name"=>"Time Again"}}) }
73
- # skip.
74
- it { _(skip_song.to_hash(user_options: {skip?: true})).must_equal({"title"=>"Time Bomb"}) }
75
-
76
- # do render all collection items.
77
- it do
78
- song = OpenStruct.new(airplays: [OpenStruct.new(station: "JJJ"), OpenStruct.new(station: "ABC")]).extend(representer)
79
- _(song.to_hash(user_options: {skip?: true})).must_equal({"airplays"=>[{"station"=>"JJJ"}, {"station"=>"ABC"}]})
80
- end
81
-
82
- # skip middle item.
83
- it do
84
- song = OpenStruct.new(
85
- airplays: [
86
- OpenStruct.new(station: "JJJ"), OpenStruct.new(station: "Radio Dreyeckland"),
87
- OpenStruct.new(station: "ABC")
88
- ]
89
- ).extend(representer)
90
- _(song.to_hash(user_options: {skip?: true})).must_equal({"airplays"=>[{"station"=>"JJJ"}, {"station"=>"ABC"}]})
91
- end
92
- end
@@ -1,39 +0,0 @@
1
- require "test_helper"
2
-
3
- class StringifyHashTest < Minitest::Spec
4
- describe "#from_hash" do
5
- representer!(:name => :song_representer) do
6
- include Representable::Hash
7
- include Representable::Hash::AllowSymbols
8
-
9
- property :title
10
- end
11
-
12
- representer!(:inject => :song_representer) do
13
- include Representable::Hash::AllowSymbols
14
-
15
- property :song, :extend => song_representer, :class => OpenStruct
16
- end
17
-
18
- it "parses symbols, too" do
19
- _(OpenStruct.new.extend(representer).from_hash({:song => {:title => "Der Optimist"}}).song.title).must_equal "Der Optimist"
20
- end
21
-
22
- it "still parses strings" do
23
- _(OpenStruct.new.extend(representer).from_hash({"song" => {"title" => "Der Optimist"}}).song.title).must_equal "Der Optimist"
24
- end
25
-
26
- describe "with :wrap" do
27
- representer!(:inject => :song_representer) do
28
- include Representable::Hash::AllowSymbols
29
-
30
- self.representation_wrap = :album
31
- property :song, :extend => song_representer, :class => OpenStruct
32
- end
33
-
34
- it "parses symbols, too" do
35
- _(OpenStruct.new.extend(representer).from_hash({:album => {:song => {:title => "Der Optimist"}}}).song.title).must_equal "Der Optimist"
36
- end
37
- end
38
- end
39
- end
data/test/test_helper.rb DELETED
@@ -1,153 +0,0 @@
1
- require "ostruct"
2
- require "representable"
3
-
4
- require "minitest/autorun"
5
-
6
- require "representable/debug"
7
- require "minitest/assertions"
8
- require "xml_assertions"
9
-
10
- module Minitest::Assertions
11
- include XmlAssertions
12
-
13
- # Arguments read (asserted, expected), the assertion order used throughout this suite.
14
- def assert_equal_xml(subject, text)
15
- assert_equal text.gsub("\n", "").gsub(/(\s\s+)/, ""), subject.gsub("\n", "").gsub(/(\s\s+)/, "")
16
- end
17
- end
18
-
19
- # TODO: delete all that in 4.0:
20
- class Album
21
- attr_accessor :songs, :best_song
22
-
23
- def initialize(songs = nil, best_song = nil)
24
- @songs = songs
25
- @best_song = best_song
26
- end
27
-
28
- def ==(other)
29
- songs == other.songs and best_song == other.best_song
30
- end
31
- end
32
-
33
- class Song
34
- attr_accessor :name, :track # never change this, track rendered with Rails#to_json.
35
-
36
- def initialize(name = nil, track = nil)
37
- @name = name
38
- @track = track
39
- end
40
-
41
- def ==(other)
42
- name == other.name and track == other.track
43
- end
44
- end
45
-
46
- module XmlHelper
47
- def xml(document)
48
- Nokogiri::XML(document).root
49
- end
50
- end
51
-
52
- module AssertJson
53
- module Assertions
54
- def assert_json(expected, actual, msg = nil)
55
- msg = message(msg, "") { diff expected, actual }
56
- assert_equal(expected.split("").sort, actual.split("").sort, msg)
57
- end
58
- end
59
- end
60
-
61
- Minitest::Spec.class_eval do
62
- include AssertJson::Assertions
63
- include XmlHelper
64
-
65
- def self.for_formats(formats)
66
- formats.each do |format, cfg|
67
- mod, output, input = cfg
68
- yield format, mod, output, input
69
- end
70
- end
71
-
72
- def render(object, *args)
73
- AssertableDocument.new(object.send("to_#{format}", *args), format, self)
74
- end
75
-
76
- def parse(object, input, *args)
77
- object.send("from_#{format}", input, *args)
78
- end
79
-
80
- class AssertableDocument
81
- attr_reader :document
82
-
83
- def initialize(document, format, test)
84
- @document = document
85
- @format = format
86
- @test = test
87
- end
88
-
89
- def must_equal_document(expected, *rest)
90
- return @test.assert_xml_equal(document, expected, *rest) if @format == :xml
91
-
92
- @test.assert_equal(expected, document, *rest)
93
- end
94
- end
95
-
96
- def self.representer!(options = {}, &block)
97
- fmt = options # we need that so the 2nd call to ::let(within a ::describe) remembers the right format.
98
-
99
- name = options[:name] || :representer
100
- format = options[:module] || Representable::Hash
101
-
102
- let(name) do
103
- mod = options[:decorator] ? Class.new(Representable::Decorator) : Module.new
104
-
105
- inject_representer(mod, fmt)
106
-
107
- mod.module_eval do
108
- include format
109
- instance_exec(&block)
110
- end
111
-
112
- mod
113
- end
114
-
115
- undef :inject_representer if method_defined? :inject_representer
116
-
117
- def inject_representer(mod, options)
118
- return unless options[:inject]
119
-
120
- injected_name = options[:inject]
121
- injected = send(injected_name) # song_representer
122
- mod.singleton_class.instance_eval do
123
- define_method(injected_name) { injected }
124
- end
125
- end
126
- end
127
-
128
- module TestMethods
129
- def representer_for(modules = [Representable::Hash], &block)
130
- Module.new do
131
- extend TestMethods
132
- include(*modules)
133
- module_exec(&block)
134
- end
135
- end
136
-
137
- alias representer! representer_for
138
- end
139
- include TestMethods
140
- end
141
-
142
- class BaseTest < Minitest::Spec
143
- let(:new_album) { OpenStruct.new.extend(representer) }
144
- let(:album) { OpenStruct.new(:songs => ["Fuck Armageddon"]).extend(representer) }
145
- let(:song) { OpenStruct.new(:title => "Resist Stance") }
146
- let(:song_representer) { Module.new { include Representable::Hash; property :title } }
147
- end
148
-
149
- Band = Struct.new(:id, :name) do
150
- def [](*attrs)
151
- attrs.collect { |attr| send(attr) }
152
- end
153
- end
@@ -1,25 +0,0 @@
1
- require "test_helper"
2
-
3
- class TestHelperTest < Minitest::Spec
4
- describe "#assert_json" do
5
- it "tests for equality" do
6
- assert_json "{\"songs\":{\"one\":\"65\",\"two\":\"Emo Boy\"}}", "{\"songs\":{\"one\":\"65\",\"two\":\"Emo Boy\"}}"
7
- end
8
-
9
- it "allows different key orders" do
10
- assert_json "{\"songs\":{\"one\":\"65\",\"two\":\"Emo Boy\"}}", "{\"songs\":{\"two\":\"Emo Boy\",\"one\":\"65\"}}"
11
- end
12
-
13
- it "complains when expected hash is subset" do
14
- assert_raises Minitest::Assertion do
15
- assert_json "{\"songs\":{\"one\":\"65\"}}", "{\"songs\":{\"two\":\"Emo Boy\",\"one\":\"65\"}}"
16
- end
17
- end
18
-
19
- it "complains when source hash is subset" do
20
- assert_raises Minitest::Assertion do
21
- assert_json "{\"songs\":{\"two\":\"Emo Boy\",\"one\":\"65\"}}", "{\"songs\":{\"one\":\"65\"}}"
22
- end
23
- end
24
- end
25
- end
@@ -1,72 +0,0 @@
1
- require "test_helper"
2
-
3
- class StopWhenIncomingObjectFragmentIsNilTest < Minitest::Spec
4
- Album = Struct.new(:id, :songs)
5
- Song = Struct.new(:title)
6
-
7
- representer!(decorator: true) do
8
- property :id
9
- collection :songs, class: Song, parse_pipeline: ->(_input, _options) { # TODO: test if :doc is set for parsing. test if options are ok and contain :user_options!
10
- Representable::Pipeline[*parse_functions.insert(3, Representable::StopOnNil)]
11
- } do
12
- property :title
13
- end
14
- end
15
-
16
- it do
17
- album = Album.new
18
- _(
19
- representer.new(album).from_hash(
20
- {
21
- "id" => 1,
22
- "songs" => [{"title"=>"Walkie Talkie"}]
23
- }
24
- ).songs
25
- ).must_equal [Song.new("Walkie Talkie")]
26
- end
27
-
28
- it do
29
- album = Album.new(2, ["original"])
30
- _(representer.new(album).from_hash({"id" => 1, "songs" => nil}).songs).must_equal ["original"]
31
- end
32
- end
33
-
34
- class RenderPipelineOptionTest < Minitest::Spec
35
- Album = Struct.new(:id, :songs)
36
- NilToNA = ->(input, _options) { input.nil? ? "n/a" : input }
37
-
38
- representer!(decorator: true) do
39
- property :id, render_pipeline: ->(_input, options) do
40
- Representable::Pipeline[*render_functions.insert(2, options[:options][:user_options][:function])]
41
- end
42
- end
43
-
44
- it { _(representer.new(Album.new).to_hash(user_options: {function: NilToNA})).must_equal({"id"=>"n/a"}) }
45
- it { _(representer.new(Album.new(1)).to_hash(user_options: {function: NilToNA})).must_equal({"id"=>1}) }
46
-
47
- it "is cached" do
48
- decorator = representer.new(Album.new)
49
- _(decorator.to_hash(user_options: {function: NilToNA})).must_equal({"id"=>"n/a"})
50
- _(decorator.to_hash(user_options: {function: nil})).must_equal({"id"=>"n/a"}) # non-sense function is not applied.
51
- end
52
- end
53
-
54
- class ParsePipelineOptionTest < Minitest::Spec
55
- Album = Struct.new(:id, :songs)
56
- NilToNA = ->(input, _options) { input.nil? ? "n/a" : input }
57
-
58
- representer!(decorator: true) do
59
- property :id, parse_pipeline: ->(_input, options) do
60
- Representable::Pipeline[*parse_functions.insert(3, options[:options][:user_options][:function])].extend(Representable::Pipeline::Debug)
61
- end
62
- end
63
-
64
- it { _(representer.new(Album.new).from_hash({"id"=>nil}, user_options: {function: NilToNA}).id).must_equal "n/a" }
65
- it { _(representer.new(Album.new(1)).to_hash(user_options: {function: NilToNA})).must_equal({"id"=>1}) }
66
-
67
- it "is cached" do
68
- decorator = representer.new(Album.new)
69
- _(decorator.from_hash({"id"=>nil}, user_options: {function: NilToNA}).id).must_equal "n/a"
70
- _(decorator.from_hash({"id"=>nil}, user_options: {function: "nonsense"}).id).must_equal "n/a" # non-sense function is not applied.
71
- end
72
- end
@@ -1,24 +0,0 @@
1
- require "test_helper"
2
-
3
- class UserOptionsTest < Minitest::Spec
4
- Song = Struct.new(:title)
5
-
6
- representer! do
7
- property :title, if: ->(options) { options[:user_options][:visible] }
8
- end
9
-
10
- it { _(Song.new("Run With It").extend(representer).to_hash).must_equal({}) }
11
- it {
12
- _(Song.new("Run With It").extend(representer).to_hash(user_options: {visible: true})).must_equal({"title"=>"Run With It"})
13
- }
14
- it { _(Song.new("Run With It").extend(representer).from_hash("title"=>"Business Conduct").title).must_equal "Run With It" }
15
- it {
16
- _(
17
- Song.new("Run With It").extend(representer).from_hash(
18
- {"title"=>"Business Conduct"},
19
- user_options: {visible: true}
20
- ).title
21
- ).must_equal "Business Conduct"
22
- }
23
- end
24
- # Representable.deprecations=false
data/test/wrap_test.rb DELETED
@@ -1,162 +0,0 @@
1
- require "test_helper"
2
-
3
- class WrapTest < Minitest::Spec
4
- class HardcoreBand
5
- include Representable::Hash
6
- end
7
-
8
- class SoftcoreBand < HardcoreBand
9
- end
10
-
11
- let(:band) { HardcoreBand.new }
12
-
13
- it "returns false per default" do
14
- assert_nil SoftcoreBand.new.send(:representation_wrap)
15
- end
16
-
17
- it "infers a printable class name if set to true" do
18
- HardcoreBand.representation_wrap = true
19
- assert_equal "hardcore_band", band.send(:representation_wrap)
20
- end
21
-
22
- it "can be set explicitely" do
23
- HardcoreBand.representation_wrap = "breach"
24
- assert_equal "breach", band.send(:representation_wrap)
25
- end
26
-
27
- for_formats(
28
- :hash => [Representable::Hash, {"Blink182"=>{"genre"=>"Pop"}}, {"Blink182"=>{"genre"=>"Poppunk"}}],
29
- :json => [Representable::JSON, "{\"Blink182\":{\"genre\":\"Pop\"}}", "{\"Blink182\":{\"genre\":\"Poppunk\"}}"],
30
- :xml => [Representable::XML, "<Blink182><genre>Pop</genre></Blink182>", "<Blink182><genre>Poppunk</genre></Blink182>"]
31
- # :yaml => [Representable::YAML, "---\nBlink182:\n"], # TODO: fix YAML.
32
- ) do |format, mod, output, input|
33
- describe "[#{format}] dynamic wrap" do
34
- let(:band) { representer.prepare(Struct.new(:name, :genre).new("Blink", "Pop")) }
35
- let(:format) { format }
36
-
37
- representer!(:module => mod) do
38
- self.representation_wrap = ->(number:, **) { "#{name}#{number}" }
39
- property :genre
40
- end
41
-
42
- it { render(band, {:number => 182}).must_equal_document(output) }
43
-
44
- it { _(parse(band, input, {:number => 182}).genre).must_equal "Poppunk" } # TODO: better test. also, xml parses _any_ wrap.
45
- end
46
- end
47
- end
48
-
49
- class HashDisableWrapTest < Minitest::Spec
50
- Band = Struct.new(:name, :label)
51
- Album = Struct.new(:band)
52
- Label = Struct.new(:name)
53
-
54
- class BandDecorator < Representable::Decorator
55
- include Representable::Hash
56
-
57
- self.representation_wrap = :bands
58
- property :name
59
-
60
- property :label do # this should have a wrap!
61
- self.representation_wrap = :important
62
- property :name
63
- end
64
- end
65
-
66
- let(:band) { BandDecorator.prepare(Band.new("Social Distortion")) }
67
-
68
- # direct, local api.
69
- it do
70
- _(band.to_hash).must_equal({"bands" => {"name"=>"Social Distortion"}})
71
- _(band.to_hash(wrap: false)).must_equal({"name"=>"Social Distortion"})
72
- _(band.to_hash(wrap: :band)).must_equal(:band=>{"name"=>"Social Distortion"})
73
- end
74
-
75
- it do
76
- _(band.from_hash({"bands" => {"name"=>"Social Distortion"}}).name).must_equal "Social Distortion"
77
- _(band.from_hash({"name"=>"Social Distortion"}, wrap: false).name).must_equal "Social Distortion"
78
- _(band.from_hash({band: {"name"=>"Social Distortion"}}, wrap: :band).name).must_equal "Social Distortion"
79
- end
80
-
81
- class AlbumDecorator < Representable::Decorator
82
- include Representable::Hash
83
-
84
- self.representation_wrap = :albums
85
-
86
- property :band, decorator: BandDecorator, wrap: false, class: Band
87
- end
88
-
89
- let(:album) { AlbumDecorator.prepare(Album.new(Band.new("Social Distortion", Label.new("Epitaph")))) }
90
-
91
- # band has wrap turned off per property definition, however, label still has wrap.
92
- it "renders" do
93
- _(album.to_hash).must_equal(
94
- {
95
- "albums" => {
96
- "band" => {
97
- "name" => "Social Distortion",
98
- "label" => {"important"=>{"name"=>"Epitaph"}}
99
- }
100
- }
101
- }
102
- )
103
- end
104
-
105
- it "parses" do
106
- _(album.from_hash({"albums" => {"band" => {"name"=>"Rvivr"}}}).band.name).must_equal "Rvivr"
107
- end
108
- end
109
-
110
- class XMLDisableWrapTest < Minitest::Spec
111
- Band = Struct.new(:name, :label)
112
- Album = Struct.new(:band)
113
- Label = Struct.new(:name)
114
-
115
- class BandDecorator < Representable::Decorator
116
- include Representable::XML
117
-
118
- self.representation_wrap = :bands # when nested, it uses this.
119
- property :name
120
-
121
- # property :label do # this should have a wrap!
122
- # self.representation_wrap = :important
123
- # property :name
124
- # end
125
- end
126
-
127
- let(:band) { BandDecorator.prepare(Band.new("Social Distortion")) }
128
-
129
- it do
130
- assert_xml_equal(band.to_xml, "<bands><name>Social Distortion</name></bands>")
131
- assert_xml_equal(band.to_xml(wrap: "combo"), "<combo><name>Social Distortion</name></combo>")
132
- end
133
-
134
- class AlbumDecorator < Representable::Decorator
135
- include Representable::XML
136
-
137
- self.representation_wrap = :albums
138
-
139
- property :band, decorator: BandDecorator, wrap: "po", class: Band
140
- end
141
-
142
- let(:album) { AlbumDecorator.prepare(Album.new(Band.new("Social Distortion", Label.new("Epitaph")))) }
143
-
144
- # band has wrap turned of per property definition, however, label still has wrap.
145
- it "rendersxx" do
146
- skip
147
- _(album.to_xml).must_equal(
148
- {
149
- "albums" => {
150
- "band" => {
151
- "name" => "Social Distortion",
152
- "label" => {"important"=>{"name"=>"Epitaph"}}
153
- }
154
- }
155
- }
156
- )
157
- end
158
-
159
- # it "parses" do
160
- # album.from_hash({"albums" => {"band" => {"name"=>"Rvivr"}}}).band.name.must_equal "Rvivr"
161
- # end
162
- end
@@ -1,29 +0,0 @@
1
- require "nokogiri"
2
-
3
- # Order-insensitive XML comparison: attributes are sorted by name and sibling
4
- # elements by their own canonical form, then both documents are compared as
5
- # strings.
6
- module XmlAssertions
7
- def self.canonicalize(xml, indent = 0)
8
- node = xml.is_a?(String) ? Nokogiri::XML.parse(xml).root : xml
9
- return "(unparsable xml)\n#{xml}" if node.nil?
10
-
11
- pad = " " * indent
12
- attrs = node.attribute_nodes.map { |a| " #{a.name}=#{a.value.inspect}" }.sort.join
13
- children = node.elements
14
-
15
- if children.empty?
16
- "#{pad}<#{node.name}#{attrs}>#{node.text.strip}</#{node.name}>\n"
17
- else
18
- body = children.map { |child| canonicalize(child, indent + 1) }.sort.join
19
- "#{pad}<#{node.name}#{attrs}>\n#{body}#{pad}</#{node.name}>\n"
20
- end
21
- end
22
-
23
- # Arguments read (asserted, expected), the assertion order used throughout this suite.
24
- def assert_xml_equal(actual, expected, msg = nil)
25
- assert_equal XmlAssertions.canonicalize(expected),
26
- XmlAssertions.canonicalize(actual),
27
- msg
28
- end
29
- end
@@ -1,55 +0,0 @@
1
- require "test_helper"
2
- require "representable/xml/hash"
3
-
4
- class XMLBindingTest < Minitest::Spec
5
- module SongRepresenter
6
- include Representable::XML
7
- property :name
8
- self.representation_wrap = :song
9
- end
10
-
11
- class SongWithRepresenter < ::Song
12
- include Representable
13
- include SongRepresenter
14
- self.representation_wrap = :song
15
- end
16
-
17
- before do
18
- @doc = Nokogiri::XML::Document.new
19
- @song = SongWithRepresenter.new("Thinning the Herd")
20
- end
21
-
22
- describe "AttributeBinding" do
23
- describe "with plain text items" do
24
- before do
25
- @property = Representable::XML::Binding::Attribute.new(Representable::Definition.new(:name, :attribute => true))
26
- end
27
-
28
- it "extracts with #read" do
29
- assert_equal "The Gargoyle", @property.read(Nokogiri::XML("<song name=\"The Gargoyle\" />").root, "name")
30
- end
31
-
32
- it "inserts with #write" do
33
- parent = Nokogiri::XML::Node.new("song", @doc)
34
- @property.write(parent, "The Gargoyle", "name")
35
- assert_xml_equal(parent.to_s, "<song name=\"The Gargoyle\" />")
36
- end
37
- end
38
- end
39
-
40
- describe "ContentBinding" do
41
- before do
42
- @property = Representable::XML::Binding::Content.new(Representable::Definition.new(:name, :content => true))
43
- end
44
-
45
- it "extracts with #read" do
46
- assert_equal "The Gargoyle", @property.read(Nokogiri::XML("<song>The Gargoyle</song>").root, "song")
47
- end
48
-
49
- it "inserts with #write" do
50
- parent = Nokogiri::XML::Node.new("song", @doc)
51
- @property.write(parent, "The Gargoyle", "song")
52
- assert_xml_equal(parent.to_s, "<song>The Gargoyle</song>")
53
- end
54
- end
55
- end