File: base_test.rb

package info (click to toggle)
ruby-enumerize 2.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: ruby: 3,712; makefile: 6
file content (254 lines) | stat: -rw-r--r-- 6,703 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# frozen_string_literal: true

require 'test_helper'

class BaseTest < Minitest::Spec
  let(:kklass) do
    Class.new do
      extend Enumerize
    end
  end

  let(:subklass) do
    Class.new(kklass)
  end

  let(:object) { kklass.new }

  it 'returns nil when not set' do
    kklass.enumerize(:foo, :in => [:a, :b])
    expect(object.foo).must_be_nil
  end

  it 'returns value that was set' do
    kklass.enumerize(:foo, :in => [:a, :b])
    object.foo = :a
    expect(object.foo).must_equal 'a'
  end

  it 'returns translation' do
    store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
      kklass.enumerize(:foo, :in => [:a, :b])
      object.foo = :a
      expect(object.foo.text).must_equal 'a text'
      expect(object.foo_text).must_equal 'a text'
      expect(object.foo_text).must_equal 'a text'
    end
  end

  it 'returns nil as translation when value is nil' do
    store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
      kklass.enumerize(:foo, :in => [:a, :b])
      expect(object.foo_text).must_be_nil
    end
  end

  it 'scopes translation by i18n key' do
    def kklass.model_name
      name = String.new("ExampleClass")
      def name.i18n_key
        'example_class'
      end

      name
    end

    store_translations(:en, :enumerize => {:example_class => {:foo => {:a => 'a text scoped'}}}) do
      kklass.enumerize(:foo, :in => [:a, :b])
      object.foo = :a
      expect(object.foo.text).must_equal 'a text scoped'
      expect(object.foo_text).must_equal 'a text scoped'
    end
  end

  it 'returns humanized value if there are no translations' do
    store_translations(:en, :enumerize => {}) do
      kklass.enumerize(:foo, :in => [:a, :b])
      object.foo = :a
      expect(object.foo_text).must_equal 'A'
    end
  end

  it 'stores value as string' do
    kklass.enumerize(:foo, :in => [:a, :b])
    object.foo = :a
    expect(object.instance_variable_get(:@foo)).must_be_instance_of String
  end

  it 'handles default value' do
    kklass.enumerize(:foo, :in => [:a, :b], :default => :b)
    expect(object.foo).must_equal 'b'
  end

  it 'handles default value with lambda' do
    kklass.enumerize(:foo, :in => [:a, :b], :default => lambda { :b })
    expect(object.foo).must_equal 'b'
  end

  it 'injects object instance into lamda default value' do
    kklass.enumerize(:foo, :in => [:a, :b], :default => lambda { |obj| :b if obj.is_a? kklass })
    expect(object.foo).must_equal 'b'
  end

  it 'raises exception on invalid default value' do
    expect(proc {
      kklass.enumerize(:foo, :in => [:a, :b], :default => :c)
    }).must_raise ArgumentError
  end

  it 'has enumerized attributes' do
    expect(kklass.enumerized_attributes).must_be_empty
    kklass.enumerize(:foo, :in => %w[a b])
    expect(kklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
  end

  it "doesn't override existing method" do
    method = kklass.method(:name)
    kklass.enumerize(:name, :in => %w[a b], :default => 'a')
    expect(kklass.method(:name)).must_equal method
  end

  it "inherits enumerized attributes from a parent class" do
    kklass.enumerize(:foo, :in => %w[a b])
    expect(subklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
  end

  it "inherits enumerized attributes from a grandparent class" do
    kklass.enumerize(:foo, :in => %w[a b])
    expect(Class.new(subklass).enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
  end

  it "doesn't add enumerized attributes to parent class" do
    kklass.enumerize(:foo, :in => %w[a b])
    subklass.enumerize(:bar, :in => %w[c d])

    expect(kklass.enumerized_attributes[:bar]).must_be_nil
  end

  it 'adds new parent class attributes to subclass' do
    subklass = Class.new(kklass)
    kklass.enumerize :foo, :in => %w[a b]
    expect(subklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
  end

  it 'stores nil value' do
    kklass.enumerize(:foo, :in => [:a, :b])
    object.foo = nil
    expect(object.instance_variable_get(:@foo)).must_be_nil
  end

  it 'casts value to string for validation' do
    kklass.enumerize(:foo, :in => [:a, :b])
    object.foo = :c
    expect(object.read_attribute_for_validation(:foo)).must_equal 'c'
  end

  it "doesn't cast nil to string for validation" do
    kklass.enumerize(:foo, :in => [:a, :b])
    object.foo = nil
    expect(object.read_attribute_for_validation(:foo)).must_be_nil
  end

  it 'calls super in the accessor method' do
    accessors = Module.new do
      def attributes
        @attributes ||= {}
      end

      def foo
        attributes[:foo]
      end

      def foo=(v)
        attributes[:foo] = v
      end
    end

    klass = Class.new do
      include accessors
      extend Enumerize

      enumerize :foo, :in => %w[test]
    end

    object = klass.new
    expect(object.foo).must_be_nil
    expect(object.attributes).must_equal({})

    object.foo = 'test'
    expect(object.foo).must_equal 'test'
    expect(object.attributes).must_equal(:foo => 'test')
  end

  it 'stores hash values' do
    kklass.enumerize(:foo, :in => {:a => 1, :b => 2})

    object.foo = :a
    expect(object.instance_variable_get(:@foo)).must_equal 1
    expect(object.foo).must_equal 'a'

    object.foo = :b
    expect(object.instance_variable_get(:@foo)).must_equal 2
    expect(object.foo).must_equal 'b'
  end

  it 'returns custom value' do
    kklass.enumerize(:foo, :in => {:a => 1, :b => 2})

    object.foo = :a
    expect(object.foo_value).must_equal 1

    object.foo = :b
    expect(object.foo_value).must_equal 2
  end

  it 'allows initialize method with arguments' do
    klass = Class.new do
      extend Enumerize

      def initialize(argument, key_word_argument: nil); end
    end

    klass.new('arg1', key_word_argument: 'kwargs1')
  end

  it 'allows initialize method with arguments for inherited classes' do
    parent_klass = Class.new do
      def initialize(argument, key_word_argument: nil); end
    end

    klass = Class.new(parent_klass) do
      extend Enumerize

      def initialize(argument, key_word_argument: nil)
        super
      end
    end

    klass.new('arg1', key_word_argument: 'kwargs1')
  end

  it 'allows initializing object without keyword arguments' do
    parent_klass = Class.new do
      attr_reader :arguments

      def initialize(arguments)
        @arguments = arguments
      end
    end

    klass = Class.new(parent_klass) do
      extend Enumerize

      def initialize(arguments)
        super
      end
    end

    params = { 'string_key' => 1, symbol_key: 2 }

    object = klass.new(params)

    expect(object.arguments).must_equal params
  end
end