File: hash_test.rb

package info (click to toggle)
ruby-representable 3.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 896 kB
  • sloc: ruby: 6,432; makefile: 3
file content (182 lines) | stat: -rw-r--r-- 5,355 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require 'test_helper'

class HashPublicMethodsTest < Minitest::Spec
  #---
  # from_hash
  class BandRepresenter < Representable::Decorator
    include Representable::Hash
    property :id
    property :name
  end

  let(:data) { {"id"=>1,"name"=>"Rancid"} }

  it { BandRepresenter.new(Band.new).from_hash(data)[:id, :name].must_equal [1, "Rancid"] }
  it { BandRepresenter.new(Band.new).parse(data)[:id, :name].must_equal [1, "Rancid"] }

  #---
  # to_hash
  let(:band) { Band.new(1, "Rancid") }

  it { BandRepresenter.new(band).to_hash.must_equal data }
  it { BandRepresenter.new(band).render.must_equal data }
end

class HashWithScalarPropertyTest < Minitest::Spec
  Album = Struct.new(:title)

  representer! do
    property :title
  end

  let(:album) { Album.new("Liar") }

  describe "#to_hash" do
    it "renders plain property" do
      album.extend(representer).to_hash.must_equal("title" => "Liar")
    end
  end


  describe "#from_hash" do
    it "parses plain property" do
      album.extend(representer).from_hash("title" => "This Song Is Recycled").title.must_equal "This Song Is Recycled"
    end

    # Fixes issue #115
    it "allows nil value in the incoming document and corresponding nil value for the represented" do
      album = Album.new
      album.title.must_be_nil
      album.extend(representer).from_hash("title" => nil)
    end
  end
end


class HashWithTypedPropertyTest < Minitest::Spec
  Album = Struct.new(:best_song)

  representer! do
    property :best_song, :class => Song do
      property :name
    end
  end

  let(:album) { Album.new(Song.new("Liar")) }

  describe "#to_hash" do
    it "renders embedded typed property" do
      album.extend(representer).to_hash.must_equal("best_song" => {"name" => "Liar"})
    end
  end

  describe "#from_hash" do
    it "parses embedded typed property" do
      album.extend(representer).from_hash("best_song" => {"name" => "Go With Me"})
      album.best_song.name.must_equal "Go With Me"
    end

    # nested nil removes nested object.
    it do
      album = Album.new(Song.new("Pre-medicated Murder"))
      album.extend(representer).from_hash("best_song" => nil)
      album.best_song.must_be_nil
    end

    # nested blank hash creates blank object when not populated.
    it do
      album = Album.new#(Song.new("Pre-medicated Murder"))
      album.extend(representer).from_hash("best_song" => {})
      album.best_song.name.must_be_nil
    end

    # Fixes issue #115
    it "allows nil value in the incoming document and corresponding nil value for the represented" do
      album = Album.new
      album.extend(representer).from_hash("best_song" => nil)
      album.best_song.must_be_nil
    end
  end
end

# TODO: move to AsTest.
class HashWithTypedPropertyAndAs < Minitest::Spec
  representer! do
    property :song, :class => Song, :as => :hit do
      property :name
    end
  end

  let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(representer) }

  it { album.to_hash.must_equal("hit" => {"name" => "Liar"}) }
  it { album.from_hash("hit" => {"name" => "Go With Me"}).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
end
  #   # describe "FIXME COMBINE WITH ABOVE with :extend and :as" do
  #   #   hash_song = Module.new do
  #   #     include Representable::XML
  #   #     self.representation_wrap = :song
  #   #     property :name
  #   #   end

  #   #   let(:hash_album) { Module.new do
  #   #     include Representable::XML
  #   #     self.representation_wrap = :album
  #   #     property :song, :extend => hash_song, :class => Song, :as => :hit
  #   #   end }

  #   #   let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(hash_album) }

  #   #   it { album.to_xml.must_equal_xml("<album><hit><name>Liar</name></hit></album>") }
  #   #   #it { album.from_hash("hit" => {"name" => "Go With Me"}).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
  #   # end
  # end



class HashWithTypedCollectionTest < Minitest::Spec
  Album = Struct.new(:songs)

  representer! do
    collection :songs, class: Song do
      property :name
      property :track
    end
  end

  let(:album) { Album.new([Song.new("Liar", 1), Song.new("What I Know", 2)]) }

  describe "#to_hash" do
    it "renders collection of typed property" do
      album.extend(representer).to_hash.must_equal("songs" => [{"name" => "Liar", "track" => 1}, {"name" => "What I Know", "track" => 2}])
    end
  end

  describe "#from_hash" do
    it "parses collection of typed property" do
      album.extend(representer).from_hash("songs" => [{"name" => "One Shot Deal", "track" => 4},
        {"name" => "Three Way Dance", "track" => 5}]).must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
    end
  end
end

class HashWithScalarCollectionTest < Minitest::Spec
  Album = Struct.new(:songs)
  representer! { collection :songs }

  let(:album) { Album.new(["Jackhammer", "Terrible Man"]) }


  describe "#to_hash" do
    it "renders a block style list per default" do
      album.extend(representer).to_hash.must_equal("songs" => ["Jackhammer", "Terrible Man"])
    end
  end


  describe "#from_hash" do
    it "parses a block style list" do
      album.extend(representer).from_hash("songs" => ["Off Key Melody", "Sinking"]).must_equal Album.new(["Off Key Melody", "Sinking"])
    end
  end
end