Update old references to Value and Block

This commit is contained in:
Michael Miller 2021-01-16 11:02:29 -07:00
parent 58e7981b0c
commit 4500ebcddc
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
44 changed files with 196 additions and 215 deletions

View file

@ -1,9 +1,9 @@
require "../block"
require "../matchers" require "../matchers"
require "../test_block" require "../value"
require "../test_value"
module Spectator module Spectator::DSL
module DSL module Matchers
# Indicates that some value should equal another. # Indicates that some value should equal another.
# The == operator is used for this check. # The == operator is used for this check.
# The value passed to this method is the expected value. # The value passed to this method is the expected value.
@ -13,8 +13,8 @@ module Spectator
# expect(1 + 2).to eq(3) # expect(1 + 2).to eq(3)
# ``` # ```
macro eq(expected) macro eq(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::EqualityMatcher.new(%test_value) ::Spectator::Matchers::EqualityMatcher.new(%value)
end end
# Indicates that some value should not equal another. # Indicates that some value should not equal another.
@ -26,8 +26,8 @@ module Spectator
# expect(1 + 2).to ne(5) # expect(1 + 2).to ne(5)
# ``` # ```
macro ne(expected) macro ne(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::InequalityMatcher.new(%test_value) ::Spectator::Matchers::InequalityMatcher.new(%value)
end end
# Indicates that some value when compared to another satisfies an operator. # Indicates that some value when compared to another satisfies an operator.
@ -61,8 +61,8 @@ module Spectator
# expect(obj.dup).to_not be(obj) # expect(obj.dup).to_not be(obj)
# ``` # ```
macro be(expected) macro be(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::ReferenceMatcher.new(%test_value) ::Spectator::Matchers::ReferenceMatcher.new(%value)
end end
# Indicates that some value should be of a specified type. # Indicates that some value should be of a specified type.
@ -173,8 +173,8 @@ module Spectator
# expect(3 - 1).to be_lt(3) # expect(3 - 1).to be_lt(3)
# ``` # ```
macro be_lt(expected) macro be_lt(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::LessThanMatcher.new(%test_value) ::Spectator::Matchers::LessThanMatcher.new(%value)
end end
# Indicates that some value should be less than or equal to another. # Indicates that some value should be less than or equal to another.
@ -186,8 +186,8 @@ module Spectator
# expect(3 - 1).to be_le(3) # expect(3 - 1).to be_le(3)
# ``` # ```
macro be_le(expected) macro be_le(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::LessThanEqualMatcher.new(%test_value) ::Spectator::Matchers::LessThanEqualMatcher.new(%value)
end end
# Indicates that some value should be greater than another. # Indicates that some value should be greater than another.
@ -199,8 +199,8 @@ module Spectator
# expect(3 + 1).to be_gt(3) # expect(3 + 1).to be_gt(3)
# ``` # ```
macro be_gt(expected) macro be_gt(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::GreaterThanMatcher.new(%test_value) ::Spectator::Matchers::GreaterThanMatcher.new(%value)
end end
# Indicates that some value should be greater than or equal to another. # Indicates that some value should be greater than or equal to another.
@ -212,8 +212,8 @@ module Spectator
# expect(3 + 1).to be_ge(3) # expect(3 + 1).to be_ge(3)
# ``` # ```
macro be_ge(expected) macro be_ge(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::GreaterThanEqualMatcher.new(%test_value) ::Spectator::Matchers::GreaterThanEqualMatcher.new(%value)
end end
# Indicates that some value should match another. # Indicates that some value should match another.
@ -230,8 +230,8 @@ module Spectator
# expect({:foo, 5}).to match({Symbol, Int32}) # expect({:foo, 5}).to match({Symbol, Int32})
# ``` # ```
macro match(expected) macro match(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::CaseMatcher.new(%test_value) ::Spectator::Matchers::CaseMatcher.new(%value)
end end
# Indicates that some value should be true. # Indicates that some value should be true.
@ -321,8 +321,8 @@ module Spectator
# NOTE: Do not attempt to mix the two use cases. # NOTE: Do not attempt to mix the two use cases.
# It likely won't work and will result in a compilation error. # It likely won't work and will result in a compilation error.
macro be_within(expected) macro be_within(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::CollectionMatcher.new(%test_value) ::Spectator::Matchers::CollectionMatcher.new(%value)
end end
# Indicates that some value should be between a lower and upper-bound. # Indicates that some value should be between a lower and upper-bound.
@ -344,8 +344,8 @@ module Spectator
macro be_between(min, max) macro be_between(min, max)
%range = Range.new({{min}}, {{max}}) %range = Range.new({{min}}, {{max}})
%label = [{{min.stringify}}, {{max.stringify}}].join(" to ") %label = [{{min.stringify}}, {{max.stringify}}].join(" to ")
%test_value = ::Spectator::TestValue.new(%range, %label) %value = ::Spectator::Value.new(%range, %label)
::Spectator::Matchers::RangeMatcher.new(%test_value) ::Spectator::Matchers::RangeMatcher.new(%value)
end end
# Indicates that some value should be within a delta of an expected value. # Indicates that some value should be within a delta of an expected value.
@ -403,8 +403,8 @@ module Spectator
# expect(%w[foo bar]).to start_with(/foo/) # expect(%w[foo bar]).to start_with(/foo/)
# ``` # ```
macro start_with(expected) macro start_with(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::StartWithMatcher.new(%test_value) ::Spectator::Matchers::StartWithMatcher.new(%value)
end end
# Indicates that some value or set should end with another value. # Indicates that some value or set should end with another value.
@ -426,8 +426,8 @@ module Spectator
# expect(%w[foo bar]).to end_with(/bar/) # expect(%w[foo bar]).to end_with(/bar/)
# ``` # ```
macro end_with(expected) macro end_with(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::EndWithMatcher.new(%test_value) ::Spectator::Matchers::EndWithMatcher.new(%value)
end end
# Indicates that some value or set should contain another value. # Indicates that some value or set should contain another value.
@ -451,11 +451,11 @@ module Spectator
# ``` # ```
macro contain(*expected) macro contain(*expected)
{% if expected.id.starts_with?("{*") %} {% if expected.id.starts_with?("{*") %}
%test_value = ::Spectator::TestValue.new({{expected.id[2...-1]}}, {{expected.splat.stringify}}) %value = ::Spectator::Value.new({{expected.id[2...-1]}}, {{expected.splat.stringify}})
::Spectator::Matchers::ContainMatcher.new(%test_value) ::Spectator::Matchers::ContainMatcher.new(%value)
{% else %} {% else %}
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.splat.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.splat.stringify}})
::Spectator::Matchers::ContainMatcher.new(%test_value) ::Spectator::Matchers::ContainMatcher.new(%value)
{% end %} {% end %}
end end
@ -475,8 +475,8 @@ module Spectator
# expect(%i[a b c]).to contain_elements(%i[a b]) # expect(%i[a b c]).to contain_elements(%i[a b])
# ``` # ```
macro contain_elements(expected) macro contain_elements(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::ContainMatcher.new(%test_value) ::Spectator::Matchers::ContainMatcher.new(%value)
end end
# Indicates that some range (or collection) should contain another value. # Indicates that some range (or collection) should contain another value.
@ -497,11 +497,11 @@ module Spectator
# ``` # ```
macro cover(*expected) macro cover(*expected)
{% if expected.id.starts_with?("{*") %} {% if expected.id.starts_with?("{*") %}
%test_value = ::Spectator::TestValue.new({{expected.id[2...-1]}}, {{expected.splat.stringify}}) %value = ::Spectator::Value.new({{expected.id[2...-1]}}, {{expected.splat.stringify}})
::Spectator::Matchers::ContainMatcher.new(%test_value) ::Spectator::Matchers::ContainMatcher.new(%value)
{% else %} {% else %}
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.splat.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.splat.stringify}})
::Spectator::Matchers::ContainMatcher.new(%test_value) ::Spectator::Matchers::ContainMatcher.new(%value)
{% end %} {% end %}
end end
@ -532,11 +532,11 @@ module Spectator
# ``` # ```
macro have(*expected) macro have(*expected)
{% if expected.id.starts_with?("{*") %} {% if expected.id.starts_with?("{*") %}
%test_value = ::Spectator::TestValue.new({{expected.id[2...-1]}}, {{expected.splat.stringify}}) %value = ::Spectator::Value.new({{expected.id[2...-1]}}, {{expected.splat.stringify}})
::Spectator::Matchers::HaveMatcher.new(%test_value) ::Spectator::Matchers::HaveMatcher.new(%value)
{% else %} {% else %}
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.splat.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.splat.stringify}})
::Spectator::Matchers::HaveMatcher.new(%test_value) ::Spectator::Matchers::HaveMatcher.new(%value)
{% end %} {% end %}
end end
@ -559,8 +559,8 @@ module Spectator
# expect([1, 2, 3, :a, :b, :c]).to have_elements([Int32, Symbol]) # expect([1, 2, 3, :a, :b, :c]).to have_elements([Int32, Symbol])
# ``` # ```
macro have_elements(expected) macro have_elements(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::HaveMatcher.new(%test_value) ::Spectator::Matchers::HaveMatcher.new(%value)
end end
# Indicates that some set, such as a `Hash`, has a given key. # Indicates that some set, such as a `Hash`, has a given key.
@ -572,8 +572,8 @@ module Spectator
# expect({"lucky" => 7}).to have_key("lucky") # expect({"lucky" => 7}).to have_key("lucky")
# ``` # ```
macro have_key(expected) macro have_key(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::HaveKeyMatcher.new(%test_value) ::Spectator::Matchers::HaveKeyMatcher.new(%value)
end end
# :ditto: # :ditto:
@ -590,8 +590,8 @@ module Spectator
# expect({"lucky" => 7}).to have_value(7) # expect({"lucky" => 7}).to have_value(7)
# ``` # ```
macro have_value(expected) macro have_value(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::HaveValueMatcher.new(%test_value) ::Spectator::Matchers::HaveValueMatcher.new(%value)
end end
# :ditto: # :ditto:
@ -607,11 +607,11 @@ module Spectator
# ``` # ```
macro contain_exactly(*expected) macro contain_exactly(*expected)
{% if expected.id.starts_with?("{*") %} {% if expected.id.starts_with?("{*") %}
%test_value = ::Spectator::TestValue.new(({{expected.id[2...-1]}}).to_a, {{expected.stringify}}) %value = ::Spectator::Value.new(({{expected.id[2...-1]}}).to_a, {{expected.stringify}})
::Spectator::Matchers::ArrayMatcher.new(%test_value) ::Spectator::Matchers::ArrayMatcher.new(%value)
{% else %} {% else %}
%test_value = ::Spectator::TestValue.new(({{expected}}).to_a, {{expected.stringify}}) %value = ::Spectator::Value.new(({{expected}}).to_a, {{expected.stringify}})
::Spectator::Matchers::ArrayMatcher.new(%test_value) ::Spectator::Matchers::ArrayMatcher.new(%value)
{% end %} {% end %}
end end
@ -623,8 +623,8 @@ module Spectator
# expect([1, 2, 3]).to match_array([3, 2, 1]) # expect([1, 2, 3]).to match_array([3, 2, 1])
# ``` # ```
macro match_array(expected) macro match_array(expected)
%test_value = ::Spectator::TestValue.new(({{expected}}).to_a, {{expected.stringify}}) %value = ::Spectator::Value.new(({{expected}}).to_a, {{expected.stringify}})
::Spectator::Matchers::ArrayMatcher.new(%test_value) ::Spectator::Matchers::ArrayMatcher.new(%value)
end end
# Indicates that some set should have a specified size. # Indicates that some set should have a specified size.
@ -634,8 +634,8 @@ module Spectator
# expect([1, 2, 3]).to have_size(3) # expect([1, 2, 3]).to have_size(3)
# ``` # ```
macro have_size(expected) macro have_size(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::SizeMatcher.new(%test_value) ::Spectator::Matchers::SizeMatcher.new(%value)
end end
# Indicates that some set should have the same size (number of elements) as another set. # Indicates that some set should have the same size (number of elements) as another set.
@ -645,8 +645,8 @@ module Spectator
# expect([1, 2, 3]).to have_size_of(%i[x y z]) # expect([1, 2, 3]).to have_size_of(%i[x y z])
# ``` # ```
macro have_size_of(expected) macro have_size_of(expected)
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}})
::Spectator::Matchers::SizeOfMatcher.new(%test_value) ::Spectator::Matchers::SizeOfMatcher.new(%value)
end end
# Indicates that some value should have a set of attributes matching some conditions. # Indicates that some value should have a set of attributes matching some conditions.
@ -661,11 +661,11 @@ module Spectator
# ``` # ```
macro have_attributes(**expected) macro have_attributes(**expected)
{% if expected.id.starts_with?("{**") %} {% if expected.id.starts_with?("{**") %}
%test_value = ::Spectator::TestValue.new({{expected.id[3...-1]}}, {{expected.double_splat.stringify}}) %value = ::Spectator::Value.new({{expected.id[3...-1]}}, {{expected.double_splat.stringify}})
::Spectator::Matchers::AttributesMatcher.new(%test_value) ::Spectator::Matchers::AttributesMatcher.new(%value)
{% else %} {% else %}
%test_value = ::Spectator::TestValue.new({{expected}}, {{expected.double_splat.stringify}}) %value = ::Spectator::Value.new({{expected}}, {{expected.double_splat.stringify}})
::Spectator::Matchers::AttributesMatcher.new(%test_value) ::Spectator::Matchers::AttributesMatcher.new(%value)
{% end %} {% end %}
end end
@ -718,37 +718,18 @@ module Spectator
# expect { subject << :foo }.to change(&.size).by(1) # expect { subject << :foo }.to change(&.size).by(1)
# ``` # ```
macro change(&expression) macro change(&expression)
{% if expression.is_a?(Nop) %} {% if block.args.size == 1 && block.args[0] =~ /^__arg\d+$/ && block.body.is_a?(Call) && block.body.id =~ /^__arg\d+\./ %}
{% raise "Block must be provided to change matcher" %} {% method_name = block.body.id.split('.')[1..-1].join('.') %}
{% end %} %block = ::Spectator::Block.new({{"#" + method_name}}) do
subject.{{method_name.id}}
# Check if the short-hand method syntax is used. end
# This is a hack, since macros don't get this as a "literal" or something similar. {% elsif block.args.empty? %}
# The Crystal compiler will translate: %block = ::Spectator::Block.new({{"`" + block.body.stringify + "`"}}) {{block}}
# ```
# &.foo
# ```
# to:
# ```
# { |__arg0| __arg0.foo }
# ```
# The hack used here is to check if it looks like a compiler-generated block.
{% if expression.args == ["__arg0".id] && expression.body.is_a?(Call) && expression.body.id =~ /^__arg0\./ %}
# Extract the method name to make it clear to the user what is tested.
# The raw block can't be used because it's not clear to the user.
{% method_name = expression.body.id.split('.')[1..-1].join('.') %}
%proc = ->{ subject.{{method_name.id}} }
%test_block = ::Spectator::TestBlock.create(%proc, {{"#" + method_name}})
{% elsif expression.args.empty? %}
# In this case, it looks like the short-hand method syntax wasn't used.
# Capture the block as a proc and pass along.
%proc = ->{{expression}}
%test_block = ::Spectator::TestBlock.create(%proc, {{"`" + expression.body.stringify + "`"}})
{% else %} {% else %}
{% raise "Unexpected block arguments in change matcher" %} {% raise "Unexpected block arguments in 'expect' call" %}
{% end %} {% end %}
::Spectator::Matchers::ChangeMatcher.new(%test_block) ::Spectator::Matchers::ChangeMatcher.new(%block)
end end
# Indicates that some block should raise an error. # Indicates that some block should raise an error.
@ -828,8 +809,8 @@ module Spectator
end end
macro have_received(method) macro have_received(method)
%test_value = ::Spectator::TestValue.new(({{method.id.symbolize}}), {{method.id.stringify}}) %value = ::Spectator::Value.new(({{method.id.symbolize}}), {{method.id.stringify}})
::Spectator::Matchers::ReceiveMatcher.new(%test_value) ::Spectator::Matchers::ReceiveMatcher.new(%value)
end end
# Used to create predicate matchers. # Used to create predicate matchers.
@ -872,8 +853,8 @@ module Spectator
{% end %} {% end %}
label << ')' label << ')'
{% end %} {% end %}
test_value = ::Spectator::TestValue.new(descriptor, label.to_s) value = ::Spectator::Value.new(descriptor, label.to_s)
::Spectator::Matchers::{{matcher.id}}.new(test_value) ::Spectator::Matchers::{{matcher.id}}.new(value)
end end
end end
end end

View file

@ -38,7 +38,7 @@ module Spectator
def to(stub : Mocks::MethodStub) : Nil def to(stub : Mocks::MethodStub) : Nil
Harness.current.mocks.expect(@expression.value, stub) Harness.current.mocks.expect(@expression.value, stub)
value = TestValue.new(stub.name, stub.to_s) value = Value.new(stub.name, stub.to_s)
matcher = Matchers::ReceiveMatcher.new(value, stub.arguments?) matcher = Matchers::ReceiveMatcher.new(value, stub.arguments?)
to_eventually(matcher) to_eventually(matcher)
end end
@ -55,7 +55,7 @@ module Spectator
end end
def to_not(stub : Mocks::MethodStub) : Nil def to_not(stub : Mocks::MethodStub) : Nil
value = TestValue.new(stub.name, stub.to_s) value = Value.new(stub.name, stub.to_s)
matcher = Matchers::ReceiveMatcher.new(value, stub.arguments?) matcher = Matchers::ReceiveMatcher.new(value, stub.arguments?)
to_never(matcher) to_never(matcher)
end end
@ -107,7 +107,7 @@ module Spectator
# Reports an expectation to the current harness. # Reports an expectation to the current harness.
private def report(match_data : Matchers::MatchData) private def report(match_data : Matchers::MatchData)
expectation = Expectation.new(match_data, @source) expectation = Expectation.new(match_data, @source)
Harness.current.report_expectation(expectation) Harness.current.report(expectation)
end end
end end
end end

View file

@ -1,4 +1,4 @@
require "../test_value" require "../value"
require "./failed_match_data" require "./failed_match_data"
require "./matcher" require "./matcher"
require "./successful_match_data" require "./successful_match_data"
@ -21,8 +21,8 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
found = test_values(actual).each do |element| found = values(actual).each do |element|
match_data = matcher.match(element) match_data = matcher.match(element)
break match_data unless match_data.matched? break match_data unless match_data.matched?
end end
@ -39,18 +39,18 @@ module Spectator::Matchers
# What if the collection is empty? # What if the collection is empty?
# #
# RSpec doesn't support this syntax either. # RSpec doesn't support this syntax either.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
{% raise "The `expect { }.to_not all()` syntax is not supported (ambiguous)." %} {% raise "The `expect { }.to_not all()` syntax is not supported (ambiguous)." %}
end end
# Maps all values in the test collection to their own test values. # Maps all values in the test collection to their own test values.
# Each value is given their own label, # Each value is given their own label,
# which is the original label with an index appended. # which is the original label with an index appended.
private def test_values(actual) private def values(actual)
label_prefix = actual.label label_prefix = actual.label
actual.value.map_with_index do |value, index| actual.value.map_with_index do |value, index|
label = "#{label_prefix}[#{index}]" label = "#{label_prefix}[#{index}]"
TestValue.new(value, label) Value.new(value, label)
end end
end end
end end

View file

@ -11,7 +11,7 @@ module Spectator::Matchers
private getter expected private getter expected
# Creates the matcher with an expected value. # Creates the matcher with an expected value.
def initialize(@expected : TestValue(Array(ExpectedType))) def initialize(@expected : Value(Array(ExpectedType)))
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -22,7 +22,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a)
@ -46,7 +46,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a)

View file

@ -1,4 +1,4 @@
require "../test_value" require "../value"
require "./failed_match_data" require "./failed_match_data"
require "./matcher" require "./matcher"
require "./successful_match_data" require "./successful_match_data"
@ -14,7 +14,7 @@ module Spectator::Matchers
private getter expected private getter expected
# Creates the matcher with an expected value. # Creates the matcher with an expected value.
def initialize(@expected : TestValue(ExpectedType)) def initialize(@expected : Value(ExpectedType))
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -25,7 +25,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value) snapshot = snapshot_values(actual.value)
if match?(snapshot) if match?(snapshot)
SuccessfulMatchData.new(description) SuccessfulMatchData.new(description)
@ -36,7 +36,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value) snapshot = snapshot_values(actual.value)
if match?(snapshot) if match?(snapshot)
FailedMatchData.new(description, "#{actual.label} has attributes #{expected.label}", negated_values(snapshot).to_a) FailedMatchData.new(description, "#{actual.label} has attributes #{expected.label}", negated_values(snapshot).to_a)

