PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/test/externals/ruby1.9/ruby/test_eval.rb

https://github.com/rkh/jruby
Ruby | 418 lines | 357 code | 49 blank | 12 comment | 7 complexity | 9977fac4a630c8073d9222caa9f15ad0 MD5 | raw file
  1. require 'test/unit'
  2. require_relative 'envutil'
  3. class TestEval < Test::Unit::TestCase
  4. @ivar = 12
  5. @@cvar = 13
  6. $gvar__eval = 14
  7. Const = 15
  8. def ruby(*args)
  9. args = ['-e', '$>.write($<.read)'] if args.empty?
  10. ruby = EnvUtil.rubybin
  11. f = IO.popen([ruby] + args, 'r+')
  12. yield(f)
  13. ensure
  14. f.close unless !f || f.closed?
  15. end
  16. def test_eval_basic
  17. assert_equal nil, eval("nil")
  18. assert_equal true, eval("true")
  19. assert_equal false, eval("false")
  20. assert_equal self, eval("self")
  21. assert_equal 1, eval("1")
  22. assert_equal :sym, eval(":sym")
  23. assert_equal 11, eval("11")
  24. @ivar = 12
  25. assert_equal 12, eval("@ivar")
  26. assert_equal 13, eval("@@cvar")
  27. assert_equal 14, eval("$gvar__eval")
  28. assert_equal 15, eval("Const")
  29. assert_equal 16, eval("7 + 9")
  30. assert_equal 17, eval("17.to_i")
  31. assert_equal "18", eval(%q("18"))
  32. assert_equal "19", eval(%q("1#{9}"))
  33. 1.times {
  34. assert_equal 12, eval("@ivar")
  35. assert_equal 13, eval("@@cvar")
  36. assert_equal 14, eval("$gvar__eval")
  37. assert_equal 15, eval("Const")
  38. }
  39. end
  40. def test_eval_binding_basic
  41. assert_equal nil, eval("nil", binding())
  42. assert_equal true, eval("true", binding())
  43. assert_equal false, eval("false", binding())
  44. assert_equal self, eval("self", binding())
  45. assert_equal 1, eval("1", binding())
  46. assert_equal :sym, eval(":sym", binding())
  47. assert_equal 11, eval("11", binding())
  48. @ivar = 12
  49. assert_equal 12, eval("@ivar", binding())
  50. assert_equal 13, eval("@@cvar", binding())
  51. assert_equal 14, eval("$gvar__eval", binding())
  52. assert_equal 15, eval("Const", binding())
  53. assert_equal 16, eval("7 + 9", binding())
  54. assert_equal 17, eval("17.to_i", binding())
  55. assert_equal "18", eval(%q("18"), binding())
  56. assert_equal "19", eval(%q("1#{9}"), binding())
  57. 1.times {
  58. assert_equal 12, eval("@ivar")
  59. assert_equal 13, eval("@@cvar")
  60. assert_equal 14, eval("$gvar__eval")
  61. assert_equal 15, eval("Const")
  62. }
  63. end
  64. def test_module_eval_string_basic
  65. c = self.class
  66. assert_equal nil, c.module_eval("nil")
  67. assert_equal true, c.module_eval("true")
  68. assert_equal false, c.module_eval("false")
  69. assert_equal c, c.module_eval("self")
  70. assert_equal :sym, c.module_eval(":sym")
  71. assert_equal 11, c.module_eval("11")
  72. @ivar = 12
  73. assert_equal 12, c.module_eval("@ivar")
  74. assert_equal 13, c.module_eval("@@cvar")
  75. assert_equal 14, c.module_eval("$gvar__eval")
  76. assert_equal 15, c.module_eval("Const")
  77. assert_equal 16, c.module_eval("7 + 9")
  78. assert_equal 17, c.module_eval("17.to_i")
  79. assert_equal "18", c.module_eval(%q("18"))
  80. assert_equal "19", c.module_eval(%q("1#{9}"))
  81. @ivar = 12
  82. 1.times {
  83. assert_equal 12, c.module_eval("@ivar")
  84. assert_equal 13, c.module_eval("@@cvar")
  85. assert_equal 14, c.module_eval("$gvar__eval")
  86. assert_equal 15, c.module_eval("Const")
  87. }
  88. end
  89. def test_module_eval_block_basic
  90. c = self.class
  91. assert_equal nil, c.module_eval { nil }
  92. assert_equal true, c.module_eval { true }
  93. assert_equal false, c.module_eval { false }
  94. assert_equal c, c.module_eval { self }
  95. assert_equal :sym, c.module_eval { :sym }
  96. assert_equal 11, c.module_eval { 11 }
  97. @ivar = 12
  98. assert_equal 12, c.module_eval { @ivar }
  99. assert_equal 13, c.module_eval { @@cvar }
  100. assert_equal 14, c.module_eval { $gvar__eval }
  101. assert_equal 15, c.module_eval { Const }
  102. assert_equal 16, c.module_eval { 7 + 9 }
  103. assert_equal 17, c.module_eval { "17".to_i }
  104. assert_equal "18", c.module_eval { "18" }
  105. assert_equal "19", c.module_eval { "1#{9}" }
  106. @ivar = 12
  107. 1.times {
  108. assert_equal 12, c.module_eval { @ivar }
  109. assert_equal 13, c.module_eval { @@cvar }
  110. assert_equal 14, c.module_eval { $gvar__eval }
  111. assert_equal 15, c.module_eval { Const }
  112. }
  113. end
  114. def forall_TYPE(mid)
  115. objects = [Object.new, [], nil, true, false, 77, :sym] # TODO: check
  116. objects.each do |obj|
  117. obj.instance_variable_set :@ivar, 12
  118. send mid, obj
  119. end
  120. end
  121. def test_instance_eval_string_basic
  122. forall_TYPE :instance_eval_string_basic_i
  123. end
  124. def instance_eval_string_basic_i(o)
  125. assert_equal nil, o.instance_eval("nil")
  126. assert_equal true, o.instance_eval("true")
  127. assert_equal false, o.instance_eval("false")
  128. assert_equal o, o.instance_eval("self")
  129. assert_equal 1, o.instance_eval("1")
  130. assert_equal :sym, o.instance_eval(":sym")
  131. assert_equal 11, o.instance_eval("11")
  132. assert_equal 12, o.instance_eval("@ivar")
  133. assert_equal 13, o.instance_eval("@@cvar")
  134. assert_equal 14, o.instance_eval("$gvar__eval")
  135. assert_equal 15, o.instance_eval("Const")
  136. assert_equal 16, o.instance_eval("7 + 9")
  137. assert_equal 17, o.instance_eval("17.to_i")
  138. assert_equal "18", o.instance_eval(%q("18"))
  139. assert_equal "19", o.instance_eval(%q("1#{9}"))
  140. 1.times {
  141. assert_equal 12, o.instance_eval("@ivar")
  142. assert_equal 13, o.instance_eval("@@cvar")
  143. assert_equal 14, o.instance_eval("$gvar__eval")
  144. assert_equal 15, o.instance_eval("Const")
  145. }
  146. end
  147. def test_instance_eval_block_basic
  148. forall_TYPE :instance_eval_block_basic_i
  149. end
  150. def instance_eval_block_basic_i(o)
  151. assert_equal nil, o.instance_eval { nil }
  152. assert_equal true, o.instance_eval { true }
  153. assert_equal false, o.instance_eval { false }
  154. assert_equal o, o.instance_eval { self }
  155. assert_equal 1, o.instance_eval { 1 }
  156. assert_equal :sym, o.instance_eval { :sym }
  157. assert_equal 11, o.instance_eval { 11 }
  158. assert_equal 12, o.instance_eval { @ivar }
  159. assert_equal 13, o.instance_eval { @@cvar }
  160. assert_equal 14, o.instance_eval { $gvar__eval }
  161. assert_equal 15, o.instance_eval { Const }
  162. assert_equal 16, o.instance_eval { 7 + 9 }
  163. assert_equal 17, o.instance_eval { 17.to_i }
  164. assert_equal "18", o.instance_eval { "18" }
  165. assert_equal "19", o.instance_eval { "1#{9}" }
  166. 1.times {
  167. assert_equal 12, o.instance_eval { @ivar }
  168. assert_equal 13, o.instance_eval { @@cvar }
  169. assert_equal 14, o.instance_eval { $gvar__eval }
  170. assert_equal 15, o.instance_eval { Const }
  171. }
  172. end
  173. def test_instance_eval_cvar
  174. [Object.new, [], 7, :sym, true, false, nil].each do |obj|
  175. assert_equal(13, obj.instance_eval("@@cvar"))
  176. assert_equal(13, obj.instance_eval{@@cvar})
  177. # assert_raise(NameError){obj.instance_eval("@@cvar")}
  178. # assert_raise(NameError){obj.instance_eval{@@cvar}}
  179. end
  180. end
  181. #
  182. # From ruby/test/ruby/test_eval.rb
  183. #
  184. def test_ev
  185. local1 = "local1"
  186. lambda {
  187. local2 = "local2"
  188. return binding
  189. }.call
  190. end
  191. def test_eval_orig
  192. assert_nil(eval(""))
  193. $bad=false
  194. eval 'while false; $bad = true; print "foo\n" end'
  195. assert(!$bad)
  196. assert(eval('TRUE'))
  197. assert(eval('true'))
  198. assert(!eval('NIL'))
  199. assert(!eval('nil'))
  200. assert(!eval('FALSE'))
  201. assert(!eval('false'))
  202. $foo = 'assert(true)'
  203. begin
  204. eval $foo
  205. rescue
  206. assert(false)
  207. end
  208. assert_equal('assert(true)', eval("$foo"))
  209. assert_equal(true, eval("true"))
  210. i = 5
  211. assert(eval("i == 5"))
  212. assert_equal(5, eval("i"))
  213. assert(eval("defined? i"))
  214. $x = test_ev
  215. assert_equal("local1", eval("local1", $x)) # normal local var
  216. assert_equal("local2", eval("local2", $x)) # nested local var
  217. $bad = true
  218. begin
  219. p eval("local1")
  220. rescue NameError # must raise error
  221. $bad = false
  222. end
  223. assert(!$bad)
  224. # !! use class_eval to avoid nested definition
  225. self.class.class_eval %q(
  226. module EvTest
  227. EVTEST1 = 25
  228. evtest2 = 125
  229. $x = binding
  230. end
  231. )
  232. assert_equal(25, eval("EVTEST1", $x)) # constant in module
  233. assert_equal(125, eval("evtest2", $x)) # local var in module
  234. $bad = true
  235. begin
  236. eval("EVTEST1")
  237. rescue NameError # must raise error
  238. $bad = false
  239. end
  240. assert(!$bad)
  241. if false
  242. # Ruby 2.0 doesn't see Proc as Binding
  243. x = proc{}
  244. eval "i4 = 1", x
  245. assert_equal(1, eval("i4", x))
  246. x = proc{proc{}}.call
  247. eval "i4 = 22", x
  248. assert_equal(22, eval("i4", x))
  249. $x = []
  250. x = proc{proc{}}.call
  251. eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
  252. assert_equal(8, $x[4].call)
  253. end
  254. x = binding
  255. eval "i = 1", x
  256. assert_equal(1, eval("i", x))
  257. x = proc{binding}.call
  258. eval "i = 22", x
  259. assert_equal(22, eval("i", x))
  260. $x = []
  261. x = proc{binding}.call
  262. eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
  263. assert_equal(8, $x[4].call)
  264. x = proc{binding}.call
  265. eval "for i6 in 1..1; j6=i6; end", x
  266. assert(eval("defined? i6", x))
  267. assert(eval("defined? j6", x))
  268. proc {
  269. p = binding
  270. eval "foo11 = 1", p
  271. foo22 = 5
  272. proc{foo11=22}.call
  273. proc{foo22=55}.call
  274. # assert_equal(eval("foo11"), eval("foo11", p))
  275. # assert_equal(1, eval("foo11"))
  276. assert_equal(eval("foo22"), eval("foo22", p))
  277. assert_equal(55, eval("foo22"))
  278. }.call
  279. if false
  280. # Ruby 2.0 doesn't see Proc as Binding
  281. p1 = proc{i7 = 0; proc{i7}}.call
  282. assert_equal(0, p1.call)
  283. eval "i7=5", p1
  284. assert_equal(5, p1.call)
  285. assert(!defined?(i7))
  286. end
  287. if false
  288. # Ruby 2.0 doesn't see Proc as Binding
  289. p1 = proc{i7 = 0; proc{i7}}.call
  290. i7 = nil
  291. assert_equal(0, p1.call)
  292. eval "i7=1", p1
  293. assert_equal(1, p1.call)
  294. eval "i7=5", p1
  295. assert_equal(5, p1.call)
  296. assert_nil(i7)
  297. end
  298. end
  299. def test_nil_instance_eval_cvar
  300. def nil.test_binding
  301. binding
  302. end
  303. bb = eval("nil.instance_eval \"binding\"", nil.test_binding)
  304. assert_raise(NameError, "[ruby-dev:24103]") { eval("@@a", bb) }
  305. class << nil
  306. remove_method :test_binding
  307. end
  308. end
  309. def test_fixnum_instance_eval_cvar
  310. assert_raise(NameError, "[ruby-dev:24213]") { 1.instance_eval "@@a" }
  311. end
  312. def test_cvar_scope_with_instance_eval
  313. # TODO: check
  314. Fixnum.class_eval "@@test_cvar_scope_with_instance_eval = 1" # depends on [ruby-dev:24229]
  315. @@test_cvar_scope_with_instance_eval = 4
  316. assert_equal(4, 1.instance_eval("@@test_cvar_scope_with_instance_eval"), "[ruby-dev:24223]")
  317. Fixnum.__send__(:remove_class_variable, :@@test_cvar_scope_with_instance_eval)
  318. end
  319. def test_eval_and_define_method
  320. assert_nothing_raised("[ruby-dev:24228]") {
  321. def temporally_method_for_test_eval_and_define_method(&block)
  322. lambda {
  323. class << Object.new; self end.send(:define_method, :zzz, &block)
  324. }
  325. end
  326. v = eval("temporally_method_for_test_eval_and_define_method {}")
  327. {}[0] = {}
  328. v.call
  329. }
  330. end
  331. def test_define_method_block
  332. cc = Class.new do
  333. define_method(:foo) {|&block|
  334. block.call if block
  335. }
  336. end
  337. c = cc.new
  338. x = "ng"
  339. c.foo() {x = "ok"}
  340. assert_equal("ok", x)
  341. end
  342. def test_eval_using_integer_as_binding
  343. assert_raise(TypeError) { eval("", 1) }
  344. end
  345. def test_eval_raise
  346. assert_raise(RuntimeError) { eval("raise ''") }
  347. end
  348. def test_eval_using_untainted_binding_under_safe4
  349. assert_raise(SecurityError) do
  350. Thread.new do
  351. b = binding
  352. $SAFE = 4
  353. eval("", b)
  354. end.join
  355. end
  356. end
  357. def test_eval_with_toplevel_binding # [ruby-dev:37142]
  358. ruby("-e", "x = 0; eval('p x', TOPLEVEL_BINDING)") do |f|
  359. f.close_write
  360. assert_equal("0", f.read.chomp)
  361. end
  362. end
  363. def test_eval_ascii_incompatible
  364. assert_raise(ArgumentError) {eval("__ENCODING__".encode("utf-16be"))}
  365. assert_raise(ArgumentError) {eval("__ENCODING__".encode("utf-16le"))}
  366. assert_raise(ArgumentError) {eval("__ENCODING__".encode("utf-32be"))}
  367. assert_raise(ArgumentError) {eval("__ENCODING__".encode("utf-32le"))}
  368. end
  369. end