simple_state_machine 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,253 +1,250 @@
1
1
  require 'spec_helper'
2
-
3
- ActiveRecord::Base.logger = Logger.new "test.log"
4
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
5
- :database => ":memory:")
6
-
7
- def setup_db
8
- ActiveRecord::Schema.define(:version => 1) do
9
- create_table :users do |t|
10
- t.column :id, :integer
11
- t.column :name, :string
12
- t.column :state, :string
13
- t.column :activation_code, :string
14
- t.column :created_at, :datetime
15
- t.column :updated_at, :datetime
16
- end
17
- create_table :tickets do |t|
18
- t.column :id, :integer
19
- t.column :ssm_state, :string
2
+ require 'logger'
3
+
4
+ if defined? ActiveRecord
5
+ ActiveRecord::Base.logger = Logger.new "test.log"
6
+ ActiveRecord::Base.establish_connection('adapter' => RUBY_PLATFORM == 'java' ? 'jdbcsqlite3' : 'sqlite3', 'database' => ':memory:')
7
+
8
+ def setup_db
9
+ ActiveRecord::Schema.define(:version => 1) do
10
+ create_table :users do |t|
11
+ t.column :name, :string
12
+ t.column :state, :string
13
+ t.column :activation_code, :string
14
+ t.column :created_at, :datetime
15
+ t.column :updated_at, :datetime
16
+ end
17
+ create_table :tickets do |t|
18
+ t.column :ssm_state, :string
19
+ end
20
20
  end
21
21
  end
22
- end
23
-
24
- def teardown_db
25
- ActiveRecord::Base.connection.tables.each do |table|
26
- ActiveRecord::Base.connection.drop_table(table)
27
- end
28
- end
29
-
30
- class Ticket < ActiveRecord::Base
31
- extend SimpleStateMachine::ActiveRecord
32
22
 
33
- state_machine_definition.state_method = :ssm_state
34
-
35
- def after_initialize
36
- self.ssm_state ||= 'open'
23
+ def teardown_db
24
+ ActiveRecord::Base.connection.tables.each do |table|
25
+ ActiveRecord::Base.connection.drop_table(table)
26
+ end
37
27
  end
38
28
 
39
- event :close, :open => :closed
40
- end
29
+ class Ticket < ActiveRecord::Base
30
+ extend SimpleStateMachine::ActiveRecord
41
31
 
42
- describe ActiveRecord do
32
+ state_machine_definition.state_method = :ssm_state
43
33
 
44
- before do
45
- setup_db
46
- end
34
+ after_initialize :after_initialize
35
+ def after_initialize
36
+ self.ssm_state ||= 'open'
37
+ end
47
38
 
48
- after do
49
- teardown_db
39
+ event :close, :open => :closed
50
40
  end
51
41
 
52
- it "has a default state" do
53
- User.new.should be_new
54
- end
42
+ describe ActiveRecord do
55
43
 
56
- # TODO needs nesting/grouping, seems to have some duplication
44
+ before do
45
+ setup_db
46
+ end
57
47
 
58
- describe "event_and_save" do
59
- it "persists transitions" do
60
- user = User.create!(:name => 'name')
61
- user.invite_and_save.should == true
62
- User.find(user.id).should be_invited
63
- User.find(user.id).activation_code.should_not be_nil
48
+ after do
49
+ teardown_db
64
50
  end
65
51
 
66
- it "persist transitions even when state is attr_protected" do
67
- user_class = Class.new(User)
68
- user_class.instance_eval { attr_protected :state }
69
- user = user_class.create!(:name => 'name', :state => 'x')
70
- user.should be_new
71
- user.invite_and_save
72
- user.reload.should be_invited
52
+ it "has a default state" do
53
+ expect(User.new).to be_new
73
54
  end
74
55
 
75
56
  it "persists transitions when using send and a symbol" do
76
57
  user = User.create!(:name => 'name')
77
- user.send(:invite_and_save).should == true
78
- User.find(user.id).should be_invited
79
- User.find(user.id).activation_code.should_not be_nil
58
+ expect(user.send(:invite_and_save)).to eq(true)
59
+ expect(User.find(user.id)).to be_invited
60
+ expect(User.find(user.id).activation_code).not_to be_nil
80
61
  end
