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