Capture expression string from expectations

This allows a string that the user had in source code instead of a resolved value.

This change requires storing a 'label' and changing expectation and matcher constructor methods to macros.
This commit is contained in:
Michael Miller 2018-09-24 22:51:26 -06:00
parent ef7e0462cb
commit 562b544223
5 changed files with 13 additions and 6 deletions

View File

@ -9,8 +9,8 @@ module Spectator
expect(subject)
end
def expect(actual : T) forall T
::Spectator::Expectation.new(actual)
macro expect(actual)
::Spectator::Expectation.new({{actual.stringify}}, {{actual}})
end
end
end

View File

@ -3,8 +3,8 @@ require "../matchers"
module Spectator
module DSL
module MatcherDSL
def eq(expected : T) forall T
::Spectator::Matchers::EqualityMatcher(T).new(expected)
macro eq(expected)
::Spectator::Matchers::EqualityMatcher.new({{expected.stringify}}, {{expected}})
end
end
end

View File

@ -2,9 +2,10 @@ require "./matchers/matcher"
module Spectator
class Expectation(T)
private getter label : String
getter actual : T
protected def initialize(@actual : T)
protected def initialize(@label : String, @actual : T)
end
def to(matcher : Matchers::Matcher)

View File

@ -3,7 +3,8 @@ require "./matcher"
module Spectator
module Matchers
class EqualityMatcher(T) < Matcher
def initialize(@expected : T)
def initialize(label, @expected : T)
super(label)
end
def match?(expectation : Expectation)

View File

@ -1,6 +1,11 @@
module Spectator
module Matchers
abstract class Matcher
private getter label : String
private def initialize(@label : String)
end
abstract def match?(expectation : Expectation)
end
end