Skip to content

Commit 9e68657

Browse files
committed
GenericTypeRegistrySpec
1 parent 0c632f4 commit 9e68657

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
require "spec_helper"
5+
6+
module Tapioca
7+
module Runtime
8+
class GenericTypeRegistrySpec < Minitest::Spec
9+
describe Tapioca::Runtime::GenericTypeRegistry do
10+
describe ".generic_type_instance?" do
11+
it "returns false for instances of non-generic classes" do
12+
refute(GenericTypeRegistry.generic_type_instance?(Object.new))
13+
end
14+
15+
it "returns true for instances of generic classes" do
16+
assert(GenericTypeRegistry.generic_type_instance?(SampleGenericClass[Object].new))
17+
end
18+
end
19+
20+
describe ".lookup_type_variables" do
21+
it "returns nil for non-generic types" do
22+
assert_nil(GenericTypeRegistry.lookup_type_variables(Object))
23+
end
24+
25+
it "returns the type variables for generic types" do
26+
assert_equal([SampleGenericClass::Element], GenericTypeRegistry.lookup_type_variables(SampleGenericClass))
27+
end
28+
end
29+
30+
describe ".register_type_variable" do
31+
it " registers a type variable that can be looked up later" do
32+
not_actually_generic = Class.new
33+
34+
fake_type_member1 = Tapioca::TypeVariableModule.new(
35+
not_actually_generic,
36+
Tapioca::TypeVariableModule::Type::Member,
37+
:invariant,
38+
nil,
39+
nil,
40+
nil,
41+
nil,
42+
)
43+
44+
fake_type_member2 = Tapioca::TypeVariableModule.new(
45+
not_actually_generic,
46+
Tapioca::TypeVariableModule::Type::Member,
47+
:invariant,
48+
nil,
49+
nil,
50+
nil,
51+
nil,
52+
)
53+
54+
GenericTypeRegistry.register_type_variable(not_actually_generic, fake_type_member1)
55+
GenericTypeRegistry.register_type_variable(not_actually_generic, fake_type_member2)
56+
57+
type_variables = T.must(GenericTypeRegistry.lookup_type_variables(not_actually_generic))
58+
59+
assert_equal([fake_type_member1, fake_type_member2], type_variables)
60+
# Let's double-check that they're not just equal, but identical:
61+
assert_same(fake_type_member1, type_variables[0])
62+
assert_same(fake_type_member2, type_variables[1])
63+
end
64+
end
65+
66+
describe "the patch for .inherited on generic classes" do
67+
# This is more of an internal detail and cross-cutting concern of all the public APIs,
68+
# but it's easier to test here on its own.
69+
70+
it "rescues exceptions that would prevent subclassing" do
71+
assert_raises(RuntimeError) do # Precondition
72+
# If not caught by the patch, the error raised from `.inherited` would have blocked our subclassing.
73+
Class.new(RaisesInInheritedCallback)
74+
end
75+
76+
result = RaisesInInheritedCallback[Object] # Should be a distinct subclass
77+
refute_same(result, RaisesInInheritedCallback)
78+
assert_operator(result, :<, RaisesInInheritedCallback)
79+
end
80+
end
81+
end
82+
83+
class SampleGenericClass
84+
extend T::Generic
85+
86+
Element = type_member
87+
end
88+
89+
class RaisesInInheritedCallback
90+
extend T::Generic
91+
92+
Element = type_member
93+
94+
class << self
95+
def inherited(subclass)
96+
super
97+
raise "Boom"
98+
end
99+
end
100+
end
101+
end
102+
end
103+
end

0 commit comments

Comments
 (0)