| 1 |
module RSpec |
| 2 |
module Matchers |
| 3 |
RSpec.describe "eq" do |
| 4 |
it_behaves_like "an RSpec value matcher", :valid_value => 1, :invalid_value => 2 do |
| 5 |
let(:matcher) { eq(1) } |
| 6 |
end |
| 7 |
|
| 8 |
it "is diffable" do |
| 9 |
expect(eq(1)).to be_diffable |
| 10 |
end |
| 11 |
|
| 12 |
it "matches when actual == expected" do |
| 13 |
expect(1).to eq(1) |
| 14 |
end |
| 15 |
|
| 16 |
it "does not match when actual != expected" do |
| 17 |
expect(1).not_to eq(2) |
| 18 |
end |
| 19 |
|
| 20 |
it "compares by sending == to actual (not expected)" do |
| 21 |
called = false |
| 22 |
actual = Class.new do |
| 23 |
define_method :== do |_other| |
| 24 |
called = true |
| 25 |
end |
| 26 |
end.new |
| 27 |
|
| 28 |
expect(actual).to eq :anything |
| 29 |
expect(called).to be_truthy |
| 30 |
end |
| 31 |
|
| 32 |
it "describes itself" do |
| 33 |
matcher = eq(1) |
| 34 |
matcher.matches?(1) |
| 35 |
expect(matcher.description).to eq "eq 1" |
| 36 |
end |
| 37 |
|
| 38 |
it "provides message, expected and actual on #failure_message" do |
| 39 |
matcher = eq("1") |
| 40 |
matcher.matches?(1) |
| 41 |
expect(matcher.failure_message).to eq "\nexpected: \"1\"\n got: 1\n\n(compared using ==)\n" |
| 42 |
end |
| 43 |
|
| 44 |
it "provides message, expected and actual on #negative_failure_message" do |
| 45 |
matcher = eq(1) |
| 46 |
matcher.matches?(1) |
| 47 |
expect(matcher.failure_message_when_negated).to eq "\nexpected: value != 1\n got: 1\n\n(compared using ==)\n" |
| 48 |
end |
| 49 |
|
| 50 |
|
| 51 |
if String.method_defined?(:encoding) |
| 52 |
context "with String encoding as UTF-16LE" do |
| 53 |
it "provides message, expected and actual on #failure_message when string encoding is the same" do |
| 54 |
matcher = eq('abc'.encode('UTF-16LE')) |
| 55 |
matcher.matches?('def'.encode('UTF-16LE')) |
| 56 |
expect(matcher.failure_message).to eq "\nexpected: \"abc\"\n got: \"def\"\n\n(compared using ==)\n" |
| 57 |
end |
| 58 |
|
| 59 |
it "matches when actual is BINARY encoding and expected is UTF-8 encoding with the same chars" do |
| 60 |
expect('abc'.encode('BINARY')).to eq 'abc'.encode('UTF-8') |
| 61 |
end |
| 62 |
|
| 63 |
it "provides message, expected and actual with encoding details on #failure_message when string encoding is different" do |
| 64 |
matcher = eq('abc'.encode('UTF-16LE')) |
| 65 |
matcher.matches?('abc'.dup.force_encoding('ASCII-8BIT')) |
| 66 |
expect(matcher.failure_message).to eq "\nexpected: #<Encoding:UTF-16LE> \"abc\"\n got: #<Encoding:ASCII-8BIT> \"abc\"\n\n(compared using ==)\n" |
| 67 |
end |
| 68 |
|
| 69 |
it "provides message, expected and actual on #negative_failure_message" do |
| 70 |
matcher = eq('abc'.encode('UTF-16LE')) |
| 71 |
matcher.matches?('abc'.encode('UTF-16LE')) |
| 72 |
expect(matcher.failure_message_when_negated).to eq "\nexpected: value != \"abc\"\n got: \"abc\"\n\n(compared using ==)\n" |
| 73 |
end |
| 74 |
end |
| 75 |
end |
| 76 |
|
| 77 |
context "with Time objects" do |
| 78 |
RSpec::Matchers.define :a_string_with_differing_output do |
| 79 |
match do |string| |
| 80 |
time_strings = /expected: (.+)\n.*got: (.+)$/.match(string).captures |
| 81 |
time_strings.uniq.count == 2 |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
let(:time1) { Time.utc(1969, 12, 31, 19, 10, 40, 101) } |
| 86 |
let(:time2) { Time.utc(1969, 12, 31, 19, 10, 40, 102) } |
| 87 |
|
| 88 |
it "provides additional precision on #failure_message" do |
| 89 |
expect { |
| 90 |
expect(time1).to eq(time2) |
| 91 |
}.to fail_with(a_string_with_differing_output) |
| 92 |
end |
| 93 |
|
| 94 |
it "provides additional precision on #negative_failure_message" do |
| 95 |
expect { |
| 96 |
expect(time1).to_not eq(time1) |
| 97 |
}.to fail_with(a_string_with_differing_output) |
| 98 |
end |
| 99 |
end |
| 100 |
|
| 101 |
it 'fails properly when the actual is an array of multiline strings' do |
| 102 |
expect { |
| 103 |
expect(["a\nb", "c\nd"]).to eq([]) |
| 104 |
}.to fail_including("expected: []") |
| 105 |
end |
| 106 |
|
| 107 |
describe '#description' do |
| 108 |
|
| 109 |
expected_seconds = Time.method_defined?(:nsec) ? '000000000' : '000000' |
| 110 |
|
| 111 |
[ |
| 112 |
[nil, 'eq nil'], |
| 113 |
[true, 'eq true'], |
| 114 |
[false, 'eq false'], |
| 115 |
[:symbol, 'eq :symbol'], |
| 116 |
[1, 'eq 1'], |
| 117 |
[1.2, 'eq 1.2'], |
| 118 |
['foo', 'eq "foo"'], |
| 119 |
[/regex/, 'eq /regex/'], |
| 120 |
[['foo'], 'eq ["foo"]'], |
| 121 |
[{ :foo => :bar }, "eq #{{ :foo=>:bar }.inspect}"], |
| 122 |
[Class, 'eq Class'], |
| 123 |
[RSpec, 'eq RSpec'], |
| 124 |
[Time.utc(2014, 1, 1), "eq 2014-01-01 00:00:00.#{expected_seconds} +0000"], |
| 125 |
].each do |expected, expected_description| |
| 126 |
context "with #{expected.inspect}" do |
| 127 |
around { |ex| with_env_vars('TZ' => 'UTC', &ex) } if expected.is_a?(Time) |
| 128 |
|
| 129 |
it "is \"#{expected_description}\"" do |
| 130 |
expect(eq(expected).description).to eq expected_description |
| 131 |
end |
| 132 |
end |
| 133 |
end |
| 134 |
|
| 135 |
context "with Date.new(2014, 1, 1)" do |
| 136 |
it "is eq to Date.new(2014, 1, 1).inspect" do |
| 137 |
in_sub_process_if_possible do |
| 138 |
require 'date' |
| 139 |
date = Date.new(2014, 1, 1) |
| 140 |
expect(eq(date).description).to eq "eq #{date.inspect}" |
| 141 |
end |
| 142 |
end |
| 143 |
end |
| 144 |
|
| 145 |
context "with Complex(1, 2)" do |
| 146 |
it "is eq to Complex(1, 2).inspect" do |
| 147 |
in_sub_process_if_possible do |
| 148 |
|
| 149 |
|
| 150 |
require 'complex' if RUBY_VERSION == '1.8.7' |
| 151 |
|
| 152 |
complex = Complex(1, 2) |
| 153 |
expect(eq(complex).description).to eq "eq #{complex.inspect}" |
| 154 |
end |
| 155 |
end |
| 156 |
end |
| 157 |
|
| 158 |
context 'with object' do |
| 159 |
it 'matches with "^eq #<Object:0x[0-9a-f]*>$"' do |
| 160 |
expect(eq(Object.new).description).to match(/^eq #<Object:0x[0-9a-f]*>$/) |
| 161 |
end |
| 162 |
end |
| 163 |
end |
| 164 |
end |
| 165 |
end |
| 166 |
end |