View file

@ -12,13 +12,13 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
expected.value === actual.value expected.value === actual.value
end end
# Overload that takes a regex so that the operands are flipped. # Overload that takes a regex so that the operands are flipped.
# This mimics RSpec's behavior. # This mimics RSpec's behavior.
private def match?(actual : TestExpression(Regex)) : Bool forall T private def match?(actual : Expression(Regex)) : Bool forall T
actual.value === expected.value actual.value === expected.value
end end

View file

@ -15,7 +15,7 @@ module Spectator::Matchers
private getter expected_after private getter expected_after
# Creates a new change matcher. # Creates a new change matcher.
def initialize(@expression : TestBlock(ExpressionType), @expected_before : FromType, @expected_after : ToType) def initialize(@expression : Block(ExpressionType), @expected_before : FromType, @expected_after : ToType)
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -26,7 +26,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
before, after = change(actual) before, after = change(actual)
if expected_before == before if expected_before == before
if before == after if before == after
@ -53,7 +53,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
before, after = change(actual) before, after = change(actual)
if expected_before == before if expected_before == before
if expected_after == after if expected_after == after

View file

@ -13,7 +13,7 @@ module Spectator::Matchers
private getter expected private getter expected
# Creates a new change matcher. # Creates a new change matcher.
def initialize(@expression : TestBlock(ExpressionType), @expected : FromType) def initialize(@expression : Block(ExpressionType), @expected : FromType)
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -24,7 +24,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
before, after = change(actual) before, after = change(actual)
if expected != before if expected != before
FailedMatchData.new(description, "#{expression.label} was not initially #{expected}", FailedMatchData.new(description, "#{expression.label} was not initially #{expected}",
@ -44,7 +44,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
before, after = change(actual) before, after = change(actual)
if expected != before if expected != before
FailedMatchData.new(description, "#{expression.label} was not initially #{expected}", FailedMatchData.new(description, "#{expression.label} was not initially #{expected}",

View file

@ -11,7 +11,7 @@ module Spectator::Matchers
private getter expression private getter expression
# Creates a new change matcher. # Creates a new change matcher.
def initialize(@expression : TestBlock(ExpressionType)) def initialize(@expression : Block(ExpressionType))
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -22,7 +22,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
before, after = change(actual) before, after = change(actual)
if before == after if before == after
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}", FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
@ -36,7 +36,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
before, after = change(actual) before, after = change(actual)
if before == after if before == after
SuccessfulMatchData.new(description) SuccessfulMatchData.new(description)

View file

@ -9,7 +9,7 @@ module Spectator::Matchers
private getter expression private getter expression
# Creates a new change matcher. # Creates a new change matcher.
def initialize(@expression : TestBlock(ExpressionType), @relativity : String, def initialize(@expression : Block(ExpressionType), @relativity : String,
&evaluator : ExpressionType, ExpressionType -> Bool) &evaluator : ExpressionType, ExpressionType -> Bool)
@evaluator = evaluator @evaluator = evaluator
end end
@ -22,7 +22,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
before, after = change(actual) before, after = change(actual)
if before == after if before == after
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}", FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
@ -41,7 +41,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
{% raise "The `expect { }.to_not change { }.by_...()` syntax is not supported (ambiguous)." %} {% raise "The `expect { }.to_not change { }.by_...()` syntax is not supported (ambiguous)." %}
end end

View file

@ -13,7 +13,7 @@ module Spectator::Matchers
private getter expected private getter expected
# Creates a new change matcher. # Creates a new change matcher.
def initialize(@expression : TestBlock(ExpressionType), @expected : ToType) def initialize(@expression : Block(ExpressionType), @expected : ToType)
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -24,7 +24,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
before, after = change(actual) before, after = change(actual)
if before == after if before == after
FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}", FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}",
@ -52,7 +52,7 @@ module Spectator::Matchers
# but it is the expected value? # but it is the expected value?
# #
# RSpec doesn't support this syntax either. # RSpec doesn't support this syntax either.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
{% raise "The `expect { }.to_not change { }.to()` syntax is not supported (ambiguous)." %} {% raise "The `expect { }.to_not change { }.to()` syntax is not supported (ambiguous)." %}
end end

View file

@ -1,4 +1,4 @@
require "../test_value" require "../value"
require "./range_matcher" require "./range_matcher"
require "./value_matcher" require "./value_matcher"
@ -13,7 +13,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
expected.value.includes?(actual.value) expected.value.includes?(actual.value)
end end
@ -55,8 +55,8 @@ module Spectator::Matchers
lower = center - diff lower = center - diff
upper = center + diff upper = center + diff
range = Range.new(lower, upper) range = Range.new(lower, upper)
test_value = TestValue.new(range, "#{center} ± #{expected.label}") value = Value.new(range, "#{center} ± #{expected.label}")
RangeMatcher.new(test_value) RangeMatcher.new(value)
end end
end end
end end

View file

@ -8,7 +8,7 @@ module Spectator::Matchers
private getter expected private getter expected
# Creates the matcher with an expected value. # Creates the matcher with an expected value.
def initialize(@expected : TestValue(ExpectedType)) def initialize(@expected : Value(ExpectedType))
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -19,7 +19,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?)
@ -42,7 +42,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?)

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:empty?) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:empty?)

