Add basics for tracking source code location

This commit is contained in:
Michael Miller 2018-09-04 13:36:19 -06:00
parent b8dcf35165
commit de55e31407
4 changed files with 29 additions and 6 deletions

View File

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

View File

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

View File

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

15
src/spectator/source.cr Normal file
View File

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