File: container.rb

package info (click to toggle)
ruby-dry-container 0.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 236 kB
  • sloc: ruby: 976; makefile: 4
file content (664 lines) | stat: -rw-r--r-- 18,971 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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
RSpec.shared_examples 'a container' do
  describe 'configuration' do
    describe 'registry' do
      describe 'default' do
        it { expect(klass.config.registry).to be_a(Dry::Container::Registry) }
      end

      describe 'custom' do
        let(:custom_registry) { double('Registry') }
        let(:key) { :key }
        let(:item) { :item }
        let(:options) { {} }

        before do
          klass.configure do |config|
            config.registry = custom_registry
          end

          allow(custom_registry).to receive(:call)
        end

        after do
          # HACK: Have to reset the configuration so that it doesn't
          # interfere with other specs
          klass.configure do |config|
            config.registry = Dry::Container::Registry.new
          end
        end

        subject! { container.register(key, item, options) }

        it do
          expect(custom_registry).to have_received(:call).with(
            container._container,
            key,
            item,
            options
          )
        end
      end
    end

    describe 'resolver' do
      describe 'default' do
        it { expect(klass.config.resolver).to be_a(Dry::Container::Resolver) }
      end

      describe 'custom' do
        let(:custom_resolver) { double('Resolver') }
        let(:item) { double('Item') }
        let(:key) { :key }

        before do
          klass.configure do |config|
            config.resolver = custom_resolver
          end

          allow(custom_resolver).to receive(:call).and_return(item)
        end

        after do
          # HACK: Have to reset the configuration so that it doesn't
          # interfere with other specs
          klass.configure do |config|
            config.resolver = Dry::Container::Resolver.new
          end
        end

        subject! { container.resolve(key) }

        it { expect(custom_resolver).to have_received(:call).with(container._container, key) }
        it { is_expected.to eq(item) }
      end
    end

    describe 'namespace_separator' do
      describe 'default' do
        it { expect(klass.config.namespace_separator).to eq('.') }
      end

      describe 'custom' do
        let(:custom_registry) { double('Registry') }
        let(:key) { 'key' }
        let(:namespace_separator) { '-' }
        let(:namespace) { 'one' }

        before do
          klass.configure do |config|
            config.namespace_separator = namespace_separator
          end

          container.namespace(namespace) do
            register('key', 'item')
          end
        end

        after do
          # HACK: Have to reset the configuration so that it doesn't
          # interfere with other specs
          klass.configure do |config|
            config.namespace_separator = '.'
          end
        end

        subject! { container.resolve([namespace, key].join(namespace_separator)) }

        it { is_expected.to eq('item') }
      end
    end
  end

  context 'with default configuration' do
    describe 'registering a block' do
      context 'without options' do
        context 'without arguments' do
          it 'registers and resolves an object' do
            container.register(:item) { 'item' }

            expect(container.keys).to eq(['item'])
            expect(container.key?(:item)).to be true
            expect(container.resolve(:item)).to eq('item')
          end
        end

        context 'with arguments' do
          it 'registers and resolves a proc' do
            container.register(:item) { |item| item }

            expect(container.resolve(:item).call('item')).to eq('item')
          end

          it 'does not call a proc on resolving if one accepts an arbitrary number of keyword arguments' do
            container.register(:item) { |*| 'item' }

            expect(container.resolve(:item)).to be_a_kind_of Proc
            expect(container.resolve(:item).call).to eq('item')
          end
        end
      end

      context 'with option call: false' do
        it 'registers and resolves a proc' do
          container.register(:item, call: false) { 'item' }

          expect(container.keys).to eq(['item'])
          expect(container.key?(:item)).to be true
          expect(container.resolve(:item).call).to eq('item')
          expect(container[:item].call).to eq('item')
        end
      end
    end

    describe 'registering a proc' do
      context 'without options' do
        context 'without arguments' do
          it 'registers and resolves an object' do
            container.register(:item, proc { 'item' })

            expect(container.keys).to eq(['item'])
            expect(container.key?(:item)).to be true
            expect(container.resolve(:item)).to eq('item')
            expect(container[:item]).to eq('item')
          end
        end

        context 'with arguments' do
          it 'registers and resolves a proc' do
            container.register(:item, proc { |item| item })

            expect(container.keys).to eq(['item'])
            expect(container.key?(:item)).to be true
            expect(container.resolve(:item).call('item')).to eq('item')
            expect(container[:item].call('item')).to eq('item')
          end
        end
      end

      context 'with option call: false' do
        it 'registers and resolves a proc' do
          container.register(:item, proc { 'item' }, call: false)

          expect(container.keys).to eq(['item'])
          expect(container.key?(:item)).to be true
          expect(container.resolve(:item).call).to eq('item')
          expect(container[:item].call).to eq('item')
        end
      end

      context 'with option memoize: true' do
        it 'registers and resolves a proc' do
          container.register(:item, proc { 'item' }, memoize: true)

          expect(container[:item]).to be container[:item]
          expect(container.keys).to eq(['item'])
          expect(container.key?(:item)).to be true
          expect(container.resolve(:item)).to eq('item')
          expect(container[:item]).to eq('item')
        end

        it 'only resolves the proc once' do
          resolved_times = 0

          container.register(:item, proc { resolved_times += 1 }, memoize: true)

          expect(container.resolve(:item)).to be 1
          expect(container.resolve(:item)).to be 1
        end

        context 'when receiving something other than a proc' do
          it do
            expect { container.register(:item, 'Hello!', memoize: true) }.to raise_error(Dry::Container::Error)
          end
        end
      end
    end

    describe 'registering an object' do
      context 'without options' do
        it 'registers and resolves the object' do
          item = 'item'
          container.register(:item, item)

          expect(container.keys).to eq(['item'])
          expect(container.key?(:item)).to be true
          expect(container.resolve(:item)).to be(item)
          expect(container[:item]).to be(item)
        end
      end

      context 'with option call: false' do
        it 'registers and resolves an object' do
          item = -> { 'test' }
          container.register(:item, item, call: false)

          expect(container.keys).to eq(['item'])
          expect(container.key?(:item)).to be true
          expect(container.resolve(:item)).to eq(item)
          expect(container[:item]).to eq(item)
        end
      end
    end

    describe 'registering with the same key multiple times' do
      it do
        container.register(:item, proc { 'item' })

        expect { container.register(:item, proc { 'item' }) }.to raise_error(Dry::Container::Error)
      end
    end

    describe 'resolving with a key that has not been registered' do
      it do
        expect(container.key?(:item)).to be false
        expect { container.resolve(:item) }.to raise_error(Dry::Container::Error)
      end
    end

    describe 'mixing Strings and Symbols' do
      it do
        container.register(:item, 'item')
        expect(container.resolve('item')).to eql('item')
      end
    end

    describe '#merge' do
      let(:key) { :key }
      let(:other) { Dry::Container.new }

      before do
        other.register(key) { :item }
      end

      context 'without namespace argument' do
        subject! { container.merge(other) }

        it { expect(container.resolve(key)).to be(:item) }
        it { expect(container[key]).to be(:item) }
      end

      context 'with namespace argument' do
        subject! { container.merge(other, namespace: namespace) }

        context 'when namespace is nil' do
          let(:namespace) { nil }

          it { expect(container.resolve(key)).to be(:item) }
          it { expect(container[key]).to be(:item) }
        end

        context 'when namespace is not nil' do
          let(:namespace) { 'namespace' }

          it { expect(container.resolve("#{namespace}.#{key}")).to be(:item) }
          it { expect(container["#{namespace}.#{key}"]).to be(:item) }
        end
      end
    end

    describe '#key?' do
      let(:key) { :key }

      before do
        container.register(key) { :item }
      end

      subject! { container.key?(resolve_key) }

      context 'when key exists in container' do
        let(:resolve_key) { key }

        it { is_expected.to be true }
      end

      context 'when key does not exist in container' do
        let(:resolve_key) { :random }

        it { is_expected.to be false }
      end
    end

    describe '#keys' do
      let(:keys) { [:key_1, :key_2] }
      let(:expected_keys) { ['key_1', 'key_2'] }

      before do
        keys.each do |key|
          container.register(key) { :item }
        end
      end

      subject! { container.keys }

      it 'returns stringified versions of all registered keys' do
        is_expected.to match_array(expected_keys)
      end
    end

    describe '#each_key' do
      let(:keys) { [:key_1, :key_2] }
      let(:expected_keys) { ['key_1', 'key_2'] }
      let!(:yielded_keys) { [] }

      before do
        keys.each do |key|
          container.register(key) { :item }
        end
      end

      subject! do
        container.each_key { |key| yielded_keys << key }
      end

      it 'yields stringified versions of all registered keys to the block' do
        expect(yielded_keys).to match_array(expected_keys)
      end

      it 'returns the container' do
        is_expected.to eq(container)
      end
    end

    describe '#each' do
      let(:keys) { [:key_1, :key_2] }
      let(:expected_key_value_pairs) { [['key_1', 'value_for_key_1'], ['key_2', 'value_for_key_2']] }
      let!(:yielded_key_value_pairs) { [] }

      before do
        keys.each do |key|
          container.register(key) { "value_for_#{key}" }
        end
      end

      subject! do
        container.each { |key, value| yielded_key_value_pairs << [key, value] }
      end

      it 'yields stringified versions of all registered keys to the block' do
        expect(yielded_key_value_pairs).to match_array(expected_key_value_pairs)
      end

      it 'returns the container' do
        is_expected.to eq(expected_key_value_pairs)
      end
    end

    describe '#decorate' do
      require 'delegate'

      let(:key) { :key }
      let(:decorated_class_spy) { spy(:decorated_class_spy) }
      let(:decorated_class) { Class.new }

      context 'for callable item' do
        before do
          allow(decorated_class_spy).to receive(:new) { decorated_class.new }
          container.register(key, memoize: memoize) { decorated_class_spy.new }
          container.decorate(key, with: SimpleDelegator)
        end

        context 'memoize false' do
          let(:memoize) { false }

          it 'does not call the block until the key is resolved' do
            expect(decorated_class_spy).not_to have_received(:new)
            container.resolve(key)
            expect(decorated_class_spy).to have_received(:new)
          end

          specify do
            expect(container[key]).to be_instance_of(SimpleDelegator)
            expect(container[key].__getobj__).to be_instance_of(decorated_class)
            expect(container[key]).not_to be(container[key])
            expect(container[key].__getobj__).not_to be(container[key].__getobj__)
          end
        end

        context 'memoize true' do
          let(:memoize) { true }

          specify do
            expect(container[key]).to be_instance_of(SimpleDelegator)
            expect(container[key].__getobj__).to be_instance_of(decorated_class)
            expect(container[key]).to be(container[key])
          end
        end
      end

      context 'for not callable item' do
        describe 'wrapping' do
          before do
            container.register(key, call: false) { "value" }
            container.decorate(key, with: SimpleDelegator)
          end

          it 'expected to be an instance of SimpleDelegator' do
            expect(container.resolve(key)).to be_instance_of(SimpleDelegator)
            expect(container.resolve(key).__getobj__.call).to eql("value")
          end
        end

        describe 'memoization' do
          before do
            @called = 0
            container.register(key, 'value')

            container.decorate(key) do |value|
              @called += 1
              "<#{value}>"
            end
          end

          it 'decorates static value only once' do
            expect(container.resolve(key)).to eql('<value>')
            expect(container.resolve(key)).to eql('<value>')
            expect(@called).to be(1)
          end
        end
      end

      context 'with an instance as a decorator' do
        let(:decorator) do
          double.tap do |decorator|
            allow(decorator).to receive(:call) { |input| "decorated #{input}" }
          end
        end

        before do
          container.register(key) { "value" }
          container.decorate(key, with: decorator)
        end

        it 'expected to pass original value to decorator#call method' do
          expect(container.resolve(key)).to eq("decorated value")
        end
      end
    end

    describe 'namespace' do
      context 'when block does not take arguments' do
        before do
          container.namespace('one') do
            register('two', 2)
          end
        end

        subject! { container.resolve('one.two') }

        it 'registers items under the given namespace' do
          is_expected.to eq(2)
        end
      end

      context 'when block takes arguments' do
        before do
          container.namespace('one') do |c|
            c.register('two', 2)
          end
        end

        subject! { container.resolve('one.two') }

        it 'registers items under the given namespace' do
          is_expected.to eq(2)
        end
      end

      context 'with nesting' do
        before do
          container.namespace('one') do
            namespace('two') do
              register('three', 3)
            end
          end
        end

        subject! { container.resolve('one.two.three') }

        it 'registers items under the given namespaces' do
          is_expected.to eq(3)
        end
      end

      context 'with nesting and when block takes arguments' do
        before do
          container.namespace('one') do |c|
            c.register('two', 2)
            c.register('three', c.resolve('two'))
          end
        end

        subject! { container.resolve('one.three') }

        it 'resolves items relative to the namespace' do
          is_expected.to eq(2)
        end
      end
    end

    describe 'import' do
      it 'allows importing of namespaces' do
        ns = Dry::Container::Namespace.new('one') do
          register('two', 2)
        end

        container.import(ns)

        expect(container.resolve('one.two')).to eq(2)
      end

      it 'allows importing of nested namespaces' do
        ns = Dry::Container::Namespace.new('two') do
          register('three', 3)
        end

        container.namespace('one') do
          import(ns)
        end

        expect(container.resolve('one.two.three')).to eq(3)
      end
    end
  end

  describe 'stubbing' do
    before do
      container.enable_stubs!

      container.register(:item, 'item')
      container.register(:foo, 'bar')
    end

    after do
      container.unstub
    end

    it 'keys can be stubbed' do
      container.stub(:item, 'stub')
      expect(container.resolve(:item)).to eql('stub')
      expect(container[:item]).to eql('stub')
    end

    it 'only other keys remain accesible' do
      container.stub(:item, 'stub')
      expect(container.resolve(:foo)).to eql('bar')
      expect(container[:foo]).to eql('bar')
    end

    it 'keys can be reverted back to their original value' do
      container.stub(:item, 'stub')
      container.unstub(:item)

      expect(container.resolve(:item)).to eql('item')
      expect(container[:item]).to eql('item')
    end

    describe 'with block argument' do
      it 'executes the block with the given stubs' do
        expect { |b| container.stub(:item, 'stub', &b) }.to yield_control
      end

      it 'keys are stubbed only while inside the block' do
        container.stub(:item, 'stub') do
          expect(container.resolve(:item)).to eql('stub')
        end

        expect(container.resolve(:item)).to eql('item')
      end
    end

    describe 'mixing Strings and Symbols' do
      it do
        container.stub(:item, 'stub')
        expect(container.resolve('item')).to eql('stub')
      end
    end

    it 'raises an error when key is missing' do
      expect { container.stub(:non_existing, 'something') }.
        to raise_error(ArgumentError, 'cannot stub "non_existing" - no such key in container')
    end
  end

  describe '.freeze' do
    before do
      container.register(:foo, 'bar')
    end

    it 'allows to freeze a container so that nothing can be registered later' do
      container.freeze
      error = RUBY_VERSION >= '2.5' ? FrozenError : RuntimeError
      expect { container.register(:baz, 'quux') }.to raise_error(error)
      expect(container).to be_frozen
    end

    it 'returns self back' do
      expect(container.freeze).to be(container)
    end
  end

  describe '.dup' do
    it "returns a copy that doesn't share registered keys with the parent" do
      container.dup.register(:foo, 'bar')
      expect(container.key?(:foo)).to be false
    end
  end

  describe '.clone' do
    it "returns a copy that doesn't share registered keys with the parent" do
      container.clone.register(:foo, 'bar')
      expect(container.key?(:foo)).to be false
    end

    it 're-uses frozen container' do
      expect(container.freeze.clone).to be_frozen
      expect(container.clone._container).to be(container._container)
    end
  end

  describe '.resolve' do
    it 'accepts a fallback block' do
      expect(container.resolve('missing') { :fallback }).to be(:fallback)
    end
  end
end