Class: Tst::Runner

Inherits:
Object
  • Object
show all
Includes:
Assertions
Defined in:
lib/tst.rb

Overview

Runs test files in a fresh scope. It’s not perfectly isolated, since requires can still add to the global scope, but it’s good enough.

Instance Method Summary collapse

Methods included from Assertions

#assert, #assert_equal, #assert_raises

Constructor Details

#initialize(file, reporter, results) ⇒ Runner

Returns a new instance of Runner.



84
85
86
87
88
# File 'lib/tst.rb', line 84

def initialize(file, reporter, results)
  @__reporter = reporter
  @__results = results
  __run_tests(file)
end

Instance Method Details

#__record(name, status, elapsed, exception = nil) ⇒ Object



112
113
114
115
116
# File 'lib/tst.rb', line 112

def __record(name, status, elapsed, exception=nil)
  result = Result.new(name, status, elapsed, exception)
  @__results << result
  @__reporter.send(status, result)
end

#__run_tests(file) ⇒ Object

Runs the tests by ‘instance-eval`-ing the test file.



91
92
93
# File 'lib/tst.rb', line 91

def __run_tests(file)
  instance_eval(File.read(file), File.expand_path(file), 1)
end

#tst(name, &block) ⇒ Object

The ‘tst` methods itself. Takes a `name` and a block. Runs the block. Records the result.



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tst.rb', line 99

def tst(name, &block)
  start = Time.now
  block.call
  status = SUCCEEDED
rescue Failure => exception
  status = FAILED
rescue StandardError => exception
  status = RAISED
ensure
  elapsed = Time.now - start
  __record(name, status, elapsed, exception)
end