From de55e314077c3b0f27c8034d35edbeee2f6a577d Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Tue, 4 Sep 2018 13:36:19 -0600 Subject: [PATCH] Add basics for tracking source code location --- src/spectator.cr | 2 +- src/spectator/dsl.cr | 12 ++++++++---- src/spectator/example.cr | 6 +++++- src/spectator/source.cr | 15 +++++++++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/spectator/source.cr diff --git a/src/spectator.cr b/src/spectator.cr index da07d87..52b99bd 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -9,7 +9,7 @@ module Spectator macro describe(what, source_file = __FILE__, source_line = __LINE__, &block) module Spectator module Examples - DSL.describe({{what}}) {{block}} + DSL.describe({{what}}, {{source_file}}, {{source_line}}) {{block}} end end end diff --git a/src/spectator/dsl.cr b/src/spectator/dsl.cr index ed5179b..b96391d 100644 --- a/src/spectator/dsl.cr +++ b/src/spectator/dsl.cr @@ -2,11 +2,11 @@ require "./example_group" module Spectator module DSL - macro describe(what, type = "Describe", &block) - context({{what}}, {{type}}) {{block}} + macro describe(what, source_file = __FILE__, source_line = __LINE__, type = "Describe", &block) + context({{what}}, {{source_file}}, {{source_line}}, {{type}}) {{block}} end - macro context(what, type = "Context", &block) + macro context(what, source_file = __FILE__, source_line = __LINE__, type = "Context", &block) {% safe_name = what.id.stringify.gsub(/\W+/, "_") %} {% module_name = (type.id + safe_name.camelcase).id %} {% parent_context_name = PARENT_CONTEXT_NAME %} @@ -23,12 +23,16 @@ module Spectator end end - macro it(description, &block) + macro it(description, source_file = __FILE__, source_line = __LINE__, &block) {% safe_name = description.id.stringify.gsub(/\W+/, "_") %} {% class_name = (safe_name.camelcase + "Example").id %} class {{class_name.id}} < ::Spectator::Example include Context + def source + Source.new({{source_file}}, {{source_line}}) + end + def run {{block.body}} end diff --git a/src/spectator/example.cr b/src/spectator/example.cr index 3b75cf8..3fdd824 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -1,9 +1,13 @@ +require "./source" + module Spectator abstract class Example macro is_expected expect(subject) end - + + abstract def source : Source + abstract def run end end diff --git a/src/spectator/source.cr b/src/spectator/source.cr new file mode 100644 index 0000000..7b4b073 --- /dev/null +++ b/src/spectator/source.cr @@ -0,0 +1,15 @@ +module Spectator + class Source + getter file : String + getter line : Int32 + + def initialize(@file, @line) + end + + def to_s(io) + io << file + io << ':' + io << line + end + end +end