File: decorator_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 (109 lines) | stat: -rw-r--r-- 3,219 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
require 'test_helper'

class DecoratorTest < Minitest::Spec
  class SongRepresentation < Representable::Decorator
    include Representable::JSON
    property :name
  end

  class AlbumRepresentation < Representable::Decorator
    include Representable::JSON

    collection :songs, :class => Song, :extend => SongRepresentation
  end

  class RatingRepresentation < Representable::Decorator
    include Representable::JSON

    property :system
    property :value
  end

  let(:song) { Song.new("Mama, I'm Coming Home") }
  let(:album) { Album.new([song]) }

  let(:rating) { OpenStruct.new(system: 'MPAA', value: 'R') }

  describe "inheritance" do
    let(:inherited_decorator) do
      Class.new(AlbumRepresentation) do
        property :best_song
      end.new(Album.new([song], "Stand Up"))
    end

    it { inherited_decorator.to_hash.must_equal({"songs"=>[{"name"=>"Mama, I'm Coming Home"}], "best_song"=>"Stand Up"}) }
  end

  let(:decorator) { AlbumRepresentation.new(album) }

  let(:rating_decorator) { RatingRepresentation.new(rating) }

  it "renders" do
    decorator.to_hash.must_equal({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]})
    album.wont_respond_to :to_hash
    song.wont_respond_to :to_hash # DISCUSS: weak test, how to assert blank slate?
    # no @representable_attrs in decorated objects
    song.wont_be(:instance_variable_defined?, :@representable_attrs)

    rating_decorator.to_hash.must_equal({"system" => "MPAA", "value" => "R"})
  end

  describe "#from_hash" do
    it "returns represented" do
      decorator.from_hash({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]}).must_equal album
    end

    it "parses" do
      decorator.from_hash({"songs"=>[{"name"=>"Atomic Garden"}]})
      album.songs.first.must_be_kind_of Song
      album.songs.must_equal [Song.new("Atomic Garden")]
      album.wont_respond_to :to_hash
      song.wont_respond_to :to_hash # DISCUSS: weak test, how to assert blank slate?
    end
  end

  describe "#decorated" do
    it "is aliased to #represented" do
      AlbumRepresentation.prepare(album).decorated.must_equal album
    end
  end


  describe "inline decorators" do
    representer!(decorator: true) do
      collection :songs, :class => Song do
        property :name
      end
    end

    it "does not pollute represented" do
      representer.new(album).from_hash({"songs"=>[{"name"=>"Atomic Garden"}]})

      # no @representable_attrs in decorated objects
      song.wont_be(:instance_variable_defined?, :@representable_attrs)
      album.wont_be(:instance_variable_defined?, :@representable_attrs)
    end
  end
end

require "uber/inheritable_attr"
class InheritanceWithDecoratorTest < Minitest::Spec
  class Twin
    extend Uber::InheritableAttr
    inheritable_attr :representer_class
    self.representer_class = Class.new(Representable::Decorator){ include Representable::Hash }
  end

  class Album < Twin
    representer_class.property :title # Twin.representer_class.clone
  end

  class Song < Twin # Twin.representer_class.clone
  end

  it do
    Twin.representer_class.definitions.size.must_equal 0
    Album.representer_class.definitions.size.must_equal 1
    Song.representer_class.definitions.size.must_equal 0
  end
end