shard-spectator/spec/matchers/contain_matcher_spec.cr

387 lines
14 KiB
Crystal
Raw Normal View History

2019-02-01 06:12:48 +00:00
require "../spec_helper"
describe Spectator::Matchers::ContainMatcher do
2019-03-04 03:03:20 +00:00
describe "#match" do
context "returned MatchData" do
describe "#matched?" do
context "with a String" do
context "one argument" do
context "against a matching string" do
it "is true" do
value = "foobarbaz"
search = "bar"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "at the beginning" do
it "is true" do
value = "foobar"
search = "foo"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
2019-02-02 02:56:46 +00:00
2019-03-04 03:03:20 +00:00
context "at the end" do
it "is true" do
value = "foobar"
search = "bar"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
2019-02-02 02:56:46 +00:00
end
2019-03-04 03:03:20 +00:00
context "against a different string" do
it "is false" do
value = "foobar"
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
2019-02-02 02:56:46 +00:00
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "against a matching character" do
it "is true" do
value = "foobar"
search = 'o'
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "at the beginning" do
it "is true" do
value = "foobar"
search = 'f'
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
2019-02-02 02:56:46 +00:00
2019-03-04 03:03:20 +00:00
context "at the end" do
it "is true" do
value = "foobar"
search = 'r'
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
2019-02-02 02:56:46 +00:00
end
2019-03-04 03:03:20 +00:00
context "against a different character" do
it "is false" do
value = "foobar"
search = 'z'
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
2019-02-02 02:56:46 +00:00
end
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "multiple arguments" do
context "against matching strings" do
it "is true" do
value = "foobarbaz"
search = {"foo", "bar", "baz"}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "against one matching string" do
it "is false" do
value = "foobarbaz"
search = {"foo", "qux"}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
2019-02-02 02:56:46 +00:00
2019-03-04 03:03:20 +00:00
context "against no matching strings" do
it "is false" do
value = "foobar"
search = {"baz", "qux"}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "against matching characters" do
it "is true" do
value = "foobarbaz"
search = {'f', 'b', 'z'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
2019-02-02 02:56:46 +00:00
2019-03-04 03:03:20 +00:00
context "against one matching character" do
it "is false" do
value = "foobarbaz"
search = {'f', 'c', 'd'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "against no matching characters" do
it "is false" do
value = "foobarbaz"
search = {'c', 'd', 'e'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
2019-02-02 02:56:46 +00:00
2019-03-04 03:03:20 +00:00
context "against a matching string and character" do
it "is true" do
value = "foobarbaz"
search = {"foo", 'z'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
2019-02-02 02:56:46 +00:00
2019-03-04 03:03:20 +00:00
context "against a matching string and non-matching character" do
it "is false" do
value = "foobarbaz"
search = {"foo", 'c'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "against a non-matching string and matching character" do
it "is false" do
value = "foobarbaz"
search = {"qux", 'f'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
2019-02-02 02:56:46 +00:00
2019-03-04 03:03:20 +00:00
context "against a non-matching string and character" do
it "is false" do
value = "foobarbaz"
search = {"qux", 'c'}
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
2019-02-02 02:56:46 +00:00
end
end
2019-03-04 03:03:20 +00:00
context "with an Enumberable" do
context "one argument" do
context "against an equal value" do
it "is true" do
array = %i[a b c]
search = :b
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "at the beginning" do
it "is true" do
array = %i[a b c]
search = :a
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
2019-02-02 02:56:46 +00:00
2019-03-04 03:03:20 +00:00
context "at the end" do
it "is true" do
array = %i[a b c]
search = :c
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
2019-02-02 02:56:46 +00:00
end
2019-03-04 03:03:20 +00:00
context "against a different value" do
it "is false" do
array = %i[a b c]
search = :z
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
2019-02-02 02:56:46 +00:00
end
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "multiple arguments" do
context "against equal values" do
it "is true" do
array = %i[a b c]
search = {:a, :b}
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "against one equal value" do
it "is false" do
array = %i[a b c]
search = {:a, :d}
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against no equal values" do
it "is false" do
array = %i[a b c]
search = {:d, :e}
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
2019-02-02 02:56:46 +00:00
end
end
2019-03-04 03:03:20 +00:00
end
2019-02-02 02:56:46 +00:00
2019-03-04 03:03:20 +00:00
describe "#values" do
describe "subset" do
it "is the expected set" do
2019-02-02 02:56:46 +00:00
array = %i[a b c]
2019-03-04 03:03:20 +00:00
search = {:d, :e}
partial = new_partial(array)
2019-02-02 02:56:46 +00:00
matcher = Spectator::Matchers::ContainMatcher.new(search)
2019-03-04 03:03:20 +00:00
match_data = matcher.match(partial)
match_data_value_sans_prefix(match_data.values, :subset)[:value].should eq(search)
2019-02-02 02:56:46 +00:00
end
end
2019-03-04 03:03:20 +00:00
describe "superset" do
it "is the actual set" do
2019-02-02 02:56:46 +00:00
array = %i[a b c]
search = {:d, :e}
partial = new_partial(array)
2019-02-02 02:56:46 +00:00
matcher = Spectator::Matchers::ContainMatcher.new(search)
2019-03-04 03:03:20 +00:00
match_data = matcher.match(partial)
match_data_value_sans_prefix(match_data.values, :superset)[:value].should eq(array)
2019-02-02 02:56:46 +00:00
end
2019-02-01 06:12:48 +00:00
end
end
2019-03-04 03:03:20 +00:00
describe "#message" do
it "contains the actual label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.message.should contain(label)
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
it "contains the expected label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}, label)
match_data = matcher.match(partial)
match_data.message.should contain(label)
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.message.should contain(search)
end
end
2019-02-01 06:12:48 +00:00
end
2019-03-04 03:03:20 +00:00
describe "#negated_message" do
it "contains the actual label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
it "contains the expected label" do
value = "foobar"
search = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search}, label)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
2019-02-01 06:12:48 +00:00
2019-03-04 03:03:20 +00:00
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
search = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::ContainMatcher.new({search})
match_data = matcher.match(partial)
match_data.negated_message.should contain(search)
end
end
2019-02-01 06:12:48 +00:00
end
end
end
end