View file

@ -11,7 +11,7 @@ module Spectator::Matchers
private getter expected private getter expected
# Creates the matcher with an expected value. # Creates the matcher with an expected value.
def initialize(@expected : TestValue(ExpectedType)) def initialize(@expected : Value(ExpectedType))
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -22,7 +22,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
value = actual.value value = actual.value
if value.is_a?(String) || value.responds_to?(:ends_with?) if value.is_a?(String) || value.responds_to?(:ends_with?)
match_ends_with(value, actual.label) match_ends_with(value, actual.label)
@ -33,7 +33,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
value = actual.value value = actual.value
if value.is_a?(String) || value.responds_to?(:ends_with?) if value.is_a?(String) || value.responds_to?(:ends_with?)
negated_match_ends_with(value, actual.label) negated_match_ends_with(value, actual.label)

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
expected.value == actual.value expected.value == actual.value
end end

View file

@ -1,4 +1,4 @@
require "../test_value" require "../value"
require "./failed_match_data" require "./failed_match_data"
require "./matcher" require "./matcher"
require "./successful_match_data" require "./successful_match_data"
@ -11,11 +11,11 @@ module Spectator::Matchers
# Creates the matcher with no expectation of the message. # Creates the matcher with no expectation of the message.
def initialize def initialize
@expected = TestValue.new(nil, ExceptionType.to_s) @expected = Value.new(nil, ExceptionType.to_s)
end end
# Creates the matcher with an expected message. # Creates the matcher with an expected message.
def initialize(@expected : TestValue(ExpectedType)) def initialize(@expected : Value(ExpectedType))
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -30,7 +30,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
exception = capture_exception { actual.value } exception = capture_exception { actual.value }
if exception.nil? if exception.nil?
FailedMatchData.new(description, "#{actual.label} did not raise", expected: ExceptionType.inspect) FailedMatchData.new(description, "#{actual.label} did not raise", expected: ExceptionType.inspect)
@ -61,7 +61,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
exception = capture_exception { actual.value } exception = capture_exception { actual.value }
if exception.nil? if exception.nil?
SuccessfulMatchData.new(description) SuccessfulMatchData.new(description)
@ -91,7 +91,7 @@ module Spectator::Matchers
end end
def with_message(message : T) forall T def with_message(message : T) forall T
value = TestValue.new(message) value = Value.new(message)
ExceptionMatcher(ExceptionType, T).new(value) ExceptionMatcher(ExceptionType, T).new(value)
end end
@ -114,13 +114,13 @@ module Spectator::Matchers
# Creates a new exception matcher with a message check. # Creates a new exception matcher with a message check.
def self.create(value, label : String) def self.create(value, label : String)
expected = TestValue.new(value, label) expected = Value.new(value, label)
ExceptionMatcher(Exception, typeof(value)).new(expected) ExceptionMatcher(Exception, typeof(value)).new(expected)
end end
# Creates a new exception matcher with a type and message check. # Creates a new exception matcher with a type and message check.
def self.create(exception_type : T.class, value, label : String) forall T def self.create(exception_type : T.class, value, label : String) forall T
expected = TestValue.new(value, label) expected = Value.new(value, label)
ExceptionMatcher(T, typeof(value)).new(expected) ExceptionMatcher(T, typeof(value)).new(expected)
end end
end end

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual.value >= expected.value actual.value >= expected.value
end end

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual.value > expected.value actual.value > expected.value
end end

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:has_key?) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:has_key?)

