Add source file and line number to expectations

This commit is contained in:
Michael Miller 2019-02-14 16:04:09 -07:00
parent 36c98db0ae
commit 61e92951ae
4 changed files with 20 additions and 14 deletions

View file

@ -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

View file

@ -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

View file

@ -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.

View file

@ -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