active_enum 0.9.12 → 1.0.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ describe ActiveEnum::Base do
4
4
 
5
5
  describe ".store" do
6
6
  it 'should load the storage class instance using the storage setting' do
7
- ActiveEnum::Base.send(:store).should be_instance_of(ActiveEnum::Storage::MemoryStore)
7
+ expect(ActiveEnum::Base.send(:store)).to be_instance_of(ActiveEnum::Storage::MemoryStore)
8
8
  end
9
9
  end
10
10
 
@@ -12,7 +12,7 @@ describe ActiveEnum::Base do
12
12
  it 'should return all enum classes defined use class definition' do
13
13
  ActiveEnum.enum_classes = []
14
14
  class NewEnum < ActiveEnum::Base; end
15
- ActiveEnum.enum_classes.should == [NewEnum]
15
+ expect(ActiveEnum.enum_classes).to eq([NewEnum])
16
16
  end
17
17
 
18
18
  it 'should return all enum classes defined using define block' do
@@ -20,13 +20,13 @@ describe ActiveEnum::Base do
20
20
  ActiveEnum.define do
21
21
  enum(:bulk_new_enum) { }
22
22
  end
23
- ActiveEnum.enum_classes.should == [BulkNewEnum]
23
+ expect(ActiveEnum.enum_classes).to eq([BulkNewEnum])
24
24
  end
25
25
  end
26
26
 
27
27
  describe ".all" do
28
28
  it 'should return an empty array when no values defined' do
29
- define_enum.all.should == []
29
+ expect(define_enum.all).to eq([])
30
30
  end
31
31
 
32
32
  it 'should return an array of arrays with all values defined as [id, name]' do
@@ -34,7 +34,7 @@ describe ActiveEnum::Base do
34
34
  value :name => 'Name 1'
35
35
  value :name => 'Name 2'
36
36
  end
37
- enum.all.should == [[1,'Name 1'], [2, 'Name 2']]
37
+ expect(enum.all).to eq([[1,'Name 1'], [2, 'Name 2']])
38
38
  end
39
39
  end
40
40
 
@@ -43,28 +43,28 @@ describe ActiveEnum::Base do
43
43
  enum = define_enum do
44
44
  value :id => 1, :name => 'Name'
45
45
  end
46
- enum.all.should == [[1,'Name']]
46
+ expect(enum.all).to eq([[1,'Name']])
47
47
  end
48
48
 
49
49
  it 'should allow me to define a value with a name only' do
50
50
  enum = define_enum do
51
51
  value :name => 'Name'
52
52
  end
53
- enum.all.should == [[1,'Name']]
53
+ expect(enum.all).to eq([[1,'Name']])
54
54
  end
55
55
 
56
56
  it 'should allow me to define a value as hash with id as key and name as value' do
57
57
  enum = define_enum do
58
58
  value 1 => 'Name'
59
59
  end
60
- enum.all.should == [[1,'Name']]
60
+ expect(enum.all).to eq([[1,'Name']])
61
61
  end
62
62
 
63
63
  it 'should allow to define meta data value with extra key value pairs' do
64
64
  enum = define_enum do
65
65
  value :id => 1, :name => 'Name', :description => 'extra'
66
66
  end
67
- enum.all.should == [[1,'Name',{:description => 'extra'}]]
67
+ expect(enum.all).to eq([[1,'Name',{:description => 'extra'}]])
68
68
  end
69
69
 
70
70
  it 'should increment value ids when defined without ids' do
@@ -72,7 +72,7 @@ describe ActiveEnum::Base do
72
72
  value :name => 'Name 1'
73
73
  value :name => 'Name 2'
74
74
  end
75
- enum.all.should == [[1,'Name 1'], [2, 'Name 2']]
75
+ expect(enum.all).to eq([[1,'Name 1'], [2, 'Name 2']])
76
76
  end
77
77
 
78
78
  it 'should raise error if the id is a duplicate' do
@@ -99,14 +99,14 @@ describe ActiveEnum::Base do
99
99
  enum = define_enum do
100
100
  value :id => 1, :name => 'Name', :description => 'extra'
101
101
  end
102
- enum.meta(1).should == {:description => 'extra'}
102
+ expect(enum.meta(1)).to eq({:description => 'extra'})
103
103
  end
104
104
 
105
105
  it 'should return empty hash for index with no meta defined' do
106
106
  enum = define_enum do
107
107
  value :id => 1, :name => 'Name'
108
108
  end
