mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Merge branch 'equality-matcher-changes' into 'master'
Equality matcher changes See merge request arctic-fox/spectator!2
This commit is contained in:
commit
29abe79809
7 changed files with 197 additions and 183 deletions
|
@ -1,5 +1,5 @@
|
||||||
name: spectator
|
name: spectator
|
||||||
version: 0.5.3
|
version: 0.6.0
|
||||||
description: |
|
description: |
|
||||||
A feature-rich spec testing framework for Crystal with similarities to RSpec.
|
A feature-rich spec testing framework for Crystal with similarities to RSpec.
|
||||||
|
|
||||||
|
|
|
@ -88,6 +88,28 @@ describe Spectator::Matchers::CaseMatcher do
|
||||||
match_data.matched?.should be_false
|
match_data.matched?.should be_false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "with a matching regex" do
|
||||||
|
it "is true" do
|
||||||
|
value = "foobar"
|
||||||
|
pattern = /foo/
|
||||||
|
partial = new_partial(value)
|
||||||
|
matcher = Spectator::Matchers::CaseMatcher.new(pattern)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.matched?.should be_true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a non-matching regex" do
|
||||||
|
it "is false" do
|
||||||
|
value = "foo"
|
||||||
|
pattern = /bar/
|
||||||
|
partial = new_partial(value)
|
||||||
|
matcher = Spectator::Matchers::CaseMatcher.new(pattern)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.matched?.should be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -116,14 +138,6 @@ describe Spectator::Matchers::CaseMatcher do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#message" do
|
describe "#message" do
|
||||||
it "mentions ===" do
|
|
||||||
value = 42
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::CaseMatcher.new(value)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.message.should contain("===")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "contains the actual label" do
|
it "contains the actual label" do
|
||||||
value = 42
|
value = 42
|
||||||
label = "everything"
|
label = "everything"
|
||||||
|
@ -155,14 +169,6 @@ describe Spectator::Matchers::CaseMatcher do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#negated_message" do
|
describe "#negated_message" do
|
||||||
it "mentions ===" do
|
|
||||||
value = 42
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::CaseMatcher.new(value)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.negated_message.should contain("===")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "contains the actual label" do
|
it "contains the actual label" do
|
||||||
value = 42
|
value = 42
|
||||||
label = "everything"
|
label = "everything"
|
||||||
|
|
152
spec/matchers/reference_matcher_spec.cr
Normal file
152
spec/matchers/reference_matcher_spec.cr
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
require "../spec_helper"
|
||||||
|
|
||||||
|
describe Spectator::Matchers::ReferenceMatcher do
|
||||||
|
describe "#match" do
|
||||||
|
context "returned MatchData" do
|
||||||
|
describe "#matched?" do
|
||||||
|
context "with the same instance" do
|
||||||
|
it "is true" do
|
||||||
|
# Box is used because it is a reference type and doesn't override the == method.
|
||||||
|
ref = Box.new([] of Int32)
|
||||||
|
partial = new_partial(ref)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(ref)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.matched?.should be_true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with different instances" do
|
||||||
|
context "with same contents" do
|
||||||
|
it "is false" do
|
||||||
|
array1 = [1, 2, 3]
|
||||||
|
array2 = [1, 2, 3]
|
||||||
|
partial = new_partial(array1)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(array2)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.matched?.should be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a duplicated instance" do
|
||||||
|
it "is false" do
|
||||||
|
array1 = [1, 2, 3]
|
||||||
|
array2 = array1.dup
|
||||||
|
partial = new_partial(array1)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(array2)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.matched?.should be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with the same type" do
|
||||||
|
it "is false" do
|
||||||
|
obj1 = "foo"
|
||||||
|
obj2 = "bar"
|
||||||
|
partial = new_partial(obj1)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(obj2)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.matched?.should be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a different type" do
|
||||||
|
it "is false" do
|
||||||
|
obj1 = "foobar"
|
||||||
|
obj2 = [1, 2, 3]
|
||||||
|
partial = new_partial(obj1)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(obj2)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.matched?.should be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#values" do
|
||||||
|
context "expected" do
|
||||||
|
it "is the expected value" do
|
||||||
|
actual = "foobar"
|
||||||
|
expected = /foo/
|
||||||
|
partial = new_partial(actual)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(expected)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data_value_sans_prefix(match_data.values, :expected)[:value].should eq(expected)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "actual" do
|
||||||
|
it "is the actual value" do
|
||||||
|
actual = "foobar"
|
||||||
|
expected = /foo/
|
||||||
|
partial = new_partial(actual)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(expected)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data_value_sans_prefix(match_data.values, :actual)[:value].should eq(actual)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#message" do
|
||||||
|
it "contains the actual label" do
|
||||||
|
value = "foobar"
|
||||||
|
label = "everything"
|
||||||
|
partial = new_partial(value, label)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(value)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.message.should contain(label)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "contains the expected label" do
|
||||||
|
value = "foobar"
|
||||||
|
label = "everything"
|
||||||
|
partial = new_partial(value)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(value, label)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.message.should contain(label)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when expected label is omitted" do
|
||||||
|
it "contains stringified form of expected value" do
|
||||||
|
obj1 = "foo"
|
||||||
|
obj2 = "bar"
|
||||||
|
partial = new_partial(obj1)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(obj2)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.message.should contain(obj2.to_s)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#negated_message" do
|
||||||
|
it "contains the actual label" do
|
||||||
|
value = "foobar"
|
||||||
|
label = "everything"
|
||||||
|
partial = new_partial(value, label)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(value)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.negated_message.should contain(label)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "contains the expected label" do
|
||||||
|
value = "foobar"
|
||||||
|
label = "everything"
|
||||||
|
partial = new_partial(value)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(value, label)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.negated_message.should contain(label)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when expected label is omitted" do
|
||||||
|
it "contains stringified form of expected value" do
|
||||||
|
obj1 = "foo"
|
||||||
|
obj2 = "bar"
|
||||||
|
partial = new_partial(obj1)
|
||||||
|
matcher = Spectator::Matchers::ReferenceMatcher.new(obj2)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.negated_message.should contain(obj2.to_s)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,147 +0,0 @@
|
||||||
require "../spec_helper"
|
|
||||||
|
|
||||||
describe Spectator::Matchers::RegexMatcher do
|
|
||||||
describe "#match" do
|
|
||||||
it "compares using #=~" do
|
|
||||||
spy = SpySUT.new
|
|
||||||
partial = new_partial(spy)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(/foobar/)
|
|
||||||
matcher.match(partial)
|
|
||||||
spy.match_call_count.should be > 0
|
|
||||||
end
|
|
||||||
|
|
||||||
context "returned MatchData" do
|
|
||||||
describe "#matched?" do
|
|
||||||
context "with a matching pattern" do
|
|
||||||
it "is true" do
|
|
||||||
value = "foobar"
|
|
||||||
pattern = /foo/
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.matched?.should be_true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "with a non-matching pattern" do
|
|
||||||
it "is false" do
|
|
||||||
value = "foo"
|
|
||||||
pattern = /bar/
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.matched?.should be_false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#values" do
|
|
||||||
context "expected" do
|
|
||||||
it "is the expected value" do
|
|
||||||
value = "foo"
|
|
||||||
pattern = /bar/
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data_value_sans_prefix(match_data.values, :expected)[:value].should eq(pattern)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "actual" do
|
|
||||||
it "is the actual value" do
|
|
||||||
value = "foo"
|
|
||||||
pattern = /bar/
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data_value_sans_prefix(match_data.values, :actual)[:value].should eq(value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#message" do
|
|
||||||
it "mentions =~" do
|
|
||||||
value = "foobar"
|
|
||||||
pattern = /foo/
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.message.should contain("=~")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "contains the actual label" do
|
|
||||||
value = "foobar"
|
|
||||||
label = "different"
|
|
||||||
pattern = /foo/
|
|
||||||
partial = new_partial(value, label)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.message.should contain(label)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "contains the expected label" do
|
|
||||||
value = "foobar"
|
|
||||||
label = "different"
|
|
||||||
pattern = /foo/
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern, label)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.message.should contain(label)
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when expected label is omitted" do
|
|
||||||
it "contains stringified form of expected value" do
|
|
||||||
value = "foobar"
|
|
||||||
pattern = /foo/
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.message.should contain(pattern.to_s)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#negated_message" do
|
|
||||||
it "mentions =~" do
|
|
||||||
value = "foobar"
|
|
||||||
pattern = /foo/
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.negated_message.should contain("=~")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "contains the actual label" do
|
|
||||||
value = "foobar"
|
|
||||||
label = "different"
|
|
||||||
pattern = /foo/
|
|
||||||
partial = new_partial(value, label)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.negated_message.should contain(label)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "contains the expected label" do
|
|
||||||
value = "foobar"
|
|
||||||
label = "different"
|
|
||||||
pattern = /foo/
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern, label)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.negated_message.should contain(label)
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when expected label is omitted" do
|
|
||||||
it "contains stringified form of expected value" do
|
|
||||||
value = "foobar"
|
|
||||||
pattern = /foo/
|
|
||||||
partial = new_partial(value)
|
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
|
||||||
match_data = matcher.match(partial)
|
|
||||||
match_data.negated_message.should contain(pattern.to_s)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -47,18 +47,18 @@ module Spectator::DSL
|
||||||
::Spectator::Matchers::TruthyMatcher.new(true)
|
::Spectator::Matchers::TruthyMatcher.new(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Indicates that some value should semantically equal another.
|
# Indicates that some object should be the same as another.
|
||||||
# The === operator is used for this check.
|
# This checks if two references are the same.
|
||||||
# This has identical behavior as a "when" condition in a case block.
|
# The `Reference#same?` method is used for this check.
|
||||||
#
|
#
|
||||||
# Examples:
|
# Examples:
|
||||||
# ```
|
# ```
|
||||||
# expect(1 + 2).to be(3)
|
# obj = "foobar"
|
||||||
# expect(5).to be(Int32) # Using `#be_a` instead is recommened here.
|
# expect(obj).to be(obj)
|
||||||
# expect(tuple).to be({1, 2})
|
# expect(obj.dup).to_not be(obj)
|
||||||
# ```
|
# ```
|
||||||
macro be(expected)
|
macro be(expected)
|
||||||
::Spectator::Matchers::CaseMatcher.new({{expected}}, {{expected.stringify}})
|
::Spectator::Matchers::ReferenceMatcher.new({{expected}}, {{expected.stringify}})
|
||||||
end
|
end
|
||||||
|
|
||||||
# Indicates that some value should be of a specified type.
|
# Indicates that some value should be of a specified type.
|
||||||
|
@ -155,17 +155,20 @@ module Spectator::DSL
|
||||||
end
|
end
|
||||||
|
|
||||||
# Indicates that some value should match another.
|
# Indicates that some value should match another.
|
||||||
# The =~ operator is used for this check.
|
# The === (case equality) operator is used for this check.
|
||||||
# Typically a regular expression is used,
|
# Typically a regular expression is used.
|
||||||
# but any type that has the =~ operator will work.
|
# This has identical behavior as a "when" condition in a case block.
|
||||||
#
|
#
|
||||||
# Examples:
|
# Examples:
|
||||||
# ```
|
# ```
|
||||||
# expect("foo").to match(/foo|bar/)
|
# expect("foo").to match(/foo|bar/)
|
||||||
# expect("BAR").to match(/foo|bar/i)
|
# expect("BAR").to match(/foo|bar/i)
|
||||||
|
# expect(1 + 2).to match(3)
|
||||||
|
# expect(5).to match(Int32) # Using `#be_a` instead is recommened here.
|
||||||
|
# expect({:foo, 5}).to match({Symbol, Int32})
|
||||||
# ```
|
# ```
|
||||||
macro match(expected)
|
macro match(expected)
|
||||||
::Spectator::Matchers::RegexMatcher.new({{expected}}, {{expected.stringify}})
|
::Spectator::Matchers::CaseMatcher.new({{expected}}, {{expected.stringify}})
|
||||||
end
|
end
|
||||||
|
|
||||||
# Indicates that some value should be true.
|
# Indicates that some value should be true.
|
||||||
|
|
|
@ -34,13 +34,13 @@ module Spectator::Matchers
|
||||||
# Describes the condition that satisfies the matcher.
|
# Describes the condition that satisfies the matcher.
|
||||||
# This is informational and displayed to the end-user.
|
# This is informational and displayed to the end-user.
|
||||||
def message
|
def message
|
||||||
"#{@values.actual_label} equals #{@values.expected_label} (using ===)"
|
"#{@values.actual_label} matches #{@values.expected_label}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Describes the condition that won't satsify the matcher.
|
# Describes the condition that won't satsify the matcher.
|
||||||
# This is informational and displayed to the end-user.
|
# This is informational and displayed to the end-user.
|
||||||
def negated_message
|
def negated_message
|
||||||
"#{@values.actual_label} does not equal #{@values.expected_label} (using ===)"
|
"#{@values.actual_label} does not match #{@values.expected_label}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
require "./value_matcher"
|
require "./value_matcher"
|
||||||
|
|
||||||
module Spectator::Matchers
|
module Spectator::Matchers
|
||||||
# Matcher that tests whether a value matches a regular expression.
|
# Matcher that tests whether two references are the same.
|
||||||
# The value is compared with the =~ operator.
|
# The values are compared with the `Reference#same?` method.
|
||||||
struct RegexMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
struct ReferenceMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
||||||
# Determines whether the matcher is satisfied with the value given to it.
|
# Determines whether the matcher is satisfied with the value given to it.
|
||||||
private def match?(actual)
|
private def match?(actual)
|
||||||
!!(actual =~ expected)
|
actual.same?(expected)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determines whether the matcher is satisfied with the partial given to it.
|
# Determines whether the matcher is satisfied with the partial given to it.
|
||||||
|
@ -34,13 +34,13 @@ module Spectator::Matchers
|
||||||
# Describes the condition that satisfies the matcher.
|
# Describes the condition that satisfies the matcher.
|
||||||
# This is informational and displayed to the end-user.
|
# This is informational and displayed to the end-user.
|
||||||
def message
|
def message
|
||||||
"#{@values.actual_label} matches #{@values.expected_label} (using =~)"
|
"#{@values.actual_label} is #{@values.expected_label}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Describes the condition that won't satsify the matcher.
|
# Describes the condition that won't satsify the matcher.
|
||||||
# This is informational and displayed to the end-user.
|
# This is informational and displayed to the end-user.
|
||||||
def negated_message
|
def negated_message
|
||||||
"#{@values.actual_label} does not match #{@values.expected_label} (using =~)"
|
"#{@values.actual_label} is not #{@values.expected_label}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Add table
Add a link
Reference in a new issue