View file

@ -9,7 +9,7 @@ module Spectator::Matchers
private getter expected private getter expected
# Creates the matcher with an expected value. # Creates the matcher with an expected value.
def initialize(@expected : TestValue(ExpectedType)) def initialize(@expected : Value(ExpectedType))
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -20,7 +20,7 @@ module Spectator::Matchers
end end
# Entrypoint for the matcher, forwards to the correct method for string or enumerable. # Entrypoint for the matcher, forwards to the correct method for string or enumerable.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
if (value = actual.value).is_a?(String) if (value = actual.value).is_a?(String)
match_string(value, actual.label) match_string(value, actual.label)
else else
@ -70,7 +70,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
if (value = actual.value).is_a?(String) if (value = actual.value).is_a?(String)
negated_match_string(value, actual.label) negated_match_string(value, actual.label)
else else

View file

@ -15,7 +15,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value) snapshot = snapshot_values(actual.value)
if match?(snapshot) if match?(snapshot)
SuccessfulMatchData.new(description) SuccessfulMatchData.new(description)
@ -26,7 +26,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value) snapshot = snapshot_values(actual.value)
if match?(snapshot) if match?(snapshot)
FailedMatchData.new(description, "#{actual.label} has #{expected.label}", values(snapshot).to_a) FailedMatchData.new(description, "#{actual.label} has #{expected.label}", values(snapshot).to_a)

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:has_value?) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:has_value?)

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
expected.value != actual.value expected.value != actual.value
end end