109
- enum.meta(1).should == {}
109
+ expect(enum.meta(1)).to eq({})
110
110
  end
111
111
 
112
112
  end
@@ -117,7 +117,7 @@ describe ActiveEnum::Base do
117
117
  value :id => 2, :name => 'Name 2'
118
118
  value :id => 1, :name => 'Name 1'
119
119
  end
120
- enum.all.should == [[1,'Name 1'], [2, 'Name 2']]
120
+ expect(enum.all).to eq([[1,'Name 1'], [2, 'Name 2']])
121
121
  end
122
122
 
123
123
  it 'should return sorted values by id using order setting' do
@@ -126,7 +126,7 @@ describe ActiveEnum::Base do
126
126
  value :id => 1, :name => 'Name 1'
127
127
  value :id => 2, :name => 'Name 2'
128
128
  end
129
- enum.all.should == [[2, 'Name 2'], [1,'Name 1']]
129
+ expect(enum.all).to eq([[2, 'Name 2'], [1,'Name 1']])
130
130
  end
131
131
 
132
132
  it 'should return sorted values by id using order setting' do
@@ -136,7 +136,7 @@ describe ActiveEnum::Base do
136
136
  value :id => 1, :name => 'Name 1'
137
137
  value :id => 2, :name => 'Name 2'
138
138
  end
139
- enum.all.should == [[3,'Name 3'], [1,'Name 1'], [2, 'Name 2']]
139
+ expect(enum.all).to eq([[3,'Name 3'], [1,'Name 1'], [2, 'Name 2']])
140
140
  end
141
141
  end
142
142
 
@@ -146,7 +146,7 @@ describe ActiveEnum::Base do
146
146
  value :id => 1, :name => 'Name 1'
147
147
  value :id => 2, :name => 'Name 2'
148
148
  end
149
- enum.ids.should == [1,2]
149
+ expect(enum.ids).to eq([1,2])
150
150
  end
151
151
  end
152
152
 
@@ -156,7 +156,7 @@ describe ActiveEnum::Base do
156
156
  value :id => 1, :name => 'Name 1'
157
157
  value :id => 2, :name => 'Name 2'
158
158
  end
159
- enum.names.should == ['Name 1', 'Name 2']
159
+ expect(enum.names).to eq(['Name 1', 'Name 2'])
160
160
  end
161
161
  end
162
162
 
@@ -169,16 +169,38 @@ describe ActiveEnum::Base do
169
169
  }
170
170
 
171
171
  it 'should return name when given an id' do
172
- enum[1].should == 'Name 1'
172
+ expect(enum[1]).to eq('Name 1')
173
173
  end
174
174
 
175
175
  it 'should return id when given a name' do
176
- enum['Name 1'].should == 1
176
+ expect(enum['Name 1']).to eq(1)
177
177
  end
178
178
 
179
179
  it 'should return id when given a symbol of the name' do
180
- enum[:Name_1].should == 1
181
- enum[:name_1].should == 1
180
+ expect(enum[:Name_1]).to eq(1)
181
+ expect(enum[:name_1]).to eq(1)
182
+ end
183
+
184
+ context "for missing value" do
185
+ it "should return nil for missing id" do
186
+ expect(enum['Not a value']).to eq nil
187
+ end
188
+
189
+ it "should return nil for missing name" do
190
+ expect(enum[0]).to eq nil
191
+ end
192
+
193
+ context "with raise_on_not_found" do
194
+ with_config :raise_on_not_found, true
195
+
196
+ it "should raise ActiveEnum::NotFound for missing id" do
197
+ expect { enum['Not a value'] }.to raise_error(ActiveEnum::NotFound)
198
+ end
199
+
200
+ it "should raise ActiveEnum::NotFound for missing name" do
201
+ expect { enum[0] }.to raise_error(ActiveEnum::NotFound)
202
+ end
203
+ end
182
204
  end
183
205
  end
184
206
 
@@ -191,19 +213,19 @@ describe ActiveEnum::Base do
191
213
  }
192
214
 
193
215
  it "should return true if value is a fixnum and matches an id" do
194
- enum.include?(1).should be_true
216
+ expect(enum.include?(1)).to be_truthy
195
217
  end
196
218
 
197
219
  it "should return false if value is a fixnum and does not match an id" do
198
- enum.include?(3).should be_false
220
+ expect(enum.include?(3)).to be_falsey
199
221
  end
200
222
 
201
223
  it "should return true if value is a string and matches a name" do
202
- enum.include?('Name 1').should be_true
224
+ expect(enum.include?('Name 1')).to be_truthy
203
225
  end
