Class: OpenC3::Suite

Inherits:
Object show all
Defined in:
lib/openc3/script/suite.rb

Overview

Base class for Script Runner suites. OpenC3 Suites inherit from Suite and can implement setup and teardown methods. Script groups are added via add_group(Group) and individual scripts added via add_script(Group, script_method).

Direct Known Subclasses

TestSuite, UnassignedSuite

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object

END PUBLIC API



82
83
84
# File 'lib/openc3/script/suite.rb', line 82

def <=>(other)
  self.name <=> other.name
end

#add_group(group_class) ⇒ Object

Add a group to the suite



43
44
45
46
47
48
49
# File 'lib/openc3/script/suite.rb', line 43

def add_group(group_class)
  @scripts ||= {}
  @plans ||= []
  group_class = Object.const_get(group_class.to_s.intern) unless group_class.class == Class
  @scripts[group_class] = group_class.new unless @scripts[group_class]
  @plans << [:GROUP, group_class, nil]
end

#add_group_setup(group_class) ⇒ Object

Add a group setup to the suite



61
62
63
64
65
66
67
# File 'lib/openc3/script/suite.rb', line 61

def add_group_setup(group_class)
  @scripts ||= {}
  @plans ||= []
  group_class = Object.const_get(group_class.to_s.intern) unless group_class.class == Class
  @scripts[group_class] = group_class.new unless @scripts[group_class]
  @plans << [:GROUP_SETUP, group_class, nil]
end

#add_group_teardown(group_class) ⇒ Object

Add a group teardown to the suite



70
71
72
73
74
75
76
# File 'lib/openc3/script/suite.rb', line 70

def add_group_teardown(group_class)
  @scripts ||= {}
  @plans ||= []
  group_class = Object.const_get(group_class.to_s.intern) unless group_class.class == Class
  @scripts[group_class] = group_class.new unless @scripts[group_class]
  @plans << [:GROUP_TEARDOWN, group_class, nil]
end

#add_script(group_class, script) ⇒ Object

Add a script to the suite



52
53
54
55
56
57
58
# File 'lib/openc3/script/suite.rb', line 52

def add_script(group_class, script)
  @scripts ||= {}
  @plans ||= []
  group_class = Object.const_get(group_class.to_s.intern) unless group_class.class == Class
  @scripts[group_class] = group_class.new unless @scripts[group_class]
  @plans << [:SCRIPT, group_class, script]
end

#get_num_scriptsObject

Returns the number of scripts in the suite including setup and teardown methods



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/openc3/script/suite.rb', line 96

def get_num_scripts
  num_scripts = 0
  @plans.each do |type, group_class, _script|
    case type
    when :GROUP
      num_scripts += group_class.get_num_scripts
    when :SCRIPT, :GROUP_SETUP, :GROUP_TEARDOWN
      num_scripts += 1
    end
  end
  num_scripts += 1 if self.class.method_defined?(:setup)
  num_scripts += 1 if self.class.method_defined?(:teardown)
  num_scripts
end

#nameObject

Name of the suite



87
88
89
90
91
92
93
# File 'lib/openc3/script/suite.rb', line 87

def name
  if self.class != Suite
    self.class.to_s.split('::')[-1]
  else
    'UnassignedSuite'
  end
end

#plansObject



32
33
34
# File 'lib/openc3/script/suite.rb', line 32

def plans
  @plans ||= []
end

#runObject

Run all the scripts



112
113
114
115
116
117
118
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
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/openc3/script/suite.rb', line 112

def run(&)
  ScriptResult.suite = name()
  ScriptStatus.instance.total = get_num_scripts()
  results = []

  # Setup the suite
  result = run_setup(true)
  if result
    results << result
    yield result if block_given?
    raise StopScript if result.stopped
  end

  # Run each script
  @plans.each do |type, group_class, script|
    case type
    when :GROUP
      results.concat(run_group(group_class, true, &))
    when :SCRIPT
      result = run_script(group_class, script, true)
      results << result
      yield result if block_given?
      raise StopScript if (result.exceptions and group_class.abort_on_exception) or result.stopped
    when :GROUP_SETUP
      result = run_group_setup(group_class, true)
      if result
        results << result
        yield result if block_given?
        raise StopScript if (result.exceptions and group_class.abort_on_exception) or result.stopped
      end
    when :GROUP_TEARDOWN
      result = run_group_teardown(group_class, true)
      if result
        results << result
        yield result if block_given?
        raise StopScript if (result.exceptions and group_class.abort_on_exception) or result.stopped
      end
    end
  end

  # Teardown the suite
  result = run_teardown(true)
  if result
    results << result
    yield result if block_given?
    raise StopScript if result.stopped
  end

  ScriptResult.suite = nil
  results
