2019-02-04 23:19:39 +00:00
|
|
|
require "../spec_helper"
|
|
|
|
|
|
|
|
describe Spectator::Matchers::AttributesMatcher do
|
|
|
|
describe "#match?" do
|
2019-02-04 23:26:53 +00:00
|
|
|
it "uses ===" do
|
|
|
|
array = %i[a b c]
|
|
|
|
spy = SpySUT.new
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:26:53 +00:00
|
|
|
matcher = Spectator::Matchers::HaveMatcher.new({spy})
|
|
|
|
matcher.match?(partial).should be_true
|
|
|
|
spy.case_eq_call_count.should be > 0
|
|
|
|
end
|
2019-02-04 23:27:40 +00:00
|
|
|
|
2019-02-04 23:19:39 +00:00
|
|
|
context "one argument" do
|
|
|
|
context "against an equal value" do
|
|
|
|
it "is true" do
|
|
|
|
array = %i[a b c]
|
|
|
|
attributes = {first: :a}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against a different value" do
|
|
|
|
it "is false" do
|
|
|
|
array = %i[a b c]
|
|
|
|
attributes = {first: :z}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against a matching type" do
|
|
|
|
it "is true" do
|
|
|
|
array = %i[a b c]
|
|
|
|
attributes = {first: Symbol}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against a non-matching type" do
|
|
|
|
it "is false" do
|
|
|
|
array = %i[a b c]
|
|
|
|
attributes = {first: Int32}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against a matching regex" do
|
|
|
|
it "is true" do
|
|
|
|
array = %w[FOO BAR BAZ]
|
|
|
|
attributes = {first: /foo/i}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against a non-matching regex" do
|
|
|
|
it "is false" do
|
|
|
|
array = %w[FOO BAR BAZ]
|
|
|
|
attributes = {first: /qux/i}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "multiple attributes" do
|
|
|
|
context "against equal values" do
|
|
|
|
it "is true" do
|
|
|
|
array = %i[a b c]
|
|
|
|
attributes = {first: :a, last: :c}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
context "matching type" do
|
|
|
|
context "matching regex" do
|
|
|
|
it "is true" do
|
|
|
|
array = [:a, 42, "FOO"]
|
|
|
|
attributes = {first: Symbol, last: /foo/i}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "non-matching regex" do
|
|
|
|
it "is false" do
|
|
|
|
array = [:a, 42, "FOO"]
|
|
|
|
attributes = {first: Symbol, last: /bar/i}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "non-matching type" do
|
|
|
|
context "matching regex" do
|
|
|
|
it "is false" do
|
|
|
|
array = [:a, 42, "FOO"]
|
|
|
|
attributes = {first: Float32, last: /foo/i}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "non-matching regex" do
|
|
|
|
it "is false" do
|
|
|
|
array = [:a, 42, "FOO"]
|
|
|
|
attributes = {first: Float32, last: /bar/i}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against one equal value" do
|
|
|
|
it "is false" do
|
|
|
|
array = %i[a b c]
|
|
|
|
attributes = {first: :a, last: :d}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against no equal values" do
|
|
|
|
it "is false" do
|
|
|
|
array = %i[a b c]
|
|
|
|
attributes = {first: :d, last: :e}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against matching types" do
|
|
|
|
it "is true" do
|
|
|
|
array = [:a, 42, "FOO"]
|
|
|
|
attributes = {first: Symbol, last: String}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against one matching type" do
|
|
|
|
it "is false" do
|
|
|
|
array = [:a, 42, "FOO"]
|
|
|
|
attributes = {first: Symbol, last: Float32}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against no matching types" do
|
|
|
|
it "is false" do
|
|
|
|
array = [:a, 42, "FOO"]
|
|
|
|
attributes = {first: Float32, last: Bytes}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against matching regexes" do
|
|
|
|
it "is true" do
|
|
|
|
array = %w[FOO BAR BAZ]
|
|
|
|
attributes = {first: /foo/i, last: /baz/i}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against one matching regex" do
|
|
|
|
it "is false" do
|
|
|
|
array = %w[FOO BAR BAZ]
|
|
|
|
attributes = {first: /foo/i, last: /qux/i}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against no matching regexes" do
|
|
|
|
it "is false" do
|
|
|
|
array = %w[FOO BAR]
|
|
|
|
attributes = {first: /baz/i, last: /qux/i}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "against equal and matching type and regex" do
|
|
|
|
it "is true" do
|
|
|
|
array = [:a, 42, "FOO"]
|
|
|
|
attributes = {first: Symbol, last: /foo/i, size: 3}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(array)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.match?(partial).should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#message" do
|
|
|
|
it "contains the actual label" do
|
|
|
|
value = "foobar"
|
|
|
|
attributes = {size: 6}
|
|
|
|
label = "everything"
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(value, label)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.message(partial).should contain(label)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "contains the expected label" do
|
|
|
|
value = "foobar"
|
|
|
|
attributes = {size: 6}
|
|
|
|
label = "everything"
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(value)
|
2019-02-28 22:17:12 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes, label)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher.message(partial).should contain(label)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when expected label is omitted" do
|
|
|
|
it "contains stringified form of expected value" do
|
|
|
|
value = "foobar"
|
|
|
|
attributes = {size: 6}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(value)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.message(partial).should contain(attributes.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#negated_message" do
|
|
|
|
it "contains the actual label" do
|
|
|
|
value = "foobar"
|
|
|
|
attributes = {size: 6}
|
|
|
|
label = "everything"
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(value, label)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.negated_message(partial).should contain(label)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "contains the expected label" do
|
|
|
|
value = "foobar"
|
|
|
|
attributes = {size: 6}
|
|
|
|
label = "everything"
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(value)
|
2019-02-28 22:17:12 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes, label)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher.negated_message(partial).should contain(label)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when expected label is omitted" do
|
|
|
|
it "contains stringified form of expected value" do
|
|
|
|
value = "foobar"
|
|
|
|
attributes = {size: 6}
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(value)
|
2019-02-04 23:19:39 +00:00
|
|
|
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
|
|
|
|
matcher.negated_message(partial).should contain(attributes.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|