Module: Blackstart

Defined in:
lib/blackstart.rb

Overview

This module provides facilities for defining and running automated tests.

Defined Under Namespace

Classes: Context

Class Method Summary collapse

Class Method Details

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

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

In detail: This method expects the sequence of test objects, source, to respond to an each message by yielding successive test objects to its block. Each test is run by converting the test object to a proc by sending it a to_proc message and calling the proc in the context of a new instance of Blackstart::Context. A failure is when the test raises an error (that is, an exception whose class is StandardError or a descendent of StandardError). After a failure, this method sends a puts message to the output stream, ostream, with these arguments (positional, in order): a string “FAILED TEST:”, the test object converted to a string via to_s, a string “…ERROR:”, a string describing the error, and an empty string. If a test raises any other exception, this method immediately raises that exception. After it has run all the tests and handled any failures, this method returns false if there were failures or true otherwise.

By default, ostream is $stdout.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/blackstart.rb', line 52

def self.run source, ostream = $stdout
  success = true
  for tst in source
    # Convert to a proc and create a context outside the vet block so that
    # this method will raise any exception that results instead of
    # potentially treating it as a test failure.
    prc = ::Proc.new(&tst)
    context = Context.new
    if err_desc = vet { context.instance_exec(&prc) }
      success = false
      ostream.puts "FAILED TEST:", tst.to_s, "...ERROR:", err_desc.to_s, ""
    end
  end
  success
end

.vet(&prc) ⇒ Object

Calls the block and returns a string describing the error raised, if any.

In detail: This method sends call with no arguments to the object representing the block. If that raises an error – that is, an exception whose class is StandardError or a descendent of StandardError – this method creates a string describing that error and returns it; the description includes the error’s class, message, and backtrace (if any). If sending the message raises any other exception, this method raises that exception. If sending the message does not raise an exception, this method returns nil.



18
19
20
21
22
23
24
# File 'lib/blackstart.rb', line 18

def self.vet &prc
  prc.call
  nil
rescue ::StandardError
  # Like IO#puts, separate lines with a line feed character rather than $\.
  ["#{$!.class}: #{$!.message}"].concat($!.backtrace || []).join "\n"
end