step_machine 0.0.3

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.
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'simplecov'
4
+ require 'pry'
5
+
6
+ # SimpleCov.start
7
+
8
+ Dir[File.expand_path("../../", __FILE__) + "/lib/**/*.rb"].each { |f| require f }
9
+
10
+ RSpec.configure do |config|
11
+ # Ativa output colorido
12
+ config.color_enabled = true
13
+ end
@@ -0,0 +1,133 @@
1
+ require "spec_helper"
2
+
3
+ include StepMachine
4
+
5
+ describe "StepMachine" do
6
+
7
+ before :each do
8
+ end
9
+
10
+ describe "on step" do
11
+
12
+ it "should create an instance of the runner" do
13
+ step(:name) {}
14
+ @step_machine_runner.should be_a(Runner)
15
+ end
16
+
17
+ it "should create an step with the given name and block" do
18
+
19
+ block = proc { x + 1 }
20
+ step = step(:name, &block)
21
+
22
+ step.name.should == :name
23
+ step.block.should == block
24
+ end
25
+
26
+ end
27
+
28
+ describe "on group" do
29
+
30
+ it "should create an group and assign to the steps" do
31
+ group(:group) do
32
+ step(:step_1)
33
+ end
34
+
35
+ step(:step_1).group.should == group(:group)
36
+ end
37
+
38
+ end
39
+
40
+ describe "on run_steps" do
41
+ it "should run the given steps" do
42
+ step(:step) { 1 + 1}
43
+
44
+ run_steps
45
+
46
+ step(:step).result.should == 2
47
+ end
48
+
49
+ it "should run the steps of the given group" do
50
+ x = 0
51
+
52
+ group(:group) do
53
+ step(:step) { x += 1 }
54
+ end
55
+
56
+ step(:step_2) { x += 1 }
57
+
58
+ run_steps({:group => :group})
59
+
60
+ x.should == 1
61
+ end
62
+
63
+ it "should run the steps up to given step" do
64
+ x = 0
65
+ step(:step_1) { x += 1 }
66
+ step(:step_2) { x += 1 }
67
+ step(:step_3) { x += 1 }
68
+
69
+ run_steps :upto => :step_2
70
+
71
+ x.should == 2
72
+ end
73
+ end
74
+
75
+ describe "callbacks" do
76
+
77
+ it "on_step_failure should execute the given block if the step fails" do
78
+ step(:step_1){ }.validate{false}
79
+
80
+ x = 0
81
+ on_step_failure{x += 1}
82
+
83
+ run_steps
84
+
85
+ x.should == 1
86
+ end
87
+
88
+ it "should execute the given block before each step" do
89
+
90
+ step(:step_1){ }
91
+ step(:step_2){ }
92
+
93
+ x = 0
94
+ before_each_step {x += 1}
95
+
96
+ run_steps
97
+
98
+ x.should == 2
99
+ end
100
+
101
+ it "should execute the given block before each step" do
102
+
103
+ step(:step_1){ }
104
+ step(:step_2){ }
105
+
106
+ x = 0
107
+ after_each_step {x += 1}
108
+
109
+ run_steps
110
+
111
+ x.should == 2
112
+ end
113
+
114
+ end
115
+
116
+ it "should return the status on run_status" do
117
+ step(:step_1){ }
118
+ step(:step_2){ }
119
+
120
+ run_steps
121
+
122
+ run_status.should == :success
123
+ end
124
+
125
+ it "should return the failed step" do
126
+ step(:step_1){ }
127
+ step(:step_2){ }.validate {false}
128
+
129
+ run_steps
130
+
131
+ failed_step.should == step(:step_2)
132
+ end
133
+ end
data/spec/step_spec.rb ADDED
@@ -0,0 +1,215 @@
1
+ require "spec_helper"
2
+
3
+ describe Step do
4
+
5
+ before :each do
6
+ @step = Step.new(:name)
7
+ @step.block = proc { 1 + 1 }
8
+ end
9
+
10
+ describe "Validate" do
11
+
12
+ it "validate should assign a block validation to the step" do
13
+
14
+ block = proc {|step| step.result = 2}
15
+
16
+ @step.validate(&block)
17
+ @step.validation.should == block
18
+ end
19
+
20
+ it "validate should return the step" do
21
+ block = proc {|step| step.result = 2}
22
+
23
+ @step.validate(&block).should == @step
24
+ end
25
+
26
+ it "success should return the step" do
27
+ block = proc {|step| step.result = 2}
28
+
29
+ @step.validate(&block).should == @step
30
+ end
31
+
32
+ it "should assign a value to validate the result of the step" do
33
+ @step.validate(1)
34
+ @step.validation.should == 1
35
+ end
36
+
37
+ it "should permit add errors on step in array format" do
38
+ @step.errors.push('error')
39
+ @step.errors.push('error 2')
40
+ expect(@step.errors).to have(2).items
41
+ expect(@step.errors).to be_a(Array)
42
+ end
43
+
44
+ it "should permit add errors on step in hash format" do
45
+ @step.errors({})[:step] = 'step error'
46
+ expect(@step.errors).to have(1).items
47
+ expect(@step.errors).to be_a(Hash)
48
+ end
49
+
50
+ it "should permit add errors on step in string format" do
51
+ @step.errors("") << 'step error'
52
+ expect(@step.errors).to eql('step error')
53
+ expect(@step.errors).to be_a(String)
54
+ end
55
+
56
+ it "should permit reset with formats: String, Array and Hash" do
57
+ @step.errors("") << 'step error'
58
+ expect(@step.errors).to be_a(String)
59
+ @step.errors([]) << 'step error'
60
+ expect(@step.errors).to be_a(Array)
61
+ @step.errors({})[:step_n] = 'error'
62
+ expect(@step.errors).to be_a(Hash)
63
+ end
64
+
65
+ it "should not clear errors when initialized with same variable format" do
66
+ @step.errors("") << 'step '
67
+ @step.errors("") << 'has '
68
+ @step.errors << 'error'
69
+
70
+ expect(@step.errors).to eql('step has error')
71
+ expect(@step.errors).to be_a(String)
72
+ end
73
+
74
+ it "should clear errors when initialized with diferent varialbe format" do
75
+ @step.errors([]) << 'step '
76
+ @step.errors("") << 'has '
77
+ @step.errors << 'error'
78
+
79
+ expect(@step.errors).to eql('has error')
80
+ expect(@step.errors).to be_a(String)
81
+ end
82
+
83
+
84
+ end
85
+
86
+ describe "next step" do
87
+ it "should assign a block to the next_step" do
88
+ block = proc {|step| }
89
+ @step.next_step(&block)
90
+ @step.next_step.should == block
91
+ end
92
+ end
93
+
94
+ describe "condition" do
95
+ it "should assign a condition block" do
96
+ block = proc {}
97
+ @step.condition(&block).should == @step
98
+ @step.condition_block.should == block
99
+ end
100
+ end
101
+
102
+ describe "next" do
103
+
104
+ it "should return the next step" do
105
+ next_step = Step.new(:step_2)
106
+
107
+ @step.next_step = next_step
108
+ @step.next.should == next_step
109
+ end
110
+
111
+ it "should return the next step evaluating the block" do
112
+ next_step = Step.new(:step_2)
113
+
114
+ @step.next_step do |s|
115
+ s.should == @step
116
+ end
117
+
118
+ @step.next
119
+ end
120
+
121
+ it "should return the next step evaluating the block" do
122
+ next_step = Step.new(:step_2)
123
+
124
+ @step.next_step { next_step }
125
+ @step.next.should == next_step
126
+ end
127
+
128
+ end
129
+
130
+ describe "on perform" do
131
+
132
+ it "should execute the block and store the result" do
133
+ @step.perform
134
+ @step.result.should == 2
135
+ end
136
+
137
+ it "should pass the step to the block" do
138
+ @step.block = proc do |step|
139
+ step.should == @step
140
+ end
141
+
142
+ @step.perform
143
+ end
144
+
145
+ it "should evaluate the validation block" do
146
+ @step.validate {|step| step.result == 1} # invalid
147
+ @step.perform.should be_false
148
+
149
+ @step.validate {|step| step.result == 2} # valid
150
+ @step.perform.should be_true
151
+ end
152
+
153
+ it "should evaluate the validation value" do
154
+ @step.validate(1) # invalid
155
+ @step.perform.should be_false
156
+
157
+ @step.validate(2) # valid
158
+ @step.perform.should be_true
159
+ end
160
+
161
+ it "should evaluate the validation regex" do
162
+ @step.block = proc { "validation" }
163
+ @step.validate(/^x/) # invalid
164
+ @step.perform.should be_false
165
+
166
+ @step.validate(/n$/) # valid
167
+ @step.perform.should be_true
168
+ end
169
+
170
+ it "should store the exception on step when raised" do
171
+ @step.block = proc { raise ArgumentError }
172
+ @step.perform.should be_false
173
+ @step.exception.class.should == ArgumentError
174
+ end
175
+
176
+ it "should execute a success block" do
177
+ x = 0
178
+ @step.success {|step| x += 1 }
179
+ x.should == 0
180
+ @step.perform
181
+ x.should == 1
182
+ end
183
+
184
+ it "should return true if step was executed" do
185
+ step_a = Step.new(:step_a)
186
+ step_a.block = proc { "step_a" }
187
+ step_a.should_not be_performed
188
+ step_a.perform
189
+ step_a.should be_performed
190
+ end
191
+
192
+ it "should evaluate the condition block to execute the step" do
193
+ @step.condition { true }
194
+ @step.perform
195
+ @step.should be_performed
196
+ end
197
+
198
+ it "should not execute the step if the condition block is false" do
199
+ @step.condition { false }
200
+ @step.perform.should be_true
201
+ @step.should_not be_performed
202
+ end
203
+
204
+ it "should not execute the step if the group condition block is false" do
205
+ group = Group.new(:group_1)
206
+ group.condition { false }
207
+ @step.group = group
208
+ expect(@step.perform).to be_true
209
+ expect(@step).not_to be_performed
210
+ end
211
+
212
+
213
+ end
214
+
215
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'step_machine/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "step_machine"
8
+ spec.version = StepMachine::VERSION
9
+ spec.authors = ["Rafael Vettori"]
10
+ spec.email = ["rafael.vettori@gmail.com"]
11
+ spec.description = %q{When you want execute lazzy block commands, you need use this gem}
12
+ spec.summary = %q{Gem to standardize the of any block execution}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
data/usage.rb ADDED
@@ -0,0 +1,101 @@
1
+ class Hdi
2
+ include StepMachine
3
+
4
+ attr_accessor :agent
5
+
6
+ def initialize
7
+ self.agent = Mechanize.new
8
+ define_steps
9
+ end
10
+
11
+ def define_steps
12
+
13
+ group :login do
14
+ step :acessaar_pagina
15
+ step :sucursal
16
+ end
17
+
18
+ step :dados_segurado do
19
+ agent.post("", "")
20
+ end.validate { |step| step.result.body.match(/cod_cotacao/) }
21
+
22
+ step :pessoa_fisica do
23
+ agent.post("", "")
24
+ end.validate { |step| step.result.code == '200' }
25
+
26
+ step :pessoa_juridica do
27
+ agent.post("", "")
28
+ end.validate { |step| step.result.code == '200' }
29
+
30
+ step[:dados_segurado].next_step = @cotacao.cliente.pessoa == 'F' ? steps[:pessoa_fisica] : steps[:pessoa_juridica]
31
+
32
+ step :pesquisar_veiculo do
33
+ veiculos = agent.post("", "").scan("")
34
+ end.validate { |step| step.result.code == '200' }.validate(/ul li/).next_step do |step|
35
+ return :selecionar_veiculo if step.result.search('ul li')
36
+ return :calcular
37
+ end
38
+
39
+ step :selecionar_veiculo do
40
+ veiculo = aproximado(veiculos, @cotacao.veiculo.descricao)
41
+ agent.post("", "")
42
+ end
43
+
44
+ step :calcular do
45
+ agent.post("", "")
46
+ end
47
+
48
+ step[:calcular].validate do |step|
49
+ raise ValidacaoSeguradoraError unless step.result.search("#cod_cotacao")
50
+ end
51
+
52
+ step[:calcular].success do |step|
53
+ preco.cod_cotacao = step.result.search("#cod_cotacao")
54
+ end
55
+
56
+ step[:pesquisar_veiculo].next_step do |step|
57
+ return step[:selecionar_veiculo] if step.result.search('ul li')
58
+ return step[:calcular]
59
+ end
60
+
61
+ on_step_failure :except => {:dados_segurado, :pesquisar_veiculo} do |step|
62
+ step.exception
63
+ step.result
64
+ mail
65
+ end
66
+
67
+ on_step_failure :only => {:dados_segurado, :pesquisar_veiculo} do |step|
68
+ go_to_step step.name
69
+ repeat_step if condition
70
+ go_to_step :calcular if condition
71
+ go_to_next_step if condition
72
+ go_to_step(:calcular).and_stop if condition
73
+ restart
74
+ end
75
+
76
+ before_each_step do
77
+ end
78
+
79
+ after_each_step do
80
+ end
81
+
82
+ run_steps
83
+ end
84
+ end
85
+
86
+ describe Hdi do
87
+
88
+ before :each do
89
+ @hdi.on_step_failure do |step|
90
+ p step
91
+ end
92
+ end
93
+
94
+ it do
95
+
96
+ @hdi.cotar
97
+ @hdi.step_result.should be_success
98
+
99
+ end
100
+
101
+ end