shard-spectator/spec/matchers/regex_matcher_spec.cr

148 lines
4.8 KiB
Crystal
Raw Normal View History

2019-01-19 21:49:13 +00:00
require "../spec_helper"
describe Spectator::Matchers::RegexMatcher do
2019-03-06 20:10:16 +00:00
describe "#match" do
2019-01-19 21:49:13 +00:00
it "compares using #=~" do
spy = SpySUT.new
partial = new_partial(spy)
2019-01-19 21:49:13 +00:00
matcher = Spectator::Matchers::RegexMatcher.new(/foobar/)
2019-03-06 20:10:16 +00:00
matcher.match(partial)
2019-01-19 21:49:13 +00:00
spy.match_call_count.should be > 0
end
2019-03-06 20:10:16 +00:00
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
2019-01-19 21:49:13 +00:00
end
2019-03-06 20:10:16 +00:00
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)
2019-03-06 20:10:16 +00:00
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)
2019-03-06 20:10:16 +00:00
end
end
2019-01-19 21:49:13 +00:00
end
2019-03-06 20:10:16 +00:00
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
2019-01-19 21:49:13 +00:00
2019-03-06 20:10:16 +00:00
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
2019-01-19 21:49:13 +00:00
2019-03-06 20:10:16 +00:00
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
2019-01-19 21:49:13 +00:00
2019-03-06 20:10:16 +00:00
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
2019-01-19 21:49:13 +00:00
end
2019-03-06 20:10:16 +00:00
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
2019-01-19 21:49:13 +00:00
2019-03-06 20:10:16 +00:00
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
2019-01-19 21:49:13 +00:00
2019-03-06 20:10:16 +00:00
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
2019-01-19 21:49:13 +00:00
2019-03-06 20:10:16 +00:00
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
2019-01-19 21:49:13 +00:00
end
end
end
end