Handle nil location

This commit is contained in:
Michael Miller 2021-06-02 23:35:41 -06:00
parent 39e917ce57
commit 835fa40773
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
11 changed files with 47 additions and 19 deletions

View file

@ -169,8 +169,10 @@ module Spectator
json.object do json.object do
json.field("description", name? || "<anonymous>") json.field("description", name? || "<anonymous>")
json.field("full_description", to_s) json.field("full_description", to_s)
if location = location?
json.field("file_path", location.path) json.field("file_path", location.path)
json.field("line_number", location.line) json.field("line_number", location.line)
end
@result.to_json(json) if @finished @result.to_json(json) if @finished
end end
end end

View file

@ -56,8 +56,10 @@ module Spectator
# Creates the JSON representation of the expectation. # Creates the JSON representation of the expectation.
def to_json(json : JSON::Builder) def to_json(json : JSON::Builder)
json.object do json.object do
json.field("file_path", @location.path) if location = @location
json.field("line_number", @location.line) json.field("file_path", location.path)
json.field("line_number", location.line)
end
json.field("satisfied", satisfied?) json.field("satisfied", satisfied?)
if (failed = @match_data.as?(Matchers::FailedMatchData)) if (failed = @match_data.as?(Matchers::FailedMatchData))
failed_to_json(failed, json) failed_to_json(failed, json)

View file

@ -11,9 +11,16 @@ module Spectator::Formatting::Components
# Produces output for running the previously specified example. # Produces output for running the previously specified example.
def to_s(io) def to_s(io)
io << "crystal spec " io << "crystal spec "
io << @example.location
io << ' ' # Use location for argument if it's available, since it's simpler.
io << Comment.colorize(@example.to_s) # Otherwise, use the example name filter argument.
if location = @example.location?
io << location
else
io << "-e " << @example
end
io << ' ' << Comment.colorize(@example.to_s)
end end
end end
end end

View file

@ -13,7 +13,7 @@ module Spectator::Formatting::Components::JUnit
def self.from_report(report) def self.from_report(report)
hostname = System.hostname hostname = System.hostname
counts = report.counts counts = report.counts
suites = report.examples.group_by(&.location.file) suites = report.examples.group_by { |example| example.location?.try(&.path) || "anonymous" }
suites = suites.map do |file, examples| suites = suites.map do |file, examples|
TestSuite.from_examples(file, examples, hostname) TestSuite.from_examples(file, examples, hostname)
end end

View file

@ -26,7 +26,7 @@ module Spectator::Formatting::Components::JUnit
private def self.package_name_from_file(file) private def self.package_name_from_file(file)
path = Path.new(file.to_s) path = Path.new(file.to_s)
name = path.stem name = path.stem
directory = path.relative_to(Dir.current).dirname directory = path.dirname
package = directory.gsub(File::SEPARATOR, '.') package = directory.gsub(File::SEPARATOR, '.')
{package, name} {package, name}
end end

View file

@ -29,8 +29,13 @@ module Spectator::Formatting::Components
io.puts example io.puts example
io << " " io << " "
io << Runtime.new(example.result.elapsed).colorize.bold io << Runtime.new(example.result.elapsed).colorize.bold
if location = example.location
io << ' ' io << ' '
io.puts example.location io.puts location
else
io.puts
end
end end
end end
end end

View file

@ -71,8 +71,10 @@ module Spectator::Formatting::Components
source = if (result = @example.result).responds_to?(:source) source = if (result = @example.result).responds_to?(:source)
result.source result.source
else else
@example.location @example.location?
end end
return unless source
line(io) { io << Comment.colorize(source) } line(io) { io << Comment.colorize(source) }
end end

View file

@ -30,8 +30,13 @@ module Spectator::Formatting::Components
io.puts example io.puts example
io << "# " io << "# "
io << Runtime.new(example.result.elapsed) io << Runtime.new(example.result.elapsed)
if location = example.location?
io << ' ' io << ' '
io.puts example.location io.puts location
else
io.puts
end
end end
end end
end end

View file

@ -28,8 +28,11 @@ module Spectator::Formatting
@json.start_object @json.start_object
@json.field("description", example.name? || "<anonymous>") @json.field("description", example.name? || "<anonymous>")
@json.field("full_description", example) @json.field("full_description", example)
@json.field("file_path", example.location.path)
@json.field("line_number", example.location.line) if location = example.location?
@json.field("file_path", location.path)
@json.field("line_number", location.line)
end
end end
# Adds fields to the example object for all result types known after the example completes. # Adds fields to the example object for all result types known after the example completes.

View file

@ -7,8 +7,10 @@ module Spectator
# Checks whether the example satisfies the filter. # Checks whether the example satisfies the filter.
def includes?(example) : Bool def includes?(example) : Bool
start_line = example.location.line return false unless location = example.location?
end_line = example.location.end_line
start_line = location.line
end_line = location.end_line
(start_line..end_line).covers?(@line) (start_line..end_line).covers?(@line)
end end
end end

View file

@ -8,7 +8,7 @@ module Spectator
# Checks whether the example satisfies the filter. # Checks whether the example satisfies the filter.
def includes?(example) : Bool def includes?(example) : Bool
@location === example.location @location === example.location?
end end
end end
end end