View file

@ -11,7 +11,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual.value.class == Expected actual.value.class == Expected
end end

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual.value <= expected.value actual.value <= expected.value
end end

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual.value < expected.value actual.value < expected.value
end end

View file

@ -16,10 +16,10 @@ module Spectator::Matchers
abstract def description : String abstract def description : String
# Actually performs the test against the expression (value or block). # Actually performs the test against the expression (value or block).
abstract def match(actual : TestExpression(T)) : MatchData forall T abstract def match(actual : Expression(T)) : MatchData forall T
# Performs the test against the expression (value or block), but inverted. # Performs the test against the expression (value or block), but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
abstract def negated_match(actual : TestExpression(T)) : MatchData forall T abstract def negated_match(actual : Expression(T)) : MatchData forall T
end end
end end

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual.value.nil? actual.value.nil?
end end

View file

@ -10,7 +10,7 @@ module Spectator::Matchers
private getter expected private getter expected
# Creates the matcher with a expected values. # Creates the matcher with a expected values.
def initialize(@expected : TestValue(ExpectedType)) def initialize(@expected : Value(ExpectedType))
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -21,7 +21,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value) snapshot = snapshot_values(actual.value)
if match?(snapshot) if match?(snapshot)
SuccessfulMatchData.new(description) SuccessfulMatchData.new(description)
@ -32,7 +32,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value) snapshot = snapshot_values(actual.value)
if match?(snapshot) if match?(snapshot)
FailedMatchData.new(description, "#{actual.label} is #{expected.label}", values(snapshot).to_a) FailedMatchData.new(description, "#{actual.label} is #{expected.label}", values(snapshot).to_a)

