File: mutate_const_spec.rb

package info (click to toggle)
ruby-spy 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 360 kB
  • sloc: ruby: 3,101; makefile: 2
file content (471 lines) | stat: -rw-r--r-- 16,917 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
require 'spec_helper'

TOP_LEVEL_VALUE_CONST = 7

class TestClass
  M = :m
  N = :n

  class Nested
    class NestedEvenMore
    end
  end
end

class TestSubClass < TestClass
  P = :p
end

module Spy
  describe "Constant Mutating" do

    def reset_rspec_mocks
      Spy.teardown
    end

    def on_const(const_name)
      if const_name.include? "::"
      args = [recursive_const_get("Object::" + const_name.sub(/(::)?[^:]+\z/, '')), const_name.split('::').last.to_sym]
      else
        args = [const_name.to_sym]
      end
      Spy.get_const(*args) || Spy.on_const(*args)
    end

    def stub_const(const_name, value)
      on_const(const_name).and_return(value)
      value
    end

    def hide_const(const_name)
      on_const(const_name).and_hide
    end

    shared_context "constant example methods" do |const_name|
      define_method :const do
        recursive_const_get(const_name)
      end

      define_method :parent_const do
        recursive_const_get("Object::" + const_name.sub(/(::)?[^:]+\z/, ''))
      end

      define_method :last_const_part do
        const_name.split('::').last
      end
    end

    shared_examples_for "loaded constant stubbing" do |const_name|
      include_context "constant example methods", const_name

      let!(:original_const_value) { const }
      after { change_const_value_to(original_const_value) }

      def change_const_value_to(value)
        parent_const.send(:remove_const, last_const_part)
        parent_const.const_set(last_const_part, value)
      end

      it 'allows it to be stubbed' do
        expect(const).not_to eq(7)
        stub_const(const_name, 7)
        expect(const).to eq(7)
      end

      it 'resets it to its original value when rspec clears its mocks' do
        original_value = const
        expect(original_value).not_to eq(:a)
        stub_const(const_name, :a)
        reset_rspec_mocks
        expect(const).to be(original_value)
      end

      it 'returns the stubbed value' do
        expect(stub_const(const_name, 7)).to eq(7)
      end
    end

    shared_examples_for "loaded constant hiding" do |const_name|
      before do
        expect(recursive_const_defined?(const_name)).to be_true
      end

      it 'allows it to be hidden' do
        hide_const(const_name)
        expect(recursive_const_defined?(const_name)).to be_false
      end

      it 'resets the constant when rspec clear its mocks' do
        hide_const(const_name)
        reset_rspec_mocks
        expect(recursive_const_defined?(const_name)).to be_true
      end

      it 'returns nil' do
        expect(hide_const(const_name)).to be_nil
      end
    end

    shared_examples_for "unloaded constant stubbing" do |const_name|
      include_context "constant example methods", const_name

      before do
        expect(recursive_const_defined?(const_name)).to be_false
      end

      it 'allows it to be stubbed' do
        stub_const(const_name, 7)
        expect(const).to eq(7)
      end

      it 'removes the constant when rspec clears its mocks' do
        stub_const(const_name, 7)
        reset_rspec_mocks
        expect(recursive_const_defined?(const_name)).to be_false
      end

      it 'returns the stubbed value' do
        expect(stub_const(const_name, 7)).to eq(7)
      end

      it 'ignores the :transfer_nested_constants option if passed' do
        stub = Module.new
        stub_const(const_name, stub, :transfer_nested_constants => true)
        expect(stub.constants).to eq([])
      end
    end

    shared_examples_for "unloaded constant hiding" do |const_name|
      include_context "constant example methods", const_name

      before do
        expect(recursive_const_defined?(const_name)).to be_false
      end

      it 'allows it to be hidden, though the operation has no effect' do
        hide_const(const_name)
        expect(recursive_const_defined?(const_name)).to be_false
      end

      it 'remains undefined after rspec clears its mocks' do
        hide_const(const_name)
        reset_rspec_mocks
        expect(recursive_const_defined?(const_name)).to be_false
      end

      it 'returns nil' do
        expect(hide_const(const_name)).to be_nil
      end
    end

    describe "#hide_const" do
      context 'for a loaded nested constant' do
        it_behaves_like "loaded constant hiding", "TestClass::Nested"
      end

      context 'for a loaded constant prefixed with ::' do
        it_behaves_like 'loaded constant hiding', "::TestClass"
      end

      context 'for an unloaded constant with nested name that matches a top-level constant' do
        it_behaves_like "unloaded constant hiding", "TestClass::Hash"

        it 'does not hide the top-level constant' do
          expect {
            hide_const("TestClass::Hash")
          }.to raise_error
        end
      end

      context 'for a loaded deeply nested constant' do
        it_behaves_like "loaded constant hiding", "TestClass::Nested::NestedEvenMore"
      end

      context 'for an unloaded unnested constant' do
        it_behaves_like "unloaded constant hiding", "X"
      end

      context 'for an unloaded nested constant' do
        it_behaves_like "unloaded constant hiding", "X::Y"
      end

      it 'can be hidden multiple times but still restores the original value properly' do
        orig_value = TestClass
        hide_const("TestClass")
        hide_const("TestClass")

        reset_rspec_mocks
        expect(TestClass).to be(orig_value)
      end

      it 'allows a constant to be hidden, then stubbed, restoring it to its original value properly' do
        orig_value = TOP_LEVEL_VALUE_CONST

        hide_const("TOP_LEVEL_VALUE_CONST")
        expect(recursive_const_defined?("TOP_LEVEL_VALUE_CONST")).to be_false

        stub_const("TOP_LEVEL_VALUE_CONST", 12345)
        expect(TOP_LEVEL_VALUE_CONST).to eq 12345

        reset_rspec_mocks
        expect(TOP_LEVEL_VALUE_CONST).to eq orig_value
      end
    end

    describe "#stub_const" do
      context 'for a loaded unnested constant' do
        it_behaves_like "loaded constant stubbing", "TestClass"

        it 'can be stubbed multiple times but still restores the original value properly' do
          orig_value = TestClass
          stub1, stub2 = Module.new, Module.new
          stub_const("TestClass", stub1)
          stub_const("TestClass", stub2)

          reset_rspec_mocks
          expect(TestClass).to be(orig_value)
        end

        it 'allows nested constants to be transferred to a stub module' do
          tc_nested = TestClass::Nested
          stub = Module.new
          stub_const("TestClass", stub, :transfer_nested_constants => true)
          expect(stub::M).to eq(:m)
          expect(stub::N).to eq(:n)
          expect(stub::Nested).to be(tc_nested)
        end

        it 'does not transfer nested constants that are inherited from a superclass' do
          stub = Module.new
          stub_const("TestSubClass", stub, :transfer_nested_constants => true)
          expect(stub::P).to eq(:p)
          expect(defined?(stub::M)).to be_false
          expect(defined?(stub::N)).to be_false
        end

        it 'raises an error when asked to transfer a nested inherited constant' do
          original_tsc = TestSubClass

          expect {
            stub_const("TestSubClass", Module.new, :transfer_nested_constants => [:M])
          }.to raise_error(ArgumentError)

          expect(TestSubClass).to be(original_tsc)
        end

        it 'allows nested constants to be selectively transferred to a stub module' do
          stub = Module.new
          stub_const("TestClass", stub, :transfer_nested_constants => [:M, :N])
          expect(stub::M).to eq(:m)
          expect(stub::N).to eq(:n)
          expect(defined?(stub::Nested)).to be_false
        end

        it 'raises an error if asked to transfer nested constants but given an object that does not support them' do
          original_tc = TestClass
          stub = Object.new
          expect {
            stub_const("TestClass", stub, :transfer_nested_constants => true)
          }.to raise_error(ArgumentError)

          expect(TestClass).to be(original_tc)

          expect {
            stub_const("TestClass", stub, :transfer_nested_constants => [:M])
          }.to raise_error(ArgumentError)

          expect(TestClass).to be(original_tc)
        end

        it 'raises an error if asked to transfer nested constants on a constant that does not support nested constants' do
          stub = Module.new
          expect {
            stub_const("TOP_LEVEL_VALUE_CONST", stub, :transfer_nested_constants => true)
          }.to raise_error(ArgumentError)

          expect(TOP_LEVEL_VALUE_CONST).to eq(7)

          expect {
            stub_const("TOP_LEVEL_VALUE_CONST", stub, :transfer_nested_constants => [:M])
          }.to raise_error(ArgumentError)

          expect(TOP_LEVEL_VALUE_CONST).to eq(7)
        end

        it 'raises an error if asked to transfer a nested constant that is not defined' do
          original_tc = TestClass
          expect(defined?(TestClass::V)).to be_false
          stub = Module.new

          expect {
            stub_const("TestClass", stub, :transfer_nested_constants => [:V])
          }.to raise_error(/cannot transfer nested constant.*V/i)

          expect(TestClass).to be(original_tc)
        end
      end

      context 'for a loaded nested constant' do
        it_behaves_like "loaded constant stubbing", "TestClass::Nested"
      end

      context 'for an unloaded constant with nested name that matches a top-level constant' do
        it_behaves_like "unloaded constant stubbing", "TestClass::Hash"
      end

      context 'for a loaded deeply nested constant' do
        it_behaves_like "loaded constant stubbing", "TestClass::Nested::NestedEvenMore"
      end

      context 'for an unloaded unnested constant' do
        it_behaves_like "unloaded constant stubbing", "X"
      end

      context 'for an unloaded constant nested within a loaded constant' do
        it_behaves_like "unloaded constant stubbing", "TestClass::X"

        it 'removes the unloaded constant but leaves the loaded constant when rspec resets its mocks' do
          expect(defined?(TestClass)).to be_true
          expect(defined?(TestClass::X)).to be_false
          stub_const("TestClass::X", 7)
          reset_rspec_mocks
          expect(defined?(TestClass)).to be_true
          expect(defined?(TestClass::X)).to be_false
        end

        it 'raises a helpful error if it cannot be stubbed due to an intermediary constant that is not a module' do
          expect(TestClass::M).to be_a(Symbol)
          expect { stub_const("TestClass::M::X", 5) }.to raise_error(/cannot stub/i)
        end
      end

      context 'for an unloaded constant nested deeply within a deeply nested loaded constant' do
        it_behaves_like "unloaded constant stubbing", "TestClass::Nested::NestedEvenMore::X::Y::Z"

        it 'removes the first unloaded constant but leaves the loaded nested constant when rspec resets its mocks' do
          expect(defined?(TestClass::Nested::NestedEvenMore)).to be_true
          expect(defined?(TestClass::Nested::NestedEvenMore::X)).to be_false
          stub_const("TestClass::Nested::NestedEvenMore::X::Y::Z", 7)
          reset_rspec_mocks
          expect(defined?(TestClass::Nested::NestedEvenMore)).to be_true
          expect(defined?(TestClass::Nested::NestedEvenMore::X)).to be_false
        end
      end
    end
  end

  describe Constant do
    def reset_rspec_mocks
      Spy.teardown
    end

    def on_const(const_name)
      if const_name.include? "::"
      args = [recursive_const_get("Object::" + const_name.sub(/(::)?[^:]+\z/, '')), const_name.split('::').last.to_sym]
      else
        args = [const_name.to_sym]
      end
      Spy.get_const(*args) || Spy.on_const(*args)
    end

    def stub_const(const_name, value)
      on_const(const_name).and_return(value)
    end

    def hide_const(const_name)
      on_const(const_name).and_hide
    end

    def original(const_name)
      if const_name.include? "::"
      args = [recursive_const_get("Object::" + const_name.sub(/(::)?[^:]+\z/, '')), const_name.split('::').last.to_sym]
      else
        args = [const_name.to_sym]
      end
      Spy.get_const(*args)
    end

    describe ".original" do
      context 'for a previously defined unstubbed constant' do
        let(:const) { original("TestClass::M") }

        it("exposes its name")                    { expect(const.name).to eq("TestClass::M") }
        it("indicates it was previously defined") { expect(const).to be_previously_defined }
        it("indicates it has not been hidden")    { expect(const).not_to be_hidden }
        it("exposes its original value")          { expect(const.original_value).to eq(:m) }
      end

      context 'for a previously defined stubbed constant' do
        before { stub_const("TestClass::M", :other) }
        let(:const) { original("TestClass::M") }

        it("exposes its name")                    { expect(const.name).to eq("TestClass::M") }
        it("indicates it was previously defined") { expect(const).to be_previously_defined }
        it("indicates it has not been hidden")    { expect(const).not_to be_hidden }
        it("exposes its original value")          { expect(const.original_value).to eq(:m) }
      end

      context 'for a previously undefined stubbed constant' do
        before { stub_const("TestClass::Undefined", :other) }
        let(:const) { original("TestClass::Undefined") }

        it("exposes its name")                        { expect(const.name).to eq("TestClass::Undefined") }
        it("indicates it was not previously defined") { expect(const).not_to be_previously_defined }
        it("indicates it has not been hidden")        { expect(const).not_to be_hidden }
        it("returns nil for the original value")      { expect(const.original_value).to be_nil }
      end

      context 'for a previously undefined unstubbed constant' do
        let(:const) { original("TestClass::Undefined") }

        it("exposes its name")                        { expect(const.name).to eq("TestClass::Undefined") }
        it("indicates it was not previously defined") { expect(const).not_to be_previously_defined }
        it("indicates it has not been hidden")        { expect(const).not_to be_hidden }
        it("returns nil for the original value")      { expect(const.original_value).to be_nil }
      end

      context 'for a previously defined constant that has been stubbed twice' do
        before { stub_const("TestClass::M", 1) }
        before { stub_const("TestClass::M", 2) }
        let(:const) { original("TestClass::M") }

        it("exposes its name")                    { expect(const.name).to eq("TestClass::M") }
        it("indicates it was previously defined") { expect(const).to be_previously_defined }
        it("indicates it has not been hidden")    { expect(const).not_to be_hidden }
        it("exposes its original value")          { expect(const.original_value).to eq(:m) }
      end

      context 'for a previously undefined constant that has been stubbed twice' do
        before { stub_const("TestClass::Undefined", 1) }
        before { stub_const("TestClass::Undefined", 2) }
        let(:const) { original("TestClass::Undefined") }

        it("exposes its name")                        { expect(const.name).to eq("TestClass::Undefined") }
        it("indicates it was not previously defined") { expect(const).not_to be_previously_defined }
        it("indicates it has not been hidden")        { expect(const).not_to be_hidden }
        it("returns nil for the original value")      { expect(const.original_value).to be_nil }
      end

      context 'for a previously defined hidden constant' do
        before { hide_const("TestClass::M") }
        let(:const) { original("TestClass::M") }

        it("exposes its name")                    { expect(const.name).to eq("TestClass::M") }
        it("indicates it was previously defined") { expect(const).to be_previously_defined }
        it("indicates it has been hidden")        { expect(const).to be_hidden }
        it("exposes its original value")          { expect(const.original_value).to eq(:m) }
      end

      context 'for a previously defined constant that has been hidden twice' do
        before { hide_const("TestClass::M") }
        before { hide_const("TestClass::M") }
        let(:const) { original("TestClass::M") }

        it("exposes its name")                    { expect(const.name).to eq("TestClass::M") }
        it("indicates it was previously defined") { expect(const).to be_previously_defined }
        it("indicates it has been hidden")        { expect(const).to be_hidden }
        it("exposes its original value")          { expect(const.original_value).to eq(:m) }
      end
    end
  end
end