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.field("description", name? || "<anonymous>")
json.field("full_description", to_s)
json.field("file_path", location.path)
json.field("line_number", location.line)
if location = location?
json.field("file_path", location.path)
json.field("line_number", location.line)
end
@result.to_json(json) if @finished
end
end

View file

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

View file

@ -11,9 +11,16 @@ module Spectator::Formatting::Components
# Produces output for running the previously specified example.
def to_s(io)
io << "crystal spec "
io << @example.location
io << ' '
io << Comment.colorize(@example.to_s)
# Use location for argument if it's available, since it's simpler.
# 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

View file

@ -13,7 +13,7 @@ module Spectator::Formatting::Components::JUnit
def self.from_report(report)
hostname = System.hostname
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|
TestSuite.from_examples(file, examples, hostname)
end

View file

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

View file

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

View file

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

View file

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

View file

@ -28,8 +28,11 @@ module Spectator::Formatting
@json.start_object
@json.field("description", example.name? || "<anonymous>")
@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
# 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.
def includes?(example) : Bool
start_line = example.location.line
end_line = example.location.end_line
return false unless location = example.location?
start_line = location.line
end_line = location.end_line
(start_line..end_line).covers?(@line)
end
end

View file

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