81
62
 
82
- it "raises an error if an invalid state_transition is called" do
83
- user = User.create!(:name => 'name')
84
- expect {
85
- user.confirm_invitation_and_save 'abc'
86
- }.to raise_error(SimpleStateMachine::IllegalStateTransitionError,
87
- "You cannot 'confirm_invitation' when state is 'new'")
88
- end
63
+ # TODO needs nesting/grouping, seems to have some duplication
89
64
 
90
- it "returns false and keeps state if record is invalid" do
91
- user = User.new
92
- user.should be_new
93
- user.should_not be_valid
94
- user.invite_and_save.should == false
95
- user.should be_new
96
- end
65
+ describe "event_and_save" do
66
+ it "persists transitions" do
67
+ user = User.create!(:name => 'name')
68
+ expect(user.invite_and_save).to eq(true)
69
+ expect(User.find(user.id)).to be_invited
70
+ expect(User.find(user.id).activation_code).not_to be_nil
71
+ end
97
72
 
98
- it "returns false, keeps state and keeps errors if event adds errors" do
99
- user = User.create!(:name => 'name')
100
- user.invite_and_save!
101
- user.should be_invited
102
- user.confirm_invitation_and_save('x').should == false
103
- user.should be_invited
104
- user.errors.entries.should == [['activation_code', 'is invalid']]
105
- end
73
+ it "persists transitions when using send and a symbol" do
74
+ user = User.create!(:name => 'name')
75
+ expect(user.send(:invite_and_save)).to eq(true)
76
+ expect(User.find(user.id)).to be_invited
77
+ expect(User.find(user.id).activation_code).not_to be_nil
78
+ end
79
+
80
+ it "raises an error if an invalid state_transition is called" do
81
+ user = User.create!(:name => 'name')
82
+ expect {
83
+ user.confirm_invitation_and_save 'abc'
84
+ }.to raise_error(SimpleStateMachine::IllegalStateTransitionError,
85
+ "You cannot 'confirm_invitation' when state is 'new'")
86
+ end
87
+
88
+ it "returns false and keeps state if record is invalid" do
89
+ user = User.new
90
+ expect(user).to be_new
91
+ expect(user).not_to be_valid
92
+ expect(user.invite_and_save).to eq(false)
93
+ expect(user).to be_new
94
+ end
95
+
96
+ it "returns false, keeps state and keeps errors if event adds errors" do
97
+ user = User.create!(:name => 'name')
98
+ user.invite_and_save!
99
+ expect(user).to be_invited
100
+ expect(user.confirm_invitation_and_save('x')).to eq(false)
101
+ expect(user).to be_invited
102
+ expect(Array(user.errors[:activation_code])).to eq(['is invalid'])
103
+ end
106
104
 
107
- it "rollsback if an exception is raised" do
108
- user_class = Class.new(User)
109
- user_class.instance_eval do
110
- define_method :invite_without_managed_state do
111
- User.create!(:name => 'name2') #this shouldn't be persisted
112
- User.create! #this should raise an error
105
+ it "rollsback if an exception is raised" do
106
+ user_class = Class.new(User)
107
+ user_class.instance_eval do
108
+ define_method :invite_without_managed_state do
109
+ User.create!(:name => 'name2') #this shouldn't be persisted
110
+ User.create! #this should raise an error
111
+ end
113
112
  end
113
+ expect(user_class.count).to eq(0)
114
+ user = user_class.create!(:name => 'name')
115
+ expect {
116
+ user.transaction { user.invite_and_save }
117
+ }.to raise_error(ActiveRecord::RecordInvalid,
118
+ "Validation failed: Name can't be blank")
119
+ expect(user_class.count).to eq(1)
120
+ expect(user_class.first.name).to eq('name')
121
+ expect(user_class.first).to be_new
122
+ end
123
+
124
+ it "raises an error if an invalid state_transition is called" do
125
+ user = User.create!(:name => 'name')
126
+ expect {
127
+ user.confirm_invitation_and_save! 'abc'
128
+ }.to raise_error(SimpleStateMachine::IllegalStateTransitionError,
129
+ "You cannot 'confirm_invitation' when state is 'new'")
114
130
  end
