diff --git a/src/spectator/dsl/matchers.cr b/src/spectator/dsl/matchers.cr index 9deb404..8d51193 100644 --- a/src/spectator/dsl/matchers.cr +++ b/src/spectator/dsl/matchers.cr @@ -1,9 +1,9 @@ +require "../block" require "../matchers" -require "../test_block" -require "../test_value" +require "../value" -module Spectator - module DSL +module Spectator::DSL + module Matchers # Indicates that some value should equal another. # The == operator is used for this check. # The value passed to this method is the expected value. @@ -13,8 +13,8 @@ module Spectator # expect(1 + 2).to eq(3) # ``` macro eq(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::EqualityMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::EqualityMatcher.new(%value) end # Indicates that some value should not equal another. @@ -26,8 +26,8 @@ module Spectator # expect(1 + 2).to ne(5) # ``` macro ne(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::InequalityMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::InequalityMatcher.new(%value) end # Indicates that some value when compared to another satisfies an operator. @@ -61,8 +61,8 @@ module Spectator # expect(obj.dup).to_not be(obj) # ``` macro be(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::ReferenceMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::ReferenceMatcher.new(%value) end # Indicates that some value should be of a specified type. @@ -173,8 +173,8 @@ module Spectator # expect(3 - 1).to be_lt(3) # ``` macro be_lt(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::LessThanMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::LessThanMatcher.new(%value) end # 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) # ``` macro be_le(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::LessThanEqualMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::LessThanEqualMatcher.new(%value) end # Indicates that some value should be greater than another. @@ -199,8 +199,8 @@ module Spectator # expect(3 + 1).to be_gt(3) # ``` macro be_gt(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::GreaterThanMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::GreaterThanMatcher.new(%value) end # 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) # ``` macro be_ge(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::GreaterThanEqualMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::GreaterThanEqualMatcher.new(%value) end # Indicates that some value should match another. @@ -230,8 +230,8 @@ module Spectator # expect({:foo, 5}).to match({Symbol, Int32}) # ``` macro match(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::CaseMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::CaseMatcher.new(%value) end # Indicates that some value should be true. @@ -321,8 +321,8 @@ module Spectator # NOTE: Do not attempt to mix the two use cases. # It likely won't work and will result in a compilation error. macro be_within(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::CollectionMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::CollectionMatcher.new(%value) end # Indicates that some value should be between a lower and upper-bound. @@ -344,8 +344,8 @@ module Spectator macro be_between(min, max) %range = Range.new({{min}}, {{max}}) %label = [{{min.stringify}}, {{max.stringify}}].join(" to ") - %test_value = ::Spectator::TestValue.new(%range, %label) - ::Spectator::Matchers::RangeMatcher.new(%test_value) + %value = ::Spectator::Value.new(%range, %label) + ::Spectator::Matchers::RangeMatcher.new(%value) end # 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/) # ``` macro start_with(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::StartWithMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::StartWithMatcher.new(%value) end # 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/) # ``` macro end_with(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::EndWithMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::EndWithMatcher.new(%value) end # Indicates that some value or set should contain another value. @@ -451,11 +451,11 @@ module Spectator # ``` macro contain(*expected) {% if expected.id.starts_with?("{*") %} - %test_value = ::Spectator::TestValue.new({{expected.id[2...-1]}}, {{expected.splat.stringify}}) - ::Spectator::Matchers::ContainMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected.id[2...-1]}}, {{expected.splat.stringify}}) + ::Spectator::Matchers::ContainMatcher.new(%value) {% else %} - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.splat.stringify}}) - ::Spectator::Matchers::ContainMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.splat.stringify}}) + ::Spectator::Matchers::ContainMatcher.new(%value) {% end %} end @@ -475,8 +475,8 @@ module Spectator # expect(%i[a b c]).to contain_elements(%i[a b]) # ``` macro contain_elements(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::ContainMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::ContainMatcher.new(%value) end # Indicates that some range (or collection) should contain another value. @@ -497,11 +497,11 @@ module Spectator # ``` macro cover(*expected) {% if expected.id.starts_with?("{*") %} - %test_value = ::Spectator::TestValue.new({{expected.id[2...-1]}}, {{expected.splat.stringify}}) - ::Spectator::Matchers::ContainMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected.id[2...-1]}}, {{expected.splat.stringify}}) + ::Spectator::Matchers::ContainMatcher.new(%value) {% else %} - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.splat.stringify}}) - ::Spectator::Matchers::ContainMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.splat.stringify}}) + ::Spectator::Matchers::ContainMatcher.new(%value) {% end %} end @@ -532,11 +532,11 @@ module Spectator # ``` macro have(*expected) {% if expected.id.starts_with?("{*") %} - %test_value = ::Spectator::TestValue.new({{expected.id[2...-1]}}, {{expected.splat.stringify}}) - ::Spectator::Matchers::HaveMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected.id[2...-1]}}, {{expected.splat.stringify}}) + ::Spectator::Matchers::HaveMatcher.new(%value) {% else %} - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.splat.stringify}}) - ::Spectator::Matchers::HaveMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.splat.stringify}}) + ::Spectator::Matchers::HaveMatcher.new(%value) {% end %} end @@ -559,8 +559,8 @@ module Spectator # expect([1, 2, 3, :a, :b, :c]).to have_elements([Int32, Symbol]) # ``` macro have_elements(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::HaveMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::HaveMatcher.new(%value) end # 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") # ``` macro have_key(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::HaveKeyMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::HaveKeyMatcher.new(%value) end # :ditto: @@ -590,8 +590,8 @@ module Spectator # expect({"lucky" => 7}).to have_value(7) # ``` macro have_value(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::HaveValueMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::HaveValueMatcher.new(%value) end # :ditto: @@ -607,11 +607,11 @@ module Spectator # ``` macro contain_exactly(*expected) {% if expected.id.starts_with?("{*") %} - %test_value = ::Spectator::TestValue.new(({{expected.id[2...-1]}}).to_a, {{expected.stringify}}) - ::Spectator::Matchers::ArrayMatcher.new(%test_value) + %value = ::Spectator::Value.new(({{expected.id[2...-1]}}).to_a, {{expected.stringify}}) + ::Spectator::Matchers::ArrayMatcher.new(%value) {% else %} - %test_value = ::Spectator::TestValue.new(({{expected}}).to_a, {{expected.stringify}}) - ::Spectator::Matchers::ArrayMatcher.new(%test_value) + %value = ::Spectator::Value.new(({{expected}}).to_a, {{expected.stringify}}) + ::Spectator::Matchers::ArrayMatcher.new(%value) {% end %} end @@ -623,8 +623,8 @@ module Spectator # expect([1, 2, 3]).to match_array([3, 2, 1]) # ``` macro match_array(expected) - %test_value = ::Spectator::TestValue.new(({{expected}}).to_a, {{expected.stringify}}) - ::Spectator::Matchers::ArrayMatcher.new(%test_value) + %value = ::Spectator::Value.new(({{expected}}).to_a, {{expected.stringify}}) + ::Spectator::Matchers::ArrayMatcher.new(%value) end # Indicates that some set should have a specified size. @@ -634,8 +634,8 @@ module Spectator # expect([1, 2, 3]).to have_size(3) # ``` macro have_size(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::SizeMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::SizeMatcher.new(%value) end # 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]) # ``` macro have_size_of(expected) - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.stringify}}) - ::Spectator::Matchers::SizeOfMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.stringify}}) + ::Spectator::Matchers::SizeOfMatcher.new(%value) end # Indicates that some value should have a set of attributes matching some conditions. @@ -661,11 +661,11 @@ module Spectator # ``` macro have_attributes(**expected) {% if expected.id.starts_with?("{**") %} - %test_value = ::Spectator::TestValue.new({{expected.id[3...-1]}}, {{expected.double_splat.stringify}}) - ::Spectator::Matchers::AttributesMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected.id[3...-1]}}, {{expected.double_splat.stringify}}) + ::Spectator::Matchers::AttributesMatcher.new(%value) {% else %} - %test_value = ::Spectator::TestValue.new({{expected}}, {{expected.double_splat.stringify}}) - ::Spectator::Matchers::AttributesMatcher.new(%test_value) + %value = ::Spectator::Value.new({{expected}}, {{expected.double_splat.stringify}}) + ::Spectator::Matchers::AttributesMatcher.new(%value) {% end %} end @@ -718,37 +718,18 @@ module Spectator # expect { subject << :foo }.to change(&.size).by(1) # ``` macro change(&expression) - {% if expression.is_a?(Nop) %} - {% raise "Block must be provided to change matcher" %} - {% end %} - - # Check if the short-hand method syntax is used. - # This is a hack, since macros don't get this as a "literal" or something similar. - # The Crystal compiler will translate: - # ``` - # &.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 + "`"}}) + {% if block.args.size == 1 && block.args[0] =~ /^__arg\d+$/ && block.body.is_a?(Call) && block.body.id =~ /^__arg\d+\./ %} + {% method_name = block.body.id.split('.')[1..-1].join('.') %} + %block = ::Spectator::Block.new({{"#" + method_name}}) do + subject.{{method_name.id}} + end + {% elsif block.args.empty? %} + %block = ::Spectator::Block.new({{"`" + block.body.stringify + "`"}}) {{block}} {% else %} - {% raise "Unexpected block arguments in change matcher" %} + {% raise "Unexpected block arguments in 'expect' call" %} {% end %} - ::Spectator::Matchers::ChangeMatcher.new(%test_block) + ::Spectator::Matchers::ChangeMatcher.new(%block) end # Indicates that some block should raise an error. @@ -828,8 +809,8 @@ module Spectator end macro have_received(method) - %test_value = ::Spectator::TestValue.new(({{method.id.symbolize}}), {{method.id.stringify}}) - ::Spectator::Matchers::ReceiveMatcher.new(%test_value) + %value = ::Spectator::Value.new(({{method.id.symbolize}}), {{method.id.stringify}}) + ::Spectator::Matchers::ReceiveMatcher.new(%value) end # Used to create predicate matchers. @@ -872,8 +853,8 @@ module Spectator {% end %} label << ')' {% end %} - test_value = ::Spectator::TestValue.new(descriptor, label.to_s) - ::Spectator::Matchers::{{matcher.id}}.new(test_value) + value = ::Spectator::Value.new(descriptor, label.to_s) + ::Spectator::Matchers::{{matcher.id}}.new(value) end end end diff --git a/src/spectator/expectation.cr b/src/spectator/expectation.cr index fcb2961..091ba2b 100644 --- a/src/spectator/expectation.cr +++ b/src/spectator/expectation.cr @@ -38,7 +38,7 @@ module Spectator def to(stub : Mocks::MethodStub) : Nil 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?) to_eventually(matcher) end @@ -55,7 +55,7 @@ module Spectator end 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?) to_never(matcher) end @@ -107,7 +107,7 @@ module Spectator # Reports an expectation to the current harness. private def report(match_data : Matchers::MatchData) expectation = Expectation.new(match_data, @source) - Harness.current.report_expectation(expectation) + Harness.current.report(expectation) end end end diff --git a/src/spectator/matchers/all_matcher.cr b/src/spectator/matchers/all_matcher.cr index d99ade4..becefad 100644 --- a/src/spectator/matchers/all_matcher.cr +++ b/src/spectator/matchers/all_matcher.cr @@ -1,4 +1,4 @@ -require "../test_value" +require "../value" require "./failed_match_data" require "./matcher" require "./successful_match_data" @@ -21,8 +21,8 @@ module Spectator::Matchers end # Actually performs the test against the expression. - def match(actual : TestExpression(T)) : MatchData forall T - found = test_values(actual).each do |element| + def match(actual : Expression(T)) : MatchData forall T + found = values(actual).each do |element| match_data = matcher.match(element) break match_data unless match_data.matched? end @@ -39,18 +39,18 @@ module Spectator::Matchers # What if the collection is empty? # # 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)." %} end # Maps all values in the test collection to their own test values. # Each value is given their own label, # which is the original label with an index appended. - private def test_values(actual) + private def values(actual) label_prefix = actual.label actual.value.map_with_index do |value, index| label = "#{label_prefix}[#{index}]" - TestValue.new(value, label) + Value.new(value, label) end end end diff --git a/src/spectator/matchers/array_matcher.cr b/src/spectator/matchers/array_matcher.cr index 2f0d264..77dc14c 100644 --- a/src/spectator/matchers/array_matcher.cr +++ b/src/spectator/matchers/array_matcher.cr @@ -11,7 +11,7 @@ module Spectator::Matchers private getter expected # Creates the matcher with an expected value. - def initialize(@expected : TestValue(Array(ExpectedType))) + def initialize(@expected : Value(Array(ExpectedType))) end # Short text about the matcher's purpose. @@ -22,7 +22,7 @@ module Spectator::Matchers end # 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 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. # 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 return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a) diff --git a/src/spectator/matchers/attributes_matcher.cr b/src/spectator/matchers/attributes_matcher.cr index ec8826f..86349b4 100644 --- a/src/spectator/matchers/attributes_matcher.cr +++ b/src/spectator/matchers/attributes_matcher.cr @@ -1,4 +1,4 @@ -require "../test_value" +require "../value" require "./failed_match_data" require "./matcher" require "./successful_match_data" @@ -14,7 +14,7 @@ module Spectator::Matchers private getter expected # Creates the matcher with an expected value. - def initialize(@expected : TestValue(ExpectedType)) + def initialize(@expected : Value(ExpectedType)) end # Short text about the matcher's purpose. @@ -25,7 +25,7 @@ module Spectator::Matchers end # 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) if match?(snapshot) SuccessfulMatchData.new(description) @@ -36,7 +36,7 @@ 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 + def negated_match(actual : Expression(T)) : MatchData forall T snapshot = snapshot_values(actual.value) if match?(snapshot) FailedMatchData.new(description, "#{actual.label} has attributes #{expected.label}", negated_values(snapshot).to_a) diff --git a/src/spectator/matchers/case_matcher.cr b/src/spectator/matchers/case_matcher.cr index fcb5c23..00618be 100644 --- a/src/spectator/matchers/case_matcher.cr +++ b/src/spectator/matchers/case_matcher.cr @@ -12,13 +12,13 @@ module Spectator::Matchers end # 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 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 + private def match?(actual : Expression(Regex)) : Bool forall T actual.value === expected.value end diff --git a/src/spectator/matchers/change_exact_matcher.cr b/src/spectator/matchers/change_exact_matcher.cr index 6aa562d..94c391a 100644 --- a/src/spectator/matchers/change_exact_matcher.cr +++ b/src/spectator/matchers/change_exact_matcher.cr @@ -15,7 +15,7 @@ module Spectator::Matchers private getter expected_after # 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 # Short text about the matcher's purpose. @@ -26,7 +26,7 @@ module Spectator::Matchers end # 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) if expected_before == before if before == after @@ -53,7 +53,7 @@ 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 + def negated_match(actual : Expression(T)) : MatchData forall T before, after = change(actual) if expected_before == before if expected_after == after diff --git a/src/spectator/matchers/change_from_matcher.cr b/src/spectator/matchers/change_from_matcher.cr index c0e08c1..cfbea50 100644 --- a/src/spectator/matchers/change_from_matcher.cr +++ b/src/spectator/matchers/change_from_matcher.cr @@ -13,7 +13,7 @@ module Spectator::Matchers private getter expected # Creates a new change matcher. - def initialize(@expression : TestBlock(ExpressionType), @expected : FromType) + def initialize(@expression : Block(ExpressionType), @expected : FromType) end # Short text about the matcher's purpose. @@ -24,7 +24,7 @@ module Spectator::Matchers end # 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) if expected != before FailedMatchData.new(description, "#{expression.label} was not initially #{expected}", @@ -44,7 +44,7 @@ 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 + def negated_match(actual : Expression(T)) : MatchData forall T before, after = change(actual) if expected != before FailedMatchData.new(description, "#{expression.label} was not initially #{expected}", diff --git a/src/spectator/matchers/change_matcher.cr b/src/spectator/matchers/change_matcher.cr index a60891b..1f60ac5 100644 --- a/src/spectator/matchers/change_matcher.cr +++ b/src/spectator/matchers/change_matcher.cr @@ -11,7 +11,7 @@ module Spectator::Matchers private getter expression # Creates a new change matcher. - def initialize(@expression : TestBlock(ExpressionType)) + def initialize(@expression : Block(ExpressionType)) end # Short text about the matcher's purpose. @@ -22,7 +22,7 @@ module Spectator::Matchers end # 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) if before == after 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. # 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) if before == after SuccessfulMatchData.new(description) diff --git a/src/spectator/matchers/change_relative_matcher.cr b/src/spectator/matchers/change_relative_matcher.cr index 4ac6e38..539c971 100644 --- a/src/spectator/matchers/change_relative_matcher.cr +++ b/src/spectator/matchers/change_relative_matcher.cr @@ -9,7 +9,7 @@ module Spectator::Matchers private getter expression # Creates a new change matcher. - def initialize(@expression : TestBlock(ExpressionType), @relativity : String, + def initialize(@expression : Block(ExpressionType), @relativity : String, &evaluator : ExpressionType, ExpressionType -> Bool) @evaluator = evaluator end @@ -22,7 +22,7 @@ module Spectator::Matchers end # 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) if before == after 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. # 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)." %} end diff --git a/src/spectator/matchers/change_to_matcher.cr b/src/spectator/matchers/change_to_matcher.cr index fb43204..51504ec 100644 --- a/src/spectator/matchers/change_to_matcher.cr +++ b/src/spectator/matchers/change_to_matcher.cr @@ -13,7 +13,7 @@ module Spectator::Matchers private getter expected # Creates a new change matcher. - def initialize(@expression : TestBlock(ExpressionType), @expected : ToType) + def initialize(@expression : Block(ExpressionType), @expected : ToType) end # Short text about the matcher's purpose. @@ -24,7 +24,7 @@ module Spectator::Matchers end # 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) if before == after FailedMatchData.new(description, "#{actual.label} did not change #{expression.label}", @@ -52,7 +52,7 @@ module Spectator::Matchers # but it is the expected value? # # 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)." %} end diff --git a/src/spectator/matchers/collection_matcher.cr b/src/spectator/matchers/collection_matcher.cr index 52b4378..277c5c3 100644 --- a/src/spectator/matchers/collection_matcher.cr +++ b/src/spectator/matchers/collection_matcher.cr @@ -1,4 +1,4 @@ -require "../test_value" +require "../value" require "./range_matcher" require "./value_matcher" @@ -13,7 +13,7 @@ module Spectator::Matchers end # 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) end @@ -55,8 +55,8 @@ module Spectator::Matchers lower = center - diff upper = center + diff range = Range.new(lower, upper) - test_value = TestValue.new(range, "#{center} ± #{expected.label}") - RangeMatcher.new(test_value) + value = Value.new(range, "#{center} ± #{expected.label}") + RangeMatcher.new(value) end end end diff --git a/src/spectator/matchers/contain_matcher.cr b/src/spectator/matchers/contain_matcher.cr index afc73fd..fe21182 100644 --- a/src/spectator/matchers/contain_matcher.cr +++ b/src/spectator/matchers/contain_matcher.cr @@ -8,7 +8,7 @@ module Spectator::Matchers private getter expected # Creates the matcher with an expected value. - def initialize(@expected : TestValue(ExpectedType)) + def initialize(@expected : Value(ExpectedType)) end # Short text about the matcher's purpose. @@ -19,7 +19,7 @@ module Spectator::Matchers end # 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 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. # 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 return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?) diff --git a/src/spectator/matchers/empty_matcher.cr b/src/spectator/matchers/empty_matcher.cr index e032794..bdfa80d 100644 --- a/src/spectator/matchers/empty_matcher.cr +++ b/src/spectator/matchers/empty_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:empty?) diff --git a/src/spectator/matchers/end_with_matcher.cr b/src/spectator/matchers/end_with_matcher.cr index 319c559..7ff3cd8 100644 --- a/src/spectator/matchers/end_with_matcher.cr +++ b/src/spectator/matchers/end_with_matcher.cr @@ -11,7 +11,7 @@ module Spectator::Matchers private getter expected # Creates the matcher with an expected value. - def initialize(@expected : TestValue(ExpectedType)) + def initialize(@expected : Value(ExpectedType)) end # Short text about the matcher's purpose. @@ -22,7 +22,7 @@ module Spectator::Matchers end # 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 if value.is_a?(String) || value.responds_to?(:ends_with?) match_ends_with(value, actual.label) @@ -33,7 +33,7 @@ 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 + def negated_match(actual : Expression(T)) : MatchData forall T value = actual.value if value.is_a?(String) || value.responds_to?(:ends_with?) negated_match_ends_with(value, actual.label) diff --git a/src/spectator/matchers/equality_matcher.cr b/src/spectator/matchers/equality_matcher.cr index bcdcd44..62f9624 100644 --- a/src/spectator/matchers/equality_matcher.cr +++ b/src/spectator/matchers/equality_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 end diff --git a/src/spectator/matchers/exception_matcher.cr b/src/spectator/matchers/exception_matcher.cr index 82d67ab..a27412b 100644 --- a/src/spectator/matchers/exception_matcher.cr +++ b/src/spectator/matchers/exception_matcher.cr @@ -1,4 +1,4 @@ -require "../test_value" +require "../value" require "./failed_match_data" require "./matcher" require "./successful_match_data" @@ -11,11 +11,11 @@ module Spectator::Matchers # Creates the matcher with no expectation of the message. def initialize - @expected = TestValue.new(nil, ExceptionType.to_s) + @expected = Value.new(nil, ExceptionType.to_s) end # Creates the matcher with an expected message. - def initialize(@expected : TestValue(ExpectedType)) + def initialize(@expected : Value(ExpectedType)) end # Short text about the matcher's purpose. @@ -30,7 +30,7 @@ module Spectator::Matchers end # 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 } if exception.nil? 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. # 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 } if exception.nil? SuccessfulMatchData.new(description) @@ -91,7 +91,7 @@ module Spectator::Matchers end def with_message(message : T) forall T - value = TestValue.new(message) + value = Value.new(message) ExceptionMatcher(ExceptionType, T).new(value) end @@ -114,13 +114,13 @@ module Spectator::Matchers # Creates a new exception matcher with a message check. def self.create(value, label : String) - expected = TestValue.new(value, label) + expected = Value.new(value, label) ExceptionMatcher(Exception, typeof(value)).new(expected) end # Creates a new exception matcher with a type and message check. 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) end end diff --git a/src/spectator/matchers/greater_than_equal_matcher.cr b/src/spectator/matchers/greater_than_equal_matcher.cr index 08eb88c..cfaf4f1 100644 --- a/src/spectator/matchers/greater_than_equal_matcher.cr +++ b/src/spectator/matchers/greater_than_equal_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 end diff --git a/src/spectator/matchers/greater_than_matcher.cr b/src/spectator/matchers/greater_than_matcher.cr index 5dfc90c..4aaf6c1 100644 --- a/src/spectator/matchers/greater_than_matcher.cr +++ b/src/spectator/matchers/greater_than_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 end diff --git a/src/spectator/matchers/have_key_matcher.cr b/src/spectator/matchers/have_key_matcher.cr index 5c07590..57224d3 100644 --- a/src/spectator/matchers/have_key_matcher.cr +++ b/src/spectator/matchers/have_key_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:has_key?) diff --git a/src/spectator/matchers/have_matcher.cr b/src/spectator/matchers/have_matcher.cr index 6e535ca..46cd446 100644 --- a/src/spectator/matchers/have_matcher.cr +++ b/src/spectator/matchers/have_matcher.cr @@ -9,7 +9,7 @@ module Spectator::Matchers private getter expected # Creates the matcher with an expected value. - def initialize(@expected : TestValue(ExpectedType)) + def initialize(@expected : Value(ExpectedType)) end # Short text about the matcher's purpose. @@ -20,7 +20,7 @@ module Spectator::Matchers end # 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) match_string(value, actual.label) else @@ -70,7 +70,7 @@ 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 + def negated_match(actual : Expression(T)) : MatchData forall T if (value = actual.value).is_a?(String) negated_match_string(value, actual.label) else diff --git a/src/spectator/matchers/have_predicate_matcher.cr b/src/spectator/matchers/have_predicate_matcher.cr index 5cb3f94..df4d90e 100644 --- a/src/spectator/matchers/have_predicate_matcher.cr +++ b/src/spectator/matchers/have_predicate_matcher.cr @@ -15,7 +15,7 @@ module Spectator::Matchers end # 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) if match?(snapshot) SuccessfulMatchData.new(description) @@ -26,7 +26,7 @@ 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 + def negated_match(actual : Expression(T)) : MatchData forall T snapshot = snapshot_values(actual.value) if match?(snapshot) FailedMatchData.new(description, "#{actual.label} has #{expected.label}", values(snapshot).to_a) diff --git a/src/spectator/matchers/have_value_matcher.cr b/src/spectator/matchers/have_value_matcher.cr index 54d5f40..aa059d8 100644 --- a/src/spectator/matchers/have_value_matcher.cr +++ b/src/spectator/matchers/have_value_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:has_value?) diff --git a/src/spectator/matchers/inequality_matcher.cr b/src/spectator/matchers/inequality_matcher.cr index f721ab4..145f5a1 100644 --- a/src/spectator/matchers/inequality_matcher.cr +++ b/src/spectator/matchers/inequality_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 end diff --git a/src/spectator/matchers/instance_matcher.cr b/src/spectator/matchers/instance_matcher.cr index c77531e..bd8f51f 100644 --- a/src/spectator/matchers/instance_matcher.cr +++ b/src/spectator/matchers/instance_matcher.cr @@ -11,7 +11,7 @@ module Spectator::Matchers end # 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 end diff --git a/src/spectator/matchers/less_than_equal_matcher.cr b/src/spectator/matchers/less_than_equal_matcher.cr index bc56dab..620afb3 100644 --- a/src/spectator/matchers/less_than_equal_matcher.cr +++ b/src/spectator/matchers/less_than_equal_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 end diff --git a/src/spectator/matchers/less_than_matcher.cr b/src/spectator/matchers/less_than_matcher.cr index 4e14cd4..05256b1 100644 --- a/src/spectator/matchers/less_than_matcher.cr +++ b/src/spectator/matchers/less_than_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 end diff --git a/src/spectator/matchers/matcher.cr b/src/spectator/matchers/matcher.cr index f5d0d4c..898d319 100644 --- a/src/spectator/matchers/matcher.cr +++ b/src/spectator/matchers/matcher.cr @@ -16,10 +16,10 @@ module Spectator::Matchers abstract def description : String # 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. # 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 diff --git a/src/spectator/matchers/nil_matcher.cr b/src/spectator/matchers/nil_matcher.cr index 5334037..187f809 100644 --- a/src/spectator/matchers/nil_matcher.cr +++ b/src/spectator/matchers/nil_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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? end diff --git a/src/spectator/matchers/predicate_matcher.cr b/src/spectator/matchers/predicate_matcher.cr index bda4f9b..da77dc8 100644 --- a/src/spectator/matchers/predicate_matcher.cr +++ b/src/spectator/matchers/predicate_matcher.cr @@ -10,7 +10,7 @@ module Spectator::Matchers private getter expected # Creates the matcher with a expected values. - def initialize(@expected : TestValue(ExpectedType)) + def initialize(@expected : Value(ExpectedType)) end # Short text about the matcher's purpose. @@ -21,7 +21,7 @@ module Spectator::Matchers end # 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) if match?(snapshot) SuccessfulMatchData.new(description) @@ -32,7 +32,7 @@ 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 + def negated_match(actual : Expression(T)) : MatchData forall T snapshot = snapshot_values(actual.value) if match?(snapshot) FailedMatchData.new(description, "#{actual.label} is #{expected.label}", values(snapshot).to_a) diff --git a/src/spectator/matchers/range_matcher.cr b/src/spectator/matchers/range_matcher.cr index fe54b7e..b4850f5 100644 --- a/src/spectator/matchers/range_matcher.cr +++ b/src/spectator/matchers/range_matcher.cr @@ -15,7 +15,7 @@ module Spectator::Matchers def inclusive label = expected.label 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) end @@ -23,12 +23,12 @@ module Spectator::Matchers def exclusive label = expected.label 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) end # 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) end diff --git a/src/spectator/matchers/receive_matcher.cr b/src/spectator/matchers/receive_matcher.cr index 9a96f82..7f3d233 100644 --- a/src/spectator/matchers/receive_matcher.cr +++ b/src/spectator/matchers/receive_matcher.cr @@ -5,7 +5,7 @@ module Spectator::Matchers struct ReceiveMatcher < StandardMatcher 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 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"}" 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.select! { |call| @args === call.args } if @args if (range = @range) @@ -23,7 +23,7 @@ module Spectator::Matchers end end - def failure_message(actual : TestExpression(T)) : String forall T + def failure_message(actual : Expression(T)) : String forall T range = @range "#{actual.label} did not receive #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}" 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"}" 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.select! { |call| @args === call.args } if @args range = @range @@ -43,7 +43,7 @@ module Spectator::Matchers } 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.select! { |call| @args === call.args } if @args range = @range @@ -115,7 +115,7 @@ module Spectator::Matchers end private struct Count - def initialize(@expected : TestExpression(Symbol), @args : Mocks::Arguments?, @range : Range) + def initialize(@expected : Expression(Symbol), @args : Mocks::Arguments?, @range : Range) end def times diff --git a/src/spectator/matchers/receive_type_matcher.cr b/src/spectator/matchers/receive_type_matcher.cr index c716f05..362707d 100644 --- a/src/spectator/matchers/receive_type_matcher.cr +++ b/src/spectator/matchers/receive_type_matcher.cr @@ -5,7 +5,7 @@ module Spectator::Matchers struct ReceiveTypeMatcher < StandardMatcher 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 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"}" 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.select! { |call| @args === call.args } if @args if (range = @range) @@ -23,17 +23,17 @@ module Spectator::Matchers end end - def failure_message(actual : TestExpression(T)) : String forall T + def failure_message(actual : Expression(T)) : String forall T range = @range "#{actual.label} did not receive #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}" 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 "#{actual.label} received #{@expected.label} #{range ? "#{humanize_range(range)} time(s)" : "at least once"} with #{@args || "any arguments"}" 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.select! { |call| @args === call.args } if @args range = @range @@ -43,7 +43,7 @@ module Spectator::Matchers } 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.select! { |call| @args === call.args } if @args range = @range @@ -115,7 +115,7 @@ module Spectator::Matchers end private struct Count - def initialize(@expected : TestExpression(Symbol), @args : Mocks::Arguments?, @range : Range) + def initialize(@expected : Expression(Symbol), @args : Mocks::Arguments?, @range : Range) end def times diff --git a/src/spectator/matchers/reference_matcher.cr b/src/spectator/matchers/reference_matcher.cr index 74ce6e7..8385eb6 100644 --- a/src/spectator/matchers/reference_matcher.cr +++ b/src/spectator/matchers/reference_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 if value && value.responds_to?(:same?) value.same?(actual.value) diff --git a/src/spectator/matchers/respond_matcher.cr b/src/spectator/matchers/respond_matcher.cr index 87f95a9..6d36f80 100644 --- a/src/spectator/matchers/respond_matcher.cr +++ b/src/spectator/matchers/respond_matcher.cr @@ -14,7 +14,7 @@ module Spectator::Matchers end # 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) if snapshot.values.all? SuccessfulMatchData.new(description) @@ -25,7 +25,7 @@ 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 + def negated_match(actual : Expression(T)) : MatchData forall T snapshot = snapshot_values(actual.value) if snapshot.values.any? FailedMatchData.new(description, "#{actual.label} responds to #{label}", values(snapshot).to_a) diff --git a/src/spectator/matchers/size_matcher.cr b/src/spectator/matchers/size_matcher.cr index f707159..ea51c89 100644 --- a/src/spectator/matchers/size_matcher.cr +++ b/src/spectator/matchers/size_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:size?) diff --git a/src/spectator/matchers/size_of_matcher.cr b/src/spectator/matchers/size_of_matcher.cr index 8784cea..ad06f99 100644 --- a/src/spectator/matchers/size_of_matcher.cr +++ b/src/spectator/matchers/size_of_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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 return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:size?) diff --git a/src/spectator/matchers/standard_matcher.cr b/src/spectator/matchers/standard_matcher.cr index 52cdc67..5b9f026 100644 --- a/src/spectator/matchers/standard_matcher.cr +++ b/src/spectator/matchers/standard_matcher.cr @@ -1,4 +1,4 @@ -require "../test_value" +require "../expression" require "./failed_match_data" require "./matcher" require "./successful_match_data" @@ -23,7 +23,7 @@ module Spectator::Matchers # If it returns true, then a `SuccessfulMatchData` instance is returned. # Otherwise, a `FailedMatchData` instance is returned. # 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) SuccessfulMatchData.new(description) else @@ -38,7 +38,7 @@ module Spectator::Matchers # If it returns true, then a `SuccessfulMatchData` instance is returned. # Otherwise, a `FailedMatchData` instance is returned. # 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. if does_not_match?(actual) SuccessfulMatchData.new(description) @@ -53,7 +53,7 @@ module Spectator::Matchers # # The message should typically only contain the test expression labels. # 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. # 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. # 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." end # 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?`. # @@ -79,7 +79,7 @@ module Spectator::Matchers # If the matcher requires custom handling of negated matches, # then this method should be overriden. # 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) end @@ -101,7 +101,7 @@ module Spectator::Matchers # # The values should typically only contain the test expression values, not the labels. # 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} end @@ -123,7 +123,7 @@ module Spectator::Matchers # # The values should typically only contain the test expression values, not the labels. # 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) end end diff --git a/src/spectator/matchers/start_with_matcher.cr b/src/spectator/matchers/start_with_matcher.cr index b459bb4..e8d0059 100644 --- a/src/spectator/matchers/start_with_matcher.cr +++ b/src/spectator/matchers/start_with_matcher.cr @@ -10,7 +10,7 @@ module Spectator::Matchers private getter expected # Creates the matcher with an expected value. - def initialize(@expected : TestValue(ExpectedType)) + def initialize(@expected : Value(ExpectedType)) end # Short text about the matcher's purpose. @@ -21,7 +21,7 @@ module Spectator::Matchers end # 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 if value.is_a?(String) || value.responds_to?(:starts_with?) match_starts_with(value, actual.label) @@ -32,7 +32,7 @@ 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 + def negated_match(actual : Expression(T)) : MatchData forall T value = actual.value if value.is_a?(String) || value.responds_to?(:starts_with?) negated_match_starts_with(value, actual.label) diff --git a/src/spectator/matchers/truthy_matcher.cr b/src/spectator/matchers/truthy_matcher.cr index bc26a6e..380dd26 100644 --- a/src/spectator/matchers/truthy_matcher.cr +++ b/src/spectator/matchers/truthy_matcher.cr @@ -28,7 +28,7 @@ module Spectator::Matchers # expect(0).to be < 1 # ``` def <(value) - expected = TestValue.new(value) + expected = Value.new(value) LessThanMatcher.new(expected) end @@ -38,7 +38,7 @@ module Spectator::Matchers # expect(0).to be <= 1 # ``` def <=(value) - expected = TestValue.new(value) + expected = Value.new(value) LessThanEqualMatcher.new(expected) end @@ -48,7 +48,7 @@ module Spectator::Matchers # expect(2).to be > 1 # ``` def >(value) - expected = TestValue.new(value) + expected = Value.new(value) GreaterThanMatcher.new(expected) end @@ -58,7 +58,7 @@ module Spectator::Matchers # expect(2).to be >= 1 # ``` def >=(value) - expected = TestValue.new(value) + expected = Value.new(value) GreaterThanEqualMatcher.new(expected) end @@ -68,7 +68,7 @@ module Spectator::Matchers # expect(0).to be == 0 # ``` def ==(value) - expected = TestValue.new(value) + expected = Value.new(value) EqualityMatcher.new(expected) end @@ -78,12 +78,12 @@ module Spectator::Matchers # expect(0).to be != 1 # ``` def !=(value) - expected = TestValue.new(value) + expected = Value.new(value) InequalityMatcher.new(expected) end # 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 end diff --git a/src/spectator/matchers/type_matcher.cr b/src/spectator/matchers/type_matcher.cr index f8401d4..dc88a24 100644 --- a/src/spectator/matchers/type_matcher.cr +++ b/src/spectator/matchers/type_matcher.cr @@ -12,7 +12,7 @@ module Spectator::Matchers end # 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) end diff --git a/src/spectator/matchers/unordered_array_matcher.cr b/src/spectator/matchers/unordered_array_matcher.cr index e408809..aeb8b2b 100644 --- a/src/spectator/matchers/unordered_array_matcher.cr +++ b/src/spectator/matchers/unordered_array_matcher.cr @@ -8,7 +8,7 @@ module Spectator::Matchers private getter expected # Creates the matcher with an expected value. - def initialize(@expected : TestValue(Array(ExpectedType))) + def initialize(@expected : Value(Array(ExpectedType))) end # Short text about the matcher's purpose. @@ -19,7 +19,7 @@ module Spectator::Matchers end # 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 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. # 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 return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a) diff --git a/src/spectator/matchers/value_matcher.cr b/src/spectator/matchers/value_matcher.cr index 5049320..c7dffe9 100644 --- a/src/spectator/matchers/value_matcher.cr +++ b/src/spectator/matchers/value_matcher.cr @@ -22,7 +22,7 @@ module Spectator::Matchers # Creates the value matcher. # The expected value is stored for later use. - def initialize(@expected : TestValue(ExpectedType)) + def initialize(@expected : Value(ExpectedType)) end # Additional information about the match failure. @@ -40,7 +40,7 @@ module Spectator::Matchers # actual: "bar", # } # ``` - private def values(actual : TestExpression(T)) forall T + private def values(actual : Expression(T)) forall T super.merge(expected: expected.value.inspect) end @@ -60,7 +60,7 @@ module Spectator::Matchers # 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}") end end diff --git a/src/spectator/mocks/expect_any_instance.cr b/src/spectator/mocks/expect_any_instance.cr index 9d9769e..0ccee05 100644 --- a/src/spectator/mocks/expect_any_instance.cr +++ b/src/spectator/mocks/expect_any_instance.cr @@ -6,9 +6,9 @@ module Spectator::Mocks end def to(stub : MethodStub) : Nil - actual = TestValue.new(T) + actual = Value.new(T) 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?) partial = Expectations::ExpectationPartial.new(actual, @source) partial.to_eventually(matcher)