From 79a095bb3106bec2eb2c174d5a1d918c2a0afad8 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 1 Aug 2019 15:00:56 -0600 Subject: [PATCH] Use new style expectations and actuals in DSL --- src/spectator/dsl/example_dsl.cr | 11 ++++++++--- src/spectator/should.cr | 16 ++++++++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/spectator/dsl/example_dsl.cr b/src/spectator/dsl/example_dsl.cr index f518b53..87261c6 100644 --- a/src/spectator/dsl/example_dsl.cr +++ b/src/spectator/dsl/example_dsl.cr @@ -19,7 +19,9 @@ module Spectator::DSL # Where the actual value is returned by the system-under-test, # and the expected value is what the actual value should be to satisfy the condition. macro expect(actual, _source_file = __FILE__, _source_line = __LINE__) - ::Spectator::Expectations::ValueExpectationPartial.new({{actual}}, {{actual.stringify}}, {{_source_file}}, {{_source_line}}) + value_actual = ::Spectator::Expectations::ValueActual.new({{actual.stringify}}, {{actual}}) + source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) + ::Spectator::Expectations::ExpectationPartial.new(value_actual, source) end # Starts an expectation on a block of code. @@ -68,12 +70,15 @@ module Spectator::DSL # The raw block can't be used because it's not clear to the user. {% method_name = block.body.id.split('.')[1..-1].join('.') %} %partial = %proc.partial(subject) - ::Spectator::Expectations::BlockExpectationPartial.new(%partial, {{"#" + method_name}}, {{_source_file}}, {{_source_line}}) + block_actual = ::Spectator::Expectations::ValueActual.new({{"#" + method_name}}, %partial) {% else %} # In this case, it looks like the short-hand method syntax wasn't used. # Just drop in the proc as-is. - ::Spectator::Expectations::BlockExpectationPartial.new(%proc, "`" + {{block.body.stringify}} + "`", {{_source_file}}, {{_source_line}}) + block_actual = ::Spectator::Expectations::ValueActual.new({{"`" + block.body.stringify + "`"}}, %proc) {% end %} + + source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) + ::Spectator::Expectations::ExpectationPartial.new(block_actual, source) end # Starts an expectation. diff --git a/src/spectator/should.cr b/src/spectator/should.cr index 2cfcf2d..0653a0e 100644 --- a/src/spectator/should.cr +++ b/src/spectator/should.cr @@ -21,13 +21,17 @@ class Object # However, since this isn't a macro and we can't "look behind" this method call # to see what it was invoked on, the argument is an empty string. # Additionally, the source file and line can't be obtained. - ::Spectator::Expectations::ValueExpectationPartial.new(self, __FILE__, __LINE__).to(matcher) + actual = ::Spectator::Expectations::ValueActual.new(self) + source = ::Spectator::Source.new(__FILE__, __LINE__) + ::Spectator::Expectations::ExpectationPartial.new(actual, source).to(matcher) end # Works the same as `#should` except the condition is inverted. # When `#should` succeeds, this method will fail, and vice-versa. def should_not(matcher : ::Spectator::Matchers::Matcher) - ::Spectator::Expectations::ValueExpectationPartial.new(self, __FILE__, __LINE__).to_not(matcher) + actual = ::Spectator::Expectations::ValueActual.new(self) + source = ::Spectator::Source.new(__FILE__, __LINE__) + ::Spectator::Expectations::ExpectationPartial.new(actual, source).to_not(matcher) end end @@ -35,12 +39,16 @@ struct Proc(*T, R) # Extension method to create an expectation for a block of code (proc). # Depending on the matcher, the proc may be executed multiple times. def should(matcher : ::Spectator::Matchers::Matcher) - ::Spectator::Expectations::BlockExpectationPartial.new(self, __FILE__, __LINE__).to(matcher) + actual = ::Spectator::Expectations::BlockActual.new(self) + source = ::Spectator::Source.new(__FILE__, __LINE__) + ::Spectator::Expectations::ExpectationPartial.new(actual, source).to(matcher) end # Works the same as `#should` except the condition is inverted. # When `#should` succeeds, this method will fail, and vice-versa. def should_not(matcher : ::Spectator::Matchers::Matcher) - ::Spectator::Expectations::BlockExpectationPartial.new(self, __FILE__, __LINE__).to_not(matcher) + actual = ::Spectator::Expectations::BlockActual.new(self) + source = ::Spectator::Source.new(__FILE__, __LINE__) + ::Spectator::Expectations::BlockExpectationPartial.new(actual, source).to_not(matcher) end end