New "actual" types

This commit is contained in:
Michael Miller 2019-07-31 20:01:39 -06:00
parent b6ac736dd2
commit 42b916bdf7
3 changed files with 80 additions and 0 deletions

View file

@ -0,0 +1,23 @@
module Spectator::Expectations
# Base class for capturing an actual value - the thing being checked/tested.
abstract struct Actual
# User-friendly string displayed for the actual expression being tested.
# For instance, in the expectation:
# ```
# expect(foo).to eq(bar)
# ```
# This property will be "foo".
# It will be the literal string "foo",
# and not the actual value of the foo.
getter label : String
# Creates the common base of the actual value.
def initialize(@label)
end
# String representation of the actual value.
def to_s(io)
io << label
end
end
end

View file

@ -0,0 +1,30 @@
require "./actual"
module Spectator::Expectations
# Captures an actual block and its label.
struct BlockActual(ReturnType) < Actual
# Calls the block and retrieves the value.
def value : ReturnType
@proc.call
end
# Creates the actual with a custom label.
# Typically the label is the code in the block/proc.
def initialize(label : String, @proc : -> ReturnType)
super(label)
end
# Creates the actual 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
# Reports complete information about the actual value.
def inspect(io)
io << label
io << " -> "
io << value
end
end
end

View file

@ -0,0 +1,27 @@
require "./actual"
module Spectator::Expectations
# Captures an actual value and its label.
struct ValueActual(T) < Actual
# Actual value.
getter value : T
# Creates the actual with a custom label.
def initialize(label : String, @value)
super(label)
end
# Creates the actual with a stringified value.
# This is used for the "should" syntax and when the label doesn't matter.
def initialize(@value)
super(@value.to_s)
end
# Reports complete information about the actual value.
def inspect(io)
io << label
io << '='
io << @value
end
end
end