File: test_variables.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 (50 lines) | stat: -rw-r--r-- 2,230 bytes parent folder | download | duplicates (9)
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
require 'test/unit'

class TestVariables < Test::Unit::TestCase

  #
  # TODO: test for new variables code will go here as well.
  # 

  # test fix for JRUBY-1295 (invalid var names permitted in set/get/remove)
  def test_instance_variable_validation
    assert_raises(NameError) { Object.new.instance_variable_set '@123', 1 }
    assert_raises(NameError) { Object.new.instance_variable_set '@$', 1 }
    assert_raises(NameError) { Object.new.instance_variable_set '@ a', 1 }
    assert_raises(NameError) { Object.new.instance_variable_get '@123' }
    assert_raises(NameError) { Object.new.instance_variable_get '@$' }
    assert_raises(NameError) { Object.new.instance_variable_get '@ a' }

    # we'll support this MRI behavior, though it seems like a bug (Pickaxe
    # says it's illegal):
    obj = Object.new
    assert_nothing_raised { obj.instance_variable_set('@', 1) }
    assert_equal('@', obj.instance_variables[0])
    assert_equal(1, obj.instance_variable_get('@'))
  end

  # test fix for JRUBY-1295 (invalid var names permitted in set/get/remove)
  def test_class_variable_validation
    assert_raises(NameError) { Class.new { class_variable_set '@@123', 1 } }
    assert_raises(NameError) { Class.new { class_variable_set '@@$', 1 } }
    assert_raises(NameError) { Class.new { class_variable_set '@@ ', 1 } }
    assert_raises(NameError) { Class.new { class_variable_get '@@123' } }
    assert_raises(NameError) { Class.new { class_variable_get '@@$' } }
    assert_raises(NameError) { Class.new { class_variable_get '@@ ' } }

    # we'll support this MRI behavior, though it seems like a bug (Pickaxe
    # says it's illegal):
    cls = Class.new
    assert_nothing_raised { cls.send :class_variable_set, '@@', 1 }
    assert_equal('@@', cls.class_variables[0] )
    assert_equal(1, cls.send(:class_variable_get, '@@'))
  end

  # test fix for JRUBY-1295 (invalid var names permitted in set/get/remove)
  def test_constant_validation
    assert_raises(NameError) { Class.new.const_set 'C no evil', 1 }
    assert_raises(NameError) { Class.new.const_get "C'est la vie!" }
    assert_raises(NameError) { Class.new { remove_const "Ciao, Marcello!" } }
  end

end