From 2ecd4ded6d7f9ac6c3cf10c47652c82aab0e81e9 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 8 Jan 2021 15:32:15 -0700 Subject: [PATCH 1/2] Fix line numbers being wrong with macro expansion Addresses https://github.com/icy-arctic-fox/spectator/issues/18 --- spec/line_number_spec.cr | 27 +++++++++++++++++++++++++++ src/spectator/dsl/assertions.cr | 4 ++-- src/spectator/dsl/examples.cr | 8 ++++---- src/spectator/dsl/groups.cr | 12 ++++++------ 4 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 spec/line_number_spec.cr diff --git a/spec/line_number_spec.cr b/spec/line_number_spec.cr new file mode 100644 index 0000000..86d858b --- /dev/null +++ b/spec/line_number_spec.cr @@ -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 diff --git a/src/spectator/dsl/assertions.cr b/src/spectator/dsl/assertions.cr index 11b5ffe..038e45a 100644 --- a/src/spectator/dsl/assertions.cr +++ b/src/spectator/dsl/assertions.cr @@ -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 diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index bcb1425..58bef0f 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -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, diff --git a/src/spectator/dsl/groups.cr b/src/spectator/dsl/groups.cr index 5a37715..492cbfb 100644 --- a/src/spectator/dsl/groups.cr +++ b/src/spectator/dsl/groups.cr @@ -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 From c2d5bdd5d12ffb5e8e100fd36301547c2630158f Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 8 Jan 2021 15:32:42 -0700 Subject: [PATCH 2/2] Bump version to 0.9.31 --- shard.yml | 2 +- src/spectator.cr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shard.yml b/shard.yml index b506fdf..5999a92 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: spectator -version: 0.9.30 +version: 0.9.31 description: | A feature-rich spec testing framework for Crystal with similarities to RSpec. diff --git a/src/spectator.cr b/src/spectator.cr index e56f7ee..6e99407 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -6,7 +6,7 @@ module Spectator extend self # Current version of the Spectator library. - VERSION = "0.9.30" + VERSION = "0.9.31" # Top-level describe method. # All specs in a file must be wrapped in this call.