File: testConstant.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (282 lines) | stat: -rw-r--r-- 4,204 bytes parent folder | download | duplicates (4)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
require 'test/minirunit'
test_check "Test Constant Scoping:"

C1 = 1

module A
  C2 = 2

  module B
    class C
      C3 = 3

      def foo
        "ABC"
      end

      i1 = ""
      
      class << i1
        test_equal(3, C3)
      end
      
      i2 = Class.new
      
      class << i2
        test_equal(3, C3)
      end
    end
  end

  class << self
    test_equal(1, C1)
    test_equal(2, C2)
  end
end

class D < A::B::C
  test_equal(3, C3)
end

module A
  module B
    test_equal(1, C1)
    test_equal(2, C2)
  end
end

module B
  class C
    def foo
      "BC"
    end
  end
end

test_equal("ABC", A::B::C.new.foo)

module Foo
  class ObjectSpace
  end

  test_equal('ObjectSpace', ::ObjectSpace.name)
end

class Gamma
end

module Bar
        class Gamma
        end

	test_equal('Gamma', ::Gamma.name)
end

FOO = "foo"

test_equal(::FOO, "foo")

module X
   def X.const_missing(name)
     "missing"
   end
 end

test_equal(X::String, "missing")

module Y
  String = 1
end

test_equal(Y::String, 1)

module Z1
  ZOOM = "zoom"
  module Z2
    module Z3
      test_equal(ZOOM, "zoom")
      test_equal(Z1::ZOOM, "zoom")
    end
  end
end

# Should not cause collision
module Out
  Argument = "foo"
end

class Switch
  include Out
  class Argument < self
  end
end

# Should cause TypeError
# TODO: Figure out why test_exception(TypeError) {} is not working for this...
hack_pass = 0
begin
  class AAAA
    FOO = "foo"
    class FOO < self
    end
  end
rescue TypeError
  hack_pass = 1
end

test_ok(1, hack_pass)

# Should not cause collision
class Out2
  NoArgument = "foo"

  class Switch
    class NoArgument < self
    end
  end
end

module OutA
end

class OutA::InA
  def ok; "ok"; end
end

module OutA::InB
  OK = "ok"
end

test_ok("ok", OutA::InA.new.ok)
test_ok("ok", OutA::InB::OK)

test_ok("constant", defined? OutA)
test_equal(nil, defined? OutNonsense)

class Empty
end

# Declare constant outside of class/module
test_equal(1, Empty::FOOT = 1)

# Declare constant outside of class/module in multi assign
b, a = 1, 1
Empty::BART, a = 1, 1
test_equal(1, Empty::BART)
# Declare a constant whose value changes scope
CONST_SCOPE_CHANGE = begin
     require 'this_will_never_load'
     true
   rescue LoadError
     false
   end
test_equal(false, CONST_SCOPE_CHANGE)
Empty::CONST_FOO = begin
     require 'this_will_never_load'
     true
   rescue LoadError
     false
   end
test_equal(false, Empty::CONST_FOO)

$! = nil
defined? NoSuchThing::ToTestSideEffect
test_equal(nil, $!)

# Constants in toplevel should be searched last
Gobble = "hello"
module Foo2
  Gobble = "goodbye"
end
class Bar2
  include Foo2
  def gobble
    Gobble
  end
end
test_equal("goodbye", Bar2.new.gobble)

# Test fix for JRUBY-1339 ("Constants nested in a Module are not included")

module Outer
  class Inner
  end
end

def const_from_name(name)
  const = ::Object
  name.sub(/\A::/, '').split('::').each do |const_str|
    if const.const_defined?(const_str)
      const = const.const_get(const_str)
      next
    end
    return nil
  end
  const
end

include Outer

test_equal(Outer::Inner, const_from_name("Inner"))
test_equal("constant", defined?Inner)

# End test fix for JRUBY-1339

# JRUBY-2004
test_exception(TypeError) {
  JRuby2004 = 5
  JRuby2004::X = 5
}

# JRUBY-3091
JRuby3091CONST1 = 1
class JRuby3091A1; end
class JRuby3091B1 < JRuby3091A1
  def const; JRuby3091CONST1; end
end

test_equal(1, JRuby3091B1.new.const)
JRuby3091A1.const_set(:JRuby3091CONST1, 2)
test_equal(2, JRuby3091B1.new.const)

JRuby3091CONST2 = 1
class JRuby3091A2
  class JRuby3091B2
    def const; JRuby3091CONST2; end
  end
end

test_equal(1, JRuby3091A2::JRuby3091B2.new.const)
JRuby3091A2.const_set(:JRuby3091CONST2, 2)
test_equal(2, JRuby3091A2::JRuby3091B2.new.const)

# More JRUBY-3091
# 7867 broke const lookup by not eventually searching Object *and* its supers
module JRuby3091A3
  JRuby3091CONST3 = 1
end

class Object
  include JRuby3091A3
end

module JRuby3091B3
  def self.const
    JRuby3091CONST3
  end
end

test_equal(1, JRuby3091B3.const)

module JRuby3117
  class ::Object
    def jruby3117
      JRuby3117Const
    end
  end
  JRuby3117Const = 1
end

test_no_exception {
  test_equal(1, jruby3117)
}