Simplify method chains by using #compact_map

This commit is contained in:
Michael Miller 2019-02-17 13:40:10 -07:00
parent 3a508cae26
commit 0b06e72f7e
2 changed files with 4 additions and 10 deletions

View file

@ -22,7 +22,7 @@ def new_report(successful_count = 5, failed_count = 5, error_count = 5, pending_
error_count.times { results << new_failure_result(Spectator::ErroredResult) }
pending_count.times { results << new_pending_result }
example_runtime = results.select(&.is_a?(Spectator::FinishedResult)).map(&.as(Spectator::FinishedResult)).sum(&.elapsed)
example_runtime = results.compact_map(&.as?(Spectator::FinishedResult)).sum(&.elapsed)
total_runtime = example_runtime + Time::Span.new(nanoseconds: overhead_time)
Spectator::Report.new(results, total_runtime)
end

View file

@ -38,25 +38,19 @@ module Spectator
# Returns a set of results for all failed examples.
def failures
@results.select(&.is_a?(FailedResult)).map(&.as(FailedResult))
@results.compact_map(&.as?(FailedResult))
end
# Returns a set of results for all errored examples.
def errors
@results.select(&.is_a?(ErroredResult)).map(&.as(ErroredResult))
@results.compact_map(&.as?(ErroredResult))
end
# Length of time it took to run just example code.
# This does not include hooks,
# but it does include pre- and post-conditions.
def example_runtime
@results.sum do |result|
if result.is_a?(FinishedResult)
result.elapsed
else
Time::Span.zero
end
end
@results.each.compact_map(&.as?(FinishedResult)).sum(&.elapsed)
end
# Length of time spent in framework processes and hooks.