204
226
 
205
227
  it "should return false if value is a string and does not match a name" do
206
- enum.include?('No match').should be_false
228
+ expect(enum.include?('No match')).to be_falsey
207
229
  end
208
230
  end
209
231
 
@@ -213,7 +235,7 @@ describe ActiveEnum::Base do
213
235
  value :id => 1, :name => 'Name 1'
214
236
  value :id => 2, :name => 'Name 2'
215
237
  end
216
- enum.to_select.should == [['Name 1',1], ['Name 2',2]]
238
+ expect(enum.to_select).to eq([['Name 1',1], ['Name 2',2]])
217
239
  end
218
240
 
219
241
  it 'should return array sorted using order setting' do
@@ -222,7 +244,7 @@ describe ActiveEnum::Base do
222
244
  value :id => 1, :name => 'Name 1'
223
245
  value :id => 2, :name => 'Name 2'
224
246
  end
225
- enum.to_select.should == [['Name 2',2], ['Name 1',1]]
247
+ expect(enum.to_select).to eq([['Name 2',2], ['Name 1',1]])
226
248
  end
227
249
  end
228
250
 
@@ -13,45 +13,45 @@ describe ActiveEnum::Extensions do
13
13
  end
14
14
 
15
15
  it 'should add class :enumerate method to ActiveRecord' do
16
- ActiveRecord::Base.should respond_to(:enumerate)
16
+ expect(ActiveRecord::Base).to respond_to(:enumerate)
17
17
  end
18
18
 
19
19
  it 'should add class :active_enum_for method to ActiveRecord' do
20
- ActiveRecord::Base.should respond_to(:active_enum_for)
20
+ expect(ActiveRecord::Base).to respond_to(:active_enum_for)
21
21
  end
22
22
 
23
23
  it 'should allow multiple attributes to be enumerated with same enum' do
24
24
  Person.enumerate :attending, :staying, :with => Accepted
25
25
 
26
- Person.active_enum_for(:attending).should == Accepted
27
- Person.active_enum_for(:staying).should == Accepted
26
+ expect(Person.active_enum_for(:attending)).to eq(Accepted)
27
+ expect(Person.active_enum_for(:staying)).to eq(Accepted)
28
28
  end
29
29
 
30
30
  it 'should allow multiple attributes to be enumerated with different enums' do
31
31
  Person.enumerate :sex, :with => Sex
32
32
  Person.enumerate :attending, :with => Accepted
33
33
 
34
- Person.active_enum_for(:sex).should == Sex
35
- Person.active_enum_for(:attending).should == Accepted
34
+ expect(Person.active_enum_for(:sex)).to eq(Sex)
35
+ expect(Person.active_enum_for(:attending)).to eq(Accepted)
36
36
  end
37
37
 
38
38
  it 'should allow implicit enumeration class from attribute name' do
39
39
  Person.enumerate :sex
40
40
 
41
- Person.active_enum_for(:sex).should == Sex
41
+ expect(Person.active_enum_for(:sex)).to eq(Sex)
42
42
  end
43
43
 
44
44
  it 'should create enum namespaced enum class from block' do
45
45
  Person.enumerate :sex do
46
46
  value :id => 1, :name => 'Male'
47
47
  end
48
- Person.active_enum_for(:sex).should == ::Person::Sex
48
+ expect(Person.active_enum_for(:sex)).to eq(::Person::Sex)
49
49
  end
50
50
 
51
51
  it 'should raise error if implicit enumeration class cannot be found' do
52
52
  expect {
53
53
  Person.enumerate :first_name
54
- }.should raise_error(ActiveEnum::EnumNotFound)
54
+ }.to raise_error(ActiveEnum::EnumNotFound)
55
55
  end
56
56
 
57
57
  context "attribute" do
@@ -65,19 +65,19 @@ describe ActiveEnum::Extensions do
65
65
 
66
66
  context "with value" do
67
67
  it 'should return value with no arg' do
68
- person.sex.should == 1
68
+ expect(person.sex).to eq(1)
69
69
  end
70
70
 
71
71
  it 'should return enum id for value' do
72
- person.sex(:id).should == 1
72
+ expect(person.sex(:id)).to eq(1)
73
73
  end
74
74
 
75
75
  it 'should return enum name for value' do
76
- person.sex(:name).should == 'Male'
76
+ expect(person.sex(:name)).to eq('Male')
77
77
  end
78
78
 
79
79
  it 'should return enum class for attribute' do
