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) # expect(&.size).to eq(5)
# ``` # ```
# The method passed will always be evaluated on the subject. # 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) %} {% if block.is_a?(Nop) %}
{% raise "Argument or block must be provided to expect" %} {% raise "Argument or block must be provided to expect" %}
{% end %} {% end %}
@ -76,7 +76,7 @@ module Spectator
{% raise "Unexpected block arguments in expect call" %} {% raise "Unexpected block arguments in expect call" %}
{% end %} {% 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) ::Spectator::Expectations::ExpectationPartial.new(%test_block, %source)
end end

View file

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

View file

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