File: test_defined.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 (307 lines) | stat: -rw-r--r-- 7,551 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
require 'test/unit'

TEST_TOPLEVEL_CONST = true

class TestDefined < Test::Unit::TestCase
  class Sample
    class << self
      def set_the_var(value)
        @@some_var = value
      end
      def the_var_inited?
        defined? @@some_var
      end
    end
  end
  
  def test_defined_class_var
    assert !Sample.the_var_inited?
    Sample.set_the_var('something')
    assert Sample.the_var_inited?
  end
  
  def test_toplevel_const_defined
    assert_equal "constant", defined?(::TEST_TOPLEVEL_CONST)
  end
  
  def test_inner_const_defined
    assert_equal "constant", defined?(TestDefined::Sample)
  end

  def test_number
    assert_equal "expression", DefinedMethods::number
  end
  def test_plus
    assert_equal "method", DefinedMethods::plus
  end
  def test_unexisting_call
    assert_nil DefinedMethods::unexisting_call
  end
  def test_existing_call
    assert_equal "method", DefinedMethods::existing_call
  end
  def test_existing_call2
    assert_equal "method", DefinedMethods::existing_call2
  end
  def test_existing_call3
    assert_equal "method", DefinedMethods::existing_call3
  end
  def test_existing_call4
    assert_equal "method", DefinedMethods::existing_call4
  end
  def test_unexisting_var
    assert_nil DefinedMethods::unexisting_var
  end
  def test_attr_assignment
    assert_equal "assignment", DefinedMethods::attr_assignment
  end
  def test_assignment
    assert_equal "assignment", DefinedMethods::assignment
  end
  def test_unexisting_const_method
    assert_nil DefinedMethods::unexisting_const_method
  end
  def test_unexisting_const
    assert_nil DefinedMethods::unexisting_const
  end
  def test_existing_const
    assert_equal "constant", DefinedMethods::existing_const
  end
  def test_existing_var
    assert_equal "local-variable", DefinedMethods::existing_var
  end
  def test_unexisting_global
    assert_nil DefinedMethods::unexisting_global
  end
  def test_existing_global
    assert_equal "global-variable", DefinedMethods::existing_global
  end
  def test_unexisting_member
    assert_nil DefinedMethods::unexisting_member
  end
  def test_existing_member
    assert_equal "instance-variable", DefinedMethods::existing_member
  end
  def test_unexisting_class_var
    assert_nil DefinedMethods::unexisting_class_var
  end
  def test_existing_class_var
    assert_equal "class variable", DefinedMethods::existing_class_var
  end
  def test_unexisting_zsuper
    assert_nil DefinedMethods::unexisting_zsuper
  end
  def test_unexisting_super
    assert_nil DefinedMethods::unexisting_super
  end
  def test_unexisting_super_with_args
    assert_nil DefinedMethods::unexisting_super_with_args
  end
  def test_existing_super
    assert_equal %w(super)*3, DefinedMethods::test_existing_super
  end
  def test_existing_colon3
    assert_equal "constant", DefinedMethods::existing_colon3
  end
  def test_unexisting_colon3
    assert_nil DefinedMethods::unexisting_colon3
  end
  def test_class_var_assign
    assert_equal "assignment", DefinedMethods::class_var_assign
  end
  def test_unexisting_block_local_variable
    assert_nil DefinedMethods::unexisting_block_local_variable
  end
  def test_existing_block_local_variable
    assert_equal "local-variable(in-block)", DefinedMethods::existing_block_local_variable
  end
  def test_global_assign
    assert_equal "assignment", DefinedMethods::global_assign
  end
  def test_unexisting_dollar_special
    assert_nil DefinedMethods::unexisting_dollar_special
  end
  def test_unexisting_dollar_number
    assert_nil DefinedMethods::unexisting_dollar_number
  end
  def test_unexisting_dollar_number2
    assert_nil DefinedMethods::unexisting_dollar_number2
  end
  def test_existing_dollar_special
    assert_equal "$`",DefinedMethods::existing_dollar_special
  end
  def test_existing_dollar_number
    assert_equal "$1",DefinedMethods::existing_dollar_number
  end
  def test_true
    assert_equal "true", DefinedMethods::test_true
  end
  def test_false
    assert_equal "false", DefinedMethods::test_false
  end
  def test_self
    assert_equal "self", DefinedMethods::test_self
  end
  def test_nil
    assert_equal "nil", DefinedMethods::test_nil
  end
  def test_yield_without_block
    assert_nil DefinedMethods::test_yield
  end
  def test_yield_with_block
    assert_equal "yield", DefinedMethods::test_yield { }
  end
  def test_match
    assert_equal "method", DefinedMethods::test_match
  end
  
  
  # These are needed because JRuby still can't compile
  # asserts.
  module DefinedMethods
    class <<self
      def foo
        nil
      end
      def foo=(val)
        val
      end
      def number
        defined?(1)
      end
      def plus
        defined?(1+1)
      end
      def unexisting_call
        defined?(gegege())
      end
      def existing_call
        defined?(number)
      end
      def existing_call2
        defined?(number())
      end
      def existing_call3
        defined?(self.number())
      end
      def existing_call4
        defined?(self.number)
      end
      def unexisting_var
        defined?(gegege)
      end
      def attr_assignment
        defined?(foo = 1+1)
      end
      def assignment
        defined?(foo2 = 1+1)
      end
      def unexisting_const_method
        defined?(A.bar("haha"))
      end
      def unexisting_const
        defined?(A)
      end
      module B;end
      def existing_const
        defined?(B)
      end
      def existing_var
        v = 1
        defined?(v)
      end
      def unexisting_global
        defined?($UNEXISTING_GLOBAL)
      end
      def existing_global
        defined?($stderr)
      end
      def unexisting_member
        defined?(@unexisting)
      end
      def existing_member
        @foo1 = 1
        defined?(@foo1)
      end
      def unexisting_class_var
        defined?(@@unexisting)
      end
      def existing_class_var
        @@foo1 = 1
        defined?(@@foo1)
      end
      def unexisting_zsuper
        defined?(super)
      end
      def unexisting_super
        defined?(super())
      end
      def unexisting_super_with_args
        defined?(super(1,2,3))
      end
      class TestExistingSuper
        def initialize(*args)
          @val = [defined?(super), defined?(super()), defined?(super(1,2,3))]
        end
        def val; @val; end
      end
      def test_existing_super
        TestExistingSuper.new.val
      end
      def existing_colon3
        defined?(File::Stat)
      end
      def unexisting_colon3
        defined?(File::State)
      end
      def class_var_assign
        defined?(@@a = 123)
      end
      def unexisting_block_local_variable
        proc{ defined?(a) }.call
      end
      def existing_block_local_variable
        proc{ aaa=1; defined?(aaa) }.call
      end
      def global_assign
        defined?($global_assign = 1)
      end
      def unexisting_dollar_special
        defined?($`)
      end
      def unexisting_dollar_number
        defined?($1)
      end
      def unexisting_dollar_number2
        /a/=~"a"
        defined?($1)
      end
      def existing_dollar_special
        /a/=~"a"
        defined?($`)
      end
      def existing_dollar_number
        /(a)/=~"a"
        defined?($1)
      end
      def test_true
        defined?(true)
      end
      def test_false
        defined?(false)
      end
      def test_self
        defined?(self)
      end
      def test_nil
        defined?(nil)
      end
      def test_yield
        defined?(yield)
      end
      def test_match
        defined?(/a/=~"a")
      end
    end
  end
end