80
- person.sex(:enum).should == Sex
80
+ expect(person.sex(:enum)).to eq(Sex)
81
81
  end
82
82
  end
83
83
 
@@ -85,19 +85,27 @@ describe ActiveEnum::Extensions do
85
85
  let(:person) { Person.new(:sex => nil) }
86
86
 
87
87
  it 'should return nil with no arg' do
88
- person.sex.should be_nil
88
+ expect(person.sex).to be_nil
89
89
  end
90
90
 
91
91
  it 'should return nil enum id' do
92
- person.sex(:id).should be_nil
92
+ expect(person.sex(:id)).to be_nil
93
93
  end
94
94
 
95
95
  it 'should return nil enum name' do
96
- person.sex(:name).should be_nil
96
+ expect(person.sex(:name)).to be_nil
97
97
  end
98
98
 
99
99
  it 'should return enum class for attribute' do
100
- person.sex(:enum).should == Sex
100
+ expect(person.sex(:enum)).to eq(Sex)
101
+ end
102
+
103
+ context "and raise_on_not_found" do
104
+ with_config :raise_on_not_found, true
105
+
106
+ it "should not raise error when attribute is nil" do
107
+ expect { person.sex(:id) }.to_not raise_error
108
+ end
101
109
  end
102
110
  end
103
111
 
@@ -105,19 +113,19 @@ describe ActiveEnum::Extensions do
105
113
  let(:person) { Person.new(:sex => -1) }
106
114
 
107
115
  it 'should return value with no arg' do
108
- person.sex.should == -1
116
+ expect(person.sex).to eq(-1)
109
117
  end
110
118
 
111
119
  it 'should return nil enum id' do
112
- person.sex(:id).should be_nil
120
+ expect(person.sex(:id)).to be_nil
113
121
  end
114
122
 
115
123
  it 'should return nil enum name' do
116
- person.sex(:name).should be_nil
124
+ expect(person.sex(:name)).to be_nil
117
125
  end
118
126
 
119
127
  it 'should return enum class for attribute' do
120
- person.sex(:enum).should == Sex
128
+ expect(person.sex(:enum)).to eq(Sex)
121
129
  end
122
130
  end
123
131
 
@@ -134,39 +142,39 @@ describe ActiveEnum::Extensions do
134
142
  end
135
143
 
136
144
  it 'should return meta value for existing key' do
137
- person.sex(:description).should == 'Man'
145
+ expect(person.sex(:description)).to eq('Man')
138
146
  end
139
147
 
140
148
  it 'should return nil for missing meta value' do
141
- person.sex(:nonexistent).should be_nil
149
+ expect(person.sex(:nonexistent)).to be_nil
142
150
  end
143
151
 
144
152
  it 'should return nil for missing index' do
145
153
  person.sex = nil
146
- person.sex(:description).should be_nil
154
+ expect(person.sex(:description)).to be_nil
147
155
  end
148
156
  end
149
157
 
150
158
  context "question method" do
151
159
  it 'should return normal value without arg' do
152
- person.sex?.should be_true
160
+ expect(person.sex?).to be_truthy
153
161
  person.sex = nil
154
- person.sex?.should be_false
162
+ expect(person.sex?).to be_falsey
155
163
  end
156
164
 
157
165
  it 'should return true if string name matches for id value' do
158
- person.sex?("Male").should be_true
166
+ expect(person.sex?("Male")).to be_truthy
159
167
  end
160
168
 
161
169
  it 'should return true if symbol name matches for id value' do
162
- person.sex?(:male).should be_true
163
- person.sex?(:Male).should be_true
170
+ expect(person.sex?(:male)).to be_truthy
171
+ expect(person.sex?(:Male)).to be_truthy
164
172
  end
165
173
 
166
174
  it 'should return false if name does not match for id value' do
167
- person.sex?("Female").should be_false
168
- person.sex?(:female).should be_false
169
- person.sex?(:Female).should be_false
175
+ expect(person.sex?("Female")).to be_falsey
176
+ expect(person.sex?(:female)).to be_falsey
177
+ expect(person.sex?(:Female)).to be_falsey
170
178
  end
171
179
  end
172
180
 
@@ -174,12 +182,12 @@ describe ActiveEnum::Extensions do
174
182
 
175
183
  it 'should store id value when valid enum name' do
176
184
  person.sex = :female
177
- person.sex.should == 2
185
+ expect(person.sex).to eq(2)
178
186
  end
179
187
 
180
188
  it 'should store nil value when invalid enum name' do
181
189
  person.sex = :invalid
