summaryrefslogtreecommitdiff
path: root/bootstraptest/test_constant_cache.rb
blob: 1fa83256ed5408f77fdbea501cc51534701f514a (plain)
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
# Constant lookup is cached.
assert_equal '1', %q{
  CONST = 1

  def const
    CONST
  end

  const
  const
}

# Invalidate when a constant is set.
assert_equal '2', %q{
  CONST = 1

  def const
    CONST
  end

  const

  CONST = 2

  const
}

# Invalidate when a constant of the same name is set.
assert_equal '1', %q{
  CONST = 1

  def const
    CONST
  end

  const

  class Container
    CONST = 2
  end

  const
}

# Invalidate when a constant is removed.
assert_equal 'missing', %q{
  class Container
    CONST = 1

    def const
      CONST
    end

    def self.const_missing(name)
      'missing'
    end

    new.const
    remove_const :CONST
  end

  Container.new.const
}

# Invalidate when a constant's visibility changes.
assert_equal 'missing', %q{
  class Container
    CONST = 1

    def self.const_missing(name)
      'missing'
    end
  end

  def const
    Container::CONST
  end

  const

  Container.private_constant :CONST

  const
}

# Invalidate when a constant's visibility changes even if the call to the
# visibility change method fails.
assert_equal 'missing', %q{
  class Container
    CONST1 = 1

    def self.const_missing(name)
      'missing'
    end
  end

  def const1
    Container::CONST1
  end

  const1

  begin
    Container.private_constant :CONST1, :CONST2
  rescue NameError
  end

  const1
}

# Invalidate when a module is included.
assert_equal 'INCLUDE', %q{
  module Include
    CONST = :INCLUDE
  end

  class Parent
    CONST = :PARENT
  end

  class Child < Parent
    def const
      CONST
    end

    new.const

    include Include
  end

  Child.new.const
}

# Invalidate when const_missing is hit.
assert_equal '2', %q{
  module Container
    Foo = 1
    Bar = 2

    class << self
      attr_accessor :count

      def const_missing(name)
        @count += 1
        @count == 1 ? Foo : Bar
      end
    end

    @count = 0
  end

  def const
    Container::Baz
  end

  const
  const
}

# Invalidate when the iseq gets cleaned up.
assert_equal '2', %q{
  CONSTANT = 1

  iseq = RubyVM::InstructionSequence.compile(<<~RUBY)
    CONSTANT
  RUBY

  iseq.eval
  iseq = nil

  GC.start
  CONSTANT = 2
}

# Invalidate when the iseq gets cleaned up even if it was never in the cache.
assert_equal '2', %q{
  CONSTANT = 1

  iseq = RubyVM::InstructionSequence.compile(<<~RUBY)
    CONSTANT
  RUBY

  iseq = nil

  GC.start
  CONSTANT = 2
}