View file

@ -15,7 +15,7 @@ module Spectator::Matchers
def inclusive def inclusive
label = expected.label label = expected.label
new_range = Range.new(range.begin, range.end, exclusive: false) new_range = Range.new(range.begin, range.end, exclusive: false)
expected = TestValue.new(new_range, label) expected = Value.new(new_range, label)
RangeMatcher.new(expected) RangeMatcher.new(expected)
end end
@ -23,12 +23,12 @@ module Spectator::Matchers
def exclusive def exclusive
label = expected.label label = expected.label
new_range = Range.new(range.begin, range.end, exclusive: true) new_range = Range.new(range.begin, range.end, exclusive: true)
expected = TestValue.new(new_range, label) expected = Value.new(new_range, label)
RangeMatcher.new(expected) RangeMatcher.new(expected)
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
expected.value.includes?(actual.value) expected.value.includes?(actual.value)
end end

View file

@ -5,7 +5,7 @@ module Spectator::Matchers
struct ReceiveMatcher < StandardMatcher struct ReceiveMatcher < StandardMatcher
alias Range = ::Range(Int32, Int32) | ::Range(Nil, Int32) | ::Range(Int32, Nil) alias Range = ::Range(Int32, Int32) | ::Range(Nil, Int32) | ::Range(Int32, Nil)
def initialize(@expected : TestExpression(Symbol), @args : Mocks::Arguments? = nil, @range : Range? = nil) def initialize(@expected : Expression(Symbol), @args : Mocks::Arguments? = nil, @range : Range? = nil)
end end
def description : String def description : String
@ -13,7 +13,7 @@ module Spectator::Matchers
"received message #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "At least once"} with #{@args || "any arguments"}" "received message #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "At least once"} with #{@args || "any arguments"}"
end end
def match?(actual : TestExpression(T)) : Bool forall T def match?(actual : Expression(T)) : Bool forall T
calls = Harness.current.mocks.calls_for(actual.value, @expected.value) calls = Harness.current.mocks.calls_for(actual.value, @expected.value)
calls.select! { |call| @args === call.args } if @args calls.select! { |call| @args === call.args } if @args
if (range = @range) if (range = @range)
@ -23,7 +23,7 @@ module Spectator::Matchers
end end
end end
def failure_message(actual : TestExpression(T)) : String forall T def failure_message(actual : Expression(T)) : String forall T
range = @range range = @range
"#{actual.label} did not receive #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}" "#{actual.label} did not receive #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}"
end end
@ -33,7 +33,7 @@ module Spectator::Matchers
"#{actual.label} received #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}" "#{actual.label} received #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}"
end end
def values(actual : TestExpression(T)) forall T def values(actual : Expression(T)) forall T
calls = Harness.current.mocks.calls_for(actual.value, @expected.value) calls = Harness.current.mocks.calls_for(actual.value, @expected.value)
calls.select! { |call| @args === call.args } if @args calls.select! { |call| @args === call.args } if @args
range = @range range = @range
@ -43,7 +43,7 @@ module Spectator::Matchers
} }
end end
def negated_values(actual : TestExpression(T)) forall T def negated_values(actual : Expression(T)) forall T
calls = Harness.current.mocks.calls_for(actual.value, @expected.value) calls = Harness.current.mocks.calls_for(actual.value, @expected.value)
calls.select! { |call| @args === call.args } if @args calls.select! { |call| @args === call.args } if @args
range = @range range = @range
@ -115,7 +115,7 @@ module Spectator::Matchers
end end
private struct Count private struct Count
def initialize(@expected : TestExpression(Symbol), @args : Mocks::Arguments?, @range : Range) def initialize(@expected : Expression(Symbol), @args : Mocks::Arguments?, @range : Range)
end end
def times def times

View file

