Use failure location in output if available

Fixes https://gitlab.com/arctic-fox/spectator/-/issues/57
This commit is contained in:
Michael Miller 2021-07-17 16:04:19 -06:00
parent 9c7f39ba45
commit 7e2b267e93
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
3 changed files with 22 additions and 3 deletions

View file

@ -1,4 +1,6 @@
require "json"
require "./example_failed"
require "./location"
require "./result"
module Spectator
@ -36,6 +38,15 @@ module Spectator
true
end
# Attempts to retrieve the location where the example failed.
# This only works if the location of the failed expectation was reported.
# If available, returns a `Location`, otherwise `nil`.
def source : Location?
return unless error = @error.as?(ExampleFailed)
error.location?
end
# One-word description of the result.
def to_s(io)
io << "fail"

View file

@ -1,11 +1,14 @@
require "../../example"
require "../../location"
require "./comment"
module Spectator::Formatting::Components
# Provides syntax for running a specific example from the command-line.
struct ExampleCommand
# Creates the component with the specified example.
def initialize(@example : Example)
# The location can be overridden, for instance, pointing to a problematic line in the example.
# Otherwise the example's location is used.
def initialize(@example : Example, @location : Location? = nil)
end
# Produces output for running the previously specified example.
@ -14,7 +17,7 @@ module Spectator::Formatting::Components
# Use location for argument if it's available, since it's simpler.
# Otherwise, use the example name filter argument.
if location = @example.location?
if location = (@location || @example.location?)
io << location
else
io << "-e " << @example

View file

@ -14,7 +14,12 @@ module Spectator::Formatting::Components
io.puts "Failed examples:"
io.puts
@failures.each do |failure|
io.puts ExampleCommand.new(failure).colorize(:red)
# Use failed location if it's available.
if (result = failure.result).responds_to?(:source)
location = result.source
end
io.puts ExampleCommand.new(failure, location).colorize(:red)
end
end
end