115
- user_class.count.should == 0
116
- user = user_class.create!(:name => 'name')
117
- expect {
118
- user.transaction { user.invite_and_save }
119
- }.to raise_error(ActiveRecord::RecordInvalid,
120
- "Validation failed: Name can't be blank")
121
- user_class.count.should == 1
122
- user_class.first.name.should == 'name'
123
- user_class.first.should be_new
124
131
  end
125
- end
126
132
 
127
- describe "event_and_save!" do
133
+ describe "event_and_save!" do
128
134
 
129
- it "persists transitions" do
130
- user = User.create!(:name => 'name')
131
- user.invite_and_save!.should == true
132
- User.find(user.id).should be_invited
133
- User.find(user.id).activation_code.should_not be_nil
134
- end
135
+ it "persists transitions" do
136
+ user = User.create!(:name => 'name')
137
+ expect(user.invite_and_save!).to eq(true)
138
+ expect(User.find(user.id)).to be_invited
139
+ expect(User.find(user.id).activation_code).not_to be_nil
140
+ end
135
141
 
136
- it "persist transitions even when state is attr_protected" do
137
- user_class = Class.new(User)
138
- user_class.instance_eval { attr_protected :state }
139
- user = user_class.create!(:name => 'name', :state => 'x')
140
- user.should be_new
141
- user.invite_and_save!
142
- user.reload.should be_invited
143
- end
142
+ it "raises an error if an invalid state_transition is called" do
143
+ user = User.create!(:name => 'name')
144
+ expect {
145
+ user.confirm_invitation_and_save! 'abc'
146
+ }.to raise_error(SimpleStateMachine::IllegalStateTransitionError,
147
+ "You cannot 'confirm_invitation' when state is 'new'")
148
+ end
144
149
 
145
- it "raises an error if an invalid state_transition is called" do
146
- user = User.create!(:name => 'name')
147
- expect {
148
- user.confirm_invitation_and_save! 'abc'
149
- }.to raise_error(SimpleStateMachine::IllegalStateTransitionError,
150
- "You cannot 'confirm_invitation' when state is 'new'")
151
- end
150
+ it "raises a RecordInvalid and keeps state if record is invalid" do
151
+ user = User.new
152
+ expect(user).to be_new
153
+ expect(user).not_to be_valid
154
+ expect {
155
+ user.invite_and_save!
156
+ }.to raise_error(ActiveRecord::RecordInvalid,
157
+ "Validation failed: Name can't be blank")
158
+ expect(user).to be_new
159
+ end
152
160
 
153
- it "raises a RecordInvalid and keeps state if record is invalid" do
154
- user = User.new
155
- user.should be_new
156
- user.should_not be_valid
157
- expect {
161
+ it "raises a RecordInvalid and keeps state if event adds errors" do
162
+ user = User.create!(:name => 'name')
158
163
  user.invite_and_save!
159
- }.to raise_error(ActiveRecord::RecordInvalid,
160
- "Validation failed: Name can't be blank")
161
- user.should be_new
162
- end
163
-
164
- it "raises a RecordInvalid and keeps state if event adds errors" do
165
- user = User.create!(:name => 'name')
166
- user.invite_and_save!
167
- user.should be_invited
168
- expect {
169
- user.confirm_invitation_and_save!('x')
170
- }.to raise_error(ActiveRecord::RecordInvalid,
171
- "Validation failed: Activation code is invalid")
172
- user.should be_invited
173
- end
164
+ expect(user).to be_invited
165
+ expect {
166
+ user.confirm_invitation_and_save!('x')
167
+ }.to raise_error(ActiveRecord::RecordInvalid,
168
+ "Validation failed: Activation code is invalid")
169
+ expect(user).to be_invited
170
+ end
174
171
 
175
- it "rollsback if an exception is raised" do
176
- user_class = Class.new(User)
177
- user_class.instance_eval do
178
- define_method :invite_without_managed_state do
179
- User.create!(:name => 'name2') #this shouldn't be persisted
180
- User.create! #this should raise an error
172
+ it "rollsback if an exception is raised" do
173
+ user_class = Class.new(User)
174
+ user_class.instance_eval do
175
+ define_method :invite_without_managed_state do
176
+ User.create!(:name => 'name2') #this shouldn't be persisted
177
+ User.create! #this should raise an error
178
+ end
181
179
  end
