Class: ATP::Processors::Relationship

Inherits:
ATP::Processor show all
Defined in:
lib/atp/processors/relationship.rb

Overview

This processor will apply the relationships between tests, e.g. if testB should only execute if testA passes, then this processor will update the AST to make testA set a flag on pass, and then update testB to only run if that flag is set.

Defined Under Namespace

Classes: ExtractTestResults

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ATP::Processor

#clean_flag, #extract_volatiles, #handler_missing, #process, #process_all, #volatile?, #volatile_flags

Instance Attribute Details

#test_resultsObject (readonly)

Returns a hash containing the IDs of all tests that have dependents



8
9
10
# File 'lib/atp/processors/relationship.rb', line 8

def test_results
  @test_results
end

Instance Method Details

#add_fail_flag(id, node) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/atp/processors/relationship.rb', line 81

def add_fail_flag(id, node)
  node = node.ensure_node_present(:on_fail)
  node.updated(nil, node.children.map do |n|
    if n.type == :on_fail
      n = n.add node.updated(:set_flag, ["#{id}_FAILED", :auto_generated])
      delayed = n.find(:delayed)
      if delayed && delayed.to_a[0]
        n
      else
        n.ensure_node_present(:continue)
      end
    else
      n
    end
  end)
end

#add_pass_flag(id, node) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/atp/processors/relationship.rb', line 62

def add_pass_flag(id, node)
  node = node.ensure_node_present(:on_pass)
  node = node.ensure_node_present(:on_fail)
  node.updated(nil, node.children.map do |n|
    if n.type == :on_pass
      n = n.add node.updated(:set_flag, ["#{id}_PASSED", :auto_generated])
    elsif n.type == :on_fail
      delayed = n.find(:delayed)
      if delayed && delayed.to_a[0]
        n
      else
        n.ensure_node_present(:continue)
      end
    else
      n
    end
  end)
end

#add_ran_flags(id, node) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/atp/processors/relationship.rb', line 98

def add_ran_flags(id, node)
  set_flag = node.updated(:set_flag, ["#{id}_RAN", :auto_generated])
  # For a group, set a flag immediately upon entry to the group to signal that
  # it ran to later tests
  if node.type == :group
    name, id, *nodes = *node
    if id.type == :id
      nodes.unshift(set_flag)
      nodes.unshift(id)
    else
      nodes.unshift(id)
      nodes.unshift(set_flag)
    end
    node.updated(nil, [name] + nodes)
  # For a test, set a flag immediately after the referenced test has executed
  # but don't change its pass/fail handling
  elsif node.type == :test
    node.updated(:inline, [node, set_flag])
  else
    fail "Don't know how to add ran flag to #{node.type}"
  end
end

#id(node) ⇒ Object

Returns the ID of the give test node (if any), caller is responsible for only passing test nodes



184
185
186
187
188
# File 'lib/atp/processors/relationship.rb', line 184

def id(node)
  if n = node.children.find { |c| c.type == :id }
    n.children.first
  end
end

#id_to_flag(id, type) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/atp/processors/relationship.rb', line 190

def id_to_flag(id, type)
  if id.is_a?(Array)
    id.map { |i| "#{i}_#{type}" }
  else
    "#{id}_#{type}"
  end
end

#on_if_all_failed(node) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/atp/processors/relationship.rb', line 142

def on_if_all_failed(node)
  ids, *children = *node
  ids.reverse_each.with_index do |id, i|
    if i == 0
      node = node.updated(:if_flag, [id_to_flag(id, 'FAILED')] + process_all(children))
    else
      node = node.updated(:if_flag, [id_to_flag(id, 'FAILED'), node])
    end
  end
  node
end

#on_if_all_passed(node) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/atp/processors/relationship.rb', line 160

def on_if_all_passed(node)
  ids, *children = *node
  ids.reverse_each.with_index do |id, i|
    if i == 0
      node = node.updated(:if_flag, [id_to_flag(id, 'PASSED')] + process_all(children))
    else
      node = node.updated(:if_flag, [id_to_flag(id, 'PASSED'), node])
    end
  end
  node
end

#on_if_failed(node) ⇒ Object Also known as: on_if_any_failed



136
137
138
139
# File 'lib/atp/processors/relationship.rb', line 136

def on_if_failed(node)
  id, *children = *node
  node.updated(:if_flag, [id_to_flag(id, 'FAILED')] + process_all(children))
end

#on_if_passed(node) ⇒ Object Also known as: on_if_any_passed



154
155
156
157
# File 'lib/atp/processors/relationship.rb', line 154

def on_if_passed(node)
  id, *children = *node
  node.updated(:if_flag, [id_to_flag(id, 'PASSED')] + process_all(children))
end

#on_if_ran(node) ⇒ Object



172
173
174
175
# File 'lib/atp/processors/relationship.rb', line 172

def on_if_ran(node)
  id, *children = *node
  node.updated(:if_flag, [id_to_flag(id, 'RAN')] + process_all(children))
end

#on_test(node) ⇒ Object Also known as: on_group

Set flags depending on the result on tests which have dependents later in the flow



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/atp/processors/relationship.rb', line 123

def on_test(node)
  node = node.updated(nil, process_all(node.children))
  nid = id(node)
  # If this test has a dependent
  if test_results[nid]
    node = add_pass_flag(nid, node) if test_results[nid][:passed]
    node = add_fail_flag(nid, node) if test_results[nid][:failed]
    node = add_ran_flags(nid, node) if test_results[nid][:ran]
  end
  node
end

#on_unless_ran(node) ⇒ Object



177
178
179
180
# File 'lib/atp/processors/relationship.rb', line 177

def on_unless_ran(node)
  id, *children = *node
  node.updated(:unless_flag, [id_to_flag(id, 'RAN')] + process_all(children))
end

#run(node) ⇒ Object



55
56
57
58
59
60
# File 'lib/atp/processors/relationship.rb', line 55

def run(node)
  t = ExtractTestResults.new
  t.process(node)
  @test_results = t.results || {}
  process(node)
end