shard-spectator/src/spectator/example.cr

196 lines
6.5 KiB
Crystal
Raw Normal View History

2020-09-06 16:31:23 +00:00
require "./example_context_delegate"
require "./example_group"
2020-11-08 03:56:30 +00:00
require "./harness"
2020-10-17 20:56:31 +00:00
require "./pending_result"
require "./result"
require "./source"
2021-01-21 07:03:39 +00:00
require "./spec/node"
require "./tags"
2018-08-19 07:15:32 +00:00
module Spectator
# Standard example that runs a test case.
2021-01-21 07:03:39 +00:00
class Example < Spec::Node
2020-11-08 03:56:30 +00:00
# Currently running example.
class_getter! current : Example
# 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
# Creates the example.
# An instance to run the test code in is given by *context*.
# The *entrypoint* defines the test code (typically inside *context*).
# 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.
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.
def initialize(@context : Context, @entrypoint : self ->,
2021-01-30 18:20:20 +00:00
name : String? = nil, source : Source? = nil,
group : ExampleGroup? = nil, tags = Tags.new)
2021-01-30 18:20:20 +00:00
super(name, source, group, tags)
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.
# The *source* tracks where the example exists in source code.
# 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-01-30 08:16:26 +00:00
def initialize(name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil,
tags = Tags.new, &block : self ->)
2021-01-30 18:20:20 +00:00
super(name, source, group, tags)
@context = NullContext.new
@entrypoint = block
2020-11-07 21:43:59 +00:00
end
# Executes the test case.
# Returns the result of the execution.
# The result will also be stored in `#result`.
def run : Result
2020-11-08 03:56:30 +00:00
Log.debug { "Running example #{self}" }
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" }
return @result = PendingResult.new
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
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
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
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))
end
# 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.
#
# The context casted to an instance of *klass* is provided as a block argument.
#
# TODO: Benchmark compiler performance using this method versus client-side casting in a proc.
protected def with_context(klass)
context = klass.cast(@context)
with context yield
end
# 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
# 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 22:24:22 +00:00
if (source = 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
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
# 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