180
+ expect(user_class.count).to eq(0)
181
+ user = user_class.create!(:name => 'name')
182
+ expect {
183
+ user.transaction { user.invite_and_save! }
184
+ }.to raise_error(ActiveRecord::RecordInvalid,
185
+ "Validation failed: Name can't be blank")
186
+ expect(user_class.count).to eq(1)
187
+ expect(user_class.first.name).to eq('name')
188
+ expect(user_class.first).to be_new
182
189
  end
183
- user_class.count.should == 0
184
- user = user_class.create!(:name => 'name')
185
- expect {
186
- user.transaction { user.invite_and_save! }
187
- }.to raise_error(ActiveRecord::RecordInvalid,
188
- "Validation failed: Name can't be blank")
189
- user_class.count.should == 1
190
- user_class.first.name.should == 'name'
191
- user_class.first.should be_new
190
+
192
191
  end
193
192
 
194
- end
193
+ describe "event" do
194
+ it "persists transitions" do
195
+ user = User.create!(:name => 'name')
196
+ expect(user.invite).to eq(true)
197
+ expect(User.find(user.id)).to be_invited
198
+ expect(User.find(user.id).activation_code).not_to be_nil
199
+ end
195
200
 
196
- describe "event" do
197
- it "persists transitions" do
198
- user = User.create!(:name => 'name')
199
- user.invite.should == true
200
- User.find(user.id).should be_invited
201
- User.find(user.id).activation_code.should_not be_nil
202
- end
201
+ it "returns false, keeps state and keeps errors if event adds errors" do
202
+ user = User.create!(:name => 'name')
203
+ user.invite_and_save!
204
+ expect(user).to be_invited
205
+ expect(user.confirm_invitation('x')).to eq(false)
206
+ expect(user).to be_invited
207
+ expect(Array(user.errors[:activation_code])).to eq(['is invalid'])
208
+ end
203
209
 
204
- it "returns false, keeps state and keeps errors if event adds errors" do
205
- user = User.create!(:name => 'name')
206
- user.invite_and_save!
207
- user.should be_invited
208
- user.confirm_invitation('x').should == false
209
- user.should be_invited
210
- user.errors.entries.should == [['activation_code', 'is invalid']]
211
210
  end
212
211
 
213
- end
212
+ describe "event!" do
214
213
 
215
- describe "event!" do
214
+ it "persists transitions" do
215
+ user = User.create!(:name => 'name')
216
+ expect(user.invite!).to eq(true)
217
+ expect(User.find(user.id)).to be_invited
218
+ expect(User.find(user.id).activation_code).not_to be_nil
219
+ end
216
220
 
217
- it "persists transitions" do
218
- user = User.create!(:name => 'name')
219
- user.invite!.should == true
220
- User.find(user.id).should be_invited
221
- User.find(user.id).activation_code.should_not be_nil
222
- end
221
+ it "raises a RecordInvalid and keeps state if record is invalid" do
222
+ user = User.new
223
+ expect(user).to be_new
224
+ expect(user).not_to be_valid
225
+ expect { user.invite! }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't be blank")
226
+ expect(user).to be_new
227
+ end
223
228
 
224
- it "raises a RecordInvalid and keeps state if record is invalid" do
225
- user = User.new
226
- user.should be_new
227
- user.should_not be_valid
228
- expect { user.invite! }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't be blank")
229
- user.should be_new
230
229
  end
231
230
 
232
- end
231
+ describe 'custom state method' do
233
232
 
234
- describe 'custom state method' do
233
+ it "persists transitions" do
234
+ ticket = Ticket.create!
235
+ expect(ticket.ssm_state).to eq('open')
236
+ expect(ticket.close!).to eq(true)
237
+ expect(ticket.ssm_state).to eq('closed')
238
+ end
235
239
 
