Module: Blackstart

Defined in:
lib/blackstart.rb,
sig/blackstart.rbs

Overview

This module provides facilities for running automated tests.

Defined Under Namespace

Modules: _ConvertsToTestProc, _OutputStream Classes: Scratchpad

Class Method Summary collapse

Class Method Details

.run(source, ostream = $stdout) ⇒ Boolean

Runs tests, writes any failure information to the output stream, and returns a boolean indicating whether there were no failures.

In detail: This method sends an each message with a block to the sequence of test objects, source, expecting it to yield to the block once for each test object in the sequence with that test object as the first argument. Each test involves creating a new scratchpad -- an instance of Blackstart::Scratchpad -- and sending an instance_exec message to it with the test object as the block, which converts the test object to a proc via to_proc if it's not already a proc. The resulting proc is called with no arguments and no block; the self object is set to the scratchpad. A failure is when the test (which includes any initial conversion to a proc) raises an error: an exception whose class is StandardError or a descendant of StandardError. After a failure, this method sends a puts message to the output stream, ostream, with no block and these arguments (positional, in order): a string "FAILED TEST:"; the test object converted to a string via to_s; a string "...ERROR:"; the error's class converted to a string via to_s; the error converted to a string via to_s; the error's backtrace or an empty array if the error signals that it has no backtrace; and an empty string. It ignores the object returned by the output stream and continues running tests. If a test raises a non-error exception, this method immediately raises that exception. If a test does not raise an exception, this method interprets it as a successful test; it ignores the object returned by the test and continues running tests. Whether the test fails or succeeds, the block sent with the each message returns nil. After it has run all the tests and handled any failures, this method returns false if there were failures, true otherwise. This method immediately raises any exception raised outside of a test.

By default, ostream is $stdout.

Parameters:

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/blackstart.rb', line 44

def self.run source, ostream = $stdout
  no_failures = true
  source.each do |tst|
    # Create the scratchpad outside the begin block so that this method will
    # raise any exception that results instead of potentially treating it as
    # a test failure.
    scratchpad = ::Blackstart::Scratchpad.new
    begin
      scratchpad.instance_exec(&tst)
    rescue ::StandardError => e
      no_failures = false
      ostream.puts "FAILED TEST:", tst.to_s, "...ERROR:", e.class.to_s,
        e.to_s, e.backtrace || [], ""
    end
    # Always return nil so that an each that's been modified to be sensitive
    # to the block's return value can't behave differently based on the
    # objects returned by the tests and the output stream.
    nil
  end
  no_failures
end