/
base_spec.rb
296 lines (238 loc) · 9.16 KB
/
base_spec.rb
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# encoding: utf-8
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe EnumerateIt::Base do
it "creates constants for each enumeration value" do
[TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3].each_with_index do |constant, idx|
expect(constant).to eq((idx + 1).to_s)
end
end
it "creates constants for camel case values" do
expect(TestEnumerationWithCamelCase::IPHONE).to eq('iPhone')
end
it "creates constants replacing its dashes with underscores" do
expect(TestEnumerationWithDash::PT_BR).to eq('pt-BR')
end
describe ".list" do
it "creates a method that returns the allowed values in the enumeration's class" do
expect(TestEnumeration.list).to eq(['1', '2', '3'])
end
context "specifying a default sort mode" do
subject { create_enumeration_class_with_sort_mode(sort_mode).list }
context "by value" do
let(:sort_mode) { :value }
it { should eq(%w(0 1 2 3)) }
end
context "by name" do
let(:sort_mode) { :name }
it { should eq(%w(2 1 3 0)) }
end
context "by translation" do
let(:sort_mode) { :translation }
it { should eq(%w(3 2 0 1)) }
end
context "by nothing" do
let(:sort_mode) { :none }
it { should eq(%w(1 2 3 0)) }
end
end
end
it "creates a method that returns the enumeration specification" do
expect(TestEnumeration.enumeration).to eq(
value_1: ['1', 'Hey, I am 1!'],
value_2: ['2', 'Hey, I am 2!'],
value_3: ['3', 'Hey, I am 3!']
)
end
describe ".length" do
it "returns the length of the enumeration" do
expect(TestEnumeration.length).to eq(3)
end
end
describe ".each_translation" do
it "yields each enumeration's value translation" do
translations = []
TestEnumeration.each_translation do |translation|
translations << translation
end
expect(translations).to eq(["Hey, I am 1!", "Hey, I am 2!", "Hey, I am 3!"])
end
end
describe ".translations" do
it "returns all translations" do
expect(TestEnumeration.translations).to eq(["Hey, I am 1!", "Hey, I am 2!", "Hey, I am 3!"])
end
end
describe ".each_value" do
it "yields each enumeration's value" do
values = []
TestEnumeration.each_value do |value|
values << value
end
expect(values).to eq(TestEnumeration.list)
end
end
describe ".to_a" do
it "returns an array with the values and human representations" do
expect(TestEnumeration.to_a).to eq([['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3']])
end
it "translates the available values" do
I18n.locale = :en
expect(TestEnumerationWithoutArray.to_a).to eq([['First Value', '1'], ['Value Two', '2']])
I18n.locale = :pt
expect(TestEnumerationWithoutArray.to_a).to eq([['Primeiro Valor', '1'], ['Value Two', '2']])
end
it "can be extended from the enumeration class" do
expect(TestEnumerationWithExtendedBehaviour.to_a).to eq([['Second', '2'],['First','1']])
end
end
describe ".to_json" do
it "gives a valid json back" do
I18n.locale = :inexsistent
expect(TestEnumerationWithoutArray.to_json).to eq('[{"value":"1","label":"Value One"},{"value":"2","label":"Value Two"}]')
end
it "give translated values when available" do
I18n.locale = :pt
expect(TestEnumerationWithoutArray.to_json).to eq('[{"value":"1","label":"Primeiro Valor"},{"value":"2","label":"Value Two"}]')
end
end
describe ".t" do
it "translates a given value" do
I18n.locale = :pt
expect(TestEnumerationWithoutArray.t('1')).to eq('Primeiro Valor')
end
end
describe ".to_range" do
it "returns a Range object containing the enumeration's value interval" do
expect(TestEnumeration.to_range).to eq("1".."3")
end
end
describe ".values_for" do
it "returns an array with the corresponding values for a string array representing some of the enumeration's values" do
expect(TestEnumeration.values_for(%w(VALUE_1 VALUE_2))).to eq([TestEnumeration::VALUE_1, TestEnumeration::VALUE_2])
end
it "returns a nil value if the a constant named after one of the given strings cannot be found" do
expect(TestEnumeration.values_for(%w(VALUE_1 THIS_IS_WRONG))).to eq([TestEnumeration::VALUE_1, nil])
end
end
describe ".value_for" do
it "returns the enumeration's value" do
expect(TestEnumeration.value_for("VALUE_1")).to eq(TestEnumeration::VALUE_1)
end
context "when a constant named after the received value cannot be found" do
it "returns nil" do
expect(TestEnumeration.value_for("THIS_IS_WRONG")).to be_nil
end
end
end
describe ".value_from_key" do
it "returns the correct value when the key is a string" do
expect(TestEnumeration.value_from_key("value_1")).to eq(TestEnumeration::VALUE_1)
end
it "returns the correct value when the key is a symbol" do
expect(TestEnumeration.value_from_key(:value_1)).to eq(TestEnumeration::VALUE_1)
end
it "returns nil when the key does not exist in the enumeration" do
expect(TestEnumeration.value_from_key("wrong")).to be_nil
end
it "returns nil when the given value is nil" do
expect(TestEnumeration.value_from_key(nil)).to be_nil
end
end
describe ".keys" do
it "returns a list with the keys used to define the enumeration" do
expect(TestEnumeration.keys).to eq([:value_1, :value_2, :value_3])
end
end
describe ".key_for" do
it "returns the key for the given value inside the enumeration" do
expect(TestEnumeration.key_for(TestEnumeration::VALUE_1)).to eq(:value_1)
end
it "returns nil if the enumeration does not have the given value" do
expect(TestEnumeration.key_for("foo")).to be_nil
end
end
context 'associate values with a list' do
it "creates constants for each enumeration value" do
expect(TestEnumerationWithList::FIRST).to eq("first")
expect(TestEnumerationWithList::SECOND).to eq("second")
end
it "returns an array with the values and human representations" do
expect(TestEnumerationWithList.to_a).to eq([['First', 'first'], ['Second', 'second']])
end
end
context "specifying a default sort mode" do
subject { create_enumeration_class_with_sort_mode(sort_mode).to_a }
context "by value" do
let(:sort_mode) { :value }
it { is_expected.to eq([["jkl", "0"], ["xyz", "1"], ["fgh", "2"], ["abc", "3"]]) }
end
context "by name" do
let(:sort_mode) { :name }
it { is_expected.to eq([["fgh", "2"], ["xyz", "1"], ["abc", "3"], ["jkl", "0"]]) }
end
context "by translation" do
let(:sort_mode) { :translation }
it { is_expected.to eq([["abc", "3"] ,["fgh", "2"], ["jkl", "0"], ["xyz", "1"]]) }
end
context "by nothing" do
let(:sort_mode) { :none }
it { is_expected.to eq([["xyz", "1"], ["fgh", "2"], ["abc", "3"], ["jkl", "0"] ]) }
end
end
context "when included in ActiveRecord::Base" do
before do
class ActiveRecordStub
attr_accessor :bla
class << self
def validates_inclusion_of(_); true; end
def validates_presence_of; true; end
end
end
allow(ActiveRecordStub).to receive(:validates_inclusion_of).and_return(true)
ActiveRecordStub.extend EnumerateIt
end
it "creates a validation for inclusion" do
expect(ActiveRecordStub).to receive(:validates_inclusion_of).with(:bla, in: TestEnumeration.list, allow_blank: true)
class ActiveRecordStub
has_enumeration_for :bla, with: TestEnumeration
end
end
context "using the :required option" do
before do
allow(ActiveRecordStub).to receive(:validates_presence_of).and_return(true)
end
it "creates a validation for presence" do
expect(ActiveRecordStub).to receive(:validates_presence_of)
class ActiveRecordStub
has_enumeration_for :bla, with: TestEnumeration, required: true
end
end
it "passes the given options to the validation method" do
expect(ActiveRecordStub).to receive(:validates_presence_of).with(:bla, if: :some_method)
class ActiveRecordStub
has_enumeration_for :bla, with: TestEnumeration, required: { if: :some_method }
end
end
it "do not require the attribute by default" do
expect(ActiveRecordStub).to_not receive(:validates_presence_of)
class ActiveRecordStub
has_enumeration_for :bla, with: TestEnumeration
end
end
end
context "using :skip_validation option" do
it "doesn't create a validation for inclusion" do
expect(ActiveRecordStub).to_not receive(:validates_inclusion_of)
class ActiveRecordStub
has_enumeration_for :bla, with: TestEnumeration, skip_validation: true
end
end
it "doesn't create a validation for presence" do
expect(ActiveRecordStub).to_not receive(:validates_presence_of)
class ActiveRecordStub
has_enumeration_for :bla, with: TestEnumeration, require: true, skip_validation: true
end
end
end
end
end