Rename again

This naming is more appropriate since these types can be used for
expected and actual values.
This commit is contained in:
Michael Miller 2019-08-01 15:38:20 -06:00
parent dd69cec536
commit 763a65beac
6 changed files with 29 additions and 27 deletions

View file

@ -19,7 +19,7 @@ module Spectator::DSL
# Where the actual value is returned by the system-under-test, # Where the actual value is returned by the system-under-test,
# and the expected value is what the actual value should be to satisfy the condition. # and the expected value is what the actual value should be to satisfy the condition.
macro expect(actual, _source_file = __FILE__, _source_line = __LINE__) macro expect(actual, _source_file = __FILE__, _source_line = __LINE__)
value_actual = ::Spectator::Expectations::ActualValue.new({{actual.stringify}}, {{actual}}) test_value = ::Spectator::TestValue.new({{actual.stringify}}, {{actual}})
source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
::Spectator::Expectations::ExpectationPartial.new(value_actual, source) ::Spectator::Expectations::ExpectationPartial.new(value_actual, source)
end end
@ -70,11 +70,11 @@ module Spectator::DSL
# The raw block can't be used because it's not clear to the user. # The raw block can't be used because it's not clear to the user.
{% method_name = block.body.id.split('.')[1..-1].join('.') %} {% method_name = block.body.id.split('.')[1..-1].join('.') %}
%partial = %proc.partial(subject) %partial = %proc.partial(subject)
block_actual = ::Spectator::Expectations::ActualBlock.new({{"#" + method_name}}, %partial) test_block = ::Spectator::Expectations::TestBlock.new({{"#" + method_name}}, %partial)
{% else %} {% else %}
# In this case, it looks like the short-hand method syntax wasn't used. # In this case, it looks like the short-hand method syntax wasn't used.
# Just drop in the proc as-is. # Just drop in the proc as-is.
block_actual = ::Spectator::Expectations::ActualBlock.new({{"`" + block.body.stringify + "`"}}, %proc) test_block = ::Spectator::Expectations::TestBlock.new({{"`" + block.body.stringify + "`"}}, %proc)
{% end %} {% end %}
source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})

View file

@ -1,6 +1,6 @@
require "../matchers/match_data" require "../matchers/match_data"
require "../source" require "../source"
require "./actual" require "../test_expression"
module Spectator::Expectations module Spectator::Expectations
# Stores part of an expectation (obviously). # Stores part of an expectation (obviously).
@ -9,7 +9,7 @@ module Spectator::Expectations
struct ExpectationPartial struct ExpectationPartial
# The actual value being tested. # The actual value being tested.
# This also contains its label. # This also contains its label.
getter actual : Actual getter actual : TestExpression
# Location where this expectation was defined. # Location where this expectation was defined.
getter source : Source getter source : Source

View file

