/
test_explicit_namespace.rb
223 lines (201 loc) · 5.82 KB
/
test_explicit_namespace.rb
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
# frozen_string_literal: true
require "test_helper"
class TestExplicitNamespace < LoaderTest
module Namespace; end
test "explicit namespaces are loaded correctly (Object)" do
files = [
["app/models/hotel.rb", "class Hotel; X = 1; end"],
["app/models/hotel/pricing.rb", "class Hotel::Pricing; end"]
]
with_setup(files, dirs: "app/models") do
assert_kind_of Class, Hotel
assert Hotel::X
assert Hotel::Pricing
end
end
test "explicit namespaces are loaded correctly (Namespace)" do
files = [
["app/models/hotel.rb", "class #{Namespace}::Hotel; X = 1; end"],
["app/models/hotel/pricing.rb", "class #{Namespace}::Hotel::Pricing; end"]
]
with_setup(files, namespace: Namespace, dirs: "app/models") do
assert_kind_of Class, Namespace::Hotel
assert Namespace::Hotel::X
assert Namespace::Hotel::Pricing
end
end
test "explicit namespaces are loaded correctly even if #name is overridden" do
files = [
["app/models/hotel.rb", <<~RUBY],
class Hotel
def self.name
"X"
end
end
RUBY
["app/models/hotel/pricing.rb", "class Hotel::Pricing; end"]
]
with_setup(files, dirs: "app/models") do
assert Hotel::Pricing
end
end
test "explicit namespaces managed by different instances" do
files = [
["a/m.rb", "module M; end"], ["a/m/n.rb", "M::N = true"],
["b/x.rb", "module X; end"], ["b/x/y.rb", "X::Y = true"],
]
with_files(files) do
new_loader(dirs: "a")
new_loader(dirs: "b")
assert M::N
assert X::Y
end
end
test "autoloads are set correctly, even if there are autoloads for the same cname in the superclass" do
files = [
["a.rb", "class A; end"],
["a/x.rb", "A::X = :A"],
["b.rb", "class B < A; end"],
["b/x.rb", "B::X = :B"]
]
with_setup(files) do
assert_kind_of Class, A
assert_kind_of Class, B
assert_equal :B, B::X
end
end
test "autoloads are set correctly, even if there are autoloads for the same cname in a module prepended to the superclass" do
files = [
["m/x.rb", "M::X = :M"],
["a.rb", "class A; prepend M; end"],
["b.rb", "class B < A; end"],
["b/x.rb", "B::X = :B"]
]
with_setup(files) do
assert_kind_of Class, A
assert_kind_of Class, B
assert_equal :B, B::X
end
end
test "autoloads are set correctly, even if there are autoloads for the same cname in other ancestors" do
files = [
["m/x.rb", "M::X = :M"],
["a.rb", "class A; include M; end"],
["b.rb", "class B < A; end"],
["b/x.rb", "B::X = :B"]
]
with_setup(files) do
assert_kind_of Class, A
assert_kind_of Class, B
assert_equal :B, B::X
end
end
test "namespace promotion updates the registry" do
# We use two root directories to make sure the loader visits the implicit
# a/m first, and the explicit b/m.rb after it.
files = [
["a/m/x.rb", "M::X = true"],
["b/m.rb", "module M; end"]
]
with_setup(files, dirs: ["a", "b"]) do
assert_nil Zeitwerk::Registry.loader_for(File.expand_path("a/m"))
assert_same loader, Zeitwerk::Registry.loader_for(File.expand_path("b/m.rb"))
end
end
# As of this writing, a tracer on the :class event does not seem to have any
# performance penalty in an ordinary code base. But I prefer to precisely
# control that we use a tracer only if needed in case this issue
#
# https://bugs.ruby-lang.org/issues/14104
#
# goes forward.
def tracer
Zeitwerk::ExplicitNamespace.tracer
end
test "the tracer starts disabled" do
assert !tracer.enabled?
end
test "simple autoloading does not enable the tracer" do
files = [["x.rb", "X = true"]]
with_setup(files) do
assert !tracer.enabled?
assert X
assert !tracer.enabled?
end
end
test "autovivification does not enable the tracer" do
files = [["foo/bar.rb", "module Foo::Bar; end"]]
with_setup(files) do
assert !tracer.enabled?
assert Foo::Bar
assert !tracer.enabled?
end
end
test "explicit namespaces enable the tracer until loaded" do
files = [
["hotel.rb", "class Hotel; end"],
["hotel/pricing.rb", "class Hotel::Pricing; end"]
]
with_setup(files) do
assert tracer.enabled?
assert Hotel
assert !tracer.enabled?
assert Hotel::Pricing
assert !tracer.enabled?
end
end
test "the tracer is enabled until everything is loaded" do
files = [
["a/m.rb", "module M; end"], ["a/m/n.rb", "M::N = true"],
["b/x.rb", "module X; end"], ["b/x/y.rb", "X::Y = true"],
]
with_files(files) do
new_loader(dirs: "a")
assert tracer.enabled?
new_loader(dirs: "b")
assert tracer.enabled?
assert M
assert tracer.enabled?
assert X
assert !tracer.enabled?
end
end
# This is a regression test.
test "the tracer handles singleton classes" do
files = [
["hotel.rb", <<-EOS],
class Hotel
class << self
def x
1
end
end
end
EOS
["hotel/pricing.rb", "class Hotel::Pricing; end"],
["car.rb", "class Car; end"],
["car/pricing.rb", "class Car::Pricing; end"],
]
with_setup(files) do
assert tracer.enabled?
assert_equal 1, Hotel.x
assert tracer.enabled?
end
end
test "non-hashable explicit namespaces are supported" do
files = [
["m.rb", <<~EOS],
module M
# This method is overridden with a different arity. Therefore, M is
# not hashable. See https://github.com/fxn/zeitwerk/issues/188.
def self.hash(_)
end
end
EOS
["m/x.rb", "M::X = true"]
]
with_setup(files) do
assert M::X
end
end
end