File: testSingletonClass.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 (151 lines) | stat: -rw-r--r-- 2,648 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
require 'test/minirunit'
test_check "Test Source Positions:"

class Object
    def meta
        class<<self;self;end
    end
end

class Foo

    meta.instance_eval do
        def bar
        end
    end
    
    meta.class_eval do
        def bar2
        end
    end

end

test_no_exception{Foo.bar2}
test_no_exception{Foo.meta.bar}
test_no_exception{Foo.meta.meta.bar}

FooC = Foo.clone

test_no_exception{FooC.bar2}
test_exception(NoMethodError){FooC.meta.bar}
test_exception(NoMethodError){FooC.meta.meta.bar}

FooD = Foo.dup

test_no_exception{FooD.bar2}
test_exception(NoMethodError){FooD.meta.bar}
test_exception(NoMethodError){FooD.meta.meta.bar}

foo = Foo.new

foo.meta.instance_eval do
    def bar3
    end
end

foo.meta.class_eval do
    def bar4
    end
end

foo.meta.meta.class_eval do
    def bar5
    end
end

test_no_exception{foo.bar4}
test_no_exception{foo.meta.bar3}
test_no_exception{foo.meta.meta.bar3}

test_no_exception{foo.meta.bar5}
test_no_exception{foo.meta.meta.bar5}

def pm m
    p m
    p m.dup
    p m.clone
end

test_equal(Class.to_s,"Class")
test_equal(Module.to_s,"Module")
test_equal(Object.to_s,"Object")
test_equal(Kernel.to_s,"Kernel")

test_ok(Class.dup.to_s =~ /^#<Class/)
test_ok(Module.dup.to_s =~ /^#<Class/)
test_ok(Object.dup.to_s =~ /^#<Class/)
test_ok(Kernel.dup.to_s =~ /^#<Module/)

test_ok(Class.clone.to_s =~ /^#<Class/)
test_ok(Module.clone.to_s =~ /^#<Class/)
test_ok(Object.clone.to_s =~ /^#<Class/)
test_ok(Kernel.clone.to_s =~ /^#<Module/)

class Object
    def meta
        class<<self;self;end
    end
end

class Foo
end

foo = Foo.new

test_ok(foo.meta.meta.to_s =~ /^(#<Class:){2}#</)
test_ok(foo.meta.meta.meta.to_s =~ /^(#<Class:){3}#</)
test_ok(foo.meta.meta.meta.meta.to_s =~ /^(#<Class:){4}#</)

# test of JRUBY-583
require 'stringio'

$orig_stdout = $stdout
$orig_stderr = $stderr

out_io = StringIO.new
$stdout = out_io
$stderr = out_io

begin
  puts class Foo583
    @@a = 1
  end
  
  f = Foo583.new
  class << f
    @@a = 3
    p @@a
  end
rescue
  $stdout = $orig_stdout
  $stderr = $orig_stderr
  # fail the test
  test_ok(false)
end


$stdout = $orig_stdout
$stderr = $orig_stderr
in_io = StringIO.new out_io.string
test_equal(1, in_io.gets.chomp.to_i)
test_ok(in_io.gets.chomp =~ /warning: class variable access from toplevel singleton method$/)
test_ok(in_io.gets.chomp =~ /warning: class variable access from toplevel singleton method$/)
test_equal(3, in_io.gets.chomp.to_i)
test_ok(in_io.eof?)


class Numeric
  def singleton_method_added a # turn off the default behaviour
  end
end

a = 1.0

class << a
  def hello
    "hello"
  end
end

test_equal("hello", a.hello)