@ -21,7 +21,7 @@ class Object
# However, since this isn't a macro and we can't "look behind" this method call # However, since this isn't a macro and we can't "look behind" this method call
# to see what it was invoked on, the argument is an empty string. # to see what it was invoked on, the argument is an empty string.
# Additionally, the source file and line can't be obtained. # Additionally, the source file and line can't be obtained.
actual = ::Spectator::Expectations::ActualValue.new(self) actual = ::Spectator::Expectations::TestValue.new(self)
source = ::Spectator::Source.new(__FILE__, __LINE__) source = ::Spectator::Source.new(__FILE__, __LINE__)
::Spectator::Expectations::ExpectationPartial.new(actual, source).to(matcher) ::Spectator::Expectations::ExpectationPartial.new(actual, source).to(matcher)
end end
@ -29,7 +29,7 @@ class Object
# Works the same as `#should` except the condition is inverted. # Works the same as `#should` except the condition is inverted.
# When `#should` succeeds, this method will fail, and vice-versa. # When `#should` succeeds, this method will fail, and vice-versa.
def should_not(matcher : ::Spectator::Matchers::Matcher) def should_not(matcher : ::Spectator::Matchers::Matcher)
actual = ::Spectator::Expectations::ActualValue.new(self) actual = ::Spectator::Expectations::TestValue.new(self)
source = ::Spectator::Source.new(__FILE__, __LINE__) source = ::Spectator::Source.new(__FILE__, __LINE__)
::Spectator::Expectations::ExpectationPartial.new(actual, source).to_not(matcher) ::Spectator::Expectations::ExpectationPartial.new(actual, source).to_not(matcher)
end end
@ -39,7 +39,7 @@ struct Proc(*T, R)
# Extension method to create an expectation for a block of code (proc). # Extension method to create an expectation for a block of code (proc).
# Depending on the matcher, the proc may be executed multiple times. # Depending on the matcher, the proc may be executed multiple times.
def should(matcher : ::Spectator::Matchers::Matcher) def should(matcher : ::Spectator::Matchers::Matcher)
actual = ::Spectator::Expectations::ActualBlock.new(self) actual = ::Spectator::Expectations::TestBlock.new(self)
source = ::Spectator::Source.new(__FILE__, __LINE__) source = ::Spectator::Source.new(__FILE__, __LINE__)
::Spectator::Expectations::ExpectationPartial.new(actual, source).to(matcher) ::Spectator::Expectations::ExpectationPartial.new(actual, source).to(matcher)
end end
@ -47,7 +47,7 @@ struct Proc(*T, R)
# Works the same as `#should` except the condition is inverted. # Works the same as `#should` except the condition is inverted.
# When `#should` succeeds, this method will fail, and vice-versa. # When `#should` succeeds, this method will fail, and vice-versa.
def should_not(matcher : ::Spectator::Matchers::Matcher) def should_not(matcher : ::Spectator::Matchers::Matcher)
actual = ::Spectator::Expectations::ActualBlock.new(self) actual = ::Spectator::Expectations::TestBlock.new(self)
source = ::Spectator::Source.new(__FILE__, __LINE__) source = ::Spectator::Source.new(__FILE__, __LINE__)
::Spectator::Expectations::BlockExpectationPartial.new(actual, source).to_not(matcher) ::Spectator::Expectations::BlockExpectationPartial.new(actual, source).to_not(matcher)
end end

View file

@ -1,26 +1,26 @@
require "./actual" require "./test_expression"
module Spectator::Expectations module Spectator
# Captures an actual block and its label. # Captures an block from a test and its label.
struct ActualBlock(ReturnType) < Actual struct TestBlock(ReturnType) < TestExpression
# Calls the block and retrieves the value. # Calls the block and retrieves the value.
def value : ReturnType def value : ReturnType
@proc.call @proc.call
end end
# Creates the actual with a custom label. # Creates the block expression with a custom label.
# Typically the label is the code in the block/proc. # Typically the label is the code in the block/proc.
def initialize(label : String, @proc : -> ReturnType) def initialize(label : String, @proc : -> ReturnType)
super(label) super(label)
end end
# Creates the actual with a generic label. # Creates the block expression with a generic label.
# This is used for the "should" syntax and when the label doesn't matter. # This is used for the "should" syntax and when the label doesn't matter.
def initialize(@proc : -> ReturnType) def initialize(@proc : -> ReturnType)
super("<Proc>") super("<Proc>")
end end
# Reports complete information about the actual value. # Reports complete information about the expression.
def inspect(io) def inspect(io)
io << label io << label
io << " -> " io << " -> "

View file

@ -1,6 +1,6 @@
module Spectator::Expectations module Spectator::Expectations
# Base class for capturing an actual value - the thing being checked/tested. # Base type for capturing an expression from a test.
abstract struct Actual abstract struct TestExpression
# User-friendly string displayed for the actual expression being tested. # User-friendly string displayed for the actual expression being tested.
# For instance, in the expectation: # For instance, in the expectation:
# ``` # ```
@ -11,11 +11,11 @@ module Spectator::Expectations
# and not the actual value of the foo. # and not the actual value of the foo.
getter label : String getter label : String
# Creates the common base of the actual value. # Creates the common base of the expression.
def initialize(@label) def initialize(@label)
end end
# String representation of the actual value. # String representation of the expression.
def to_s(io) def to_s(io)
io << label io << label
end end

View file

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