Class: ATP::Runner

Inherits:
Processor show all
Defined in:
lib/atp/runner.rb

Overview

This class is responsible for executing the given test flow based on a given set of runtime conditions. A subset of the input AST will be returned containing only the nodes that would be hit when the flow is executed under the given conditions.

Instance Method Summary collapse

Methods inherited from Processor

#handler_missing, #n, #n0, #n1, #process

Instance Method Details

#clean_job(job) ⇒ Object



188
189
190
# File 'lib/atp/runner.rb', line 188

def clean_job(job)
  [job].flatten.map { |j| j.to_s.upcase }
end

#completed?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/atp/runner.rb', line 209

def completed?
  @completed
end

#containerObject



220
221
222
# File 'lib/atp/runner.rb', line 220

def container
  @containers.last
end

#failed_test_idsObject



196
197
198
# File 'lib/atp/runner.rb', line 196

def failed_test_ids
  @failed_test_ids ||= [@options[:failed_test_id] || @options[:failed_test_ids]].flatten.compact
end

#flow_flagsObject

Returns an array of enabled flow flags



205
206
207
# File 'lib/atp/runner.rb', line 205

def flow_flags
  @flow_flags ||= [@options[:flow_flag] || @options[:flow_flags]].flatten.compact
end

#jobObject



192
193
194
# File 'lib/atp/runner.rb', line 192

def job
  @options[:job].to_s.upcase if @options[:job]
end

#on_disable_flow_flag(node) ⇒ Object



166
167
168
# File 'lib/atp/runner.rb', line 166

def on_disable_flow_flag(node)
  flow_flags.delete(node.value)
end

#on_enable_flow_flag(node) ⇒ Object



162
163
164
# File 'lib/atp/runner.rb', line 162

def on_enable_flow_flag(node)
  flow_flags << node.value unless flow_flags.include?(node.value)
end

#on_flow(node) ⇒ Object



23
24
25
26
27
28
# File 'lib/atp/runner.rb', line 23

def on_flow(node)
  c = open_container do
    process_all(node.children)
  end
  node.updated(nil, c)
end

#on_flow_flag(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/atp/runner.rb', line 34

def on_flow_flag(node)
  if @options[:evaluate_flow_flags]
    flag, enabled, *nodes = *node
    flag = [flag].flatten
    active = flag.any? { |f| flow_flags.include?(f) }
    if (enabled && active) || (!enabled && !active)
      process_all(nodes)
    end
  else
    c = open_container do
      process_all(node.children)
    end
    container << node.updated(nil, node.children.take(2) + c)
  end
end

#on_group(node) ⇒ Object



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
# File 'lib/atp/runner.rb', line 119

def on_group(node)
  on_fail = node.find(:on_fail)
  on_pass = node.find(:on_pass)
  c = open_container do
    @groups << true  # This will be set to false by any tests that fail within the group
    @groups_on_fail << on_fail
    @groups_on_pass << on_pass
    if on_fail
      orig = @continue
      @continue = !!on_fail.find(:continue)
      process_all(node.children - [on_fail, on_pass])
      @continue = orig
    else
      process_all(node.children - [on_fail, on_pass])
    end
    if !@groups.pop # If failed
      if on_fail
        @continue = !!on_fail.find(:continue)
        process_all(on_fail)
        @continue = false
      end
    else
      if on_pass
        process_all(on_pass)
      end
    end
    @groups_on_fail.pop
    @groups_on_pass.pop
  end
  container << node.updated(nil, c)
end

#on_job(node) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/atp/runner.rb', line 175

def on_job(node)
  jobs, state, *nodes = *node
  jobs = clean_job(jobs)
  unless job
    fail 'Flow contains JOB-based conditions and no current JOB has been given!'
  end
  if state
    process_all(node) if jobs.include?(job)
  else
    process_all(node) unless jobs.include?(job)
  end
end

#on_log(node) ⇒ Object Also known as: on_render



170
171
172
# File 'lib/atp/runner.rb', line 170

def on_log(node)
  container << node unless completed?
end

#on_name(node) ⇒ Object



30
31
32
# File 'lib/atp/runner.rb', line 30

def on_name(node)
  container << node
end

#on_run_flag(node) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/atp/runner.rb', line 50

def on_run_flag(node)
  if @options[:evaluate_run_flags]
    flag, enabled, *nodes = *node
    flag = [flag].flatten
    active = flag.any? { |f| run_flags.include?(f) }
    if (enabled && active) || (!enabled && !active)
      process_all(nodes)
    end
  else
    c = open_container do
      process_all(node.children)
    end
    container << node.updated(nil, node.children.take(2) + c)
  end
end

#on_set_result(node) ⇒ Object



151
152
153
154
155
156
# File 'lib/atp/runner.rb', line 151

def on_set_result(node)
  unless @continue
    container << node unless completed?
    @completed = true if @options[:evaluate_set_result]
  end
end

#on_set_run_flag(node) ⇒ Object



158
159
160
# File 'lib/atp/runner.rb', line 158

def on_set_run_flag(node)
  run_flags << node.to_a[0]
end

#on_test(node) ⇒ Object



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
# File 'lib/atp/runner.rb', line 76

def on_test(node)
  if id = node.find(:id)
    id = id.to_a[0]
    if failed_test_ids.include?(id)
      node = node.add(n0(:failed))
      failed = true
      if n_on_fail = node.find(:on_fail)
        node = node.remove(n_on_fail)
      end
    end
  end
  unless failed
    if n_on_pass = node.find(:on_pass)
      node = node.remove(n_on_pass)
    end
  end

  unless completed?
    container << node
    process_all(n_on_fail) if n_on_fail
    process_all(n_on_pass) if n_on_pass
  end

  if failed
    # Give indication to the parent group that at least one test within it failed
    if @groups.last
      @groups.pop
      @groups << false
    end
    if n = node.find(:on_fail)
      # If it has been set by a parent group, don't clear it
      orig = @continue
      @continue ||= !!n.find(:continue)
      process_all(n)
      @continue = orig
    end
  else
    if n = node.find(:on_pass)
      process_all(n)
    end
  end
end

#on_test_result(node) ⇒ Object

Not sure why this method is here, all test_result nodes should have been converted to run_flag nodes by now



68
69
70
71
72
73
74
# File 'lib/atp/runner.rb', line 68

def on_test_result(node)
  id, passed, *nodes = *node
  if (passed && !failed_test_ids.include?(id)) ||
     (!passed && failed_test_ids.include?(id))
    process_all(nodes)
  end
end

#open_container(c = []) ⇒ Object



213
214
215
216
217
218
# File 'lib/atp/runner.rb', line 213

def open_container(c = [])
  @containers ||= []
  @containers << c
  yield
  @containers.pop
end

#run(node, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/atp/runner.rb', line 7

def run(node, options = {})
  options = {
    evaluate_flow_flags: true,
    evaluate_run_flags:  true,
    evaluate_set_result: true
  }.merge(options)
  @options = options
  @completed = false
  @groups = []
  @groups_on_fail = []
  @groups_on_pass = []
  node = Processors::AddIDs.new.run(node)
  node = Processors::AddSetResult.new.run(node)
  process(node)
end

#run_flagsObject



200
201
202
# File 'lib/atp/runner.rb', line 200

def run_flags
  @run_flags ||= []
end