File: lonely_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 (239 lines) | stat: -rw-r--r-- 7,725 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
require 'test_helper'

require 'representable/json/hash'

class LonelyRepresenterTest < Minitest::Spec

  # test ::items without arguments, render-only.
  for_formats(
    :hash => [Representable::Hash::Collection, [{"name"=>"Resist Stance"}, {"name"=>"Suffer"}]],
    :json => [Representable::JSON::Collection, "[{\"name\":\"Resist Stance\"},{\"name\":\"Suffer\"}]"],
    :xml  => [Representable::XML::Collection, "<array><song><name>Resist Stance</name></song><song><name>Suffer</name></song></array>"],
  ) do |format, mod, output, input|

    describe "[#{format}] lonely collection, render-only" do # TODO: introduce :representable option?
      let(:format) { format }

      representer!(module: mod) do
        items do
          property :name
        end
      end

      let(:album) { [Song.new("Resist Stance"), Song.new("Suffer")].extend(representer) }

      it "calls #to_hash on song instances, nothing else" do
        render(album).must_equal_document(output)
      end
    end
  end



  module SongRepresenter
    include Representable::JSON

    property :name
  end

  let(:decorator) { rpr = representer; Class.new(Representable::Decorator) { include Representable::Hash; include rpr } }

  describe "JSON::Collection" do
    let(:songs) { [Song.new("Days Go By"), Song.new("Can't Take Them All")] }
    let(:json)  { "[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]" }

    describe "with contained objects" do
      let(:representer) {
        Module.new do
          include Representable::JSON::Collection
          items :class => Song, :extend => SongRepresenter
        end
      }


      it "renders array" do
        assert_json json, songs.extend(representer).to_json
      end

      it "renders array with decorator" do
        assert_json json, decorator.new(songs).to_json
      end

      it "parses array" do
        [].extend(representer).from_json(json).must_equal songs
      end

      it "parses array with decorator" do
        decorator.new([]).from_json(json).must_equal songs
      end
    end

    describe "with inline representer" do
      representer!(:module => Representable::JSON::Collection) do
        items :class => Song do
          property :name
        end
      end

      it { songs.extend(representer).to_json.must_equal json }
      it { [].extend(representer).from_json(json).must_equal songs }
    end

    describe "with contained text" do
      let(:representer) {
        Module.new do
          include Representable::JSON::Collection
        end
      }
      let(:songs) { ["Days Go By", "Can't Take Them All"] }
      let(:json)  { "[\"Days Go By\",\"Can't Take Them All\"]" }

      it "renders contained items #to_json" do
        assert_json json, songs.extend(representer).to_json
      end

      it "returns objects array from #from_json" do
        [].extend(representer).from_json(json).must_equal songs
      end
    end
  end

  describe "Hash::Collection with dynamic options" do
    class One < Representable::Decorator
      def to_hash(*); "One: #{represented}"; end
    end

    class Two < Representable::Decorator
      def to_hash(*); "Two: #{represented}"; end
    end

    representer!(module: Representable::Hash::Collection) do
      items extend: ->(options) { options[:input] == 1 ? options[:user_options][:one] : options[:user_options][:two] }
    end


    it { [1,2].extend(representer).to_hash(user_options: {one: One, two: Two}).must_equal(["One: 1", "Two: 2"]) }
  end


  describe "JSON::Hash" do  # TODO: move to HashTest.
    describe "with contained objects" do
      let(:representer) {
        Module.new do
          include Representable::JSON::Hash
          values :class => Song, :extend => SongRepresenter
        end
      }
      let(:json)  { "{\"one\":{\"name\":\"Days Go By\"},\"two\":{\"name\":\"Can't Take Them All\"}}" }
      let(:songs) { {"one" => Song.new("Days Go By"), "two" => Song.new("Can't Take Them All")} }

      describe "#to_json" do
        it "renders hash" do
          songs.extend(representer).to_json.must_equal json
        end

        it "renders hash with decorator" do
          decorator.new(songs).to_json.must_equal json
        end

        it "respects :exclude" do
          assert_json "{\"two\":{\"name\":\"Can't Take Them All\"}}", {:one => Song.new("Days Go By"), :two => Song.new("Can't Take Them All")}.extend(representer).to_json(:exclude => [:one])
        end

        it "respects :include" do
          assert_json "{\"two\":{\"name\":\"Can't Take Them All\"}}", {:one => Song.new("Days Go By"), :two => Song.new("Can't Take Them All")}.extend(representer).to_json(:include => [:two])
        end
      end

      describe "#from_json" do
        it "returns objects array" do
          {}.extend(representer).from_json(json).must_equal songs
        end

        it "parses hash with decorator" do
          decorator.new({}).from_json(json).must_equal songs
        end

        it "respects :exclude" do
          assert_equal({"two" => Song.new("Can't Take Them All")}, {}.extend(representer).from_json(json, :exclude => [:one]))
        end

        it "respects :include" do
          assert_equal({"one" => Song.new("Days Go By")}, {}.extend(representer).from_json(json, :include => [:one]))
        end
      end


      describe "with inline representer" do
        representer!(:module => Representable::JSON::Hash) do
          values :class => Song do
            property :name
          end
        end

        it { songs.extend(representer).to_json.must_equal json }
        it { {}.extend(representer).from_json(json).must_equal songs }
      end
    end


    describe "with scalar" do
      let(:representer) {
        Module.new do
          include Representable::JSON::Hash
        end
      }
      let(:json)  { %{{"one":1,"two":2}} }
      let(:data) { {one: 2, two: 3} }

      describe "#to_json" do
        it { data.extend(representer).to_json.must_equal %{{"one":2,"two":3}} }

        # it "respects :exclude" do
        #   assert_json "{\"two\":{\"name\":\"Can't Take Them All\"}}", {:one => Song.new("Days Go By"), :two => Song.new("Can't Take Them All")}.extend(representer).to_json(:exclude => [:one])
        # end

        # it "respects :include" do
        #   assert_json "{\"two\":{\"name\":\"Can't Take Them All\"}}", {:one => Song.new("Days Go By"), :two => Song.new("Can't Take Them All")}.extend(representer).to_json(:include => [:two])
        # end
      end

      describe "#from_json" do # FIXME: what's the point of this?
        it { data.extend(representer).from_hash(data).must_equal data }
      end
    end


    describe "with contained text" do
      before do
        @songs_representer = Module.new do
          include Representable::JSON::Collection
        end
      end

      it "renders contained items #to_json" do
        assert_json "[\"Days Go By\",\"Can't Take Them All\"]", ["Days Go By", "Can't Take Them All"].extend(@songs_representer).to_json
      end

      it "returns objects array from #from_json" do
        assert_equal ["Days Go By", "Can't Take Them All"], [].extend(@songs_representer).from_json("[\"Days Go By\",\"Can't Take Them All\"]")
      end
    end
  end
end


# describe "Hash::Collection with :include" do
class CollectionWithIncludeTest < Minitest::Spec
  Song = Struct.new(:id, :title)

  representer!(decorator: true, module: Representable::Hash::Collection) do
    items do
      property :id
      property :title
    end
  end

  it { representer.new([Song.new(1, "ACAB")]).to_hash.must_equal([{"id"=>1, "title"=>"ACAB"}]) }
  it { representer.new([Song.new(1, "ACAB")]).to_hash(include: [:title]).must_equal([{"title"=>"ACAB"}]) }
end