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



87
88
89
# File 'lib/openc3/script/suite.rb', line 87

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

#add_group(group_class) ⇒ Object

Add a group to the suite



48
49
50
51
52
53
54
# File 'lib/openc3/script/suite.rb', line 48

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



66
67
68
69
70
71
72
# File 'lib/openc3/script/suite.rb', line 66

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



75
76
77
78
79
80
81
# File 'lib/openc3/script/suite.rb', line 75

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



57
58
59
60
61
62
63
# File 'lib/openc3/script/suite.rb', line 57

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/openc3/script/suite.rb', line 101

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



92
93
94
95
96
97
98
# File 'lib/openc3/script/suite.rb', line 92

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

#plansObject



37
38
39
# File 'lib/openc3/script/suite.rb', line 37

def plans
  @plans ||= []
end

#runObject

Run all the scripts



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
163
164
165
166
167
# File 'lib/openc3/script/suite.rb', line 117

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



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
218
219
220
221
222
# File 'lib/openc3/script/suite.rb', line 170

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



257
258
259
260
261
262
263
# File 'lib/openc3/script/suite.rb', line 257

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



265
266
267
268
269
270
271
# File 'lib/openc3/script/suite.rb', line 265

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



225
226
227
228
229
230
231
# File 'lib/openc3/script/suite.rb', line 225

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



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/openc3/script/suite.rb', line 233

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



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/openc3/script/suite.rb', line 245

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



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

def scripts
  @scripts ||= {}
end