-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathloader_spec.rb
More file actions
127 lines (100 loc) · 3.11 KB
/
loader_spec.rb
File metadata and controls
127 lines (100 loc) · 3.11 KB
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
# frozen_string_literal: true
require "dry/system/loader"
require "dry/inflector"
require "dry/system/component"
require "dry/system/config/namespace"
require "dry/system/identifier"
require "singleton"
RSpec.describe Dry::System::Loader do
subject(:loader) { described_class }
describe "#require!" do
let(:component) {
Dry::System::Component.new(
Dry::System::Identifier.new("test.bar"),
namespace: Dry::System::Config::Namespace.default_root
)
}
before do
expect(loader).to receive(:require).with("test/bar").at_least(1)
end
it "requires the components's path" do
loader.require!(component)
end
it "returns self" do
expect(loader.require!(component)).to eql loader
end
end
describe "#call" do
shared_examples_for "object loader" do
let(:instance) { loader.call(component) }
context "not singleton" do
it "returns a new instance of the constant" do
expect(instance).to be_instance_of(constant)
expect(instance).not_to be(loader.call(component))
end
end
context "singleton" do
before { constant.send(:include, Singleton) }
it "returns singleton instance" do
expect(instance).to be(constant.instance)
end
end
end
context "with a singular name" do
let(:component) {
Dry::System::Component.new(
Dry::System::Identifier.new("test.bar"),
namespace: Dry::System::Config::Namespace.default_root
)
}
let(:constant) { Test::Bar }
before do
expect(loader).to receive(:require).with("test/bar").at_least(1)
module Test
Bar = Class.new
end
end
it_behaves_like "object loader"
it "isolates component by removing its constant" do
constant
instance = loader.call(component, isolate: true)
expect(Test.const_defined?("Bar")).to eq(false)
expect(instance).to be_a(constant)
end
end
context "with a constructor accepting args" do
let(:component) {
Dry::System::Component.new(
Dry::System::Identifier.new("test.bar"),
namespace: Dry::System::Config::Namespace.default_root
)
}
before do
expect(loader).to receive(:require).with("test/bar").at_least(1)
module Test
Bar = Struct.new(:one, :two)
end
end
it "passes args to the constructor" do
instance = loader.call(component, 1, 2)
expect(instance.one).to be(1)
expect(instance.two).to be(2)
end
end
context "with a custom inflector" do
let(:component) {
Dry::System::Component.new(
Dry::System::Identifier.new("test.api_bar"),
namespace: Dry::System::Config::Namespace.default_root,
inflector: Dry::Inflector.new { |i| i.acronym("API") }
)
}
let(:constant) { Test::APIBar }
before do
expect(loader).to receive(:require).with("test/api_bar").at_least(1)
Test::APIBar = Class.new
end
it_behaves_like "object loader"
end
end
end