Class: OpenC3::Group

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

Overview

Base class for a group. All OpenC3 Script Runner scripts should inherit Group and then implement scripts methods starting with ‘script_’, ‘test_’, or ‘op_’ e.g. script_mech_open, test_mech_open, op_mech_open.

Direct Known Subclasses

Test

Constant Summary collapse

@@abort_on_exception =
false
@@current_result =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.abort_on_exceptionObject

Explictly avoid creating an initialize method which forces end users to call super()



283
284
285
# File 'lib/openc3/script/suite.rb', line 283

def self.abort_on_exception
  @@abort_on_exception
end

.abort_on_exception=(value) ⇒ Object



287
288
289
# File 'lib/openc3/script/suite.rb', line 287

def self.abort_on_exception=(value)
  @@abort_on_exception = value
end

.current_groupObject



476
477
478
479
480
481
482
# File 'lib/openc3/script/suite.rb', line 476

def self.current_group
  if @@current_result
    @@current_result.group
  else
    nil
  end
end

.current_scriptObject



484
485
486
487
488
489
490
# File 'lib/openc3/script/suite.rb', line 484

def self.current_script
  if @@current_result
    @@current_result.script
  else
    nil
  end
end

.current_suiteObject



468
469
470
471
472
473
474
# File 'lib/openc3/script/suite.rb', line 468

def self.current_suite
  if @@current_result
    @@current_result.suite
  else
    nil
  end
end

.get_num_scriptsObject



451
452
453
454
455
456
457
# File 'lib/openc3/script/suite.rb', line 451

def self.get_num_scripts
  num_scripts = 0
  num_scripts += 1 if self.method_defined?(:setup)
  num_scripts += 1 if self.method_defined?(:teardown)
  num_scripts += self.scripts.length
  num_scripts
end

.puts(string) ⇒ Object



459
460
461
462
463
464
465
466
# File 'lib/openc3/script/suite.rb', line 459

def self.puts(string)
  $stdout.puts string
  if @@current_result
    @@current_result.message ||= ''
    @@current_result.message << string.chomp
    @@current_result.message << "\n"
  end
end

.scriptsObject



291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/openc3/script/suite.rb', line 291

def self.scripts
  # Find all the script methods
  methods = []
  self.instance_methods.each do |method_name|
    if /^test|^script|op_/.match?(method_name.to_s)
      methods << method_name.to_s
    end
  end
  # Sort by name for all found methods
  methods.sort!
  methods
end

Instance Method Details

#nameObject

Name of the script group



305
306
307
308
309
310
311
# File 'lib/openc3/script/suite.rb', line 305

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

#runObject

Run all the scripts



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/openc3/script/suite.rb', line 314

def run
  results = []

  # Setup the script group
  result = run_setup()
  if result
    results << result
    yield result if block_given?
    raise StopScript if (results[-1].exceptions and @@abort_on_exception) or results[-1].stopped
  end

  # Run all the scripts
  self.class.scripts.each do |method_name|
    results << run_script(method_name)
    yield results[-1] if block_given?
    raise StopScript if (results[-1].exceptions and @@abort_on_exception) or results[-1].stopped
  end

  # Teardown the script group
  result = run_teardown()
  if result
    results << result
    yield result if block_given?
    raise StopScript if (results[-1].exceptions and @@abort_on_exception) or results[-1].stopped
  end

  results
end

#run_method(object, method_name) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/openc3/script/suite.rb', line 349

def run_method(object, method_name)
  # Convert to a symbol to use as a method_name
  method_name = method_name.to_s.intern unless method_name.class == Symbol

  result = ScriptResult.new
  @@current_result = result

  # Verify script method exists
  if object.class.method_defined?(method_name)
    @output_io ||= StringIO.new('', 'r+')
    # Capture STDOUT and STDERR
    # $stdout & $stderr must be set to change output
    $stdout = Stdout.instance
    $stderr = Stderr.instance
    $stdout.add_stream(@output_io)
    $stderr.add_stream(@output_io)

    result.group = object.class.to_s.split('::')[-1]
    result.script = method_name.to_s
    begin
      object.public_send(method_name)
      result.result = :PASS

      if RunningScript.instance and RunningScript.instance.exceptions
        result.exceptions = RunningScript.instance.exceptions
        result.result     = :FAIL
        RunningScript.instance.exceptions = nil
      end
    rescue StandardError, SyntaxError => error
      # Check that the error belongs to the StopScript inheritance chain
      if error.class <= StopScript
        result.stopped = true
        result.result  = :STOP
      end
      # Check that the error belongs to the SkipScript inheritance chain
      if error.class <= SkipScript
        result.result  = :SKIP
        result.message ||= ''
        result.message << error.message + "\n"
      else
        if error.class != StopScript and
           (not RunningScript.instance or
             not RunningScript.instance.exceptions or
             not RunningScript.instance.exceptions.include? error)
          result.exceptions ||= []
          result.exceptions << error
          puts "*** Exception in Control Statement:"
          error.formatted.each_line do |line|
            puts '  ' + line
          end
        end
        if RunningScript.instance and RunningScript.instance.exceptions
          result.exceptions ||= []
          result.exceptions.concat(RunningScript.instance.exceptions)
          RunningScript.instance.exceptions = nil
        end
      end

      result.result = :FAIL if result.exceptions
    ensure
      result.output     = @output_io.string
      @output_io.string = ''
      $stdout.remove_stream(@output_io)
      $stderr.remove_stream(@output_io)

      case result.result
      when :FAIL
        ScriptStatus.instance.fail_count += 1
      when :SKIP
        ScriptStatus.instance.skip_count += 1
      when :PASS
        ScriptStatus.instance.pass_count += 1
      end
    end

  else
    @@current_result = nil
    raise "Unknown method #{method_name} for #{object.class}"
  end

  @@current_result = nil
  result
end

#run_script(method_name) ⇒ Object

Run a specific script method



344
345
346
347
# File 'lib/openc3/script/suite.rb', line 344

def run_script(method_name)
  ScriptStatus.instance.status = "#{self.class} : #{method_name}"
  run_method(self, method_name)
end

#run_setupObject



433
434
435
436
437
438
439
440
# File 'lib/openc3/script/suite.rb', line 433

def run_setup
  result = nil
  if self.class.method_defined?(:setup)
    ScriptStatus.instance.status = "#{self.class} : setup"
    result = run_script(:setup)
  end
  result
end

#run_teardownObject



442
443
444
445
446
447
448
449
# File 'lib/openc3/script/suite.rb', line 442

def run_teardown
  result = nil
  if self.class.method_defined?(:teardown)
    ScriptStatus.instance.status = "#{self.class} : teardown"
    result = run_script(:teardown)
  end
  result
end