File: for_collection_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 (74 lines) | stat: -rw-r--r-- 2,235 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
require 'test_helper'

class ForCollectionTest < Minitest::Spec
  module SongRepresenter
    include Representable::JSON

    property :name
  end

  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\"}]" }


  # Module.for_collection
  # Decorator.for_collection
  for_formats(
    :hash => [Representable::Hash, out=[{"name" => "Days Go By"}, {"name"=>"Can't Take Them All"}], out],
    :json => [Representable::JSON, out="[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]", out],
    # :xml  => [Representable::XML,  out="<a><song></song><song></song></a>", out]
  ) do |format, mod, output, input|

    describe "Module::for_collection [#{format}]" do
      let(:format) { format }

      let(:representer) {
        Module.new do
          include mod
          property :name#, :as => :title

          collection_representer :class => Song

          # self.representation_wrap = :songs if format == :xml
        end
      }

      it { render(songs.extend(representer.for_collection)).must_equal_document output }
      it { render(representer.for_collection.prepare(songs)).must_equal_document output }
      # parsing needs the class set, at least
      it { parse([].extend(representer.for_collection), input).must_equal songs }
    end

    describe "Module::for_collection without configuration [#{format}]" do
      let(:format) { format }

      let(:representer) {
        Module.new do
          include mod
          property :name
        end
      }

      # rendering works out of the box, no config necessary
      it { render(songs.extend(representer.for_collection)).must_equal_document output }
    end


    describe "Decorator::for_collection [#{format}]" do
      let(:format) { format }
      let(:representer) {
        Class.new(Representable::Decorator) do
          include mod
          property :name

          collection_representer :class => Song
        end
      }

      it { render(representer.for_collection.new(songs)).must_equal_document output }
      it { parse(representer.for_collection.new([]), input).must_equal songs }
    end
  end

  # with module including module
end