File: instance_cache_spec.rb

package info (click to toggle)
r10k 5.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,228 kB
  • sloc: ruby: 18,180; makefile: 10; sh: 1
file content (78 lines) | stat: -rw-r--r-- 2,165 bytes parent folder | download | duplicates (5)
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
require 'spec_helper'

require 'r10k/instance_cache'

describe R10K::InstanceCache do

  describe "setting up a new instance cache" do
    let(:klass) do
      dubs = double('test class')
      allow(dubs).to receive(:new) { |*args| args }
      dubs
    end

    it "can create new objects" do
      registry = described_class.new(klass)
      expect(registry.generate).to eq []
    end

    describe "defining object arity" do

      it "handles unary objects" do
        expect(klass).to receive(:new).with(:foo)

        registry = described_class.new(klass)
        expect(registry.generate(:foo)).to eq [:foo]
      end

      it "handles ternary objects" do
        expect(klass).to receive(:new).with(:foo, :bar, :baz)

        registry = described_class.new(klass)
        expect(registry.generate(:foo, :bar, :baz)).to eq [:foo, :bar, :baz]
      end

      it 'handles n-ary objects' do
        args = %w[a bunch of arbitrary objects]
        expect(klass).to receive(:new).with(*args)

        registry = described_class.new(klass)
        expect(registry.generate(*args)).to eq args
      end

      it 'fails when the required arguments are not matched' do
        expect(klass).to receive(:new).and_raise ArgumentError, "not enough args"

        registry = described_class.new(klass)
        expect { registry.generate('arity is hard') }.to raise_error ArgumentError, "not enough args"
      end
    end

    it "can specify the constructor method" do
      expect(klass).to receive(:from_json).and_return "this is json, right?"

      registry = described_class.new(klass, :from_json)
      expect(registry.generate).to eq "this is json, right?"
    end
  end


  it "returns a memoized object if it's been created before" do
    registry = described_class.new(String)
    first = registry.generate "bam!"
    second = registry.generate "bam!"

    expect(first.object_id).to eq second.object_id
  end

  it 'can clear registered objects' do
    registry = described_class.new(String)

    first = registry.generate "bam!"
    registry.clear!
    second = registry.generate "bam!"

    expect(first.object_id).to_not eq second.object_id

  end
end