Help infer the block's return type

This commit is contained in:
Michael Miller 2019-08-09 11:29:53 -06:00
parent e763296c26
commit 751c15434b
3 changed files with 16 additions and 2 deletions

View file

@ -1,3 +1,7 @@
require "../expectations/expectation_partial"
require "../source"
require "../test_block"
require "../test_value"
require "./matcher_dsl" require "./matcher_dsl"
module Spectator::DSL module Spectator::DSL
@ -70,11 +74,11 @@ module Spectator::DSL
# 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('.') %}
%partial = %proc.partial(subject) %partial = %proc.partial(subject)
test_block = ::Spectator::TestBlock.new(%partial, {{"#" + method_name}}) test_block = ::Spectator::TestBlock.create(%partial, {{"#" + method_name}})
{% 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 proc as-is. # Just drop in the proc as-is.
test_block = ::Spectator::TestBlock.new(%proc, {{"`" + block.body.stringify + "`"}}) test_block = ::Spectator::TestBlock.create(%proc, {{"`" + block.body.stringify + "`"}})
{% end %} {% end %}
source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})

View file

@ -1,4 +1,6 @@
require "../matchers" require "../matchers"
require "../test_block"
require "../test_value"
module Spectator::DSL module Spectator::DSL
# Methods for defining matchers for expectations. # Methods for defining matchers for expectations.

View file

@ -14,12 +14,20 @@ module Spectator
super(label) super(label)
end end
def self.create(proc : -> T, label : String) forall T
TestBlock(T).new(proc, label)
end
# Creates the block expression with a generic label. # Creates the block expression with a generic label.
# This is used for the "should" syntax and when the label doesn't matter. # This is used for the "should" syntax and when the label doesn't matter.
def initialize(@proc : -> ReturnType) def initialize(@proc : -> ReturnType)
super("<Proc>") super("<Proc>")
end end
def self.create(proc : -> T) forall T
TestBlock(T).new(proc)
end
# Reports complete information about the expression. # Reports complete information about the expression.
def inspect(io) def inspect(io)
io << label io << label