Merge branch 'specs' of gitlab.com:arctic-fox/spectator into specs

This commit is contained in:
Michael Miller 2020-01-14 20:10:53 -07:00
commit b25388a165
9 changed files with 264 additions and 17 deletions

View file

@ -16,6 +16,12 @@ module Spectator::Matchers
expected.value === actual.value
end
# Overload that takes a regex so that the operands are flipped.
# This mimics RSpec's behavior.
private def match?(actual : TestExpression(Regex)) : Bool forall T
actual.value === expected.value
end
# Message displayed when the matcher isn't satisifed.
#
# This is only called when `#match?` returns false.

View file

@ -23,7 +23,8 @@ module Spectator::Matchers
# Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T
if (value = actual.value).responds_to?(:ends_with?)
value = actual.value
if value.is_a?(String) || value.responds_to?(:ends_with?)
match_ends_with(value, actual.label)
else
match_last(value, actual.label)
@ -33,10 +34,11 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T
if actual.value.responds_to?(:ends_with?)
negated_match_ends_with(actual)
value = actual.value
if value.is_a?(String) || value.responds_to?(:ends_with?)
negated_match_ends_with(value, actual.label)
else
negated_match_last(actual)
negated_match_last(value, actual.label)
end
end
@ -72,11 +74,11 @@ module Spectator::Matchers
# Checks whether the actual value does not end with the expected value.
# This method expects (and uses) the `#ends_with?` method on the value.
private def negated_match_ends_with(actual)
if actual.value.ends_with?(expected.value)
FailedMatchData.new(description, "#{actual.label} ends with #{expected.label} (using #ends_with?)",
expected: expected.value.inspect,
actual: actual.value.inspect
private def negated_match_ends_with(actual_value, actual_label)
if actual_value.ends_with?(expected.value)
FailedMatchData.new(description, "#{actual_label} ends with #{expected.label} (using #ends_with?)",
expected: "Not #{expected.value.inspect}",
actual: actual_value.inspect
)
else
SuccessfulMatchData.new(description)
@ -85,13 +87,13 @@ module Spectator::Matchers
# Checks whether the last element of the value is not the expected value.
# This method expects that the actual value is a set (enumerable).
private def negated_match_last(actual)
list = actual.value.to_a
private def negated_match_last(actual_value, actual_label)
list = actual_value.to_a
last = list.last
if expected.value === last
FailedMatchData.new(description, "#{actual.label} ends with #{expected.label} (using expected === last)",
expected: expected.value.inspect,
FailedMatchData.new(description, "#{actual_label} ends with #{expected.label} (using expected === last)",
expected: "Not #{expected.value.inspect}",
actual: last.inspect,
list: list.inspect
)

View file

@ -90,6 +90,11 @@ module Spectator::Matchers
end
end
def with_message(message : T) forall T
value = TestValue.new(message)
ExceptionMatcher(ExceptionType, T).new(value)
end
# Runs a block of code and returns the exception it threw.
# If no exception was thrown, *nil* is returned.
private def capture_exception

View file

@ -22,7 +22,8 @@ module Spectator::Matchers
# Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T
if (value = actual.value).responds_to?(:starts_with?)
value = actual.value
if value.is_a?(String) || value.responds_to?(:starts_with?)
match_starts_with(value, actual.label)
else
match_first(value, actual.label)
@ -32,7 +33,8 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T
if (value = actual.value).responds_to?(:starts_with?)
value = actual.value
if value.is_a?(String) || value.responds_to?(:starts_with?)
negated_match_starts_with(value, actual.label)
else
negated_match_first(value, actual.label)
@ -74,7 +76,7 @@ module Spectator::Matchers
private def negated_match_starts_with(actual_value, actual_label)
if actual_value.starts_with?(expected.value)
FailedMatchData.new(description, "#{actual_label} starts with #{expected.label} (using #starts_with?)",
expected: expected.value.inspect,
expected: "Not #{expected.value.inspect}",
actual: actual_value.inspect
)
else
@ -90,7 +92,7 @@ module Spectator::Matchers
if expected.value === first
FailedMatchData.new(description, "#{actual_label} starts with #{expected.label} (using expected === first)",
expected: expected.value.inspect,
expected: "Not #{expected.value.inspect}",
actual: first.inspect,
list: list.inspect
)