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