mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
New "actual" types
This commit is contained in:
parent
b6ac736dd2
commit
42b916bdf7
3 changed files with 80 additions and 0 deletions
23
src/spectator/expectations/actual.cr
Normal file
23
src/spectator/expectations/actual.cr
Normal 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
|
30
src/spectator/expectations/block_actual.cr
Normal file
30
src/spectator/expectations/block_actual.cr
Normal 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
|
27
src/spectator/expectations/value_actual.cr
Normal file
27
src/spectator/expectations/value_actual.cr
Normal 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
|
Loading…
Reference in a new issue