Add context instances

This commit is contained in:
Michael Miller 2018-09-10 19:59:57 -06:00
parent 65da494e52
commit 387e129a89
5 changed files with 30 additions and 6 deletions

View file

@ -4,7 +4,7 @@ require "./spectator/*"
module Spectator
VERSION = "0.1.0"
ALL_EXAMPLES = [] of Example
ROOT_CONTEXT = Context.new
macro describe(what, source_file = __FILE__, source_line = __LINE__, &block)
module Spectator
@ -15,6 +15,6 @@ module Spectator
end
at_exit do
Runner.new(ALL_EXAMPLES).run
Runner.new(ROOT_CONTEXT).run
end
end

20
src/spectator/context.cr Normal file
View file

@ -0,0 +1,20 @@
require "./example"
module Spectator
class Context
getter examples = [] of Example
getter contexts = [] of Context
def all_examples
add_examples
end
protected def add_examples(array = [] of Example)
array.concat(@examples)
contexts.each do |context|
context.add_examples(array)
end
array
end
end
end

View file

@ -14,6 +14,9 @@ module Spectator
module {{module_name.id}}
include ::Spectator::DSL
CURRENT_CONTEXT = ::Spectator::Context.new
{{context_module.id}}::CURRENT_CONTEXT.contexts << CURRENT_CONTEXT
CONTEXT_MODULE = {{context_module.id}}::{{module_name.id}}
GIVEN_VARIABLES = [
{{ parent_given_vars.join(", ").id }}
@ -55,14 +58,14 @@ module Spectator
end
{% if given_vars.empty? %}
::Spectator::ALL_EXAMPLES << {{class_name.id}}.new
CURRENT_CONTEXT.examples << {{class_name.id}}.new
{% else %}
{% for given_var in given_vars %}
{% var_name = given_var[0] %}
{% collection = given_var[1] %}
{{collection}}.each do |{{var_name}}|
{% end %}
::Spectator::ALL_EXAMPLES << {{class_name.id}}.new({{var_names.join(", ").id}})
CURRENT_CONTEXT.examples << {{class_name.id}}.new({{var_names.join(", ").id}})
{% for given_var in given_vars %}
end
{% end %}

View file

@ -4,6 +4,7 @@ module Spectator
module Examples
include ::Spectator::DSL
CURRENT_CONTEXT = Spectator::ROOT_CONTEXT
CONTEXT_MODULE = ::Spectator::Examples
GIVEN_VARIABLES = [] of Object

View file

@ -3,7 +3,7 @@ require "./successful_example_result"
module Spectator
class Runner
def initialize(@examples : Enumerable(Example),
def initialize(@context : Context,
@reporter : Reporters::Reporter = Reporters::StandardReporter.new)
end
@ -11,7 +11,7 @@ module Spectator
results = [] of ExampleResult
elapsed = Time.measure do
@reporter.start_suite
results = @examples.map do |example|
results = @context.all_examples.map do |example|
@reporter.start_example(example)
run_example(example).tap do |result|
@reporter.end_example(result)