236
- it "persists transitions" do
237
- ticket = Ticket.create!
238
- ticket.ssm_state.should == 'open'
239
- ticket.close!.should == true
240
- ticket.ssm_state.should == 'closed'
241
- end
240
+ it "persists transitions with !" do
241
+ ticket = Ticket.create!
242
+ expect(ticket.ssm_state).to eq('open')
243
+ ticket.close!
244
+ expect(ticket.ssm_state).to eq('closed')
245
+ end
242
246
 
243
- it "persists transitions with !" do
244
- ticket = Ticket.create!
245
- ticket.ssm_state.should == 'open'
246
- ticket.close!
247
- ticket.ssm_state.should == 'closed'
248
247
  end
249
248
 
250
249
  end
251
-
252
250
  end
253
-
@@ -10,7 +10,7 @@ describe SimpleStateMachine::Decorator::Default do
10
10
  @state_machine_definition ||= SimpleStateMachine::StateMachineDefinition.new
11
11
  end
12
12
  end
13
- decorator = SimpleStateMachine::Decorator::Default.new klass
13
+ decorator = described_class.new klass
14
14
  decorator.decorate SimpleStateMachine::Transition.new(:event, :state1, :state2)
15
15
  @instance = klass.new
16
16
  @instance.state = 'state1'
@@ -18,26 +18,26 @@ describe SimpleStateMachine::Decorator::Default do
18
18
 
19
19
  describe "#initialize" do
20
20
  it "defines a state_machine method" do
21
- @instance.state_machine.should be_an(SimpleStateMachine::StateMachine)
21
+ expect(@instance.state_machine).to be_an(SimpleStateMachine::StateMachine)
22
22
  end
23
23
 
24
24
  it "defines a state getter method" do
25
- @instance.should respond_to(:state)
25
+ expect(@instance).to respond_to(:state)
26
26
  end
27
27
 
28
28
  it "defines a state setter method" do
29
- @instance.should respond_to(:state=)
29
+ expect(@instance).to respond_to(:state=)
30
30
  end
31
31
  end
32
32
 
33
33
  describe "#decorate" do
34
34
  it "defines state_helper_methods for both states" do
35
- @instance.state1?.should == true
36
- @instance.state2?.should == false
35
+ expect(@instance.state1?).to eq(true)
36
+ expect(@instance.state2?).to eq(false)
37
37
  end
38
38
 
39
39
  it "defines an event method" do
40
- @instance.should respond_to(:event)
40
+ expect(@instance).to respond_to(:event)
41
41
  end
42
42
  end
43
43
  end
@@ -55,7 +55,7 @@ describe SimpleStateMachine::Decorator::Default do
55
55
  def event() "predefined method" end
56
56
  end
57
57
  transition = SimpleStateMachine::Transition.new(:event, :state1, :state2)
58
- decorator = SimpleStateMachine::Decorator::Default.new klass
58
+ decorator = described_class.new klass
59
59
  decorator.decorate transition
60
60
  klass.state_machine_definition.transitions << transition
61
61
  @instance = klass.new
@@ -64,26 +64,26 @@ describe SimpleStateMachine::Decorator::Default do
64
64
 
65
65
  describe "#initialize" do
66
66
  it "defines a state_machine method" do
67
- @instance.state_machine.should be_an(SimpleStateMachine::StateMachine)
67
+ expect(@instance.state_machine).to be_an(SimpleStateMachine::StateMachine)
68
68
  end
69
69
 
70
70
  it "defines a state getter method" do
71
- @instance.should respond_to(:state)
71
+ expect(@instance).to respond_to(:state)
72
72
  end
73
73
 
74
74
  it "defines a state setter method" do
75
- @instance.should respond_to(:state=)
75
+ expect(@instance).to respond_to(:state=)
76
76
  end
77
77
  end
78
78
 
79
79
  describe "#decorate" do
80
80
  it "does not overwrite predefined state_helper_methods" do
81
- @instance.state1?.should == "state1"
82
- @instance.state2?.should == "state2"
81
+ expect(@instance.state1?).to eq("state1")
82
+ expect(@instance.state2?).to eq("state2")
83
83
  end
84
84
 
85
85
  it "does not overwrite predefined event method" do
86
- @instance.event.should == "predefined method"
86
+ expect(@instance.event).to eq("predefined method")
87
87
  end
