shard-spectator/spec/matchers/start_with_matcher_spec.cr

394 lines
14 KiB
Crystal
Raw Normal View History

2019-02-01 02:36:35 +00:00
require "../spec_helper"
describe Spectator::Matchers::StartWithMatcher do
describe "#match" do
context "returned MatchData" do
describe "#matched?" do
context "with a String" do
context "against a matching string" do
it "is true" do
value = "foobar"
start = "foo"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
value = "foobar"
start = "bar"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a different string" do
it "is false" do
value = "foobar"
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against a matching character" do
it "is true" do
value = "foobar"
start = 'f'
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
value = "foobar"
start = 'b'
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a different character" do
it "is false" do
value = "foobar"
start = 'z'
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against a matching regex" do
it "is true" do
value = "FOOBAR"
start = /foo/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
value = "FOOBAR"
start = /bar/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a non-matching regex" do
it "is false" do
value = "FOOBAR"
start = /baz/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
2019-02-01 02:36:35 +00:00
end
context "with an Enumberable" do
context "against an equal value" do
it "is true" do
array = %i[a b c]
start = :a
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
array = %i[a b c]
start = :b
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a different value" do
it "is false" do
array = %i[a b c]
start = :z
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against matching element type" do
it "is true" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(Symbol)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
array = [1, 2, 3, :a, :b, :c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(Symbol)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against different element type" do
it "is false" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(Int32)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
context "against a matching regex" do
it "is true" do
array = %w[FOO BAR BAZ]
start = /foo/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
context "not at start" do
it "is false" do
array = %w[FOO BAR BAZ]
start = /bar/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a non-matching regex" do
it "is false" do
array = %w[FOO BAR BAZ]
start = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
2019-02-01 02:36:35 +00:00
end
end
end
describe "#values" do
context "with a String" do
context "expected" do
it "is the expected value" do
value = "FOOBAR"
first = /baz/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data_value_sans_prefix(match_data, :expected)[:value].should eq(first)
end
end
context "actual" do
it "is the actual value" do
value = "FOOBAR"
first = /baz/i
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data_value_sans_prefix(match_data, :actual)[:value].should eq(value)
end
end
2019-02-01 02:36:35 +00:00
end
context "with an Indexable" do
context "expected" do
it "is the expected value" do
array = %w[FOO BAR BAZ]
first = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data_value_sans_prefix(match_data, :expected)[:value].should eq(first)
end
end
context "actual" do
it "is the first element" do
array = %w[FOO BAR BAZ]
first = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data_value_sans_prefix(match_data, :actual)[:value].should eq(array.first)
end
end
context "list" do
it "is the full actual list" do
array = %w[FOO BAR BAZ]
first = /qux/i
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data_value_sans_prefix(match_data, :list)[:value].should eq(array)
end
end
2019-02-01 02:36:35 +00:00
end
end
2019-02-01 02:36:35 +00:00
describe "#message" do
context "with a String" do
it "mentions #starts_with?" do
2019-02-01 02:36:35 +00:00
value = "foobar"
start = "baz"
partial = new_partial(value)
2019-02-01 02:36:35 +00:00
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.message.should contain("#starts_with?")
2019-02-01 02:36:35 +00:00
end
end
context "with an Enumerable" do
it "mentions ===" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
match_data = matcher.match(partial)
match_data.message.should contain("===")
end
it "mentions first" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
match_data = matcher.match(partial)
match_data.message.should contain("first")
end
end
it "contains the actual label" do
2019-02-01 02:36:35 +00:00
value = "foobar"
start = "baz"
label = "everything"
partial = new_partial(value, label)
2019-02-01 02:36:35 +00:00
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.message.should contain(label)
2019-02-01 02:36:35 +00:00
end
it "contains the expected label" do
value = "foobar"
start = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start, label)
match_data = matcher.match(partial)
match_data.message.should contain(label)
2019-02-01 02:36:35 +00:00
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
start = "baz"
partial = new_partial(value)
2019-02-01 02:36:35 +00:00
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.message.should contain(start)
2019-02-01 02:36:35 +00:00
end
end
end
describe "#negated_message" do
context "with a String" do
it "mentions #starts_with?" do
value = "foobar"
start = "baz"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.negated_message.should contain("#starts_with?")
end
2019-02-01 02:36:35 +00:00
end
context "with an Enumerable" do
it "mentions ===" do
2019-02-01 02:36:35 +00:00
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
match_data = matcher.match(partial)
match_data.negated_message.should contain("===")
2019-02-01 02:36:35 +00:00
end
it "mentions first" do
array = %i[a b c]
partial = new_partial(array)
matcher = Spectator::Matchers::StartWithMatcher.new(array.first)
match_data = matcher.match(partial)
match_data.negated_message.should contain("first")
2019-02-01 02:36:35 +00:00
end
end
it "contains the actual label" do
value = "foobar"
start = "baz"
label = "everything"
partial = new_partial(value, label)
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
2019-02-01 02:36:35 +00:00
end
it "contains the expected label" do
value = "foobar"
start = "baz"
label = "everything"
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(start, label)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
2019-02-01 02:36:35 +00:00
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value = "foobar"
start = "baz"
partial = new_partial(value)
2019-02-01 02:36:35 +00:00
matcher = Spectator::Matchers::StartWithMatcher.new(start)
match_data = matcher.match(partial)
match_data.negated_message.should contain(start)
2019-02-01 02:36:35 +00:00
end
end
end
end
end
end