Class: ATP::Flow

Inherits:
Object
  • Object
show all
Defined in:
lib/atp/flow.rb

Overview

Implements the main user API for building and interacting with an abstract test program

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program, name = nil, options = {}) ⇒ Flow

Returns a new instance of Flow.



10
11
12
13
14
15
16
# File 'lib/atp/flow.rb', line 10

def initialize(program, name = nil, options = {})
  name, options = nil, name if name.is_a?(Hash)
  extract_meta!(options)
  @program = program
  @name = name
  @raw = builder.flow(name)
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/atp/flow.rb', line 5

def name
  @name
end

#programObject (readonly)

Returns the value of attribute program.



5
6
7
# File 'lib/atp/flow.rb', line 5

def program
  @program
end

#rawObject

Returns the raw AST



7
8
9
# File 'lib/atp/flow.rb', line 7

def raw
  @raw
end

Instance Method Details

#astObject

Returns a processed/optimized AST, this is the one that should be used to build and represent the given test flow



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/atp/flow.rb', line 30

def ast
  ast = Processors::PreCleaner.new.process(raw)
  # File.open("a1.txt", "w") { |f| f.write(ast) }
  ast = Processors::FlowID.new.run(ast, id) if id
  # File.open("a2.txt", "w") { |f| f.write(ast) }
  Validators::DuplicateIDs.new(self).process(ast)
  Validators::MissingIDs.new(self).process(ast)
  ast = Processors::Condition.new.process(ast)
  ast = Processors::Relationship.new.process(ast)
  ast = Processors::PostCleaner.new.process(ast)
  Validators::Jobs.new(self).process(ast)
  ast
end

#bin(number, options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/atp/flow.rb', line 99

def bin(number, options = {})
  extract_meta!(options)
  t = apply_open_conditions(options) do |options|
    fail 'A :type option set to :pass or :fail is required when calling bin' unless options[:type]
    options[:bin] = number
    options[:softbin] ||= options[:soft_bin] || options[:sbin]
    builder.set_result(options[:type], options)
  end
  append(t)
end

#contextObject



176
177
178
# File 'lib/atp/flow.rb', line 176

def context
  builder.context
end

#context_changed?(options) ⇒ Boolean

Returns true if the test context generated from the supplied options + existing condition wrappers, is different from that which was applied to the previous test.

Returns:

  • (Boolean)


170
171
172
173
174
# File 'lib/atp/flow.rb', line 170

def context_changed?(options)
  a = context
  b = build_context(options)
  !context_equal?(a, b)
end

#context_equal?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
190
# File 'lib/atp/flow.rb', line 180

def context_equal?(a, b)
  if a.size == b.size
    a = clean_condition(a[:conditions])
    b = clean_condition(b[:conditions])
    if a.keys.sort == b.keys.sort
      a.all? do |key, value|
        value.flatten.uniq.sort == b[key].flatten.uniq.sort
      end
    end
  end
end

#cz(instance, cz_setup, options = {}) ⇒ Object Also known as: characterize



110
111
112
113
114
115
116
117
118
# File 'lib/atp/flow.rb', line 110

def cz(instance, cz_setup, options = {})
  extract_meta!(options)
  t = apply_open_conditions(options) do |options|
    conditions = options.delete(:conditions)
    options[:return] = true
    builder.cz(cz_setup, test(instance, options.merge(dont_apply_conditions: true)), conditions: conditions)
  end
  append(t)
end

#disable(var, options = {}) ⇒ Object

Disable a flow control variable



140
141
142
143
144
145
146
# File 'lib/atp/flow.rb', line 140

def disable(var, options = {})
  extract_meta!(options)
  t = apply_open_conditions(options) do |options|
    builder.disable_flow_flag(var, options)
  end
  append(t)
end

#enable(var, options = {}) ⇒ Object

Enable a flow control variable



131
132
133
134
135
136
137
# File 'lib/atp/flow.rb', line 131

def enable(var, options = {})
  extract_meta!(options)
  t = apply_open_conditions(options) do |options|
    builder.enable_flow_flag(var, options)
  end
  append(t)
end

#group(name, options = {}) ⇒ Object

Group all tests generated within the given block

Examples:

flow.group "RAM Tests" do
  flow.test ...
  flow.test ...
end


51
52
53
54
55
56
# File 'lib/atp/flow.rb', line 51

def group(name, options = {})
  open_groups.push([])
  yield
  extract_meta!(options)
  append builder.group(name, open_groups.pop, options)
end

#log(message, options = {}) ⇒ Object

Append a log message line to the flow



122
123
124
125
126
127
128
# File 'lib/atp/flow.rb', line 122

def log(message, options = {})
  extract_meta!(options)
  t = apply_open_conditions(options) do |options|
    builder.log(message, options)
  end
  append(t)
end

#marshal_dumpObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/atp/flow.rb', line 19

def marshal_dump
  [@name, @program, Processors::Marshal.new.process(@raw)]
end

#marshal_load(array) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



24
25
26
# File 'lib/atp/flow.rb', line 24

def marshal_load(array)
  @name, @program, @raw = array
end

#render(str, options = {}) ⇒ Object

Insert explicitly rendered content in to the flow



149
150
151
152
# File 'lib/atp/flow.rb', line 149

def render(str, options = {})
  extract_meta!(options)
  append builder.render(str)
end

#run(options = {}) ⇒ Object

Execute the given flow in the console



163
164
165
166
# File 'lib/atp/flow.rb', line 163

def run(options = {})
  Formatters::Datalog.run_and_format(ast, options)
  nil
end

#test(instance, options = {}) ⇒ Object

Add a test line to the flow

Parameters:

  • the (String, Symbol)

    name of the test

  • options (Hash) (defaults to: {})

    a hash to describe the test’s attributes

Options Hash (options):

  • :id (Symbol)

    A unique test ID

  • :description (String)

    A description of what the test does, usually formatted in markdown

  • :on_fail (Hash)

    What action to take if the test fails, e.g. assign a bin

  • :on_pass (Hash)

    What action to take if the test passes

  • :conditions (Hash)

    What conditions must be met to execute the test



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/atp/flow.rb', line 67

def test(instance, options = {})
  extract_meta!(options)
  r = options.delete(:return)
  t = apply_open_conditions(options) do |options|
    # Allows any continue, bin, or soft bin argument passed in at the options top-level to be assumed
    # to be the action to take if the test fails
    if b = options.delete(:bin)
      options[:on_fail] ||= {}
      options[:on_fail][:bin] = b
    end
    if b = options.delete(:softbin) || b = options.delete(:sbin) || b = options.delete(:soft_bin)
      options[:on_fail] ||= {}
      options[:on_fail][:softbin] = b
    end
    if options.delete(:continue)
      options[:on_fail] ||= {}
      options[:on_fail][:continue] = true
    end
    if f = options.delete(:flag_pass)
      options[:on_pass] ||= {}
      options[:on_pass][:set_run_flag] = f
    end
    if f = options.delete(:flag_fail)
      options[:on_fail] ||= {}
      options[:on_fail][:set_run_flag] = f
    end
    builder.test(instance, options)
  end
  append(t) unless r
  t
end

#with_condition(options) ⇒ Object Also known as: with_conditions



154
155
156
157
158
159
# File 'lib/atp/flow.rb', line 154

def with_condition(options)
  extract_meta!(options)
  open_conditions.push(options)
  yield
  open_conditions.pop
end