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) expect(subject)
end end
def expect(actual : T) forall T macro expect(actual)
::Spectator::Expectation.new(actual) ::Spectator::Expectation.new({{actual.stringify}}, {{actual}})
end end
end end
end end

View file

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

View file

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

View file

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

View file

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