2020-09-06 16:31:23 +00:00
|
|
|
require "./example_context_delegate"
|
2020-09-05 21:01:22 +00:00
|
|
|
require "./example_group"
|
2020-09-06 01:54:55 +00:00
|
|
|
require "./example_node"
|
2020-10-17 20:56:31 +00:00
|
|
|
require "./pass_result"
|
|
|
|
require "./pending_result"
|
2020-09-05 21:01:22 +00:00
|
|
|
require "./result"
|
|
|
|
require "./source"
|
2018-10-14 23:10:12 +00:00
|
|
|
|
2018-08-19 07:15:32 +00:00
|
|
|
module Spectator
|
2020-09-05 21:01:22 +00:00
|
|
|
# Standard example that runs a test case.
|
2020-09-06 01:54:55 +00:00
|
|
|
class Example < ExampleNode
|
2020-09-05 21:01:22 +00:00
|
|
|
# Indicates whether the example already ran.
|
|
|
|
getter? finished : Bool = false
|
|
|
|
|
|
|
|
# Retrieves the result of the last time the example ran.
|
2020-10-17 20:56:31 +00:00
|
|
|
getter result : Result = PendingResult.new
|
2020-09-05 21:01:22 +00:00
|
|
|
|
|
|
|
# Creates the example.
|
|
|
|
# The *delegate* contains the test context and method that runs the test case.
|
|
|
|
# The *name* describes the purpose of the example.
|
|
|
|
# It can be a `Symbol` to describe a type.
|
|
|
|
# The *source* tracks where the example exists in source code.
|
|
|
|
# The example will be assigned to *group* if it is provided.
|
2020-09-06 16:31:23 +00:00
|
|
|
def initialize(@delegate : ExampleContextDelegate,
|
2020-09-27 00:14:59 +00:00
|
|
|
name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil)
|
2020-09-05 21:01:22 +00:00
|
|
|
super(name, source, group)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Executes the test case.
|
|
|
|
# Returns the result of the execution.
|
|
|
|
# The result will also be stored in `#result`.
|
2018-10-10 19:05:17 +00:00
|
|
|
def run : Result
|
2020-11-07 20:47:31 +00:00
|
|
|
runner = Runner.new(self, @delegate)
|
|
|
|
@result = runner.run
|
|
|
|
ensure
|
2020-10-17 20:56:31 +00:00
|
|
|
@finished = true
|
2019-03-23 03:29:20 +00:00
|
|
|
end
|
2020-09-06 01:54:55 +00:00
|
|
|
|
|
|
|
# Exposes information about the example useful for debugging.
|
|
|
|
def inspect(io)
|
2020-10-17 17:51:16 +00:00
|
|
|
# Full example name.
|
|
|
|
io << '"'
|
|
|
|
to_s(io)
|
|
|
|
io << '"'
|
|
|
|
|
|
|
|
# Add source if it's available.
|
2020-11-07 20:47:39 +00:00
|
|
|
if (sourse = self.source)
|
2020-10-17 17:51:16 +00:00
|
|
|
io << " @ "
|
2020-11-07 20:47:39 +00:00
|
|
|
io << source
|
2020-10-17 17:51:16 +00:00
|
|
|
end
|
|
|
|
|
2020-10-17 20:56:31 +00:00
|
|
|
io << result
|
2020-09-06 01:54:55 +00:00
|
|
|
end
|
2020-11-07 20:47:31 +00:00
|
|
|
|
|
|
|
# Responsible for executing example code and reporting results.
|
|
|
|
private struct Runner
|
|
|
|
# Creates the runner.
|
|
|
|
# *example* is the example being tested.
|
|
|
|
# The *delegate* is the entrypoint of the example's test code.
|
|
|
|
def initialize(@example : Example, @delegate : ExampleContextDelegate)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Executes the example's test code and produces a result.
|
|
|
|
def run : Result
|
|
|
|
Log.debug { "Running example #{@example}" }
|
|
|
|
elapsed = Time.measure do
|
|
|
|
@delegate.call(@example)
|
|
|
|
end
|
|
|
|
PassResult.new(elapsed)
|
|
|
|
end
|
|
|
|
end
|
2018-08-19 07:15:32 +00:00
|
|
|
end
|
|
|
|
end
|