diff --git a/src/spectator/expectations/actual.cr b/src/spectator/expectations/actual.cr new file mode 100644 index 0000000..a62a11a --- /dev/null +++ b/src/spectator/expectations/actual.cr @@ -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 diff --git a/src/spectator/expectations/block_actual.cr b/src/spectator/expectations/block_actual.cr new file mode 100644 index 0000000..aad9afd --- /dev/null +++ b/src/spectator/expectations/block_actual.cr @@ -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("") + end + + # Reports complete information about the actual value. + def inspect(io) + io << label + io << " -> " + io << value + end + end +end diff --git a/src/spectator/expectations/value_actual.cr b/src/spectator/expectations/value_actual.cr new file mode 100644 index 0000000..c5a6de4 --- /dev/null +++ b/src/spectator/expectations/value_actual.cr @@ -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