Add source to example groups

This commit is contained in:
Michael Miller 2019-09-26 16:23:13 -06:00
parent dacca0bf1e
commit e3e4cac9c1
6 changed files with 36 additions and 23 deletions

View file

@ -2,19 +2,22 @@ require "../spec_builder"
module Spectator module Spectator
module DSL module DSL
macro context(what, &block) macro context(what, _source_file = __FILE__, _source_line = __LINE__, &block)
class Context%context < {{@type.id}} class Context%context < {{@type.id}}
::Spectator::SpecBuilder.start_group( {%
{% if what.is_a?(StringLiteral) %} description = if what.is_a?(StringLiteral)
{% if what.starts_with?("#") || what.starts_with?(".") %} if what.starts_with?("#") || what.starts_with?(".")
{{what.id.symbolize}} what.id.symbolize
{% else %} else
{{what}} what
{% end %} end
{% else %} else
{{what.symbolize}} what.symbolize
{% end %} end
) %}
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
::Spectator::SpecBuilder.start_group({{description}}, %source)
{% if what.is_a?(Path) || what.is_a?(Generic) %} {% if what.is_a?(Path) || what.is_a?(Generic) %}
macro described_class macro described_class
@ -36,7 +39,7 @@ module Spectator
context({{what}}) {{block}} context({{what}}) {{block}}
end 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 %} {% name = block.args.empty? ? :value.id : block.args.first.id %}
def %collection def %collection
@ -52,7 +55,8 @@ module Spectator
end end
class Context%sample < {{@type.id}} 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 = {{@type.id}}.new(values)
sample.%to_a sample.%to_a
end end
@ -67,7 +71,7 @@ module Spectator
end end
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 %} {% name = block.args.empty? ? :value.id : block.args.first.id %}
def %collection def %collection
@ -83,7 +87,8 @@ module Spectator
end end
class Context%sample < {{@type.id}} 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 = {{@type.id}}.new(values)
collection = sample.%to_a collection = sample.%to_a
{% if count %} {% if count %}

View file

@ -6,6 +6,8 @@ module Spectator
# TODO: Rename to description. # TODO: Rename to description.
abstract def what : Symbol | String abstract def what : Symbol | String
abstract def source : Source
# Indicates whether the example (or group) has been completely run. # Indicates whether the example (or group) has been completely run.
abstract def finished? : Bool abstract def finished? : Bool

View file

@ -8,6 +8,8 @@ module Spectator
# This is a symbol when referencing a type. # This is a symbol when referencing a type.
getter what : Symbol | String getter what : Symbol | String
getter source : Source
# Group that this is nested in. # Group that this is nested in.
getter parent : ExampleGroup getter parent : ExampleGroup
@ -18,7 +20,7 @@ module Spectator
# The parent's children must contain this group, # The parent's children must contain this group,
# otherwise there may be unexpected behavior. # otherwise there may be unexpected behavior.
# The *hooks* are stored to be triggered later. # The *hooks* are stored to be triggered later.
def initialize(@what, @parent, context) def initialize(@what, @source, @parent, context)
super(context) super(context)
end end

View file

@ -9,6 +9,10 @@ module Spectator
"ROOT" "ROOT"
end end
def source : Source
Source.new(__FILE__, __LINE__)
end
# Indicates that the group is symbolic. # Indicates that the group is symbolic.
def symbolic? : Bool def symbolic? : Bool
true true

View file

@ -3,12 +3,12 @@ require "./example_group_builder"
module Spectator::SpecBuilder module Spectator::SpecBuilder
class NestedExampleGroupBuilder < ExampleGroupBuilder class NestedExampleGroupBuilder < ExampleGroupBuilder
def initialize(@what : String | Symbol) def initialize(@what : String | Symbol, @source : Source)
end end
def build(parent_group) def build(parent_group)
context = TestContext.new(parent_group.context, build_hooks, parent_group.context.values) 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| group.children = children.map do |child|
child.build(group).as(ExampleComponent) child.build(group).as(ExampleComponent)
end end

View file

@ -2,15 +2,15 @@ require "./nested_example_group_builder"
module Spectator::SpecBuilder module Spectator::SpecBuilder
class SampleExampleGroupBuilder(T) < NestedExampleGroupBuilder class SampleExampleGroupBuilder(T) < NestedExampleGroupBuilder
def initialize(what : String | Symbol, @id : Symbol, @label : String, @collection_builder : TestValues -> Array(T)) def initialize(what : String | Symbol, source : Source, @id : Symbol, @label : String, @collection_builder : TestValues -> Array(T))
super(what) super(what, source)
end end
def build(parent_group) def build(parent_group)
values = parent_group.context.values values = parent_group.context.values
collection = @collection_builder.call(values) collection = @collection_builder.call(values)
context = TestContext.new(parent_group.context, build_hooks, 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| group.children = collection.map do |element|
build_sub_group(group, element).as(ExampleComponent) build_sub_group(group, element).as(ExampleComponent)
end end
@ -20,7 +20,7 @@ module Spectator::SpecBuilder
private def build_sub_group(parent_group, element) private def build_sub_group(parent_group, element)
values = parent_group.context.values.add(@id, @what.to_s, element) values = parent_group.context.values.add(@id, @what.to_s, element)
context = TestContext.new(parent_group.context, ExampleHooks.empty, values) 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| group.children = children.map do |child|
child.build(group).as(ExampleComponent) child.build(group).as(ExampleComponent)
end end