2021-01-09 20:56:32 +00:00
|
|
|
# Base class that all test cases run in.
|
|
|
|
# This type is used to store all test case contexts as a single type.
|
|
|
|
# The instance must be downcast to the correct type before calling a context method.
|
|
|
|
# This type is intentionally outside the `Spectator` module.
|
|
|
|
# The reason for this is to prevent name collision when using the DSL to define a spec.
|
|
|
|
abstract class SpectatorContext
|
2022-12-21 03:32:40 +00:00
|
|
|
# Evaluates the contents of a block within the scope of the context.
|
2023-01-27 00:19:31 +00:00
|
|
|
def eval(&)
|
2022-12-21 03:32:40 +00:00
|
|
|
with self yield
|
|
|
|
end
|
|
|
|
|
2021-04-26 23:11:53 +00:00
|
|
|
# Produces a dummy string to represent the context as a string.
|
|
|
|
# This prevents the default behavior, which normally stringifies instance variables.
|
|
|
|
# Due to the sheer amount of types Spectator can create
|
|
|
|
# and that the Crystal compiler instantiates a `#to_s` and/or `#inspect` for each of those types,
|
|
|
|
# an explosion in method instances can be created.
|
|
|
|
# The compile time is drastically reduced by using a dummy string instead.
|
2022-11-05 02:56:02 +00:00
|
|
|
def to_s(io : IO) : Nil
|
2021-01-31 03:20:30 +00:00
|
|
|
io << "Context"
|
|
|
|
end
|
|
|
|
|
2021-04-26 23:11:53 +00:00
|
|
|
# :ditto:
|
2022-11-05 02:56:02 +00:00
|
|
|
def inspect(io : IO) : Nil
|
2021-05-30 20:21:42 +00:00
|
|
|
io << "Context<" << self.class << '>'
|
2021-01-31 03:20:30 +00:00
|
|
|
end
|
2021-01-09 20:56:32 +00:00
|
|
|
end
|
2020-09-05 20:55:49 +00:00
|
|
|
|
|
|
|
module Spectator
|
|
|
|
# Base class that all test cases run in.
|
|
|
|
# This type is used to store all test case contexts as a single type.
|
|
|
|
# The instance must be downcast to the correct type before calling a context method.
|
2020-09-27 00:14:22 +00:00
|
|
|
#
|
|
|
|
# Nested contexts, such as those defined by `context` and `describe` in the DSL, can define their own methods.
|
|
|
|
# The intent is that a proc will downcast to the correct type and call one of those methods.
|
|
|
|
# This is how methods that contain test cases, hooks, and other context-specific code blocks get invoked.
|
2020-09-05 20:55:49 +00:00
|
|
|
alias Context = ::SpectatorContext
|
|
|
|
end
|