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

#handler_missing, #n, #n0, #n1, #run

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_executed_flag(id, node) ⇒ Object



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

def add_executed_flag(id, node)
  node = node.ensure_node_present(:on_fail)
  node = node.ensure_node_present(:on_pass)
  node.updated(nil, node.children.map do |n|
    if n.type == :on_pass || n.type == :on_fail
      n = n.add n1(:set_run_flag, "#{id}_RAN")
    else
      n
    end
  end)
end

#add_fail_flag(id, node) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/atp/processors/relationship.rb', line 73

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 n1(:set_run_flag, "#{id}_FAILED")
      n.ensure_node_present(:continue)
    else
      n
    end
  end)
end

#add_pass_flag(id, node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/atp/processors/relationship.rb', line 59

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 n1(:set_run_flag, "#{id}_PASSED")
    elsif n.type == :on_fail
      n.ensure_node_present(:continue)
    else
      n
    end
  end)
end

#id(node) ⇒ Object

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



139
140
141
142
143
# File 'lib/atp/processors/relationship.rb', line 139

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

#id_to_flag(id, type) ⇒ Object



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

def id_to_flag(id, type)
  if id.is_a?(Array)
    id.map { |i| "#{i}_#{type}" }
  else
    "#{id}_#{type}"
  end
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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/atp/processors/relationship.rb', line 99

def on_test(node)
  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_executed_flag(nid, node) if test_results[nid][:executed]
  end
  if node.type == :group
    node.updated(nil, process_all(node))
  else
    node
  end
end

#on_test_executed(node) ⇒ Object

Remove test_result nodes and replace with references to the flags set up stream by the parent node



130
131
132
133
134
135
# File 'lib/atp/processors/relationship.rb', line 130

def on_test_executed(node)
  children = node.children.dup
  id = children.shift
  state = children.shift
  n(:run_flag, [id_to_flag(id, 'RAN'), state] + children)
end

#on_test_result(node) ⇒ Object

Remove test_result nodes and replace with references to the flags set up stream by the parent node



117
118
119
120
121
122
123
124
125
126
# File 'lib/atp/processors/relationship.rb', line 117

def on_test_result(node)
  children = node.children.dup
  id = children.shift
  state = children.shift
  if state
    n(:run_flag, [id_to_flag(id, 'PASSED'), true] + process_all(children))
  else
    n(:run_flag, [id_to_flag(id, 'FAILED'), true] + process_all(children))
  end
end

#process(node) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/atp/processors/relationship.rb', line 43

def process(node)
  # On first call extract the test_result nodes from the given AST,
  # then process as normal thereafter
  if @first_call_done
    result = super
  else
    @first_call_done = true
    t = ExtractTestResults.new
    t.process(node)
    @test_results = t.results || {}
    result = super
    @first_call_done = false
  end
  result
end