Pass subject to proc via partial

This commit is contained in:
Michael Miller 2019-03-21 21:09:46 -06:00
parent e552cbffd2
commit 72ccafb8b7

View file

@ -49,6 +49,9 @@ module Spectator::DSL
{% raise "Argument or block must be provided to expect" %} {% raise "Argument or block must be provided to expect" %}
{% end %} {% end %}
# Create a proc to capture the block.
%proc = ->({{block.args.splat}}) {{block}}
# Check if the short-hand method syntax is used. # Check if the short-hand method syntax is used.
# This is a hack, since macros don't get this as a "literal" or something similar. # This is a hack, since macros don't get this as a "literal" or something similar.
# The Crystal compiler will translate: # The Crystal compiler will translate:
@ -64,12 +67,11 @@ module Spectator::DSL
# Extract the method name to make it clear to the user what is tested. # Extract the method name to make it clear to the user what is tested.
# The raw block can't be used because it's not clear to the user. # The raw block can't be used because it's not clear to the user.
{% method_name = block.body.id.split('.')[1..-1].join('.') %} {% method_name = block.body.id.split('.')[1..-1].join('.') %}
# TODO: Maybe pass the subject in as __arg0 instead of prefixing the method name. %partial = %proc.partial(subject)
::Spectator::Expectations::ValueExpectationPartial.new(subject.{{method_name.id}}, {{"#" + method_name}}, {{_source_file}}, {{_source_line}}) ::Spectator::Expectations::BlockExpectationPartial.new(%partial, {{"#" + method_name}}, {{_source_file}}, {{_source_line}})
{% else %} {% else %}
# In this case, it looks like the short-hand method syntax wasn't used. # In this case, it looks like the short-hand method syntax wasn't used.
# Just drop in the block as-is. # Just drop in the proc as-is.
%proc = ->({{block.args.splat}}) {{block}}
::Spectator::Expectations::BlockExpectationPartial.new(%proc, {{block.body.stringify}}, {{_source_file}}, {{_source_line}}) ::Spectator::Expectations::BlockExpectationPartial.new(%proc, {{block.body.stringify}}, {{_source_file}}, {{_source_line}})
{% end %} {% end %}
end end