PageRenderTime 31ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_object.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 687 lines | 591 code | 96 blank | 0 comment | 5 complexity | 250c60b9bff2fdfef11d6e7d8fd319e1 MD5 | raw file
  1. require 'test/unit'
  2. require_relative 'envutil'
  3. class TestObject < Test::Unit::TestCase
  4. def setup
  5. @verbose = $VERBOSE
  6. $VERBOSE = nil
  7. end
  8. def teardown
  9. $VERBOSE = @verbose
  10. end
  11. def test_dup
  12. assert_raise(TypeError) { 1.dup }
  13. assert_raise(TypeError) { true.dup }
  14. assert_raise(TypeError) { nil.dup }
  15. assert_raise(TypeError) do
  16. Object.new.instance_eval { initialize_copy(1) }
  17. end
  18. end
  19. def test_instance_of
  20. assert_raise(TypeError) { 1.instance_of?(1) }
  21. end
  22. def test_kind_of
  23. assert_raise(TypeError) { 1.kind_of?(1) }
  24. end
  25. def test_taint_frozen_obj
  26. o = Object.new
  27. o.freeze
  28. assert_raise(RuntimeError) { o.taint }
  29. o = Object.new
  30. o.taint
  31. o.freeze
  32. assert_raise(RuntimeError) { o.untaint }
  33. end
  34. def test_freeze_under_safe_4
  35. o = Object.new
  36. assert_raise(SecurityError) do
  37. Thread.new do
  38. $SAFE = 4
  39. o.freeze
  40. end.join
  41. end
  42. end
  43. def test_freeze_immediate
  44. assert_equal(false, 1.frozen?)
  45. 1.freeze
  46. assert_equal(true, 1.frozen?)
  47. assert_equal(false, 2.frozen?)
  48. end
  49. def test_nil_to_f
  50. assert_equal(0.0, nil.to_f)
  51. end
  52. def test_not
  53. assert_equal(false, Object.new.send(:!))
  54. assert_equal(true, nil.send(:!))
  55. end
  56. def test_true_and
  57. assert_equal(true, true & true)
  58. assert_equal(true, true & 1)
  59. assert_equal(false, true & false)
  60. assert_equal(false, true & nil)
  61. end
  62. def test_true_or
  63. assert_equal(true, true | true)
  64. assert_equal(true, true | 1)
  65. assert_equal(true, true | false)
  66. assert_equal(true, true | nil)
  67. end
  68. def test_true_xor
  69. assert_equal(false, true ^ true)
  70. assert_equal(false, true ^ 1)
  71. assert_equal(true, true ^ false)
  72. assert_equal(true, true ^ nil)
  73. end
  74. def test_false_and
  75. assert_equal(false, false & true)
  76. assert_equal(false, false & 1)
  77. assert_equal(false, false & false)
  78. assert_equal(false, false & nil)
  79. end
  80. def test_false_or
  81. assert_equal(true, false | true)
  82. assert_equal(true, false | 1)
  83. assert_equal(false, false | false)
  84. assert_equal(false, false | nil)
  85. end
  86. def test_false_xor
  87. assert_equal(true, false ^ true)
  88. assert_equal(true, false ^ 1)
  89. assert_equal(false, false ^ false)
  90. assert_equal(false, false ^ nil)
  91. end
  92. def test_methods
  93. o = Object.new
  94. a1 = o.methods
  95. a2 = o.methods(false)
  96. def o.foo; end
  97. assert_equal([:foo], o.methods(true) - a1)
  98. assert_equal([:foo], o.methods(false) - a2)
  99. end
  100. def test_methods2
  101. c0 = Class.new
  102. c1 = Class.new(c0)
  103. c1.module_eval do
  104. public ; def foo; end
  105. protected; def bar; end
  106. private ; def baz; end
  107. end
  108. c2 = Class.new(c1)
  109. c2.module_eval do
  110. public ; def foo2; end
  111. protected; def bar2; end
  112. private ; def baz2; end
  113. end
  114. o0 = c0.new
  115. o2 = c2.new
  116. assert_equal([:baz, :baz2], (o2.private_methods - o0.private_methods).sort)
  117. assert_equal([:baz2], (o2.private_methods(false) - o0.private_methods(false)).sort)
  118. assert_equal([:bar, :bar2], (o2.protected_methods - o0.protected_methods).sort)
  119. assert_equal([:bar2], (o2.protected_methods(false) - o0.protected_methods(false)).sort)
  120. assert_equal([:foo, :foo2], (o2.public_methods - o0.public_methods).sort)
  121. assert_equal([:foo2], (o2.public_methods(false) - o0.public_methods(false)).sort)
  122. end
  123. def test_instance_variable_get
  124. o = Object.new
  125. o.instance_eval { @foo = :foo }
  126. assert_equal(:foo, o.instance_variable_get(:@foo))
  127. assert_equal(nil, o.instance_variable_get(:@bar))
  128. assert_raise(NameError) { o.instance_variable_get(:foo) }
  129. end
  130. def test_instance_variable_set
  131. o = Object.new
  132. o.instance_variable_set(:@foo, :foo)
  133. assert_equal(:foo, o.instance_eval { @foo })
  134. assert_raise(NameError) { o.instance_variable_set(:foo, 1) }
  135. end
  136. def test_instance_variable_defined
  137. o = Object.new
  138. o.instance_eval { @foo = :foo }
  139. assert_equal(true, o.instance_variable_defined?(:@foo))
  140. assert_equal(false, o.instance_variable_defined?(:@bar))
  141. assert_raise(NameError) { o.instance_variable_defined?(:foo) }
  142. end
  143. def test_remove_instance_variable
  144. o = Object.new
  145. o.instance_eval { @foo = :foo }
  146. o.instance_eval { remove_instance_variable(:@foo) }
  147. assert_equal(false, o.instance_variable_defined?(:@foo))
  148. end
  149. def test_convert_type
  150. o = Object.new
  151. def o.to_s; 1; end
  152. assert_raise(TypeError) { String(o) }
  153. def o.to_s; "o"; end
  154. assert_equal("o", String(o))
  155. def o.respond_to?(*) false; end
  156. assert_raise(TypeError) { String(o) }
  157. end
  158. def test_check_convert_type
  159. o = Object.new
  160. def o.to_a; 1; end
  161. assert_raise(TypeError) { Array(o) }
  162. def o.to_a; [1]; end
  163. assert_equal([1], Array(o))
  164. def o.respond_to?(*) false; end
  165. assert_equal([o], Array(o))
  166. end
  167. def test_to_integer
  168. o = Object.new
  169. def o.to_i; nil; end
  170. assert_raise(TypeError) { Integer(o) }
  171. def o.to_i; 42; end
  172. assert_equal(42, Integer(o))
  173. def o.respond_to?(*) false; end
  174. assert_raise(TypeError) { Integer(o) }
  175. end
  176. class MyInteger
  177. def initialize(n); @num = n; end
  178. def to_int; @num; end
  179. def <=>(n); @num <=> n.to_int; end
  180. def <=(n); @num <= n.to_int; end
  181. def +(n); MyInteger.new(@num + n.to_int); end
  182. end
  183. def test_check_to_integer
  184. o1 = MyInteger.new(1)
  185. o9 = MyInteger.new(9)
  186. n = 0
  187. Range.new(o1, o9).step(2) {|x| n += x.to_int }
  188. assert_equal(1+3+5+7+9, n)
  189. end
  190. def test_add_method_under_safe4
  191. o = Object.new
  192. assert_raise(SecurityError) do
  193. Thread.new do
  194. $SAFE = 4
  195. def o.foo
  196. end
  197. end.join
  198. end
  199. end
  200. def test_redefine_method_under_verbose
  201. assert_in_out_err([], <<-INPUT, %w(2), /warning: method redefined; discarding old foo$/)
  202. $VERBOSE = true
  203. o = Object.new
  204. def o.foo; 1; end
  205. def o.foo; 2; end
  206. p o.foo
  207. INPUT
  208. end
  209. def test_redefine_method_which_may_case_serious_problem
  210. assert_in_out_err([], <<-INPUT, [], /warning: redefining `object_id' may cause serious problems$/)
  211. $VERBOSE = false
  212. def (Object.new).object_id; end
  213. INPUT
  214. assert_in_out_err([], <<-INPUT, [], /warning: redefining `__send__' may cause serious problems$/)
  215. $VERBOSE = false
  216. def (Object.new).__send__; end
  217. INPUT
  218. end
  219. def test_remove_method
  220. assert_raise(SecurityError) do
  221. Thread.new do
  222. $SAFE = 4
  223. Object.instance_eval { remove_method(:foo) }
  224. end.join
  225. end
  226. assert_raise(SecurityError) do
  227. Thread.new do
  228. $SAFE = 4
  229. Class.instance_eval { remove_method(:foo) }
  230. end.join
  231. end
  232. c = Class.new
  233. c.freeze
  234. assert_raise(RuntimeError) do
  235. c.instance_eval { remove_method(:foo) }
  236. end
  237. c = Class.new do
  238. def meth1; "meth" end
  239. end
  240. d = Class.new(c) do
  241. alias meth2 meth1
  242. end
  243. o1 = c.new
  244. assert_respond_to(o1, :meth1)
  245. assert_equal("meth", o1.meth1)
  246. o2 = d.new
  247. assert_respond_to(o2, :meth1)
  248. assert_equal("meth", o2.meth1)
  249. assert_respond_to(o2, :meth2)
  250. assert_equal("meth", o2.meth2)
  251. d.class_eval do
  252. remove_method :meth2
  253. end
  254. bug2202 = '[ruby-core:26074]'
  255. assert_raise(NoMethodError, bug2202) {o2.meth2}
  256. %w(object_id __send__ initialize).each do |m|
  257. assert_in_out_err([], <<-INPUT, %w(:ok), /warning: removing `#{m}' may cause serious problems$/)
  258. $VERBOSE = false
  259. begin
  260. Class.new.instance_eval { remove_method(:#{m}) }
  261. rescue NameError
  262. p :ok
  263. end
  264. INPUT
  265. end
  266. end
  267. def test_method_missing
  268. assert_raise(ArgumentError) do
  269. 1.instance_eval { method_missing }
  270. end
  271. c = Class.new
  272. c.class_eval do
  273. protected
  274. def foo; end
  275. end
  276. assert_raise(NoMethodError) do
  277. c.new.foo
  278. end
  279. assert_raise(NoMethodError) do
  280. 1.instance_eval { method_missing(:method_missing) }
  281. end
  282. c.class_eval do
  283. undef_method(:method_missing)
  284. end
  285. assert_raise(ArgumentError) do
  286. c.new.method_missing
  287. end
  288. bug2494 = '[ruby-core:27219]'
  289. c = Class.new do
  290. def method_missing(meth, *args)
  291. super
  292. end
  293. end
  294. b = c.new
  295. foo rescue nil
  296. assert_nothing_raised(bug2494) {[b].flatten}
  297. end
  298. def test_respond_to_missing
  299. c = Class.new do
  300. def respond_to_missing?(id, priv=false)
  301. if id == :foobar
  302. true
  303. else
  304. false
  305. end
  306. end
  307. def method_missing(id,*args)
  308. if id == :foobar
  309. return [:foo, *args]
  310. else
  311. super
  312. end
  313. end
  314. end
  315. foo = c.new
  316. assert_equal([:foo], foo.foobar);
  317. assert_equal([:foo, 1], foo.foobar(1));
  318. assert_equal([:foo, 1, 2, 3, 4, 5], foo.foobar(1, 2, 3, 4, 5));
  319. assert(foo.respond_to?(:foobar))
  320. assert_equal(false, foo.respond_to?(:foobarbaz))
  321. assert_raise(NoMethodError) do
  322. foo.foobarbaz
  323. end
  324. foobar = foo.method(:foobar)
  325. assert_equal(-1, foobar.arity);
  326. assert_equal([:foo], foobar.call);
  327. assert_equal([:foo, 1], foobar.call(1));
  328. assert_equal([:foo, 1, 2, 3, 4, 5], foobar.call(1, 2, 3, 4, 5));
  329. assert_equal(foobar, foo.method(:foobar))
  330. assert_not_equal(foobar, c.new.method(:foobar))
  331. c = Class.new(c)
  332. assert_equal(false, c.method_defined?(:foobar))
  333. assert_raise(NameError, '[ruby-core:25748]') do
  334. c.instance_method(:foobar)
  335. end
  336. m = Module.new
  337. assert_equal(false, m.method_defined?(:foobar))
  338. assert_raise(NameError, '[ruby-core:25748]') do
  339. m.instance_method(:foobar)
  340. end
  341. end
  342. def test_implicit_respond_to
  343. bug5158 = '[ruby-core:38799]'
  344. p = Object.new
  345. called = []
  346. p.singleton_class.class_eval do
  347. define_method(:to_ary) do
  348. called << [:to_ary, bug5158]
  349. end
  350. end
  351. [[p]].flatten
  352. assert_equal([[:to_ary, bug5158]], called, bug5158)
  353. called = []
  354. p.singleton_class.class_eval do
  355. define_method(:respond_to?) do |*a|
  356. called << [:respond_to?, *a]
  357. false
  358. end
  359. end
  360. [[p]].flatten
  361. assert_equal([[:respond_to?, :to_ary, true]], called, bug5158)
  362. end
  363. def test_implicit_respond_to_arity_1
  364. p = Object.new
  365. called = []
  366. p.singleton_class.class_eval do
  367. define_method(:respond_to?) do |a|
  368. called << [:respond_to?, a]
  369. false
  370. end
  371. end
  372. [[p]].flatten
  373. assert_equal([[:respond_to?, :to_ary]], called, '[bug:6000]')
  374. end
  375. def test_method_missing_passed_block
  376. bug5731 = '[ruby-dev:44961]'
  377. c = Class.new do
  378. def method_missing(meth, *args) yield(meth, *args) end
  379. end
  380. a = c.new
  381. result = nil
  382. assert_nothing_raised(LocalJumpError, bug5731) do
  383. a.foo {|x| result = x}
  384. end
  385. assert_equal(:foo, result, bug5731)
  386. result = nil
  387. e = a.enum_for(:foo)
  388. assert_nothing_raised(LocalJumpError, bug5731) do
  389. e.each {|x| result = x}
  390. end
  391. assert_equal(:foo, result, bug5731)
  392. c = Class.new do
  393. def respond_to_missing?(id, priv)
  394. true
  395. end
  396. def method_missing(id, *args, &block)
  397. return block.call(:foo, *args)
  398. end
  399. end
  400. foo = c.new
  401. result = nil
  402. assert_nothing_raised(LocalJumpError, bug5731) do
  403. foo.foobar {|x| result = x}
  404. end
  405. assert_equal(:foo, result, bug5731)
  406. result = nil
  407. assert_nothing_raised(LocalJumpError, bug5731) do
  408. foo.enum_for(:foobar).each {|x| result = x}
  409. end
  410. assert_equal(:foo, result, bug5731)
  411. result = nil
  412. foobar = foo.method(:foobar)
  413. foobar.call {|x| result = x}
  414. assert_equal(:foo, result, bug5731)
  415. result = nil
  416. foobar = foo.method(:foobar)
  417. foobar.enum_for(:call).each {|x| result = x}
  418. assert_equal(:foo, result, bug5731)
  419. end
  420. def test_send_with_no_arguments
  421. assert_raise(ArgumentError) { 1.send }
  422. end
  423. def test_no_superclass_method
  424. bug2312 = '[ruby-dev:39581]'
  425. o = Object.new
  426. e = assert_raise(NoMethodError) {
  427. o.method(:__send__).call(:never_defined_test_no_superclass_method)
  428. }
  429. m1 = e.message
  430. assert_no_match(/no superclass method/, m1, bug2312)
  431. e = assert_raise(NoMethodError) {
  432. o.method(:__send__).call(:never_defined_test_no_superclass_method)
  433. }
  434. assert_equal(m1, e.message, bug2312)
  435. e = assert_raise(NoMethodError) {
  436. o.never_defined_test_no_superclass_method
  437. }
  438. assert_equal(m1, e.message, bug2312)
  439. end
  440. def test_superclass_method
  441. bug2312 = '[ruby-dev:39581]'
  442. assert_in_out_err(["-e", "module Enumerable;undef min;end; (1..2).min{}"],
  443. "", [], /no superclass method/, bug2312)
  444. end
  445. def test_specific_eval_with_wrong_arguments
  446. assert_raise(ArgumentError) do
  447. 1.instance_eval("foo") { foo }
  448. end
  449. assert_raise(ArgumentError) do
  450. 1.instance_eval
  451. end
  452. assert_raise(ArgumentError) do
  453. 1.instance_eval("", 1, 1, 1)
  454. end
  455. end
  456. class InstanceExec
  457. INSTANCE_EXEC = 123
  458. end
  459. def test_instance_exec
  460. x = 1.instance_exec(42) {|a| self + a }
  461. assert_equal(43, x)
  462. x = "foo".instance_exec("bar") {|a| self + a }
  463. assert_equal("foobar", x)
  464. assert_raise(NameError) do
  465. InstanceExec.new.instance_exec { INSTANCE_EXEC }
  466. end
  467. end
  468. def test_extend
  469. assert_raise(ArgumentError) do
  470. 1.extend
  471. end
  472. end
  473. def test_untrusted
  474. obj = lambda {
  475. $SAFE = 4
  476. x = Object.new
  477. x.instance_eval { @foo = 1 }
  478. x
  479. }.call
  480. assert_equal(true, obj.untrusted?)
  481. assert_equal(true, obj.tainted?)
  482. x = Object.new
  483. assert_equal(false, x.untrusted?)
  484. assert_raise(SecurityError) do
  485. lambda {
  486. $SAFE = 4
  487. x.instance_eval { @foo = 1 }
  488. }.call
  489. end
  490. x = Object.new
  491. x.taint
  492. assert_raise(SecurityError) do
  493. lambda {
  494. $SAFE = 4
  495. x.instance_eval { @foo = 1 }
  496. }.call
  497. end
  498. x.untrust
  499. assert_equal(true, x.untrusted?)
  500. assert_nothing_raised do
  501. lambda {
  502. $SAFE = 4
  503. x.instance_eval { @foo = 1 }
  504. }.call
  505. end
  506. x.trust
  507. assert_equal(false, x.untrusted?)
  508. assert_raise(SecurityError) do
  509. lambda {
  510. $SAFE = 4
  511. x.instance_eval { @foo = 1 }
  512. }.call
  513. end
  514. a = Object.new
  515. a.untrust
  516. assert_equal(true, a.untrusted?)
  517. b = a.dup
  518. assert_equal(true, b.untrusted?)
  519. c = a.clone
  520. assert_equal(true, c.untrusted?)
  521. a = Object.new
  522. b = lambda {
  523. $SAFE = 4
  524. a.dup
  525. }.call
  526. assert_equal(true, b.untrusted?)
  527. a = Object.new
  528. b = lambda {
  529. $SAFE = 4
  530. a.clone
  531. }.call
  532. assert_equal(true, b.untrusted?)
  533. end
  534. def test_to_s
  535. x = Object.new
  536. x.taint
  537. x.untrust
  538. s = x.to_s
  539. assert_equal(true, s.untrusted?)
  540. assert_equal(true, s.tainted?)
  541. end
  542. def test_exec_recursive
  543. Thread.current[:__recursive_key__] = nil
  544. a = [[]]
  545. a.inspect
  546. assert_nothing_raised do
  547. -> do
  548. $SAFE = 4
  549. begin
  550. a.hash
  551. rescue ArgumentError
  552. end
  553. end.call
  554. end
  555. -> do
  556. assert_nothing_raised do
  557. $SAFE = 4
  558. a.inspect
  559. end
  560. end.call
  561. -> do
  562. o = Object.new
  563. def o.to_ary(x); end
  564. def o.==(x); $SAFE = 4; false; end
  565. a = [[o]]
  566. b = []
  567. b << b
  568. assert_nothing_raised do
  569. b == a
  570. end
  571. end.call
  572. end
  573. def test_singleton_class
  574. x = Object.new
  575. xs = class << x; self; end
  576. assert_equal(xs, x.singleton_class)
  577. y = Object.new
  578. ys = y.singleton_class
  579. assert_equal(class << y; self; end, ys)
  580. assert_equal(NilClass, nil.singleton_class)
  581. assert_equal(TrueClass, true.singleton_class)
  582. assert_equal(FalseClass, false.singleton_class)
  583. assert_raise(TypeError) do
  584. 123.singleton_class
  585. end
  586. assert_raise(TypeError) do
  587. :foo.singleton_class
  588. end
  589. end
  590. end