2019-11-10 06:22:21 +00:00
|
|
|
require "./mocks/registry"
|
|
|
|
|
2019-09-26 21:11:54 +00:00
|
|
|
module Spectator
|
2018-10-09 20:25:39 +00:00
|
|
|
# Helper class that acts as a gateway between example code and the test framework.
|
2019-02-18 06:01:43 +00:00
|
|
|
# Every example must be invoked by passing it to `#run`.
|
2018-10-09 20:25:39 +00:00
|
|
|
# This sets up the harness so that the example code can use it.
|
|
|
|
# The test framework does the following:
|
|
|
|
# ```
|
|
|
|
# result = Harness.run(example)
|
|
|
|
# # Do something with the result.
|
|
|
|
# ```
|
|
|
|
# Then from the example code, the harness can be accessed via `#current` like so:
|
|
|
|
# ```
|
2019-09-26 21:11:54 +00:00
|
|
|
# harness = ::Spectator::Harness.current
|
2018-10-09 20:25:39 +00:00
|
|
|
# # Do something with the harness.
|
|
|
|
# ```
|
|
|
|
# Of course, the end-user shouldn't see this or work directly with the harness.
|
|
|
|
# Instead, methods the user calls can access it.
|
|
|
|
# For instance, an expectation reporting a result.
|
|
|
|
class Harness
|
|
|
|
# Retrieves the harness for the current running example.
|
|
|
|
class_getter! current : self
|
|
|
|
|
|
|
|
# Wraps an example with a harness and runs the example.
|
|
|
|
# The `#current` harness will be set
|
|
|
|
# prior to running the example, and reset after.
|
2019-02-18 06:01:43 +00:00
|
|
|
# The *example* argument will be the example to run.
|
2018-10-09 20:25:39 +00:00
|
|
|
# The result returned from `Example#run` will be returned.
|
|
|
|
def self.run(example : Example) : Result
|
2019-11-10 19:35:03 +00:00
|
|
|
@@current = new(example)
|
2018-10-20 00:52:00 +00:00
|
|
|
example.run
|
|
|
|
ensure
|
2018-10-09 20:25:39 +00:00
|
|
|
@@current = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Retrieves the current running example.
|
|
|
|
getter example : Example
|
|
|
|
|
2019-11-10 19:35:03 +00:00
|
|
|
getter mocks : Mocks::Registry
|
2019-11-10 06:22:21 +00:00
|
|
|
|
2019-08-30 20:46:24 +00:00
|
|
|
# Retrieves the group for the current running example.
|
|
|
|
def group
|
|
|
|
example.group
|
|
|
|
end
|
|
|
|
|
2018-10-09 20:41:22 +00:00
|
|
|
# Reports the outcome of an expectation.
|
|
|
|
# An exception will be raised when a failing result is given.
|
2018-11-14 04:44:25 +00:00
|
|
|
def report_expectation(expectation : Expectations::Expectation) : Nil
|
|
|
|
@reporter.report(expectation)
|
2018-10-09 20:41:22 +00:00
|
|
|
end
|
|
|
|
|
2018-11-14 04:44:25 +00:00
|
|
|
# Generates the reported expectations from the example.
|
2018-10-09 20:41:22 +00:00
|
|
|
# This should be run after the example has finished.
|
2018-11-14 04:44:25 +00:00
|
|
|
def expectations : Expectations::ExampleExpectations
|
|
|
|
@reporter.expectations
|
2018-10-09 20:41:22 +00:00
|
|
|
end
|
2018-10-09 20:25:39 +00:00
|
|
|
|
2019-11-15 01:30:48 +00:00
|
|
|
# Marks a block of code to run later.
|
|
|
|
def defer(&block : ->) : Nil
|
|
|
|
@deferred << block
|
|
|
|
end
|
|
|
|
|
|
|
|
# Runs all deferred blocks.
|
|
|
|
def run_deferred : Nil
|
|
|
|
@deferred.each(&.call)
|
|
|
|
@deferred.clear
|
|
|
|
end
|
|
|
|
|
2018-10-09 20:25:39 +00:00
|
|
|
# Creates a new harness.
|
|
|
|
# The example the harness is for should be passed in.
|
|
|
|
private def initialize(@example)
|
2018-10-09 20:41:22 +00:00
|
|
|
@reporter = Expectations::ExpectationReporter.new
|
2019-11-15 01:48:43 +00:00
|
|
|
@mocks = Mocks::Registry.new(@example.group.context)
|
2019-11-15 01:30:48 +00:00
|
|
|
@deferred = Deque(->).new
|
2018-10-09 20:25:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|