From 80fbe89b4c09cd992495345e07630213be6523ca Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 15 Sep 2018 16:58:59 -0600 Subject: [PATCH] Track children in DSL --- src/spectator/dsl/structure_dsl.cr | 16 ++++++++++++---- src/spectator/examples.cr | 3 ++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 0393ad3..01688e9 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -15,12 +15,14 @@ module Spectator module_name = (type.id + safe_name.camelcase).id absolute_module_name = [parent_module, module_name].join("::").id what_arg = what.is_a?(StringLiteral) ? what : what.stringify - parent_given = ::Spectator::Definitions::ALL[parent_module.id][:given] + parent_def = ::Spectator::Definitions::ALL[parent_module.id] + parent_def[:children] << absolute_module_name ::Spectator::Definitions::ALL[absolute_module_name] = { name: module_name, parent: parent_module, - given: parent_given.map { |e| e } # Duplicate elements without dup method. + given: parent_def[:given].map { |e| e }, # Duplicate elements without dup method. + children: [] of Object } %} @@ -127,8 +129,11 @@ module Spectator parent_module = @type safe_name = description.id.stringify.chars.map { |c| ::Spectator::Definitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_") class_name = (safe_name.camelcase + "Example").id - given_vars = ::Spectator::Definitions::ALL[parent_module.id][:given] + absolute_class_name = [parent_module.id, class_name].join("::").id + parent_def = ::Spectator::Definitions::ALL[parent_module.id] + given_vars = parent_def[:given] var_names = given_vars.map { |v| v[:name] } + parent_def[:children] << absolute_class_name %} class Example%example @@ -188,8 +193,11 @@ module Spectator parent_module = @type safe_name = description.id.stringify.chars.map { |c| ::Spectator::Definitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_") class_name = (safe_name.camelcase + "Example").id - given_vars = ::Spectator::Definitions::ALL[parent_module.id][:given] + absolute_class_name = [parent_module.id, class_name].join("::").id + parent_def = ::Spectator::Definitions::ALL[parent_module.id] + given_vars = parent_def[:given] var_names = given_vars.map { |v| v[:name] } + parent_def[:children] << absolute_class_name %} class Example%example diff --git a/src/spectator/examples.cr b/src/spectator/examples.cr index 4fe1af7..b1eb3d0 100644 --- a/src/spectator/examples.cr +++ b/src/spectator/examples.cr @@ -8,7 +8,8 @@ module Spectator {% ::Spectator::Definitions::ALL[@type.id] = { name: "ROOT", parent: nil, - given: [] of Object + given: [] of Object, + children: [] of Object } %} ::Spectator::Definitions::MAPPING[{{@type.stringify}}] = ::Spectator::ExampleGroup::ROOT end