diff --git a/src/spectator/fail_result.cr b/src/spectator/fail_result.cr index 41a4aba..f331d07 100644 --- a/src/spectator/fail_result.cr +++ b/src/spectator/fail_result.cr @@ -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" diff --git a/src/spectator/formatting/components/example_command.cr b/src/spectator/formatting/components/example_command.cr index 8b3c0b9..92d7627 100644 --- a/src/spectator/formatting/components/example_command.cr +++ b/src/spectator/formatting/components/example_command.cr @@ -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 diff --git a/src/spectator/formatting/components/failure_command_list.cr b/src/spectator/formatting/components/failure_command_list.cr index 7da5ed2..177cf1a 100644 --- a/src/spectator/formatting/components/failure_command_list.cr +++ b/src/spectator/formatting/components/failure_command_list.cr @@ -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