From 61e92951aeecd2260128a1dec6b3eda9df6cfde6 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 14 Feb 2019 16:04:09 -0700 Subject: [PATCH] Add source file and line number to expectations --- src/spectator/dsl/example_dsl.cr | 10 +++++----- .../expectations/block_expectation_partial.cr | 8 ++++---- src/spectator/expectations/expectation_partial.cr | 8 +++++++- .../expectations/value_expectation_partial.cr | 8 ++++---- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/spectator/dsl/example_dsl.cr b/src/spectator/dsl/example_dsl.cr index 205a1c4..2c791bc 100644 --- a/src/spectator/dsl/example_dsl.cr +++ b/src/spectator/dsl/example_dsl.cr @@ -16,8 +16,8 @@ module Spectator::DSL # ``` # Where the actual value is returned by the system-under-test, # and the expected value is what the actual value should be to satisfy the condition. - macro expect(actual) - ::Spectator::Expectations::ValueExpectationPartial.new({{actual.stringify}}, {{actual}}) + macro expect(actual, _source_file = __FILE__, _source_line = __LINE__) + ::Spectator::Expectations::ValueExpectationPartial.new({{actual.stringify}}, {{_source_file}}, {{_source_line}}, {{actual}}) end # Starts an expectation on a block of code. @@ -41,7 +41,7 @@ module Spectator::DSL # expect(&.size).to eq(5) # ``` # The method passed will always be evaluated on `#subject`. - macro expect(&block) + macro expect(_source_file = __FILE__, _source_line = __LINE__, &block) {% if block.is_a?(Nop) %} {% raise "Argument or block must be provided to expect" %} {% end %} @@ -55,11 +55,11 @@ module Spectator::DSL # The raw block can't be used because it's not clear to the user. {% method_name = block.body.id.split('.').last %} # TODO: Maybe pass the subject in as __arg0 instead of prefixing with `subject.`. - ::Spectator::Expectations::ValueExpectationPartial.new({{"#" + method_name}}, subject.{{method_name.id}}) + ::Spectator::Expectations::ValueExpectationPartial.new({{"#" + method_name}}, {{_source_file}}, {{_source_line}}, subject.{{method_name.id}}) {% else %} # In this case, it looks like the short-hand method syntax wasn't used. # Just drop in the block as-is. - ::Spectator::Expectations::ValueExpectationPartial.new({{block.body.stringify}}, {{block.body}}) + ::Spectator::Expectations::ValueExpectationPartial.new({{block.body.stringify}}, {{_source_file}}, {{_source_line}}, {{block.body}}) {% end %} end diff --git a/src/spectator/expectations/block_expectation_partial.cr b/src/spectator/expectations/block_expectation_partial.cr index 18c27dc..7fd0c97 100644 --- a/src/spectator/expectations/block_expectation_partial.cr +++ b/src/spectator/expectations/block_expectation_partial.cr @@ -11,15 +11,15 @@ module Spectator::Expectations # Creates the expectation partial. # The label should be a string representation of the block. # The block is stored for later use. - protected def initialize(label : String, @block : -> ReturnType) - super(label) + protected def initialize(label, source_file, source_line, @block : -> ReturnType) + super(label, source_file, source_line) end # Creates the expectation partial. # The label is generated by calling `#to_s` on the block. # The block is stored for later use. - protected def initialize(@block : -> ReturnType) - super(@block.to_s) + protected def initialize(source_file, source_line, @block : -> ReturnType) + super(@block.to_s, source_file, source_line) end end end diff --git a/src/spectator/expectations/expectation_partial.cr b/src/spectator/expectations/expectation_partial.cr index a012787..172c187 100644 --- a/src/spectator/expectations/expectation_partial.cr +++ b/src/spectator/expectations/expectation_partial.cr @@ -17,8 +17,14 @@ module Spectator::Expectations # and not the actual value of the foo. getter label : String + # Source file the expectation originated from. + getter source_file : String + + # Line number in the source file the expectation originated from. + getter source_line : Int32 + # Creates the base of the partial. - private def initialize(@label) + private def initialize(@label, @source_file, @source_line) end # Asserts that the `#actual` value matches some criteria. diff --git a/src/spectator/expectations/value_expectation_partial.cr b/src/spectator/expectations/value_expectation_partial.cr index 9b8b648..7f364f0 100644 --- a/src/spectator/expectations/value_expectation_partial.cr +++ b/src/spectator/expectations/value_expectation_partial.cr @@ -10,15 +10,15 @@ module Spectator::Expectations # Creates the expectation partial. # The label should be a string representation of the actual value. # The actual value is stored for later use. - protected def initialize(label : String, @actual : ActualType) - super(label) + protected def initialize(label : String, source_file, source_line, @actual : ActualType) + super(label, source_file, source_line) end # Creates the expectation partial. # The label is generated by calling `#to_s` on the actual value. # The actual value is stored for later use. - protected def initialize(@actual : ActualType) - super(@actual.to_s) + protected def initialize(source_file, source_line, @actual : ActualType) + super(@actual.to_s, source_file, source_line) end end end