88
88
  end
89
89
  end
@@ -102,7 +102,7 @@ describe SimpleStateMachine::Decorator::Default do
102
102
  def event() "predefined method" end
103
103
  end
104
104
  transition = SimpleStateMachine::Transition.new(:event, :state1, :state2)
105
- decorator = SimpleStateMachine::Decorator::Default.new klass
105
+ decorator = described_class.new klass
106
106
  decorator.decorate transition
107
107
  klass.state_machine_definition.transitions << transition
108
108
  @instance = klass.new
@@ -111,22 +111,22 @@ describe SimpleStateMachine::Decorator::Default do
111
111
 
112
112
  describe "#initialize" do
113
113
  it "defines a state_machine method" do
114
- @instance.state_machine.should be_an(SimpleStateMachine::StateMachine)
114
+ expect(@instance.state_machine).to be_an(SimpleStateMachine::StateMachine)
115
115
  end
116
116
 
117
117
  it "defines a state getter method" do
118
- @instance.should respond_to(:state)
118
+ expect(@instance).to respond_to(:state)
119
119
  end
120
120
 
121
121
  it "defines a state setter method" do
122
- @instance.should respond_to(:state=)
122
+ expect(@instance).to respond_to(:state=)
123
123
  end
124
124
  end
125
125
 
126
126
  describe "#decorate" do
127
127
  it "does not overwrite predefined protected state_helper_methods" do
128
- @instance.send(:state1?).should == "state1"
129
- @instance.send(:state2?).should == "state2"
128
+ expect(@instance.send(:state1?)).to eq("state1")
129
+ expect(@instance.send(:state2?)).to eq("state2")
130
130
  end
131
131
 
132
132
  it "keeps predefined protected state_helper_methods protected" do
@@ -135,7 +135,7 @@ describe SimpleStateMachine::Decorator::Default do
135
135
  end
136
136
 
137
137
  it "does not overwrite predefined protected event method" do
138
- @instance.event.should == "predefined method"
138
+ expect(@instance.event).to eq("predefined method")
139
139
  end
140
140
  end
141
141
  end
@@ -154,7 +154,7 @@ describe SimpleStateMachine::Decorator::Default do
154
154
  def event() "predefined method" end
155
155
  end
156
156
  transition = SimpleStateMachine::Transition.new(:event, :state1, :state2)
157
- decorator = SimpleStateMachine::Decorator::Default.new klass
157
+ decorator = described_class.new klass
158
158
  decorator.decorate transition
159
159
  klass.state_machine_definition.transitions << transition
160
160
  @instance = klass.new
@@ -163,22 +163,22 @@ describe SimpleStateMachine::Decorator::Default do
163
163
 
164
164
  describe "#initialize" do
165
165
  it "defines a state_machine method" do
166
- @instance.state_machine.should be_an(SimpleStateMachine::StateMachine)
166
+ expect(@instance.state_machine).to be_an(SimpleStateMachine::StateMachine)
167
167
  end
168
168
 
169
169
  it "defines a state getter method" do
170
- @instance.should respond_to(:state)
170
+ expect(@instance).to respond_to(:state)
171
171
  end
172
172
 
173
173
  it "defines a state setter method" do
174
- @instance.should respond_to(:state=)
174
+ expect(@instance).to respond_to(:state=)
175
175
  end
176
176
  end
177
177
 
178
178
  describe "#decorate" do
179
179
  it "does not overwrite predefined private state_helper_methods" do
180
- @instance.send(:state1?).should == "state1"
181
- @instance.send(:state2?).should == "state2"
180
+ expect(@instance.send(:state1?)).to eq("state1")
181
+ expect(@instance.send(:state2?)).to eq("state2")
182
182
  end
183
183
 
184
184
  it "keeps predefined private state_helper_methods private" do
@@ -187,7 +187,7 @@ describe SimpleStateMachine::Decorator::Default do
187
187
  end
188
188
 
189
189
  it "does not overwrite predefined protected event method" do
190
- @instance.event.should == "predefined method"
190
+ expect(@instance.event).to eq("predefined method")
191
191
  end
192
192
  end
193
193
  end