end

#run_group(group_class, internal = false) ⇒ Object

Run a specific group



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/openc3/script/suite.rb', line 165

def run_group(group_class, internal = false, &)
  ScriptResult.suite = name() unless internal

  # Determine if this group_class is in the plan and the number of scripts associated with this group_class
  in_plan = false
  num_scripts = 0
  @plans.each do |plan_type, plan_group_class, plan_script|
    if plan_type == :GROUP and group_class == plan_group_class
      in_plan = true
    end
    if (plan_type == :GROUP_SETUP and group_class == plan_group_class) or
       (plan_type == :GROUP_TEARDOWN and group_class == plan_group_class) or
       (plan_script and group_class == plan_group_class)
      num_scripts += 1
    end
  end

  if in_plan
    ScriptStatus.instance.total = group_class.get_num_scripts() unless internal
    results = @scripts[group_class].run(&)
  else
    results = []
    ScriptStatus.instance.total = num_scripts unless internal

    # Run each setup, teardown, or script associated with this group_class in the order
    # defined in the plan
    @plans.each do |plan_type, plan_group_class, plan_script|
      if plan_group_class == group_class
        case plan_type
        when :SCRIPT
          result = run_script(plan_group_class, plan_script, true)
          results << result
          yield result if block_given?
        when :GROUP_SETUP
          result = run_group_setup(plan_group_class, true)
          if result
            results << result
            yield result if block_given?
          end
        when :GROUP_TEARDOWN
          result = run_group_teardown(plan_group_class, true)
          if result
            results << result
            yield result if block_given?
          end
        end
      end
    end
  end

  ScriptResult.suite = nil unless internal
  return results
end

#run_group_setup(group_class, internal = false) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/openc3/script/suite.rb', line 252

def run_group_setup(group_class, internal = false)
  ScriptResult.suite = name() unless internal
  ScriptStatus.instance.total = 1 unless internal
  result = @scripts[group_class].run_setup
  ScriptResult.suite = nil unless internal
  result
end

#run_group_teardown(group_class, internal = false) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/openc3/script/suite.rb', line 260

def run_group_teardown(group_class, internal = false)
  ScriptResult.suite = name() unless internal
  ScriptStatus.instance.total = 1 unless internal
  result = @scripts[group_class].run_teardown
  ScriptResult.suite = nil unless internal
  result
end

#run_script(group_class, script, internal = false) ⇒ Object

Run a specific script



220
221
222
223
224
225
226
# File 'lib/openc3/script/suite.rb', line 220

def run_script(group_class, script, internal = false)
  ScriptResult.suite = name() unless internal
  ScriptStatus.instance.total = 1 unless internal
  result = @scripts[group_class].run_script(script)
  ScriptResult.suite = nil unless internal
  result
end

#run_setup(internal = false) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/openc3/script/suite.rb', line 228

def run_setup(internal = false)
  ScriptResult.suite = name() unless internal
  result = nil
  if self.class.method_defined?(:setup) and @scripts.length > 0
    ScriptStatus.instance.total = 1 unless internal
    ScriptStatus.instance.status = "#{self.class} : setup"
    result = @scripts[@scripts.keys[0]].run_method(self, :setup)
  end
  ScriptResult.suite = nil unless internal
  result
end

#run_teardown(internal = false) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/openc3/script/suite.rb', line 240

def run_teardown(internal = false)
  ScriptResult.suite = name() unless internal
  result = nil
  if self.class.method_defined?(:teardown) and @scripts.length > 0
    ScriptStatus.instance.total = 1 unless internal
    ScriptStatus.instance.status = "#{self.class} : teardown"
    result = @scripts[@scripts.keys[0]].run_method(self, :teardown)
  end
  ScriptResult.suite = nil unless internal
  result
end

#scriptsObject



28
29
30
# File 'lib/openc3/script/suite.rb', line 28

def scripts
  @scripts ||= {}
end