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 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 %}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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