182
- person.sex.should == nil
190
+ expect(person.sex).to eq(nil)
183
191
  end
184
192
 
185
193
  end
@@ -195,7 +203,11 @@ describe ActiveEnum::Extensions do
195
203
  end
196
204
 
197
205
  it 'should return text name value for attribute' do
198
- person.sex.should == 'Male'
206
+ expect(person.sex).to eq('Male')
207
+ end
208
+
209
+ it 'should return true for boolean match' do
210
+ expect(person.sex?(:male)).to be_truthy
199
211
  end
200
212
 
201
213
  after(:all) { ActiveEnum.use_name_as_value = false }
@@ -7,7 +7,7 @@ describe ActiveEnum::FormHelpers::SimpleForm, :type => :helper do
7
7
  include SimpleForm::ActionViewExtensions::FormHelper
8
8
 
9
9
  before do
10
- controller.stub!(:action_name).and_return('new')
10
+ allow(controller).to receive(:action_name).and_return('new')
11
11
  reset_class Person do
12
12
  enumerate :sex do
13
13
  value :id => 1, :name => 'Male'
@@ -20,25 +20,25 @@ describe ActiveEnum::FormHelpers::SimpleForm, :type => :helper do
20
20
  output = simple_form_for(Person.new, :url => people_path) do |f|
21
21
  concat f.input(:sex)
22
22
  end
23
- output.should have_selector('select#person_sex')
24
- output.should have_xpath('//option[@value=1]', :content => 'Male')
25
- output.should have_xpath('//option[@value=2]', :content => 'Female')
23
+ expect(output).to have_selector('select#person_sex')
24
+ expect(output).to have_xpath('//option[@value=1]', :text => 'Male')
25
+ expect(output).to have_xpath('//option[@value=2]', :text => 'Female')
26
26
  end
27
27
 
28
28
  it "should use explicit :enum input type" do
29
29
  output = simple_form_for(Person.new, :url => people_path) do |f|
30
30
  concat f.input(:sex, :as => :enum)
31
31
  end
32
- output.should have_selector('select#person_sex')
33
- output.should have_xpath('//option[@value=1]', :content => 'Male')
34
- output.should have_xpath('//option[@value=2]', :content => 'Female')
32
+ expect(output).to have_selector('select#person_sex')
33
+ expect(output).to have_xpath('//option[@value=1]', :text => 'Male')
34
+ expect(output).to have_xpath('//option[@value=2]', :text => 'Female')
35
35
  end
36
36
 
37
37
  it "should not use enum input type if :as option indicates other type" do
38
38
  output = simple_form_for(Person.new, :url => people_path) do |f|
39
39
  concat f.input(:sex, :as => :string)
40
40
  end
41
- output.should have_selector('input#person_sex')
41
+ expect(output).to have_selector('input#person_sex')
42
42
  end
43
43
 
44
44
  it "should raise error if attribute for enum input is not enumerated" do
@@ -46,28 +46,28 @@ describe ActiveEnum::FormHelpers::SimpleForm, :type => :helper do
46
46
  simple_form_for(Person.new, :url => people_path) do |f|
47
47
  f.input(:attending, :as => :enum)
48
48
  end
49
- }.should raise_error "Attribute 'attending' has no enum class"
49
+ }.to raise_error "Attribute 'attending' has no enum class"
50
50
  end
51
51
 
52
52
  it "should not use enum input type if class does not support ActiveEnum" do
53
53
  output = simple_form_for(NotActiveRecord.new, :as => :not_active_record, :url => people_path) do |f|
54
54
  concat f.input(:name)
55
55
  end
56
- output.should have_selector('input#not_active_record_name')
56
+ expect(output).to have_selector('input#not_active_record_name')
57
57
  end
58
58
 
59
59
  it "should allow non-enum fields to use default input determination" do
60
60
  output = simple_form_for(Person.new, :url => people_path) do |f|
61
61
  concat f.input(:first_name)
62
62
  end
63
- output.should have_selector('input#person_first_name')
63
+ expect(output).to have_selector('input#person_first_name')
64
64
  end
65
65
 
66
66
  it "should allow models without enumerated attributes to behave normally" do
67
67
  output = simple_form_for(NoEnumPerson.new, :url => people_path) do |f|
68
68
  concat f.input(:first_name)
69
69
  end
70
- output.should have_selector('input#no_enum_person_first_name')
70
+ expect(output).to have_selector('input#no_enum_person_first_name')
71
71
  end
72
72
 
73
73
  def people_path