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.
abstract class Example < ExampleComponent
@finished = false
@description : String? = nil
protected setter description
# Indicates whether the example has already been run.
def finished? : Bool
@ -23,11 +26,13 @@ module Spectator
@test_wrapper.source
end
def description : String | Symbol?
@test_wrapper.description
def description : String | Symbol
@description || @test_wrapper.description
end
def symbolic? : Bool
return false unless @test_wrapper.description?
description = @test_wrapper.description
description.starts_with?('#') || description.starts_with?('.')
end

View file

@ -44,6 +44,10 @@ module Spectator::Expectations
values?.not_nil!
end
def description
@match_data.description
end
# Creates the JSON representation of the expectation.
def to_json(json : ::JSON::Builder)
json.object do

View file

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