From a725c0d5bebadb00eac233ae5dd170eaab0bc741 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 10 Sep 2018 21:51:14 -0600 Subject: [PATCH] Pass context to examples --- src/spectator/context.cr | 7 +++++++ src/spectator/dsl.cr | 17 +++++++++++------ src/spectator/example.cr | 5 +++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/spectator/context.cr b/src/spectator/context.cr index 4a78092..a391e5f 100644 --- a/src/spectator/context.cr +++ b/src/spectator/context.cr @@ -2,6 +2,7 @@ require "./example" module Spectator class Context + getter parent : Context? getter examples = [] of Example getter contexts = [] of Context getter before_all_hooks = [] of -> @@ -10,6 +11,12 @@ module Spectator getter after_each_hooks = [] of -> getter around_each_hooks = [] of Example -> + def initialize(@parent = nil) + if (parent = @parent) + parent.contexts << self + end + end + def all_examples add_examples end diff --git a/src/spectator/dsl.cr b/src/spectator/dsl.cr index 9aeb834..11765c6 100644 --- a/src/spectator/dsl.cr +++ b/src/spectator/dsl.cr @@ -14,8 +14,8 @@ module Spectator module {{module_name.id}} include ::Spectator::DSL - CURRENT_CONTEXT = ::Spectator::Context.new - {{context_module.id}}::CURRENT_CONTEXT.contexts << CURRENT_CONTEXT + PARENT_CONTEXT = {{context_module.id}}::CURRENT_CONTEXT + CURRENT_CONTEXT = ::Spectator::Context.new(PARENT_CONTEXT) CONTEXT_MODULE = {{context_module.id}}::{{module_name.id}} GIVEN_VARIABLES = [ @@ -44,8 +44,13 @@ module Spectator class {{class_name.id}} < ::Spectator::Example include Locals - {% unless given_vars.empty? %} - def initialize({{ var_names.join(", ").id }}) + {% if given_vars.empty? %} + def initialize(context) + super(context) + end + {% else %} + def initialize(context, {{ var_names.join(", ").id }}) + super(context) {% for var_name in var_names %} self.{{var_name}} = {{var_name}} {% end %} @@ -58,14 +63,14 @@ module Spectator end {% if given_vars.empty? %} - CURRENT_CONTEXT.examples << {{class_name.id}}.new + CURRENT_CONTEXT.examples << {{class_name.id}}.new(CURRENT_CONTEXT) {% else %} {% for given_var in given_vars %} {% var_name = given_var[0] %} {% collection = given_var[1] %} {{collection}}.each do |{{var_name}}| {% end %} - CURRENT_CONTEXT.examples << {{class_name.id}}.new({{var_names.join(", ").id}}) + CURRENT_CONTEXT.examples << {{class_name.id}}.new(CURRENT_CONTEXT, {{var_names.join(", ").id}}) {% for given_var in given_vars %} end {% end %} diff --git a/src/spectator/example.cr b/src/spectator/example.cr index 28811ad..8cf2be3 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -2,6 +2,11 @@ require "./source" module Spectator abstract class Example + getter context : Context + + def initialize(@context) + end + macro is_expected expect(subject) end