File: testEval.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 (184 lines) | stat: -rw-r--r-- 3,508 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
require 'test/minirunit'

# ensure binding is setting self correctly
def x
  "foo"
end

Z = binding

class A
  def x
    "bar"
  end

  def y
    eval("x", Z)
  end
end

old_self = self
test_equal("foo", A.new.y)
test_equal("foo", x)

#### ensure self is back to pre bound eval
test_equal(old_self, self)

#### ensure returns within ensures that cross evalstates during an eval are handled properly (whew!)
def inContext &proc 
   begin
     proc.call
   ensure
   end
end

def x2
  inContext do
     return "foo"
  end
end

test_equal(x2, "foo")

# test that evaling a proc doesn't goof up the module nesting for a binding
proc_binding = eval("proc{binding}.call", TOPLEVEL_BINDING)
nesting = eval("$nesting = nil; class A; $nesting = Module.nesting; end; $nesting", TOPLEVEL_BINDING)
test_equal("A", nesting.to_s)

class Foo
  def initialize(p)
    @prefix = p
  end

  def result(val)
    redefine_result
    result val
  end
  
  def redefine_result
    method_decl = "def result(val); \"#{@prefix}: \#\{val\}\"; end"
    instance_eval method_decl, "generated code (#{__FILE__}:#{__LINE__})"
  end
end

f = Foo.new("foo")
test_equal "foo: hi", f.result("hi")

g = Foo.new("bar")
test_equal "bar: hi", g.result("hi")

test_equal "foo: bye", f.result("bye")
test_equal "bar: bye", g.result("bye")

# JRUBY-214 - eval should call to_str on arg 0
class Bar
  def to_str
    "magic_number"
  end
end
magic_number = 1
test_equal(magic_number, eval(Bar.new))

test_exception(TypeError) { eval(Object.new) }

# JRUBY-386 tests
# need at least one arg
test_exception(ArgumentError) { eval }
test_exception(ArgumentError) {self.class.module_eval}
test_exception(ArgumentError) {self.class.class_eval}
test_exception(ArgumentError) {3.instance_eval}

# args must respond to #to_str
test_exception(TypeError) {eval 3}
test_exception(TypeError) {self.class.module_eval 3}
test_exception(TypeError) {self.class.class_eval 4}
test_exception(TypeError) {3.instance_eval 4}

begin
  eval 'return'
rescue LocalJumpError => e
  test_ok(e.message =~ /unexpected return$/)
end

begin
  eval 'break'
rescue LocalJumpError => e
  test_ok(e.message =~ /unexpected break$/)
end

begin
  "".instance_eval 'break'
rescue LocalJumpError => e
  test_ok(e.message =~ /unexpected break$/)
end

begin
  "".instance_eval 'return'
rescue LocalJumpError => e
  test_ok(e.message =~ /unexpected return$/)
end

# If getBindingRubyClass isn't used, this test case will fail,
# since when eval gets called, Kernel will get pushed on the
# parent-stack, and this will always become the RubyClass for
# the evaled string, which is incorrect.
class AbcTestFooAbc
  eval <<-ENDT
  def foofoo_foofoo
  end
ENDT
end

test_equal ["foofoo_foofoo"], AbcTestFooAbc.instance_methods.grep(/foofoo_foofoo/)
test_equal [], Object.instance_methods.grep(/foofoo_foofoo/)

# test Binding.of_caller
def foo
  x = 1
  bar
end

def bar
  eval "x + 1", Binding.of_caller
end

test_equal(2, foo)

# test returns within an eval
def foo
  eval 'return 1'
  return 2
end
def foo2
  x = "blah"
  x.instance_eval "return 1"
  return 2
end

test_equal(1, foo)
# this case is still broken
test_equal(1, foo2)

$a = 1
eval 'BEGIN { $a = 2 }'
test_equal(1, $a)

$b = nil
class Foo
  $b = binding
end

a = Object.new
main = self
b = binding
a.instance_eval { 
  eval("test_equal(a, self)") 
  eval("test_equal(main,self)", b)
  eval("test_equal(Foo, self)", $b)
}

module EvalScope
  eval "class Bar; def self.foo; 'foo'; end; end"
end

test_equal("foo", EvalScope::Bar.foo)