File: inherit_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 (135 lines) | stat: -rw-r--r-- 3,618 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
require 'test_helper'

# tests defining representers in modules, decorators and classes and the inheritance when combined.

class ConfigInheritTest < Minitest::Spec
  def assert_cloned(child, parent, property)
    child_def  = child.representable_attrs.get(property)
    parent_def = parent.representable_attrs.get(property)

    child_def.merge!(:alias => property)

    child_def[:alias].wont_equal parent_def[:alias]
    child_def.object_id.wont_equal parent_def.object_id
  end
  # class Object

  # end
  module GenreModule
    include Representable::Hash
    property :genre
  end


  # in Decorator ------------------------------------------------
  class Decorator < Representable::Decorator
    include Representable::Hash
    property :title
    property :artist do
      property :id
    end
  end

  it { Decorator.definitions.keys.must_equal ["title", "artist"] }

  # in inheriting Decorator

  class InheritingDecorator < Decorator
    property :location
  end

  it { InheritingDecorator.definitions.keys.must_equal ["title", "artist", "location"] }
  it { assert_cloned(InheritingDecorator, Decorator, "title") }
  it do
    InheritingDecorator.representable_attrs.get(:artist).representer_module.object_id.wont_equal Decorator.representable_attrs.get(:artist).representer_module.object_id
  end

  # in inheriting and including Decorator

  class InheritingAndIncludingDecorator < Decorator
    include GenreModule
    property :location
  end

  it { InheritingAndIncludingDecorator.definitions.keys.must_equal ["title", "artist", "genre", "location"] }
  it { assert_cloned(InheritingAndIncludingDecorator, GenreModule, :genre) }


  # in module ---------------------------------------------------
  module Module
    include Representable
    property :title
  end

  it { Module.definitions.keys.must_equal ["title"] }


  # in module including module
  module SubModule
    include Representable
    include Module

    property :location
  end

  it { SubModule.definitions.keys.must_equal ["title", "location"] }
  it { assert_cloned(SubModule, Module, :title) }

  # including preserves order
  module IncludingModule
    include Representable
    property :genre
    include Module

    property :location
  end

  it { IncludingModule.definitions.keys.must_equal ["genre", "title", "location"] }


  # included in class -------------------------------------------
  class Class
    include Representable
    include IncludingModule
  end

  it { Class.definitions.keys.must_equal ["genre", "title", "location"] }
  it { assert_cloned(Class, IncludingModule, :title) }
  it { assert_cloned(Class, IncludingModule, :location) }
  it { assert_cloned(Class, IncludingModule, :genre) }

  # included in class with order
  class DefiningClass
    include Representable
    property :street_cred
    include IncludingModule
  end

  it { DefiningClass.definitions.keys.must_equal ["street_cred", "genre", "title", "location"] }

  # in class
  class RepresenterClass
    include Representable
    property :title
  end

  it { RepresenterClass.definitions.keys.must_equal ["title"] }


  # in inheriting class
  class InheritingClass < RepresenterClass
    include Representable
    property :location
  end

  it { InheritingClass.definitions.keys.must_equal ["title", "location"] }
  it { assert_cloned(InheritingClass, RepresenterClass, :title) }

  # in inheriting class and including
  class InheritingAndIncludingClass < RepresenterClass
    property :location
    include GenreModule
  end

  it { InheritingAndIncludingClass.definitions.keys.must_equal ["title", "location", "genre"] }
end