@ -5,7 +5,7 @@ module Spectator::Matchers
struct ReceiveTypeMatcher < StandardMatcher struct ReceiveTypeMatcher < StandardMatcher
alias Range = ::Range(Int32, Int32) | ::Range(Nil, Int32) | ::Range(Int32, Nil) alias Range = ::Range(Int32, Int32) | ::Range(Nil, Int32) | ::Range(Int32, Nil)
def initialize(@expected : TestExpression(Symbol), @args : Mocks::Arguments? = nil, @range : Range? = nil) def initialize(@expected : Expression(Symbol), @args : Mocks::Arguments? = nil, @range : Range? = nil)
end end
def description : String def description : String
@ -13,7 +13,7 @@ module Spectator::Matchers
"received message #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "At least once"} with #{@args || "any arguments"}" "received message #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "At least once"} with #{@args || "any arguments"}"
end end
def match?(actual : TestExpression(T)) : Bool forall T def match?(actual : Expression(T)) : Bool forall T
calls = Harness.current.mocks.calls_for_type(actual.value, @expected.value) calls = Harness.current.mocks.calls_for_type(actual.value, @expected.value)
calls.select! { |call| @args === call.args } if @args calls.select! { |call| @args === call.args } if @args
if (range = @range) if (range = @range)
@ -23,17 +23,17 @@ module Spectator::Matchers
end end
end end
def failure_message(actual : TestExpression(T)) : String forall T def failure_message(actual : Expression(T)) : String forall T
range = @range range = @range
"#{actual.label} did not receive #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}" "#{actual.label} did not receive #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}"
end end
def failure_message_when_negated(actual : TestExpression(T)) : String forall T def failure_message_when_negated(actual : Expression(T)) : String forall T
range = @range range = @range
"#{actual.label} received #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}" "#{actual.label} received #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}"
end end
def values(actual : TestExpression(T)) forall T def values(actual : Expression(T)) forall T
calls = Harness.current.mocks.calls_for_type(T, @expected.value) calls = Harness.current.mocks.calls_for_type(T, @expected.value)
calls.select! { |call| @args === call.args } if @args calls.select! { |call| @args === call.args } if @args
range = @range range = @range
@ -43,7 +43,7 @@ module Spectator::Matchers
} }
end end
def negated_values(actual : TestExpression(T)) forall T def negated_values(actual : Expression(T)) forall T
calls = Harness.current.mocks.calls_for_type(T, @expected.value) calls = Harness.current.mocks.calls_for_type(T, @expected.value)
calls.select! { |call| @args === call.args } if @args calls.select! { |call| @args === call.args } if @args
range = @range range = @range
@ -115,7 +115,7 @@ module Spectator::Matchers
end end
private struct Count private struct Count
def initialize(@expected : TestExpression(Symbol), @args : Mocks::Arguments?, @range : Range) def initialize(@expected : Expression(Symbol), @args : Mocks::Arguments?, @range : Range)
end end
def times def times

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
value = expected.value value = expected.value
if value && value.responds_to?(:same?) if value && value.responds_to?(:same?)
value.same?(actual.value) value.same?(actual.value)

View file

@ -14,7 +14,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value) snapshot = snapshot_values(actual.value)
if snapshot.values.all? if snapshot.values.all?
SuccessfulMatchData.new(description) SuccessfulMatchData.new(description)
@ -25,7 +25,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
snapshot = snapshot_values(actual.value) snapshot = snapshot_values(actual.value)
if snapshot.values.any? if snapshot.values.any?
FailedMatchData.new(description, "#{actual.label} responds to #{label}", values(snapshot).to_a) FailedMatchData.new(description, "#{actual.label} responds to #{label}", values(snapshot).to_a)

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:size?) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:size?)

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:size?) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:size?)

View file

@ -1,4 +1,4 @@
require "../test_value" require "../expression"
require "./failed_match_data" require "./failed_match_data"
require "./matcher" require "./matcher"
require "./successful_match_data" require "./successful_match_data"
@ -23,7 +23,7 @@ module Spectator::Matchers
# If it returns true, then a `SuccessfulMatchData` instance is returned. # If it returns true, then a `SuccessfulMatchData` instance is returned.
# Otherwise, a `FailedMatchData` instance is returned. # Otherwise, a `FailedMatchData` instance is returned.
# Additionally, `#failure_message` and `#values` are called for a failed match. # Additionally, `#failure_message` and `#values` are called for a failed match.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
if match?(actual) if match?(actual)
SuccessfulMatchData.new(description) SuccessfulMatchData.new(description)
else else
@ -38,7 +38,7 @@ module Spectator::Matchers
# If it returns true, then a `SuccessfulMatchData` instance is returned. # If it returns true, then a `SuccessfulMatchData` instance is returned.
# Otherwise, a `FailedMatchData` instance is returned. # Otherwise, a `FailedMatchData` instance is returned.
# Additionally, `#failure_message_when_negated` and `#negated_values` are called for a failed match. # Additionally, `#failure_message_when_negated` and `#negated_values` are called for a failed match.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
# TODO: Invert description. # TODO: Invert description.
if does_not_match?(actual) if does_not_match?(actual)
SuccessfulMatchData.new(description) SuccessfulMatchData.new(description)
@ -53,7 +53,7 @@ module Spectator::Matchers
# #
# The message should typically only contain the test expression labels. # The message should typically only contain the test expression labels.
# Actual values should be returned by `#values`. # Actual values should be returned by `#values`.
private abstract def failure_message(actual : TestExpression(T)) : String forall T private abstract def failure_message(actual : Expression(T)) : String forall T
# Message displayed when the matcher isn't satisifed and is negated. # Message displayed when the matcher isn't satisifed and is negated.
# This is essentially what would satisfy the matcher if it wasn't negated. # This is essentially what would satisfy the matcher if it wasn't negated.
@ -66,12 +66,12 @@ module Spectator::Matchers
# #
# The message should typically only contain the test expression labels. # The message should typically only contain the test expression labels.
# Actual values should be returned by `#values`. # Actual values should be returned by `#values`.
private def failure_message_when_negated(actual : TestExpression(T)) : String forall T private def failure_message_when_negated(actual : Expression(T)) : String forall T
raise "Negation with #{self.class} is not supported." raise "Negation with #{self.class} is not supported."
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private abstract def match?(actual : TestExpression(T)) : Bool forall T private abstract def match?(actual : Expression(T)) : Bool forall T
# If the expectation is negated, then this method is called instead of `#match?`. # If the expectation is negated, then this method is called instead of `#match?`.
# #
@ -79,7 +79,7 @@ module Spectator::Matchers
# If the matcher requires custom handling of negated matches, # If the matcher requires custom handling of negated matches,
# then this method should be overriden. # then this method should be overriden.
# Remember to override `#failure_message_when_negated` as well. # Remember to override `#failure_message_when_negated` as well.
private def does_not_match?(actual : TestExpression(T)) : Bool forall T private def does_not_match?(actual : Expression(T)) : Bool forall T
!match?(actual) !match?(actual)
end end
@ -101,7 +101,7 @@ module Spectator::Matchers
# #
# The values should typically only contain the test expression values, not the labels. # The values should typically only contain the test expression values, not the labels.
# Labeled should be returned by `#failure_message`. # Labeled should be returned by `#failure_message`.
private def values(actual : TestExpression(T)) forall T private def values(actual : Expression(T)) forall T
{actual: actual.value.inspect} {actual: actual.value.inspect}
end end
@ -123,7 +123,7 @@ module Spectator::Matchers
# #
# The values should typically only contain the test expression values, not the labels. # The values should typically only contain the test expression values, not the labels.
# Labeled should be returned by `#failure_message_when_negated`. # Labeled should be returned by `#failure_message_when_negated`.
private def negated_values(actual : TestExpression(T)) forall T private def negated_values(actual : Expression(T)) forall T
values(actual) values(actual)
end end
end end

