2020-09-06 16:31:23 +00:00
|
|
|
require "./example_context_delegate"
|
2020-09-05 21:01:22 +00:00
|
|
|
require "./example_group"
|
2020-11-08 03:56:30 +00:00
|
|
|
require "./harness"
|
2021-02-13 05:46:22 +00:00
|
|
|
require "./location"
|
2021-05-08 02:09:33 +00:00
|
|
|
require "./node"
|
2020-10-17 20:56:31 +00:00
|
|
|
require "./pending_result"
|
2020-09-05 21:01:22 +00:00
|
|
|
require "./result"
|
2021-01-30 19:07:23 +00:00
|
|
|
require "./tags"
|
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.
|
2021-05-08 02:09:33 +00:00
|
|
|
class Example < Node
|
2020-11-08 03:56:30 +00:00
|
|
|
# Currently running example.
|
|
|
|
class_getter! current : Example
|
|
|
|
|
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.
|
2021-01-31 02:42:46 +00:00
|
|
|
def result : Result
|
|
|
|
# TODO: Set to pending immediately (requires circular dependency between Example <-> Result removed).
|
|
|
|
@result ||= PendingResult.new(self)
|
|
|
|
end
|
2020-09-05 21:01:22 +00:00
|
|
|
|
|
|
|
# Creates the example.
|
2020-11-08 22:06:49 +00:00
|
|
|
# An instance to run the test code in is given by *context*.
|
|
|
|
# The *entrypoint* defines the test code (typically inside *context*).
|
2020-09-05 21:01:22 +00:00
|
|
|
# The *name* describes the purpose of the example.
|
|
|
|
# It can be a `Symbol` to describe a type.
|
2021-02-13 05:46:22 +00:00
|
|
|
# The *location* tracks where the example exists in source code.
|
2020-09-05 21:01:22 +00:00
|
|
|
# The example will be assigned to *group* if it is provided.
|
2021-01-30 18:20:20 +00:00
|
|
|
# A set of *tags* can be used for filtering and modifying example behavior.
|
2021-01-30 23:39:41 +00:00
|
|
|
# Note: The tags will not be merged with the parent tags.
|
2020-11-08 23:53:54 +00:00
|
|
|
def initialize(@context : Context, @entrypoint : self ->,
|
2021-02-13 05:46:22 +00:00
|
|
|
name : String? = nil, location : Location? = nil,
|
2021-01-30 19:07:23 +00:00
|
|
|
group : ExampleGroup? = nil, tags = Tags.new)
|
2021-02-13 05:46:22 +00:00
|
|
|
super(name, location, group, tags)
|
2020-09-05 21:01:22 +00:00
|
|
|
end
|
|
|
|
|
2020-11-07 21:43:59 +00:00
|
|
|
# Creates a dynamic example.
|
|
|
|
# A block provided to this method will be called as-if it were the test code for the example.
|
|
|
|
# The block will be given this example instance as an argument.
|
|
|
|
# The *name* describes the purpose of the example.
|
|
|
|
# It can be a `Symbol` to describe a type.
|
2021-02-13 05:46:22 +00:00
|
|
|
# The *location* tracks where the example exists in source code.
|
2020-11-07 21:43:59 +00:00
|
|
|
# The example will be assigned to *group* if it is provided.
|
2021-01-30 18:20:20 +00:00
|
|
|
# A set of *tags* can be used for filtering and modifying example behavior.
|
2021-01-30 23:39:41 +00:00
|
|
|
# Note: The tags will not be merged with the parent tags.
|
2021-02-13 05:46:22 +00:00
|
|
|
def initialize(name : String? = nil, location : Location? = nil, group : ExampleGroup? = nil,
|
2021-01-30 19:07:23 +00:00
|
|
|
tags = Tags.new, &block : self ->)
|
2021-02-13 05:46:22 +00:00
|
|
|
super(name, location, group, tags)
|
2020-11-08 22:06:49 +00:00
|
|
|
@context = NullContext.new
|
|
|
|
@entrypoint = block
|
2020-11-07 21:43:59 +00:00
|
|
|
end
|
|
|
|
|
2020-09-05 21:01:22 +00:00
|
|
|
# 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-08 03:56:30 +00:00
|
|
|
Log.debug { "Running example #{self}" }
|
2020-11-08 22:06:49 +00:00
|
|
|
Log.warn { "Example #{self} already ran" } if @finished
|
2020-11-15 18:25:07 +00:00
|
|
|
|
2021-01-30 23:36:15 +00:00
|
|
|
if pending?
|
|
|
|
Log.debug { "Skipping example #{self} - marked pending" }
|
2021-01-31 02:42:46 +00:00
|
|
|
return @result = PendingResult.new(self)
|
2021-01-30 23:36:15 +00:00
|
|
|
end
|
|
|
|
|
2021-01-16 23:28:33 +00:00
|
|
|
previous_example = @@current
|
|
|
|
@@current = self
|
2020-11-15 18:25:07 +00:00
|
|
|
|
2021-01-16 23:28:33 +00:00
|
|
|
begin
|
|
|
|
@result = Harness.run do
|
2021-01-17 00:04:42 +00:00
|
|
|
group?.try(&.call_once_before_all)
|
2021-01-16 23:28:33 +00:00
|
|
|
if (parent = group?)
|
|
|
|
parent.call_around_each(self) { run_internal }
|
|
|
|
else
|
|
|
|
run_internal
|
|
|
|
end
|
2021-01-17 00:04:42 +00:00
|
|
|
if (parent = group?)
|
|
|
|
parent.call_once_after_all if parent.finished?
|
|
|
|
end
|
2020-11-15 18:25:07 +00:00
|
|
|
end
|
2021-01-16 23:28:33 +00:00
|
|
|
ensure
|
|
|
|
@@current = previous_example
|
|
|
|
@finished = true
|
2020-11-15 18:25:07 +00:00
|
|
|
end
|
2021-01-16 23:28:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private def run_internal
|
2021-01-17 00:04:42 +00:00
|
|
|
group?.try(&.call_before_each(self))
|
2021-01-16 23:28:33 +00:00
|
|
|
@entrypoint.call(self)
|
2020-10-17 20:56:31 +00:00
|
|
|
@finished = true
|
2021-01-17 00:07:07 +00:00
|
|
|
group?.try(&.call_after_each(self))
|
2019-03-23 03:29:20 +00:00
|
|
|
end
|
2020-09-06 01:54:55 +00:00
|
|
|
|
2020-11-08 22:06:49 +00:00
|
|
|
# Executes code within the example's test context.
|
|
|
|
# This is an advanced method intended for internal usage only.
|
|
|
|
#
|
|
|
|
# The *klass* defines the type of the test context.
|
|
|
|
# This is typically only known by the code constructing the example.
|
|
|
|
# An error will be raised if *klass* doesn't match the test context's type.
|
|
|
|
# The block given to this method will be executed within the test context.
|
|
|
|
#
|
2021-01-16 23:52:16 +00:00
|
|
|
# The context casted to an instance of *klass* is provided as a block argument.
|
|
|
|
#
|
2020-11-08 22:06:49 +00:00
|
|
|
# TODO: Benchmark compiler performance using this method versus client-side casting in a proc.
|
2021-01-16 23:52:16 +00:00
|
|
|
protected def with_context(klass)
|
2020-11-08 23:53:54 +00:00
|
|
|
context = klass.cast(@context)
|
2020-11-08 22:06:49 +00:00
|
|
|
with context yield
|
|
|
|
end
|
|
|
|
|
2021-01-16 23:52:16 +00:00
|
|
|
# Casts the example's test context to a specific type.
|
|
|
|
# This is an advanced method intended for internal usage only.
|
|
|
|
#
|
|
|
|
# The *klass* defines the type of the test context.
|
|
|
|
# This is typically only known by the code constructing the example.
|
|
|
|
# An error will be raised if *klass* doesn't match the test context's type.
|
|
|
|
#
|
|
|
|
# The context casted to an instance of *klass* is returned.
|
|
|
|
#
|
|
|
|
# TODO: Benchmark compiler performance using this method versus client-side casting in a proc.
|
|
|
|
protected def cast_context(klass)
|
|
|
|
klass.cast(@context)
|
|
|
|
end
|
|
|
|
|
2021-01-16 18:49:43 +00:00
|
|
|
# Constructs the full name or description of the example.
|
|
|
|
# This prepends names of groups this example is part of.
|
|
|
|
def to_s(io)
|
|
|
|
if name?
|
|
|
|
super
|
|
|
|
else
|
|
|
|
io << "<anonymous>"
|
|
|
|
end
|
|
|
|
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 << '"'
|
|
|
|
|
2021-02-13 05:46:22 +00:00
|
|
|
# Add location if it's available.
|
|
|
|
if (location = self.location)
|
2020-10-17 17:51:16 +00:00
|
|
|
io << " @ "
|
2021-02-13 05:46:22 +00:00
|
|
|
io << location
|
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
|
2021-01-16 23:28:33 +00:00
|
|
|
|
2021-01-31 03:07:36 +00:00
|
|
|
# Creates the JSON representation of the example,
|
|
|
|
# which is just its name.
|
|
|
|
def to_json(json : ::JSON::Builder)
|
|
|
|
json.string(to_s)
|
2021-01-31 02:42:46 +00:00
|
|
|
end
|
|
|
|
|
2021-01-16 23:28:33 +00:00
|
|
|
# Wraps an example to behave like a `Proc`.
|
|
|
|
# This is typically used for an *around_each* hook.
|
|
|
|
# Invoking `#call` or `#run` will run the example.
|
|
|
|
struct Procsy
|
|
|
|
# Underlying example that will run.
|
|
|
|
getter example : Example
|
|
|
|
|
|
|
|
# Creates the example proxy.
|
|
|
|
# The *example* should be run eventually.
|
|
|
|
# The *proc* defines the block of code to run when `#call` or `#run` is invoked.
|
|
|
|
def initialize(@example : Example, &@proc : ->)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Invokes the proc.
|
|
|
|
def call : Nil
|
|
|
|
@proc.call
|
|
|
|
end
|
|
|
|
|
|
|
|
# Invokes the proc.
|
|
|
|
def run : Nil
|
|
|
|
@proc.call
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a new procsy for a block and the example from this instance.
|
|
|
|
def wrap(&block : ->) : self
|
|
|
|
self.class.new(@example, &block)
|
|
|
|
end
|
|
|
|
|
2021-01-16 23:52:16 +00:00
|
|
|
# Executes code within the example's test context.
|
|
|
|
# This is an advanced method intended for internal usage only.
|
|
|
|
#
|
|
|
|
# The *klass* defines the type of the test context.
|
|
|
|
# This is typically only known by the code constructing the example.
|
|
|
|
# An error will be raised if *klass* doesn't match the test context's type.
|
|
|
|
# The block given to this method will be executed within the test context.
|
|
|
|
#
|
|
|
|
# TODO: Benchmark compiler performance using this method versus client-side casting in a proc.
|
|
|
|
protected def with_context(klass)
|
|
|
|
context = @example.cast_context(klass)
|
|
|
|
with context yield
|
|
|
|
end
|
|
|
|
|
2021-01-16 23:28:33 +00:00
|
|
|
# Allow instance to behave like an example.
|
|
|
|
forward_missing_to @example
|
|
|
|
end
|
2018-08-19 07:15:32 +00:00
|
|
|
end
|
|
|
|
end
|