From 9103bfde0f52b78251ed3fc416536ddbec6e9eba Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 14 Sep 2020 20:00:17 -0600 Subject: [PATCH] Playing around with line numbers Trying to find some pattern in the line descripancies reported to the macros compared to the source file. --- src/spectator.cr | 17 +-------------- src/spectator/dsl/examples.cr | 40 ++++++++++++++++++++++++++++------- src/spectator/dsl/groups.cr | 16 +++++--------- src/spectator/spec/builder.cr | 1 - 4 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/spectator.cr b/src/spectator.cr index 7dde698..1b60f69 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -25,23 +25,8 @@ module Spectator # Actually, prefixing methods and macros with `Spectator` # most likely won't work and can cause compiler errors. macro describe(description, &block) - # This macro creates the foundation for all specs. - # Every group of examples is defined a separate module - `SpectatorExamples`. - # There's multiple reasons for this. - # - # The first reason is to provide namespace isolation. - # We don't want the spec code to accidentally pickup types and values from the `Spectator` module. - # Another reason is that we need a root module to put all examples and groups in. - # And lastly, the spec DSL needs to be given to the block of code somehow. - # The DSL is included in the `SpectatorTest` class. - # - # For more information on how the DSL works, see the `DSL` module. - - # Root-level class that contains all examples and example groups. class ::SpectatorTestContext - # Pass off the description argument and block to `DSL::StructureDSL.describe`. - # That method will handle creating a new group for this spec. - describe({{description}}) {{block}} + example_group({{description}}) {{block}} end end diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index ba39adb..cca80d1 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -3,12 +3,30 @@ require "./builder" module Spectator::DSL module Examples - macro example(what = nil, *, _source_file = __FILE__, _source_line = __LINE__, &block) + macro define_example(name) + macro {{name.id}}(what = nil) + def %test + \{{yield}} + end + + %source = ::Spectator::Source.new(__FILE__, __LINE__) + ::Spectator::DSL::Builder.add_example( + \{{what.is_a?(StringLiteral) || what.is_a?(NilLiteral) ? what : what.stringify}}, + %source, + \{{@type.name}}.new + ) { |example, context| context.as(\{{@type.name}}).%test } + end + end + + define_example(:example) + + macro it(what = nil, *, _source_file = __FILE__, _source_line = __LINE__, &block) + {% puts "#{_source_file}:#{_source_line}" %} def %test - {{block.body}} + {{yield}} end - %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) + %source = ::Spectator::Source.new(__FILE__, __LINE__) ::Spectator::DSL::Builder.add_example( {{what.is_a?(StringLiteral | NilLiteral) ? what : what.stringify}}, %source, @@ -16,12 +34,18 @@ module Spectator::DSL ) { |example, context| context.as({{@type.name}}).%test } end - macro it(what = nil, *, _source_file = __FILE__, _source_line = __LINE__, &block) - example({{what}}, _source_file: {{_source_file}}, _source_line: {{_source_line}}) {{block}} - end - macro specify(what = nil, *, _source_file = __FILE__, _source_line = __LINE__, &block) - example({{what}}, _source_file: {{_source_file}}, _source_line: {{_source_line}}) {{block}} + {% puts "#{_source_file}:#{_source_line}" %} + def %test + {{yield}} + end + + %source = ::Spectator::Source.new(__FILE__, __LINE__) + ::Spectator::DSL::Builder.add_example( + {{what.is_a?(StringLiteral | NilLiteral) ? what : what.stringify}}, + %source, + {{@type.name}}.new + ) { |example, context| context.as({{@type.name}}).%test } end end macro it(description = nil, _source_file = __FILE__, _source_line = __LINE__, &block) diff --git a/src/spectator/dsl/groups.cr b/src/spectator/dsl/groups.cr index 7c820b7..a029083 100644 --- a/src/spectator/dsl/groups.cr +++ b/src/spectator/dsl/groups.cr @@ -8,19 +8,13 @@ module Spectator::DSL # Defines a new example group. # The *what* argument is a name or description of the group. # If it isn't a string literal, then it is symbolized for `ExampleNode#name`. - macro example_group(what, *, _source_file = __FILE__, _source_line = __LINE__, &block) + + # Example group: {{what.stringify}} # Source: {{_source_file}}:{{_source_line}} - class Group%group < {{@type.id}} - _spectator_group_subject({{what}}) - - %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) - ::Spectator::DSL::Builder.start_group({{what.is_a?(StringLiteral) ? what : what.stringify}}, %source) - - {{block.body}} - - ::Spectator::DSL::Builder.end_group - end + macro example_group(what, *, _source_file = __FILE__, _source_line = __LINE__, &block) + class Group%group < {{@type.id}}; _spectator_group_subject({{what}}); ::Spectator::DSL::Builder.start_group({{what.is_a?(StringLiteral) ? what : what.stringify}}, ::Spectator::Source.new(__FILE__, __LINE__)); {{block.body}}; ::Spectator::DSL::Builder.end_group; end + {% debug(false) %} end macro describe(what, *, _source_file = __FILE__, _source_line = __LINE__, &block) diff --git a/src/spectator/spec/builder.cr b/src/spectator/spec/builder.cr index 27a97b9..1eb16b3 100644 --- a/src/spectator/spec/builder.cr +++ b/src/spectator/spec/builder.cr @@ -42,7 +42,6 @@ module Spectator def add_example(name, source, context, &block : Example, Context ->) {% if flag?(:spectator_debug) %} puts "Add example: #{name} @ #{source}" - puts "Context: #{context}" {% end %} delegate = ExampleContextDelegate.new(context, block) Example.new(delegate, name, source, current_group)