Bubble up matcher description

The last run matcher's description is used if one wasn't provided by the 
user.
Only applies to examples that actually run and have expectations.
This commit is contained in:
Michael Miller 2020-01-02 19:19:55 -07:00
parent d64ecc4192
commit 88ed415191
3 changed files with 12 additions and 2 deletions

View file

@ -6,6 +6,9 @@ module Spectator
# Concrete types must implement the `#run_impl` method. # Concrete types must implement the `#run_impl` method.
abstract class Example < ExampleComponent abstract class Example < ExampleComponent
@finished = false @finished = false
@description : String? = nil
protected setter description
# Indicates whether the example has already been run. # Indicates whether the example has already been run.
def finished? : Bool def finished? : Bool
@ -23,11 +26,13 @@ module Spectator
@test_wrapper.source @test_wrapper.source
end end
def description : String | Symbol? def description : String | Symbol
@test_wrapper.description @description || @test_wrapper.description
end end
def symbolic? : Bool def symbolic? : Bool
return false unless @test_wrapper.description?
description = @test_wrapper.description description = @test_wrapper.description
description.starts_with?('#') || description.starts_with?('.') description.starts_with?('#') || description.starts_with?('.')
end end

View file

@ -44,6 +44,10 @@ module Spectator::Expectations
values?.not_nil! values?.not_nil!
end end
def description
@match_data.description
end
# 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

View file

@ -46,6 +46,7 @@ module Spectator
# Reports the outcome of an expectation. # Reports the outcome of an expectation.
# An exception will be raised when a failing result is given. # An exception will be raised when a failing result is given.
def report_expectation(expectation : Expectations::Expectation) : Nil def report_expectation(expectation : Expectations::Expectation) : Nil
@example.description = expectation.description unless @example.test_wrapper.description?
@reporter.report(expectation) @reporter.report(expectation)
end end