File: eq_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (166 lines) | stat: -rw-r--r-- 6,068 bytes parent folder | download
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
module RSpec
  module Matchers
    RSpec.describe "eq" do
      it_behaves_like "an RSpec value matcher", :valid_value => 1, :invalid_value => 2 do
        let(:matcher) { eq(1) }
      end

      it "is diffable" do
        expect(eq(1)).to be_diffable
      end

      it "matches when actual == expected" do
        expect(1).to eq(1)
      end

      it "does not match when actual != expected" do
        expect(1).not_to eq(2)
      end

      it "compares by sending == to actual (not expected)" do
        called = false
        actual = Class.new do
          define_method :== do |_other|
            called = true
          end
        end.new

        expect(actual).to eq :anything # to trigger the matches? method
        expect(called).to be_truthy
      end

      it "describes itself" do
        matcher = eq(1)
        matcher.matches?(1)
        expect(matcher.description).to eq "eq 1"
      end

      it "provides message, expected and actual on #failure_message" do
        matcher = eq("1")
        matcher.matches?(1)
        expect(matcher.failure_message).to eq "\nexpected: \"1\"\n     got: 1\n\n(compared using ==)\n"
      end

      it "provides message, expected and actual on #negative_failure_message" do
        matcher = eq(1)
        matcher.matches?(1)
        expect(matcher.failure_message_when_negated).to eq "\nexpected: value != 1\n     got: 1\n\n(compared using ==)\n"
      end

      # Older versions of Ruby such as less than 1.9 do not have String#encoding available, they are an array of bytes
      if String.method_defined?(:encoding)
        context "with String encoding as UTF-16LE" do
          it "provides message, expected and actual on #failure_message when string encoding is the same" do
            matcher = eq('abc'.encode('UTF-16LE'))
            matcher.matches?('def'.encode('UTF-16LE'))
            expect(matcher.failure_message).to eq "\nexpected: \"abc\"\n     got: \"def\"\n\n(compared using ==)\n"
          end

          it "matches when actual is BINARY encoding and expected is UTF-8 encoding with the same chars" do
            expect('abc'.encode('BINARY')).to eq 'abc'.encode('UTF-8')
          end

          it "provides message, expected and actual with encoding details on #failure_message when string encoding is different" do
            matcher = eq('abc'.encode('UTF-16LE'))
            matcher.matches?('abc'.force_encoding('ASCII-8BIT'))
            expect(matcher.failure_message).to eq "\nexpected: #<Encoding:UTF-16LE> \"abc\"\n     got: #<Encoding:ASCII-8BIT> \"abc\"\n\n(compared using ==)\n"
          end

          it "provides message, expected and actual on #negative_failure_message" do
            matcher = eq('abc'.encode('UTF-16LE'))
            matcher.matches?('abc'.encode('UTF-16LE'))
            expect(matcher.failure_message_when_negated).to eq "\nexpected: value != \"abc\"\n     got: \"abc\"\n\n(compared using ==)\n"
          end
        end
      end

      context "with Time objects" do
        RSpec::Matchers.define :a_string_with_differing_output do
          match do |string|
            time_strings = /expected: (.+)\n.*got: (.+)$/.match(string).captures
            time_strings.uniq.count == 2
          end
        end

        let(:time1) { Time.utc(1969, 12, 31, 19, 10, 40, 101) }
        let(:time2) { Time.utc(1969, 12, 31, 19, 10, 40, 102) }

        it "provides additional precision on #failure_message" do
          expect {
            expect(time1).to eq(time2)
          }.to fail_with(a_string_with_differing_output)
        end

        it "provides additional precision on #negative_failure_message" do
          expect {
            expect(time1).to_not eq(time1)
          }.to fail_with(a_string_with_differing_output)
        end
      end

      it 'fails properly when the actual is an array of multiline strings' do
        expect {
          expect(["a\nb", "c\nd"]).to eq([])
        }.to fail_including("expected: []")
      end

      describe '#description' do
        # Ruby 1.8.7 produces a less precise output
        expected_seconds = Time.method_defined?(:nsec) ? '000000000' : '000000'

        [
            [nil, 'eq nil'],
            [true, 'eq true'],
            [false, 'eq false'],
            [:symbol, 'eq :symbol'],
            [1, 'eq 1'],
            [1.2, 'eq 1.2'],
            ['foo', 'eq "foo"'],
            [/regex/, 'eq /regex/'],
            [['foo'], 'eq ["foo"]'],
            [{ :foo => :bar }, 'eq {:foo=>:bar}'],
            [Class, 'eq Class'],
            [RSpec, 'eq RSpec'],
            [Time.utc(2014, 1, 1), "eq 2014-01-01 00:00:00.#{expected_seconds} +0000"],
        ].each do |expected, expected_description|
          context "with #{expected.inspect}" do
            around { |ex| with_env_vars('TZ' => 'UTC', &ex) } if expected.is_a?(Time)

            it "is \"#{expected_description}\"" do
              expect(eq(expected).description).to eq expected_description
            end
          end
        end

        context "with Date.new(2014, 1, 1)" do
          it "is eq to Date.new(2014, 1, 1).inspect" do
            in_sub_process_if_possible do
              require 'date'
              date = Date.new(2014, 1, 1)
              expect(eq(date).description).to eq "eq #{date.inspect}"
            end
          end
        end

        context "with Complex(1, 2)" do
          it "is eq to Complex(1, 2).inspect" do
            in_sub_process_if_possible do
              # complex is available w/o requiring on ruby 1.9+.
              # Loading it on 1.9+ issues a warning, so we only load it on 1.8.7.
              require 'complex' if RUBY_VERSION == '1.8.7'

              complex = Complex(1, 2)
              expect(eq(complex).description).to eq "eq #{complex.inspect}"
            end
          end
        end

        context 'with object' do
          it 'matches with "^eq #<Object:0x[0-9a-f]*>$"' do
            expect(eq(Object.new).description).to match(/^eq #<Object:0x[0-9a-f]*>$/)
          end
        end
      end
    end
  end
end