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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSuite

Create a new Suite



41
42
43
44
# File 'lib/openc3/script/suite.rb', line 41

def initialize
  @scripts = {}
  @plans = []
end

Instance Attribute Details

#plansObject (readonly)

Returns the value of attribute plans.



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

def plans
  @plans
end

#scriptsObject (readonly)

Returns the value of attribute scripts.



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

def scripts
  @scripts
end

Instance Method Details

#<=>(other_suite) ⇒ Object

END PUBLIC API



78
79
80
# File 'lib/openc3/script/suite.rb', line 78

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

#add_group(group_class) ⇒ Object

Add a group to the suite



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

def add_group(group_class)
  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
# File 'lib/openc3/script/suite.rb', line 61

def add_group_setup(group_class)
  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



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

def add_group_teardown(group_class)
  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



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

def add_script(group_class, script)
  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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/openc3/script/suite.rb', line 92

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



83
84
85
86
87
88
89
# File 'lib/openc3/script/suite.rb', line 83

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

#run(&block) ⇒ Object

Run all the scripts



108
109
110
111
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
# File 'lib/openc3/script/suite.rb', line 108

def run(&block)
  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, &block))
    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, &block) ⇒ Object

Run a specific group



161
162
163
164
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
# File 'lib/openc3/script/suite.rb', line 161

def run_group(group_class, internal = false, &block)
  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(&block)
  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



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

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



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

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



216
217
218
219
220
221
222
# File 'lib/openc3/script/suite.rb', line 216

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



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

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



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/openc3/script/suite.rb', line 236

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