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"
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.
{% method_name = block.body.id.split('.')[1..-1].join('.') %}
%partial = %proc.partial(subject)
test_block = ::Spectator::TestBlock.new(%partial, {{"#" + method_name}})
test_block = ::Spectator::TestBlock.create(%partial, {{"#" + method_name}})
{% else %}
# In this case, it looks like the short-hand method syntax wasn't used.
# 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 %}
source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})

View File

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

View File

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