shard-spectator/src/spectator/block.cr

35 lines
1.2 KiB
Crystal
Raw Normal View History

require "./expression"
2021-01-09 07:13:22 +00:00
require "./label"
module Spectator
# Represents a block from a test.
# This is typically captured by an `expect` macro.
2021-11-28 22:45:17 +00:00
# It consists of a label and parameter-less block.
2021-01-09 07:13:22 +00:00
# The label should be a string recognizable by the user,
# or nil if one isn't available.
class Block(T) < Expression(T)
2021-01-09 07:13:22 +00:00
# Creates the block expression from a proc.
# The *proc* will be called to evaluate the value of the expression.
# The *label* is usually the Crystal code for the *proc*.
# It can be nil if it isn't available.
2021-01-16 18:11:42 +00:00
def initialize(@block : -> T, label : Label = nil)
2021-01-09 07:13:22 +00:00
super(label)
end
# Creates the block expression by capturing a block as a proc.
# The block will be called to evaluate the value of the expression.
# The *label* is usually the Crystal code for the *block*.
# It can be nil if it isn't available.
2021-01-16 18:11:42 +00:00
def initialize(label : Label = nil, &@block : -> T)
2021-01-09 07:13:22 +00:00
super(label)
end
# Evaluates the block and returns the value from it.
# This method _does not_ cache the resulting value like `#value` does.
# Successive calls to this method may return different values.
def value : T
2021-01-09 07:13:22 +00:00
@block.call
end
end
end