View file

@ -10,7 +10,7 @@ module Spectator::Matchers
private getter expected private getter expected
# Creates the matcher with an expected value. # Creates the matcher with an expected value.
def initialize(@expected : TestValue(ExpectedType)) def initialize(@expected : Value(ExpectedType))
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -21,7 +21,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
value = actual.value value = actual.value
if value.is_a?(String) || value.responds_to?(:starts_with?) if value.is_a?(String) || value.responds_to?(:starts_with?)
match_starts_with(value, actual.label) match_starts_with(value, actual.label)
@ -32,7 +32,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
value = actual.value value = actual.value
if value.is_a?(String) || value.responds_to?(:starts_with?) if value.is_a?(String) || value.responds_to?(:starts_with?)
negated_match_starts_with(value, actual.label) negated_match_starts_with(value, actual.label)

View file

@ -28,7 +28,7 @@ module Spectator::Matchers
# expect(0).to be < 1 # expect(0).to be < 1
# ``` # ```
def <(value) def <(value)
expected = TestValue.new(value) expected = Value.new(value)
LessThanMatcher.new(expected) LessThanMatcher.new(expected)
end end
@ -38,7 +38,7 @@ module Spectator::Matchers
# expect(0).to be <= 1 # expect(0).to be <= 1
# ``` # ```
def <=(value) def <=(value)
expected = TestValue.new(value) expected = Value.new(value)
LessThanEqualMatcher.new(expected) LessThanEqualMatcher.new(expected)
end end
@ -48,7 +48,7 @@ module Spectator::Matchers
# expect(2).to be > 1 # expect(2).to be > 1
# ``` # ```
def >(value) def >(value)
expected = TestValue.new(value) expected = Value.new(value)
GreaterThanMatcher.new(expected) GreaterThanMatcher.new(expected)
end end
@ -58,7 +58,7 @@ module Spectator::Matchers
# expect(2).to be >= 1 # expect(2).to be >= 1
# ``` # ```
def >=(value) def >=(value)
expected = TestValue.new(value) expected = Value.new(value)
GreaterThanEqualMatcher.new(expected) GreaterThanEqualMatcher.new(expected)
end end
@ -68,7 +68,7 @@ module Spectator::Matchers
# expect(0).to be == 0 # expect(0).to be == 0
# ``` # ```
def ==(value) def ==(value)
expected = TestValue.new(value) expected = Value.new(value)
EqualityMatcher.new(expected) EqualityMatcher.new(expected)
end end
@ -78,12 +78,12 @@ module Spectator::Matchers
# expect(0).to be != 1 # expect(0).to be != 1
# ``` # ```
def !=(value) def !=(value)
expected = TestValue.new(value) expected = Value.new(value)
InequalityMatcher.new(expected) InequalityMatcher.new(expected)
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
@truthy == !!actual.value @truthy == !!actual.value
end end

View file

@ -12,7 +12,7 @@ module Spectator::Matchers
end end
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T private def match?(actual : Expression(T)) : Bool forall T
actual.value.is_a?(Expected) actual.value.is_a?(Expected)
end end

View file

@ -8,7 +8,7 @@ module Spectator::Matchers
private getter expected private getter expected
# Creates the matcher with an expected value. # Creates the matcher with an expected value.
def initialize(@expected : TestValue(Array(ExpectedType))) def initialize(@expected : Value(Array(ExpectedType)))
end end
# Short text about the matcher's purpose. # Short text about the matcher's purpose.
@ -19,7 +19,7 @@ module Spectator::Matchers
end end
# Actually performs the test against the expression. # Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T def match(actual : Expression(T)) : MatchData forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a)
@ -41,7 +41,7 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted. # Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa. # A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T def negated_match(actual : Expression(T)) : MatchData forall T
actual_value = actual.value actual_value = actual.value
return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a) return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a)

View file

@ -22,7 +22,7 @@ module Spectator::Matchers
# Creates the value matcher. # Creates the value matcher.
# The expected value is stored for later use. # The expected value is stored for later use.
def initialize(@expected : TestValue(ExpectedType)) def initialize(@expected : Value(ExpectedType))
end end
# Additional information about the match failure. # Additional information about the match failure.
@ -40,7 +40,7 @@ module Spectator::Matchers
# actual: "bar", # actual: "bar",
# } # }
# ``` # ```
private def values(actual : TestExpression(T)) forall T private def values(actual : Expression(T)) forall T
super.merge(expected: expected.value.inspect) super.merge(expected: expected.value.inspect)
end end
@ -60,7 +60,7 @@ module Spectator::Matchers
# actual: "bar", # actual: "bar",
# } # }
# ``` # ```
private def negated_values(actual : TestExpression(T)) forall T private def negated_values(actual : Expression(T)) forall T
super.merge(expected: "Not #{expected.value.inspect}") super.merge(expected: "Not #{expected.value.inspect}")
end end
end end

View file

@ -6,9 +6,9 @@ module Spectator::Mocks
end end
def to(stub : MethodStub) : Nil def to(stub : MethodStub) : Nil
actual = TestValue.new(T) actual = Value.new(T)
Harness.current.mocks.expect(T, stub) Harness.current.mocks.expect(T, stub)
value = TestValue.new(stub.name, stub.to_s) value = Value.new(stub.name, stub.to_s)
matcher = Matchers::ReceiveTypeMatcher.new(value, stub.arguments?) matcher = Matchers::ReceiveTypeMatcher.new(value, stub.arguments?)
partial = Expectations::ExpectationPartial.new(actual, @source) partial = Expectations::ExpectationPartial.new(actual, @source)
partial.to_eventually(matcher) partial.to_eventually(matcher)