Fix line numbers being wrong with macro expansion

Addresses https://github.com/icy-arctic-fox/spectator/issues/18
This commit is contained in:
Michael Miller 2021-01-08 15:32:15 -07:00
parent 891cd4bbf7
commit 2ecd4ded6d
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
4 changed files with 39 additions and 12 deletions

27
spec/line_number_spec.cr Normal file
View file

@ -0,0 +1,27 @@
require "./spec_helper"
Spectator.describe Spectator do
let(current_example) { ::Spectator::Harness.current.example }
subject(source) { current_example.source }
context "line numbers" do
subject { source.line }
it "match source code" do
is_expected.to eq(__LINE__ - 1)
end
it "handles multiple lines and examples" do
# Offset is important.
is_expected.to eq(__LINE__ - 2)
end
end
context "file names" do
subject { source.file }
it "match source code" do
is_expected.to eq(__FILE__)
end
end
end

View file

@ -45,7 +45,7 @@ module Spectator
# expect(&.size).to eq(5)
# ```
# The method passed will always be evaluated on the subject.
macro expect(_source_file = __FILE__, _source_line = __LINE__, &block)
macro expect(&block)
{% if block.is_a?(Nop) %}
{% raise "Argument or block must be provided to expect" %}
{% end %}
@ -76,7 +76,7 @@ module Spectator
{% raise "Unexpected block arguments in expect call" %}
{% end %}
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
%source = ::Spectator::Source.new({{block.filename}}, {{block.line_number}})
::Spectator::Expectations::ExpectationPartial.new(%test_block, %source)
end

View file

@ -3,7 +3,7 @@ require "../spec_builder"
module Spectator
module DSL
macro it(description = nil, _source_file = __FILE__, _source_line = __LINE__, &block)
macro it(description = nil, &block)
{% if block.is_a?(Nop) %}
{% if description.is_a?(Call) %}
def %run
@ -18,7 +18,7 @@ module Spectator
end
{% end %}
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
%source = ::Spectator::Source.new({{block.filename}}, {{block.line_number}})
::Spectator::SpecBuilder.add_example(
{{description.is_a?(StringLiteral) || description.is_a?(StringInterpolation) || description.is_a?(NilLiteral) ? description : description.stringify}},
%source,
@ -30,7 +30,7 @@ module Spectator
it({{description}}) {{block}}
end
macro pending(description = nil, _source_file = __FILE__, _source_line = __LINE__, &block)
macro pending(description = nil, &block)
{% if block.is_a?(Nop) %}
{% if description.is_a?(Call) %}
def %run
@ -45,7 +45,7 @@ module Spectator
end
{% end %}
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
%source = ::Spectator::Source.new({{block.filename}}, {{block.line_number}})
::Spectator::SpecBuilder.add_pending_example(
{{description.is_a?(StringLiteral) || description.is_a?(StringInterpolation) || description.is_a?(NilLiteral) ? description : description.stringify}},
%source,

View file

@ -2,7 +2,7 @@ require "../spec_builder"
module Spectator
module DSL
macro context(what, _source_file = __FILE__, _source_line = __LINE__, &block)
macro context(what, &block)
class Context%context < {{@type.id}}
{%
description = if what.is_a?(StringLiteral) || what.is_a?(StringInterpolation)
@ -16,7 +16,7 @@ module Spectator
end
%}
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
%source = ::Spectator::Source.new({{block.filename}}, {{block.line_number}})
::Spectator::SpecBuilder.start_group({{description}}, %source)
# Oddly, `#resolve?` can return a constant's value, which isn't a TypeNode.
@ -49,7 +49,7 @@ module Spectator
context({{what}}) {{block}}
end
macro sample(collection, count = nil, _source_file = __FILE__, _source_line = __LINE__, &block)
macro sample(collection, count = nil, &block)
{% name = block.args.empty? ? :value.id : block.args.first.id %}
def %collection
@ -65,7 +65,7 @@ module Spectator
end
class Context%sample < {{@type.id}}
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
%source = ::Spectator::Source.new({{block.filename}}, {{block.line_number}})
::Spectator::SpecBuilder.start_sample_group({{collection.stringify}}, %source, :%sample, {{name.stringify}}) do |values|
sample = {{@type.id}}.new(values)
sample.%to_a
@ -81,7 +81,7 @@ module Spectator
end
end
macro random_sample(collection, count = nil, _source_file = __FILE__, _source_line = __LINE__, &block)
macro random_sample(collection, count = nil, &block)
{% name = block.args.empty? ? :value.id : block.args.first.id %}
def %collection
@ -97,7 +97,7 @@ module Spectator
end
class Context%sample < {{@type.id}}
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
%source = ::Spectator::Source.new({{block.filename}}, {{block.line_number}})
::Spectator::SpecBuilder.start_sample_group({{collection.stringify}}, %source, :%sample, {{name.stringify}}) do |values|
sample = {{@type.id}}.new(values)
collection = sample.%to_a