Class: Cucumber::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(step_mother) ⇒ Executor

Returns a new instance of Executor.



7
8
9
10
11
12
13
14
15
16
# File 'lib/cucumber/executor.rb', line 7

def initialize(step_mother)
  @world_procs = []
  @before_scenario_procs = []
  @after_scenario_procs = []
  @after_step_procs = []
  @step_mother = step_mother

  @executed_scenarios = {}
  @regular_scenario_cache = {}
end

Instance Attribute Details

#failedObject (readonly)

Returns the value of attribute failed.



3
4
5
# File 'lib/cucumber/executor.rb', line 3

def failed
  @failed
end

#formattersObject

Returns the value of attribute formatters.



4
5
6
# File 'lib/cucumber/executor.rb', line 4

def formatters
  @formatters
end

#lines_for_features=(value) ⇒ Object (writeonly)

Sets the attribute lines_for_features

Parameters:

  • value

    the value to set the attribute lines_for_features to.



5
6
7
# File 'lib/cucumber/executor.rb', line 5

def lines_for_features=(value)
  @lines_for_features = value
end

#scenario_names=(value) ⇒ Object (writeonly)

Sets the attribute scenario_names

Parameters:

  • value

    the value to set the attribute scenario_names to.



5
6
7
# File 'lib/cucumber/executor.rb', line 5

def scenario_names=(value)
  @scenario_names = value
end

Instance Method Details

#accept_feature?(feature) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/cucumber/executor.rb', line 95

def accept_feature?(feature)
  feature.scenarios.any? { |s| accept_scenario?(s) }
end

#accept_scenario?(scenario) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/cucumber/executor.rb', line 90

def accept_scenario?(scenario)
  scenario_at_specified_line?(scenario) &&
  scenario_has_specified_name?(scenario)
end

#create_worldObject



180
181
182
183
184
185
186
# File 'lib/cucumber/executor.rb', line 180

def create_world
  world = Object.new
  @world_procs.each do |world_proc|
    world = world_proc.call(world)
  end
  world
end

#define_step_call_methods(world) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cucumber/executor.rb', line 140

def define_step_call_methods(world)
  world.instance_variable_set('@__executor', self)
  world.instance_eval do
    class << self
      def run_step(name)
        _, args, proc = @__executor.instance_variable_get(:@step_mother).regexp_args_proc(name)
        proc.call_in(self, *args)
      end

      %w{given when then and but}.each do |keyword|
        alias_method Cucumber.language[keyword], :run_step
      end
    end
  end
end

#execute_scenario(scenario) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cucumber/executor.rb', line 75

def execute_scenario(scenario)
  @error = nil
  @pending = nil

  @world = create_world
  @world.extend(Spec::Matchers) if defined?(Spec::Matchers)
  define_step_call_methods(@world)

  formatters.scenario_executing(scenario)
  @before_scenario_procs.each{|p| p.call_in(@world, *[])}
  scenario.accept(self)
  @after_scenario_procs.each{|p| p.call_in(@world, *[])}
  formatters.scenario_executed(scenario)
end

#executing_unprepared_row_scenario?(scenario) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/cucumber/executor.rb', line 156

def executing_unprepared_row_scenario?(scenario)
  accept_scenario?(scenario) && !@executed_scenarios[scenario.name]
end

#lines_defined_for_current_feature?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/cucumber/executor.rb', line 176

def lines_defined_for_current_feature?
  @lines_for_features && !@lines_for_features[@feature_file].nil? && !@lines_for_features[@feature_file].empty?
end

#record_pending_step(step, regexp, args) ⇒ Object



135
136
137
138
# File 'lib/cucumber/executor.rb', line 135

def record_pending_step(step, regexp, args)
  @pending = true
  formatters.step_pending(step, regexp, args)
end

#register_after_scenario_proc(&proc) ⇒ Object



27
28
29
30
# File 'lib/cucumber/executor.rb', line 27

def register_after_scenario_proc(&proc)
  proc.extend(CoreExt::CallIn)
  @after_scenario_procs << proc
end

#register_after_step_proc(&proc) ⇒ Object



