From e3e4cac9c13ef0b407720ba626692e299ccf1a03 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 26 Sep 2019 16:23:13 -0600 Subject: [PATCH] Add source to example groups --- src/spectator/dsl/groups.cr | 37 +++++++++++-------- src/spectator/example_component.cr | 2 + src/spectator/nested_example_group.cr | 4 +- src/spectator/root_example_group.cr | 4 ++ .../nested_example_group_builder.cr | 4 +- .../sample_example_group_builder.cr | 8 ++-- 6 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/spectator/dsl/groups.cr b/src/spectator/dsl/groups.cr index d0aa425..7b3b851 100644 --- a/src/spectator/dsl/groups.cr +++ b/src/spectator/dsl/groups.cr @@ -2,19 +2,22 @@ require "../spec_builder" module Spectator module DSL - macro context(what, &block) + macro context(what, _source_file = __FILE__, _source_line = __LINE__, &block) class Context%context < {{@type.id}} - ::Spectator::SpecBuilder.start_group( - {% if what.is_a?(StringLiteral) %} - {% if what.starts_with?("#") || what.starts_with?(".") %} - {{what.id.symbolize}} - {% else %} - {{what}} - {% end %} - {% else %} - {{what.symbolize}} - {% end %} - ) + {% + description = if what.is_a?(StringLiteral) + if what.starts_with?("#") || what.starts_with?(".") + what.id.symbolize + else + what + end + else + what.symbolize + end + %} + + %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) + ::Spectator::SpecBuilder.start_group({{description}}, %source) {% if what.is_a?(Path) || what.is_a?(Generic) %} macro described_class @@ -36,7 +39,7 @@ module Spectator context({{what}}) {{block}} end - macro sample(collection, count = nil, &block) + macro sample(collection, count = nil, _source_file = __FILE__, _source_line = __LINE__, &block) {% name = block.args.empty? ? :value.id : block.args.first.id %} def %collection @@ -52,7 +55,8 @@ module Spectator end class Context%sample < {{@type.id}} - ::Spectator::SpecBuilder.start_sample_group({{collection.stringify}}, :%sample, {{name.stringify}}) do |values| + %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) + ::Spectator::SpecBuilder.start_sample_group({{collection.stringify}}, %source, :%sample, {{name.stringify}}) do |values| sample = {{@type.id}}.new(values) sample.%to_a end @@ -67,7 +71,7 @@ module Spectator end end - macro random_sample(collection, count = nil, &block) + macro random_sample(collection, count = nil, _source_file = __FILE__, _source_line = __LINE__, &block) {% name = block.args.empty? ? :value.id : block.args.first.id %} def %collection @@ -83,7 +87,8 @@ module Spectator end class Context%sample < {{@type.id}} - ::Spectator::SpecBuilder.start_sample_group({{collection.stringify}}, :%sample, {{name.stringify}}) do |values| + %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) + ::Spectator::SpecBuilder.start_sample_group({{collection.stringify}}, %source, :%sample, {{name.stringify}}) do |values| sample = {{@type.id}}.new(values) collection = sample.%to_a {% if count %} diff --git a/src/spectator/example_component.cr b/src/spectator/example_component.cr index d2b651e..4311f54 100644 --- a/src/spectator/example_component.cr +++ b/src/spectator/example_component.cr @@ -6,6 +6,8 @@ module Spectator # TODO: Rename to description. abstract def what : Symbol | String + abstract def source : Source + # Indicates whether the example (or group) has been completely run. abstract def finished? : Bool diff --git a/src/spectator/nested_example_group.cr b/src/spectator/nested_example_group.cr index c529696..fb1902c 100644 --- a/src/spectator/nested_example_group.cr +++ b/src/spectator/nested_example_group.cr @@ -8,6 +8,8 @@ module Spectator # This is a symbol when referencing a type. getter what : Symbol | String + getter source : Source + # Group that this is nested in. getter parent : ExampleGroup @@ -18,7 +20,7 @@ module Spectator # The parent's children must contain this group, # otherwise there may be unexpected behavior. # The *hooks* are stored to be triggered later. - def initialize(@what, @parent, context) + def initialize(@what, @source, @parent, context) super(context) end diff --git a/src/spectator/root_example_group.cr b/src/spectator/root_example_group.cr index 65836c3..ee787c0 100644 --- a/src/spectator/root_example_group.cr +++ b/src/spectator/root_example_group.cr @@ -9,6 +9,10 @@ module Spectator "ROOT" end + def source : Source + Source.new(__FILE__, __LINE__) + end + # Indicates that the group is symbolic. def symbolic? : Bool true diff --git a/src/spectator/spec_builder/nested_example_group_builder.cr b/src/spectator/spec_builder/nested_example_group_builder.cr index 0c2bf1e..7525858 100644 --- a/src/spectator/spec_builder/nested_example_group_builder.cr +++ b/src/spectator/spec_builder/nested_example_group_builder.cr @@ -3,12 +3,12 @@ require "./example_group_builder" module Spectator::SpecBuilder class NestedExampleGroupBuilder < ExampleGroupBuilder - def initialize(@what : String | Symbol) + def initialize(@what : String | Symbol, @source : Source) end def build(parent_group) context = TestContext.new(parent_group.context, build_hooks, parent_group.context.values) - NestedExampleGroup.new(@what, parent_group, context).tap do |group| + NestedExampleGroup.new(@what, @source, parent_group, context).tap do |group| group.children = children.map do |child| child.build(group).as(ExampleComponent) end diff --git a/src/spectator/spec_builder/sample_example_group_builder.cr b/src/spectator/spec_builder/sample_example_group_builder.cr index ae2909e..a2c6ce3 100644 --- a/src/spectator/spec_builder/sample_example_group_builder.cr +++ b/src/spectator/spec_builder/sample_example_group_builder.cr @@ -2,15 +2,15 @@ require "./nested_example_group_builder" module Spectator::SpecBuilder class SampleExampleGroupBuilder(T) < NestedExampleGroupBuilder - def initialize(what : String | Symbol, @id : Symbol, @label : String, @collection_builder : TestValues -> Array(T)) - super(what) + def initialize(what : String | Symbol, source : Source, @id : Symbol, @label : String, @collection_builder : TestValues -> Array(T)) + super(what, source) end def build(parent_group) values = parent_group.context.values collection = @collection_builder.call(values) context = TestContext.new(parent_group.context, build_hooks, values) - NestedExampleGroup.new(@what, parent_group, context).tap do |group| + NestedExampleGroup.new(@what, @source, parent_group, context).tap do |group| group.children = collection.map do |element| build_sub_group(group, element).as(ExampleComponent) end @@ -20,7 +20,7 @@ module Spectator::SpecBuilder private def build_sub_group(parent_group, element) values = parent_group.context.values.add(@id, @what.to_s, element) context = TestContext.new(parent_group.context, ExampleHooks.empty, values) - NestedExampleGroup.new("#{@label} = #{element.inspect}", parent_group, context).tap do |group| + NestedExampleGroup.new("#{@label} = #{element.inspect}", @source, parent_group, context).tap do |group| group.children = children.map do |child| child.build(group).as(ExampleComponent) end