1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
module RSpec
module Matchers
RSpec.describe "equal" do
it_behaves_like "an RSpec value matcher", :valid_value => :a, :invalid_value => :b do
let(:matcher) { equal(:a) }
end
def inspect_object(o)
"#<#{o.class}:#{o.object_id}> => #{o.inspect}"
end
it "matches when actual.equal?(expected)" do
expect(1).to equal(1)
end
it "does not match when !actual.equal?(expected)" do
expect("1").not_to equal("1".dup)
end
it "describes itself" do
matcher = equal(1)
matcher.matches?(1)
expect(matcher.description).to eq "equal 1"
end
context "when the expected object is falsey in conditional semantics" do
it "describes itself with the expected object" do
matcher = equal(nil)
matcher.matches?(nil)
expect(matcher.description).to eq "equal nil"
end
end
context "when the expected object's #equal? always returns true" do
let(:strange_string) do
string = "foo".dup
def string.equal?(_other)
true
end
string
end
it "describes itself with the expected object" do
matcher = equal(strange_string)
matcher.matches?(strange_string)
expect(matcher.description).to eq 'equal "foo"'
end
end
context "the output for expected" do
it "doesn't include extra object detail for `true`" do
expected, actual = true, "1"
expect {
expect(actual).to equal(expected)
}.to fail_with "\nexpected true\n got #{inspect_object(actual)}\n"
end
it "doesn't include extra object detail for `false`" do
expected, actual = false, "1"
expect {
expect(actual).to equal(expected)
}.to fail_with "\nexpected false\n got #{inspect_object(actual)}\n"
end
it "doesn't include extra object detail for `nil`" do
expected, actual = nil, "1"
expect {
expect(actual).to equal(expected)
}.to fail_with "\nexpected nil\n got #{inspect_object(actual)}\n"
end
end
context "the output for actual" do
it "doesn't include extra object detail for `true`" do
expected, actual = true, false
expect {
expect(actual).to equal(expected)
}.to fail_with "\nexpected true\n got false\n"
end
it "doesn't include extra object detail for `false`" do
expected, actual = false, nil
expect {
expect(actual).to equal(expected)
}.to fail_with "\nexpected false\n got nil\n"
end
it "doesn't include extra object detail for `nil`" do
expected, actual = nil, false
expect {
expect(actual).to equal(expected)
}.to fail_with "\nexpected nil\n got false\n"
end
end
it "suggests the `eq` matcher on failure" do
expected, actual = "1", "1".dup
expect {
expect(actual).to equal(expected)
}.to fail_with <<-MESSAGE
expected #{inspect_object(expected)}
got #{inspect_object(actual)}
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
`expect(actual).to eq(expected)` if you don't care about
object identity in this example.
MESSAGE
end
it "provides message on #negative_failure_message" do
expected = actual = "1"
matcher = equal(expected)
matcher.matches?(actual)
expect(matcher.failure_message_when_negated).to eq <<-MESSAGE
expected not #{inspect_object(expected)}
got #{inspect_object(actual)}
Compared using equal?, which compares object identity.
MESSAGE
end
end
end
end
|