From 562b544223f40ac3c079f490614c99e85204d4fa Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 24 Sep 2018 22:51:26 -0600 Subject: [PATCH] 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. --- src/spectator/dsl/example_dsl.cr | 4 ++-- src/spectator/dsl/matcher_dsl.cr | 4 ++-- src/spectator/expectation.cr | 3 ++- src/spectator/matchers/equality_matcher.cr | 3 ++- src/spectator/matchers/matcher.cr | 5 +++++ 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/spectator/dsl/example_dsl.cr b/src/spectator/dsl/example_dsl.cr index ba1d49c..2bc13bf 100644 --- a/src/spectator/dsl/example_dsl.cr +++ b/src/spectator/dsl/example_dsl.cr @@ -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 diff --git a/src/spectator/dsl/matcher_dsl.cr b/src/spectator/dsl/matcher_dsl.cr index 403a1c8..7841149 100644 --- a/src/spectator/dsl/matcher_dsl.cr +++ b/src/spectator/dsl/matcher_dsl.cr @@ -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 diff --git a/src/spectator/expectation.cr b/src/spectator/expectation.cr index f7e296d..4bec79d 100644 --- a/src/spectator/expectation.cr +++ b/src/spectator/expectation.cr @@ -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) diff --git a/src/spectator/matchers/equality_matcher.cr b/src/spectator/matchers/equality_matcher.cr index f9a3000..1c3b00b 100644 --- a/src/spectator/matchers/equality_matcher.cr +++ b/src/spectator/matchers/equality_matcher.cr @@ -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) diff --git a/src/spectator/matchers/matcher.cr b/src/spectator/matchers/matcher.cr index 7de1d15..3b847b2 100644 --- a/src/spectator/matchers/matcher.cr +++ b/src/spectator/matchers/matcher.cr @@ -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