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) ⇒ Flow

Returns a new instance of Flow.



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

def initialize(program, name = nil)
  @program = program
  @name = name
  @raw = builder.flow
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



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

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



88
89
90
91
92
93
94
95
96
97
# File 'lib/atp/flow.rb', line 88

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



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

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)


159
160
161
162
163
# File 'lib/atp/flow.rb', line 159

def context_changed?(options)
  a = context[:conditions]
  b = build_context(options)[:conditions]
  !conditions_equal?(a, b)
end

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



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

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), conditions: conditions)
  end
  append(t)
end

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

Disable a flow control variable



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

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



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

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


49
50
51
52
53
# File 'lib/atp/flow.rb', line 49

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

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

Append a log message line to the flow



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

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.



17
18
19
# File 'lib/atp/flow.rb', line 17

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.



22
23
24
# File 'lib/atp/flow.rb', line 22

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

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

Insert explicitly rendered content in to the flow



138
139
140
141
# File 'lib/atp/flow.rb', line 138

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

#run(options = {}) ⇒ Object

Execute the given flow in the console



152
153
154
155
# File 'lib/atp/flow.rb', line 152

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/atp/flow.rb', line 64

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
    builder.test(instance, options)
  end
  append(t) unless r
  t
end

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



143
144
145
146
147
148
# File 'lib/atp/flow.rb', line 143

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