32
33
34
35
# File 'lib/cucumber/executor.rb', line 32

def register_after_step_proc(&proc)
  proc.extend(CoreExt::CallIn)
  @after_step_procs << proc
end

#register_before_scenario_proc(&proc) ⇒ Object



22
23
24
25
# File 'lib/cucumber/executor.rb', line 22

def register_before_scenario_proc(&proc)
  proc.extend(CoreExt::CallIn)
  @before_scenario_procs << proc
end

#register_world_proc(&proc) ⇒ Object



18
19
20
# File 'lib/cucumber/executor.rb', line 18

def register_world_proc(&proc)
  @world_procs << proc
end

#scenario_at_specified_line?(scenario) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
# File 'lib/cucumber/executor.rb', line 160

def scenario_at_specified_line?(scenario)
  if lines_defined_for_current_feature?
    @lines_for_features[@feature_file].inject(false) { |at_line, line| at_line || scenario.at_line?(line) }
  else
    true
  end
end

#scenario_has_specified_name?(scenario) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
# File 'lib/cucumber/executor.rb', line 168

def scenario_has_specified_name?(scenario)
  if @scenario_names && !@scenario_names.empty?
    @scenario_names.include?(scenario.name)
  else
    true
  end
end

#visit_feature(feature) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/cucumber/executor.rb', line 43

def visit_feature(feature)
  @feature_file = feature.file
        
  if accept_feature?(feature)
    formatters.feature_executing(feature)
    feature.accept(self)
    @executed_scenarios = {}
    @regular_scenario_cache = {}
  end
end

#visit_features(features) ⇒ Object



37
38
39
40
41
# File 'lib/cucumber/executor.rb', line 37

def visit_features(features)
  formatters.visit_features(features)
  features.accept(self)
  formatters.dump
end

#visit_header(header) ⇒ Object



54
55
56
# File 'lib/cucumber/executor.rb', line 54

def visit_header(header)
  formatters.header_executing(header)
end

#visit_regular_scenario(scenario) ⇒ Object



63
64
65
66
# File 'lib/cucumber/executor.rb', line 63

def visit_regular_scenario(scenario)
  @regular_scenario_cache[scenario.name] = scenario
  visit_scenario(scenario)
end

#visit_regular_step(step) ⇒ Object



103
104
105
# File 'lib/cucumber/executor.rb', line 103

def visit_regular_step(step)
  visit_step(step)
end

#visit_row_scenario(scenario) ⇒ Object



58
59
60
61
# File 'lib/cucumber/executor.rb', line 58

def visit_row_scenario(scenario)
  execute_scenario(@regular_scenario_cache[scenario.name]) if executing_unprepared_row_scenario?(scenario)
  visit_scenario(scenario)
end

#visit_row_step(step) ⇒ Object



99
100
101
# File 'lib/cucumber/executor.rb', line 99

def visit_row_step(step)
  visit_step(step)
end

#visit_scenario(scenario) ⇒ Object



68
69
70
71
72
73
# File 'lib/cucumber/executor.rb', line 68

def visit_scenario(scenario)
  if accept_scenario?(scenario)
    @executed_scenarios[scenario.name] = true
    execute_scenario(scenario)
  end
end

#visit_step(step) ⇒ Object



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
# File 'lib/cucumber/executor.rb', line 107

def visit_step(step)
  unless @pending || @error
    begin
      regexp, args, proc = step.regexp_args_proc(@step_mother)
      formatters.step_executing(step, regexp, args)
      step.execute_in(@world, regexp, args, proc)
      @after_step_procs.each{|p| p.call_in(@world, *[])}
      formatters.step_passed(step, regexp, args)
    rescue Pending
      record_pending_step(step, regexp, args)
    rescue => e
      @failed = true
      @error = step.error = e
      formatters.step_failed(step, regexp, args)
    end
  else
    begin
      regexp, args, proc = step.regexp_args_proc(@step_mother)
      step.execute_in(@world, regexp, args, proc)
      formatters.step_skipped(step, regexp, args)
    rescue Pending
      record_pending_step(step, regexp, args)
    rescue Exception
      formatters.step_skipped(step, regexp, args)
    end
  end
end