File: testNesting.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 (71 lines) | stat: -rw-r--r-- 1,083 bytes parent folder | download | duplicates (5)
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
require 'test/minirunit'

class Object
  def add(name, c)
    class_eval("#{name} = c")
  end
end

module A1
  add("FOO", String)
  def do
    FOO.new("foo")
  end
end

class B1
  include A1
end

test_equal(["FOO"], A1.constants)
test_equal(["FOO"], B1.constants)
test_equal(B1.const_get("FOO"), String)
test_equal("foo", B1.new.do)

# tests nesting of classes inside modues
module A2
  class B2
    add("FOO", String)
    def do
      FOO.new("foo")
    end
  end
end

test_equal(["FOO"], A2::B2.constants)
test_equal(A2::B2.const_get("FOO"), String)
test_equal("foo", A2::B2.new.do)

# confirm class_eval is nested the same as the calling scope
class A3
  class B3
    test_equal([A3::B3, A3], Module.nesting)
  end
end

test_equal([A3::B3], A3::B3.class_eval("Module.nesting"))

class C3
  test_equal([A3::B3, C3], A3::B3.class_eval("Module.nesting"))
end

# tests lookup of :: scoped classes
class A4
  class B4
  end
end

class A4::B4
  class ::A4
    test_ok(self == A4)
    def foo
      "foo"
    end
  end
  
  class ::C4
  end
end

test_equal("foo", A4.new.foo)
test_ok(C4)