shard-spectator/spec/matchers/have_key_matcher_spec.cr

167 lines
5.3 KiB
Crystal
Raw Normal View History

2019-02-06 02:26:46 +00:00
require "../spec_helper"
2019-03-06 09:02:55 +00:00
private struct FakeKeySet
def has_key?(key)
true
end
end
2019-02-06 02:26:46 +00:00
describe Spectator::Matchers::HaveKeyMatcher do
2019-03-06 09:02:55 +00:00
describe "#match" do
context "returned MatchData" do
describe "#matched?" do
context "against a Hash" do
context "with an existing key" do
it "is true" do
hash = Hash{"foo" => "bar"}
key = "foo"
partial = new_partial(hash)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "with a non-existent key" do
it "is false" do
hash = Hash{"foo" => "bar"}
key = "baz"
partial = new_partial(hash)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
end
context "against a NamedTuple" do
context "with an existing key" do
it "is true" do
tuple = {foo: "bar"}
key = :foo
partial = new_partial(tuple)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.matched?.should be_true
end
end
context "with a non-existent key" do
it "is false" do
tuple = {foo: "bar"}
key = :baz
partial = new_partial(tuple)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.matched?.should be_false
end
end
2019-02-06 02:26:46 +00:00
end
end
2019-03-06 09:02:55 +00:00
describe "#values" do
context "key" do
it "is the expected key" do
tuple = {foo: "bar"}
key = :baz
partial = new_partial(tuple)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.values[:key].should eq(key)
end
end
context "actual" do
context "when #keys is available" do
it "is the list of keys" do
tuple = {foo: "bar"}
key = :baz
partial = new_partial(tuple)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.values[:actual].should eq(tuple.keys)
end
end
context "when #keys isn't available" do
it "is the actual value" do
actual = FakeKeySet.new
key = :baz
partial = new_partial(actual)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.values[:actual].should eq(actual)
end
end
2019-02-06 02:26:46 +00:00
end
end
2019-03-06 09:02:55 +00:00
describe "#message" do
it "contains the actual label" do
2019-02-06 02:26:46 +00:00
tuple = {foo: "bar"}
key = :foo
2019-03-06 09:02:55 +00:00
label = "blah"
partial = new_partial(tuple, label)
2019-02-06 02:26:46 +00:00
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
2019-03-06 09:02:55 +00:00
match_data = matcher.match(partial)
match_data.message.should contain(label)
2019-02-06 02:26:46 +00:00
end
2019-03-06 09:02:55 +00:00
it "contains the expected label" do
2019-02-06 02:26:46 +00:00
tuple = {foo: "bar"}
2019-03-06 09:02:55 +00:00
key = :foo
label = "blah"
partial = new_partial(tuple)
2019-03-06 09:02:55 +00:00
matcher = Spectator::Matchers::HaveKeyMatcher.new(key, label)
match_data = matcher.match(partial)
match_data.message.should contain(label)
2019-02-06 02:26:46 +00:00
end
2019-03-06 09:02:55 +00:00
context "when the expected label is omitted" do
it "contains the stringified key" do
tuple = {foo: "bar"}
key = :foo
partial = new_partial(tuple)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.message.should contain(key.to_s)
end
end
2019-02-06 02:26:46 +00:00
end
2019-03-06 09:02:55 +00:00
describe "#negated_message" do
it "contains the actual label" do
tuple = {foo: "bar"}
key = :foo
label = "blah"
partial = new_partial(tuple, label)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
2019-02-06 02:26:46 +00:00
2019-03-06 09:02:55 +00:00
it "contains the expected label" do
tuple = {foo: "bar"}
key = :foo
label = "blah"
partial = new_partial(tuple)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key, label)
match_data = matcher.match(partial)
match_data.negated_message.should contain(label)
end
2019-02-06 02:26:46 +00:00
2019-03-06 09:02:55 +00:00
context "when the expected label is omitted" do
it "contains the stringified key" do
tuple = {foo: "bar"}
key = :foo
partial = new_partial(tuple)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.negated_message.should contain(key.to_s)
end
end
2019-02-06 02:26:46 +00:00
end
end
end
end