Method: Cosmos::Group#run_method

Defined in:
lib/cosmos/script/suite.rb

#run_method(object, method_name) ⇒ Object



341
342
343
344
345
346
347
348
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
# File 'lib/cosmos/script/suite.rb', line 341

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)
    # Capture STDOUT and STDERR
    $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