File: diffing_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 (314 lines) | stat: -rw-r--r-- 12,409 bytes parent folder | download | duplicates (2)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
require "spec_helper"
require "pp"

RSpec.describe "Diffs printed when arguments don't match" do
  include RSpec::Support::Spec::DiffHelpers

  before do
    allow(RSpec::Mocks.configuration).to receive(:color?).and_return(false)
  end

  context "with a non matcher object" do
    it "does not print a diff when single line arguments are mismatched" do
      with_unfulfilled_double do |d|
        expect(d).to receive(:foo).with("some string")
        expect {
          d.foo("this other string")
        }.to fail_with(a_string_excluding("Diff:"))
      end
    end

    it "does not print a diff when differ returns a string of only whitespace" do
      differ = instance_double(RSpec::Support::Differ, :diff => "  \n  \t ")
      allow(RSpec::Support::Differ).to receive_messages(:new => differ)

      with_unfulfilled_double do |d|
        expect(d).to receive(:foo).with("some string\nline2")
        expect {
          d.foo("this other string")
        }.to fail_with(a_string_excluding("Diff:"))
      end
    end

    it "does not print a diff when differ returns a string of only whitespace when colour is enabled" do
      allow(RSpec::Mocks.configuration).to receive(:color?) { true }
      differ = instance_double(RSpec::Support::Differ, :diff => "\e[0m\n  \t\e[0m")
      allow(RSpec::Support::Differ).to receive_messages(:new => differ)

      with_unfulfilled_double do |d|
        expect(d).to receive(:foo).with("some string\nline2")
        expect {
          d.foo("this other string")
        }.to fail_with(a_string_excluding("Diff:"))
      end
    end

    it "prints a diff of the strings for individual mismatched multi-line string arguments" do
      with_unfulfilled_double do |d|
        expect(d).to receive(:foo).with("some string\nline2")
        expect {
          d.foo("this other string")
        }.to fail_with("#<Double \"double\"> received :foo with unexpected arguments\n" \
          "  expected: (\"some string\\nline2\")\n       got: (\"this other string\")\n" \
          "Diff:\n@@ -1,3 +1,2 @@\n-some string\n-line2\n+this other string\n")
      end
    end

    it "prints a diff of the args lists for multiple mismatched string arguments" do
      with_unfulfilled_double do |d|
        expect(d).to receive(:foo).with("some string\nline2", "some other string")
        expect {
          d.foo("this other string")
        }.to fail_with("#<Double \"double\"> received :foo with unexpected arguments\n" \
          "  expected: (\"some string\\nline2\", \"some other string\")\n" \
          "       got: (\"this other string\")\nDiff:\n@@ -1,3 +1,2 @@\n-some string\\nline2\n-some other string\n+this other string\n")
      end
    end

    it "does not print a diff when multiple single-line string arguments are mismatched" do
      with_unfulfilled_double do |d|
        expect(d).to receive(:foo).with("some string", "some other string")
        expect {
          d.foo("this other string", "a fourth string")
        }.to fail_with(a_string_excluding("Diff:"))
      end
    end

    let(:expected_hash) { {:baz => :quz, :foo => :bar } }

    let(:actual_hash) { {:bad => :hash} }

    it "prints a diff with hash args" do
      with_unfulfilled_double do |d|
        expect(d).to receive(:foo).with(expected_hash)
        expect {
          d.foo({:bad => :hash})
        }.to fail_with(/\A#<Double "double"> received :foo with unexpected arguments\n  expected: \(#{hash_regex_inspect expected_hash}\)\n       got: \(#{hash_regex_inspect actual_hash}\)\nDiff:\n@@ #{Regexp.escape one_line_header} @@\n\-\[#{hash_regex_inspect expected_hash}\]\n\+\[#{hash_regex_inspect actual_hash}\]\n\z/)
      end
    end

    it "prints a diff with an expected hash arg and a non-hash actual arg" do
      with_unfulfilled_double do |d|
        expect(d).to receive(:foo).with(expected_hash)
        expect {
          d.foo(Object.new)
        }.to fail_with(/-\[#{hash_regex_inspect expected_hash}\].*\+\[#<Object.*>\]/m)
      end
    end

    context 'with keyword arguments on normal doubles' do
      if RSpec::Support::RubyFeatures.distincts_kw_args_from_positional_hash?
        eval <<-'RUBY', nil, __FILE__, __LINE__ + 1
          it "prints a diff when keyword argument were expected but got an option hash (using splat)" do
            with_unfulfilled_double do |d|
              expect(d).to receive(:foo).with(**expected_hash)
              expect {
                d.foo(expected_hash)
              }.to fail_with(
                "#<Double \"double\"> received :foo with unexpected arguments\n" \
                "  expected: ({:baz=>:quz, :foo=>:bar}) (keyword arguments)\n" \
                "       got: ({:baz=>:quz, :foo=>:bar}) (options hash)"
              )
            end
          end
        RUBY

        eval <<-'RUBY', nil, __FILE__, __LINE__ + 1
          it "prints a diff when keyword argument were expected but got an option hash (literal)" do
            with_unfulfilled_double do |d|
              expect(d).to receive(:foo).with(:positional, keyword: 1)
              expect {
                options = { keyword: 1 }
                d.foo(:positional, options)
              }.to fail_with(
                "#<Double \"double\"> received :foo with unexpected arguments\n" \
                "  expected: (:positional, {:keyword=>1}) (keyword arguments)\n" \
                "       got: (:positional, {:keyword=>1}) (options hash)"
              )
            end
          end
        RUBY

        eval <<-'RUBY', nil, __FILE__, __LINE__ + 1
          it "prints a diff when the positional argument doesnt match" do
            with_unfulfilled_double do |d|
              input = Class.new

              expected_input = input.new()
              actual_input = input.new()

              expect(d).to receive(:foo).with(expected_input, one: 1)

              expect {
                options = { one: 1 }
                d.foo(actual_input, options)
              }.to fail_with(
                "#<Double \"double\"> received :foo with unexpected arguments\n" \
                "  expected: (#{expected_input.inspect}, {:one=>1}) (keyword arguments)\n" \
                "       got: (#{actual_input.inspect}, {:one=>1}) (options hash)\n" \
                "Diff:\n" \
                "@@ -1 +1 @@\n" \
                "-[#{expected_input.inspect}, {:one=>1}]\n" \
                "+[#{actual_input.inspect}, {:one=>1}]\n"
              )
            end
          end
        RUBY
      end
    end

    context 'with keyword arguments on partial doubles' do
      include_context "with isolated configuration"

      let(:d) { Class.new { def foo(a, b); end }.new }

      before(:example) do
        RSpec::Mocks.configuration.verify_partial_doubles = true
        allow(RSpec.configuration).to receive(:color_enabled?) { false }
      end

      after(:example) { reset d }

      if RSpec::Support::RubyFeatures.distincts_kw_args_from_positional_hash?
        eval <<-'RUBY', nil, __FILE__, __LINE__ + 1
          it "prints a diff when keyword argument were expected but got an option hash (using splat)" do
            expect(d).to receive(:foo).with(:positional, **expected_hash)
            expect {
              d.foo(:positional, expected_hash)
            }.to fail_with(
              "#{d.inspect} received :foo with unexpected arguments\n" \
              "  expected: (:positional, {:baz=>:quz, :foo=>:bar}) (keyword arguments)\n" \
              "       got: (:positional, {:baz=>:quz, :foo=>:bar}) (options hash)"
            )
          end
        RUBY

        eval <<-'RUBY', nil, __FILE__, __LINE__ + 1
          it "prints a diff when keyword argument were expected but got an option hash (literal)" do
            expect(d).to receive(:foo).with(:positional, keyword: 1)
            expect {
              options = { keyword: 1 }
              d.foo(:positional, options)
            }.to fail_with(
              "#{d.inspect} received :foo with unexpected arguments\n" \
              "  expected: (:positional, {:keyword=>1}) (keyword arguments)\n" \
              "       got: (:positional, {:keyword=>1}) (options hash)"
            )
          end
        RUBY

        eval <<-'RUBY', nil, __FILE__, __LINE__ + 1
          it "prints a diff when the positional argument doesnt match" do
            input = Class.new

            expected_input = input.new()
            actual_input = input.new()

            expect(d).to receive(:foo).with(expected_input, one: 1)

            expect {
              options = { one: 1 }
              d.foo(actual_input, options)
            }.to fail_with(
              "#{d.inspect} received :foo with unexpected arguments\n" \
              "  expected: (#{expected_input.inspect}, {:one=>1}) (keyword arguments)\n" \
              "       got: (#{actual_input.inspect}, {:one=>1}) (options hash)\n" \
              "Diff:\n" \
              "@@ -1 +1 @@\n" \
              "-[#{expected_input.inspect}, {:one=>1}]\n" \
              "+[#{actual_input.inspect}, {:one=>1}]\n"
            )
          end
        RUBY
      end
    end

    if RUBY_VERSION.to_f < 1.9
      # Ruby 1.8 hashes are not ordered, but `#inspect` on a particular unchanged
      # hash instance should return consistent output. However, on Travis that does
      # not always seem to be true and we have no idea why. Somehow, the travis build
      # has occasionally failed due to the output ordering varying between `inspect`
      # calls to the same hash. This regex allows us to work around that.
      def hash_regex_inspect(hash)
        "\\{(#{hash.map { |key, value| "#{key.inspect}=>#{value.inspect}.*" }.join "|"}){#{hash.size}}\\}"
      end
    else
      def hash_regex_inspect(hash)
        Regexp.escape(hash.inspect)
      end
    end

    it "prints a diff with array args" do
      with_unfulfilled_double do |d|
        expect(d).to receive(:foo).with([:a, :b, :c])
        expect {
          d.foo([])
        }.to fail_with("#<Double \"double\"> received :foo with unexpected arguments\n  expected: ([:a, :b, :c])\n       got: ([])\nDiff:\n@@ #{one_line_header} @@\n-[[:a, :b, :c]]\n+[[]]\n")
      end
    end

    context "that defines #description" do
      it "does not use the object's description for a non-matcher object that implements #description" do
        with_unfulfilled_double do |d|

          collab = double(:collab, :description => "This string")
          collab_inspect = collab.inspect

          expect(d).to receive(:foo).with(collab)
          expect {
            d.foo([])
          }.to fail_with("#<Double \"double\"> received :foo with unexpected arguments\n" \
            "  expected: (#{collab_inspect})\n" \
            "       got: ([])\nDiff:\n@@ #{one_line_header} @@\n-[#{collab_inspect}]\n+[[]]\n")
        end
      end
    end
  end

  context "with a matcher object" do
    context "that defines #description" do
      it "uses the object's description" do
        with_unfulfilled_double do |d|

          collab = fake_matcher(Object.new)
          collab_description = collab.description

          expect(d).to receive(:foo).with(collab)
          expect {
            d.foo([:a, :b])
          }.to fail_with("#<Double \"double\"> received :foo with unexpected arguments\n" \
            "  expected: (#{collab_description})\n" \
            "       got: ([:a, :b])\nDiff:\n@@ #{one_line_header} @@\n-[\"#{collab_description}\"]\n+[[:a, :b]]\n")
        end
      end
    end

    context "that does not define #description" do
      it "for a matcher object that does not implement #description" do
        with_unfulfilled_double do |d|
          collab = Class.new do
            def self.name
              "RSpec::Mocks::ArgumentMatchers::"
            end

            def inspect
              "#<MyCollab>"
            end
          end.new

          expect(RSpec::Support.is_a_matcher?(collab)).to be true

          collab_inspect = collab.inspect
          collab_pp = PP.pp(collab, "".dup).strip

          expect(d).to receive(:foo).with(collab)
          expect {
            d.foo([:a, :b])
          }.to fail_with("#<Double \"double\"> received :foo with unexpected arguments\n" \
            "  expected: (#{collab_inspect})\n" \
            "       got: ([:a, :b])\nDiff:\n@@ #{one_line_header} @@\n-[#{collab_pp}]\n+[[:a, :b]]\n")
        end
      end
    end
  end
end