PageRenderTime 102ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ruby/test_object.rb

http://github.com/ruby/ruby
Ruby | 1002 lines | 869 code | 130 blank | 3 comment | 6 complexity | 85a010dc3e88af1f2ced960d00cb7ad9 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # -*- coding: us-ascii -*-
  2. # frozen_string_literal: false
  3. require 'test/unit'
  4. class TestObject < Test::Unit::TestCase
  5. def setup
  6. @verbose = $VERBOSE
  7. $VERBOSE = nil
  8. end
  9. def teardown
  10. $VERBOSE = @verbose
  11. end
  12. def test_itself
  13. feature6373 = '[ruby-core:44704] [Feature #6373]'
  14. object = Object.new
  15. assert_same(object, object.itself, feature6373)
  16. end
  17. def test_yield_self
  18. feature = '[ruby-core:46320] [Feature #6721]'
  19. object = Object.new
  20. assert_same(self, object.yield_self {self}, feature)
  21. assert_same(object, object.yield_self {|x| break x}, feature)
  22. enum = object.yield_self
  23. assert_instance_of(Enumerator, enum)
  24. assert_equal(1, enum.size)
  25. end
  26. def test_dup
  27. assert_equal 1, 1.dup
  28. assert_equal true, true.dup
  29. assert_equal nil, nil.dup
  30. assert_equal false, false.dup
  31. x = :x; assert_equal x, x.dup
  32. x = "bug13145".intern; assert_equal x, x.dup
  33. x = 1 << 64; assert_equal x, x.dup
  34. x = 1.72723e-77; assert_equal x, x.dup
  35. assert_raise(TypeError) do
  36. Object.new.instance_eval { initialize_copy(1) }
  37. end
  38. end
  39. def test_clone
  40. a = Object.new
  41. def a.b; 2 end
  42. c = a.clone
  43. assert_equal(false, c.frozen?)
  44. assert_equal(false, a.frozen?)
  45. assert_equal(2, c.b)
  46. c = a.clone(freeze: true)
  47. assert_equal(true, c.frozen?)
  48. assert_equal(false, a.frozen?)
  49. assert_equal(2, c.b)
  50. a.freeze
  51. c = a.clone
  52. assert_equal(true, c.frozen?)
  53. assert_equal(true, a.frozen?)
  54. assert_equal(2, c.b)
  55. assert_raise(ArgumentError) {a.clone(freeze: [])}
  56. d = a.clone(freeze: false)
  57. def d.e; 3; end
  58. assert_equal(false, d.frozen?)
  59. assert_equal(true, a.frozen?)
  60. assert_equal(2, d.b)
  61. assert_equal(3, d.e)
  62. assert_equal 1, 1.clone
  63. assert_equal true, true.clone
  64. assert_equal nil, nil.clone
  65. assert_equal false, false.clone
  66. x = :x; assert_equal x, x.dup
  67. x = "bug13145".intern; assert_equal x, x.dup
  68. x = 1 << 64; assert_equal x, x.clone
  69. x = 1.72723e-77; assert_equal x, x.clone
  70. assert_raise(ArgumentError) {1.clone(freeze: false)}
  71. assert_raise(ArgumentError) {true.clone(freeze: false)}
  72. assert_raise(ArgumentError) {nil.clone(freeze: false)}
  73. assert_raise(ArgumentError) {false.clone(freeze: false)}
  74. x = EnvUtil.labeled_class("\u{1f4a9}").new
  75. assert_raise_with_message(ArgumentError, /\u{1f4a9}/) do
  76. Object.new.clone(freeze: x)
  77. end
  78. c = Class.new do
  79. attr_reader :f
  80. end
  81. o = c.new
  82. def o.initialize_clone(_, freeze: true)
  83. @f = freeze
  84. super
  85. end
  86. clone = o.clone
  87. assert_kind_of c, clone
  88. assert_equal true, clone.f
  89. clone = o.clone(freeze: false)
  90. assert_kind_of c, clone
  91. assert_equal false, clone.f
  92. class << o
  93. remove_method(:initialize_clone)
  94. end
  95. def o.initialize_clone(_)
  96. super
  97. end
  98. assert_kind_of c, o.clone
  99. assert_raise(ArgumentError) { o.clone(freeze: false) }
  100. end
  101. def test_init_dupclone
  102. cls = Class.new do
  103. def initialize_clone(orig); throw :initialize_clone; end
  104. def initialize_dup(orig); throw :initialize_dup; end
  105. end
  106. obj = cls.new
  107. assert_throw(:initialize_clone) {obj.clone}
  108. assert_throw(:initialize_dup) {obj.dup}
  109. end
  110. def test_instance_of
  111. assert_raise(TypeError) { 1.instance_of?(1) }
  112. end
  113. def test_kind_of
  114. assert_raise(TypeError) { 1.kind_of?(1) }
  115. end
  116. def test_freeze_immediate
  117. assert_equal(true, 1.frozen?)
  118. 1.freeze
  119. assert_equal(true, 1.frozen?)
  120. assert_equal(true, 2.frozen?)
  121. assert_equal(true, true.frozen?)
  122. assert_equal(true, false.frozen?)
  123. assert_equal(true, nil.frozen?)
  124. end
  125. def test_frozen_error_message
  126. name = "C\u{30c6 30b9 30c8}"
  127. klass = EnvUtil.labeled_class(name) {
  128. attr_accessor :foo
  129. }
  130. obj = klass.new.freeze
  131. assert_raise_with_message(FrozenError, /#{name}/) {
  132. obj.foo = 1
  133. }
  134. end
  135. def test_nil_to_f
  136. assert_equal(0.0, nil.to_f)
  137. end
  138. def test_nil_to_s
  139. str = nil.to_s
  140. assert_equal("", str)
  141. assert_predicate(str, :frozen?)
  142. assert_same(str, nil.to_s)
  143. end
  144. def test_true_to_s
  145. str = true.to_s
  146. assert_equal("true", str)
  147. assert_predicate(str, :frozen?)
  148. assert_same(str, true.to_s)
  149. end
  150. def test_false_to_s
  151. str = false.to_s
  152. assert_equal("false", str)
  153. assert_predicate(str, :frozen?)
  154. assert_same(str, false.to_s)
  155. end
  156. def test_not
  157. assert_equal(false, Object.new.send(:!))
  158. assert_equal(true, nil.send(:!))
  159. end
  160. def test_true_and
  161. assert_equal(true, true & true)
  162. assert_equal(true, true & 1)
  163. assert_equal(false, true & false)
  164. assert_equal(false, true & nil)
  165. end
  166. def test_true_or
  167. assert_equal(true, true | true)
  168. assert_equal(true, true | 1)
  169. assert_equal(true, true | false)
  170. assert_equal(true, true | nil)
  171. end
  172. def test_true_xor
  173. assert_equal(false, true ^ true)
  174. assert_equal(false, true ^ 1)
  175. assert_equal(true, true ^ false)
  176. assert_equal(true, true ^ nil)
  177. end
  178. def test_false_and
  179. assert_equal(false, false & true)
  180. assert_equal(false, false & 1)
  181. assert_equal(false, false & false)
  182. assert_equal(false, false & nil)
  183. end
  184. def test_false_or
  185. assert_equal(true, false | true)
  186. assert_equal(true, false | 1)
  187. assert_equal(false, false | false)
  188. assert_equal(false, false | nil)
  189. end
  190. def test_false_xor
  191. assert_equal(true, false ^ true)
  192. assert_equal(true, false ^ 1)
  193. assert_equal(false, false ^ false)
  194. assert_equal(false, false ^ nil)
  195. end
  196. def test_methods
  197. o = Object.new
  198. a1 = o.methods
  199. a2 = o.methods(false)
  200. def o.foo; end
  201. assert_equal([:foo], o.methods(true) - a1)
  202. assert_equal([:foo], o.methods(false) - a2)
  203. end
  204. def test_methods2
  205. c0 = Class.new
  206. c1 = Class.new(c0)
  207. c1.module_eval do
  208. public ; def foo; end
  209. protected; def bar; end
  210. private ; def baz; end
  211. end
  212. c2 = Class.new(c1)
  213. c2.module_eval do
  214. public ; def foo2; end
  215. protected; def bar2; end
  216. private ; def baz2; end
  217. end
  218. o0 = c0.new
  219. o2 = c2.new
  220. assert_equal([:baz, :baz2], (o2.private_methods - o0.private_methods).sort)
  221. assert_equal([:baz2], (o2.private_methods(false) - o0.private_methods(false)).sort)
  222. assert_equal([:bar, :bar2], (o2.protected_methods - o0.protected_methods).sort)
  223. assert_equal([:bar2], (o2.protected_methods(false) - o0.protected_methods(false)).sort)
  224. assert_equal([:foo, :foo2], (o2.public_methods - o0.public_methods).sort)
  225. assert_equal([:foo2], (o2.public_methods(false) - o0.public_methods(false)).sort)
  226. end
  227. def test_methods_prepend
  228. bug8044 = '[ruby-core:53207] [Bug #8044]'
  229. o = Object.new
  230. def o.foo; end
  231. assert_equal([:foo], o.methods(false))
  232. class << o; prepend Module.new; end
  233. assert_equal([:foo], o.methods(false), bug8044)
  234. end
  235. def test_methods_prepend_singleton
  236. c = Class.new(Module) {private def foo; end}
  237. k = c.new
  238. k.singleton_class
  239. c.module_eval {prepend(Module.new)}
  240. assert_equal([:foo], k.private_methods(false))
  241. end
  242. def test_instance_variable_get
  243. o = Object.new
  244. o.instance_eval { @foo = :foo }
  245. assert_equal(:foo, o.instance_variable_get(:@foo))
  246. assert_equal(nil, o.instance_variable_get(:@bar))
  247. assert_raise(NameError) { o.instance_variable_get('@') }
  248. assert_raise(NameError) { o.instance_variable_get(:'@') }
  249. assert_raise(NameError) { o.instance_variable_get(:foo) }
  250. assert_raise(NameError) { o.instance_variable_get("bar") }
  251. assert_raise(TypeError) { o.instance_variable_get(1) }
  252. n = Object.new
  253. def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@foo"; end
  254. def n.count; @count; end
  255. assert_equal(:foo, o.instance_variable_get(n))
  256. assert_equal(1, n.count)
  257. end
  258. def test_instance_variable_set
  259. o = Object.new
  260. o.instance_variable_set(:@foo, :foo)
  261. assert_equal(:foo, o.instance_eval { @foo })
  262. assert_raise(NameError) { o.instance_variable_set(:'@', 1) }
  263. assert_raise(NameError) { o.instance_variable_set('@', 1) }
  264. assert_raise(NameError) { o.instance_variable_set(:foo, 1) }
  265. assert_raise(NameError) { o.instance_variable_set("bar", 1) }
  266. assert_raise(TypeError) { o.instance_variable_set(1, 1) }
  267. n = Object.new
  268. def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@foo"; end
  269. def n.count; @count; end
  270. o.instance_variable_set(n, :bar)
  271. assert_equal(:bar, o.instance_eval { @foo })
  272. assert_equal(1, n.count)
  273. end
  274. def test_instance_variable_defined
  275. o = Object.new
  276. o.instance_eval { @foo = :foo }
  277. assert_equal(true, o.instance_variable_defined?(:@foo))
  278. assert_equal(false, o.instance_variable_defined?(:@bar))
  279. assert_raise(NameError) { o.instance_variable_defined?(:'@') }
  280. assert_raise(NameError) { o.instance_variable_defined?('@') }
  281. assert_raise(NameError) { o.instance_variable_defined?(:foo) }
  282. assert_raise(NameError) { o.instance_variable_defined?("bar") }
  283. assert_raise(TypeError) { o.instance_variable_defined?(1) }
  284. n = Object.new
  285. def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@foo"; end
  286. def n.count; @count; end
  287. assert_equal(true, o.instance_variable_defined?(n))
  288. assert_equal(1, n.count)
  289. end
  290. def test_remove_instance_variable
  291. { 'T_OBJECT' => Object.new,
  292. 'T_CLASS,T_MODULE' => Class.new(Object),
  293. 'generic ivar' => '',
  294. }.each do |desc, o|
  295. e = assert_raise(NameError, "#{desc} iv removal raises before set") do
  296. o.remove_instance_variable(:@foo)
  297. end
  298. assert_equal([o, :@foo], [e.receiver, e.name])
  299. o.instance_eval { @foo = :foo }
  300. assert_equal(:foo, o.remove_instance_variable(:@foo),
  301. "#{desc} iv removal returns original value")
  302. assert_not_send([o, :instance_variable_defined?, :@foo],
  303. "#{desc} iv removed successfully")
  304. e = assert_raise(NameError, "#{desc} iv removal raises after removal") do
  305. o.remove_instance_variable(:@foo)
  306. end
  307. assert_equal([o, :@foo], [e.receiver, e.name])
  308. end
  309. end
  310. def test_convert_string
  311. o = Object.new
  312. def o.to_s; 1; end
  313. assert_raise(TypeError) { String(o) }
  314. def o.to_s; "o"; end
  315. assert_equal("o", String(o))
  316. def o.to_str; "O"; end
  317. assert_equal("O", String(o))
  318. def o.respond_to?(*) false; end
  319. assert_raise(TypeError) { String(o) }
  320. end
  321. def test_convert_array
  322. o = Object.new
  323. def o.to_a; 1; end
  324. assert_raise(TypeError) { Array(o) }
  325. def o.to_a; [1]; end
  326. assert_equal([1], Array(o))
  327. def o.to_ary; [2]; end
  328. assert_equal([2], Array(o))
  329. def o.respond_to?(*) false; end
  330. assert_equal([o], Array(o))
  331. end
  332. def test_convert_hash
  333. assert_equal({}, Hash(nil))
  334. assert_equal({}, Hash([]))
  335. assert_equal({key: :value}, Hash(key: :value))
  336. assert_raise(TypeError) { Hash([1,2]) }
  337. assert_raise(TypeError) { Hash(Object.new) }
  338. o = Object.new
  339. def o.to_hash; {a: 1, b: 2}; end
  340. assert_equal({a: 1, b: 2}, Hash(o))
  341. def o.to_hash; 9; end
  342. assert_raise(TypeError) { Hash(o) }
  343. end
  344. def test_to_integer
  345. o = Object.new
  346. def o.to_i; nil; end
  347. assert_raise(TypeError) { Integer(o) }
  348. def o.to_i; 42; end
  349. assert_equal(42, Integer(o))
  350. def o.respond_to?(*) false; end
  351. assert_raise(TypeError) { Integer(o) }
  352. end
  353. class MyInteger
  354. def initialize(n); @num = n; end
  355. def to_int; @num; end
  356. def <=>(n); @num <=> n.to_int; end
  357. def <=(n); @num <= n.to_int; end
  358. def +(n); MyInteger.new(@num + n.to_int); end
  359. end
  360. def test_check_to_integer
  361. o1 = MyInteger.new(1)
  362. o9 = MyInteger.new(9)
  363. n = 0
  364. Range.new(o1, o9).step(2) {|x| n += x.to_int }
  365. assert_equal(1+3+5+7+9, n)
  366. end
  367. def test_redefine_method_under_verbose
  368. assert_in_out_err([], <<-INPUT, %w(2), /warning: method redefined; discarding old foo$/)
  369. $VERBOSE = true
  370. o = Object.new
  371. def o.foo; 1; end
  372. def o.foo; 2; end
  373. p o.foo
  374. INPUT
  375. end
  376. def test_redefine_method_which_may_case_serious_problem
  377. assert_in_out_err([], <<-INPUT, [], %r"warning: redefining `object_id' may cause serious problems$")
  378. $VERBOSE = false
  379. def (Object.new).object_id; end
  380. INPUT
  381. assert_in_out_err([], <<-INPUT, [], %r"warning: redefining `__send__' may cause serious problems$")
  382. $VERBOSE = false
  383. def (Object.new).__send__; end
  384. INPUT
  385. bug10421 = '[ruby-dev:48691] [Bug #10421]'
  386. assert_in_out_err([], <<-INPUT, ["1"], [], bug10421)
  387. $VERBOSE = false
  388. class C < BasicObject
  389. def object_id; 1; end
  390. end
  391. puts C.new.object_id
  392. INPUT
  393. end
  394. def test_remove_method
  395. c = Class.new
  396. c.freeze
  397. assert_raise(FrozenError) do
  398. c.instance_eval { remove_method(:foo) }
  399. end
  400. c = Class.new do
  401. def meth1; "meth" end
  402. end
  403. d = Class.new(c) do
  404. alias meth2 meth1
  405. end
  406. o1 = c.new
  407. assert_respond_to(o1, :meth1)
  408. assert_equal("meth", o1.meth1)
  409. o2 = d.new
  410. assert_respond_to(o2, :meth1)
  411. assert_equal("meth", o2.meth1)
  412. assert_respond_to(o2, :meth2)
  413. assert_equal("meth", o2.meth2)
  414. d.class_eval do
  415. remove_method :meth2
  416. end
  417. bug2202 = '[ruby-core:26074]'
  418. assert_raise(NoMethodError, bug2202) {o2.meth2}
  419. %w(object_id __send__ initialize).each do |m|
  420. assert_in_out_err([], <<-INPUT, %w(:ok), %r"warning: removing `#{m}' may cause serious problems$")
  421. $VERBOSE = false
  422. begin
  423. Class.new.instance_eval { remove_method(:#{m}) }
  424. rescue NameError
  425. p :ok
  426. end
  427. INPUT
  428. end
  429. m = "\u{30e1 30bd 30c3 30c9}"
  430. c = Class.new
  431. assert_raise_with_message(NameError, /#{m}/) do
  432. c.class_eval {remove_method m}
  433. end
  434. c = Class.new {
  435. define_method(m) {}
  436. remove_method(m)
  437. }
  438. assert_raise_with_message(NameError, /#{m}/) do
  439. c.class_eval {remove_method m}
  440. end
  441. end
  442. def test_method_missing
  443. assert_raise(ArgumentError) do
  444. 1.instance_eval { method_missing }
  445. end
  446. c = Class.new
  447. c.class_eval do
  448. protected
  449. def foo; end
  450. end
  451. assert_raise(NoMethodError) do
  452. c.new.foo
  453. end
  454. assert_raise(NoMethodError) do
  455. 1.instance_eval { method_missing(:method_missing) }
  456. end
  457. c.class_eval do
  458. undef_method(:method_missing)
  459. end
  460. assert_raise(ArgumentError) do
  461. c.new.method_missing
  462. end
  463. bug2494 = '[ruby-core:27219]'
  464. c = Class.new do
  465. def method_missing(meth, *args)
  466. super
  467. end
  468. end
  469. b = c.new
  470. foo rescue nil
  471. assert_nothing_raised(bug2494) {[b].flatten}
  472. end
  473. def test_respond_to_missing_string
  474. c = Class.new do
  475. def respond_to_missing?(id, priv)
  476. !(id !~ /\Agadzoks\d+\z/) ^ priv
  477. end
  478. end
  479. foo = c.new
  480. assert_equal(false, foo.respond_to?("gadzooks16"))
  481. assert_equal(true, foo.respond_to?("gadzooks17", true))
  482. assert_equal(true, foo.respond_to?("gadzoks16"))
  483. assert_equal(false, foo.respond_to?("gadzoks17", true))
  484. end
  485. def test_respond_to_missing
  486. c = Class.new do
  487. def respond_to_missing?(id, priv)
  488. if id == :foobar
  489. true
  490. else
  491. false
  492. end
  493. end
  494. def method_missing(id, *args)
  495. if id == :foobar
  496. return [:foo, *args]
  497. else
  498. super
  499. end
  500. end
  501. end
  502. foo = c.new
  503. assert_equal([:foo], foo.foobar);
  504. assert_equal([:foo, 1], foo.foobar(1));
  505. assert_equal([:foo, 1, 2, 3, 4, 5], foo.foobar(1, 2, 3, 4, 5));
  506. assert_respond_to(foo, :foobar)
  507. assert_not_respond_to(foo, :foobarbaz)
  508. assert_raise(NoMethodError) do
  509. foo.foobarbaz
  510. end
  511. foobar = foo.method(:foobar)
  512. assert_equal(-1, foobar.arity);
  513. assert_equal([:foo], foobar.call);
  514. assert_equal([:foo, 1], foobar.call(1));
  515. assert_equal([:foo, 1, 2, 3, 4, 5], foobar.call(1, 2, 3, 4, 5));
  516. assert_equal(foobar, foo.method(:foobar))
  517. assert_not_equal(foobar, c.new.method(:foobar))
  518. c = Class.new(c)
  519. assert_equal(false, c.method_defined?(:foobar))
  520. assert_raise(NameError, '[ruby-core:25748]') do
  521. c.instance_method(:foobar)
  522. end
  523. m = Module.new
  524. assert_equal(false, m.method_defined?(:foobar))
  525. assert_raise(NameError, '[ruby-core:25748]') do
  526. m.instance_method(:foobar)
  527. end
  528. end
  529. def test_implicit_respond_to
  530. bug5158 = '[ruby-core:38799]'
  531. p = Object.new
  532. called = []
  533. p.singleton_class.class_eval do
  534. define_method(:to_ary) do
  535. called << [:to_ary, bug5158]
  536. end
  537. end
  538. [[p]].flatten
  539. assert_equal([[:to_ary, bug5158]], called, bug5158)
  540. called = []
  541. p.singleton_class.class_eval do
  542. define_method(:respond_to?) do |*a|
  543. called << [:respond_to?, *a]
  544. false
  545. end
  546. end
  547. [[p]].flatten
  548. assert_equal([[:respond_to?, :to_ary, true]], called, bug5158)
  549. end
  550. def test_implicit_respond_to_arity_1
  551. p = Object.new
  552. called = []
  553. p.singleton_class.class_eval do
  554. define_method(:respond_to?) do |a|
  555. called << [:respond_to?, a]
  556. false
  557. end
  558. end
  559. [[p]].flatten
  560. assert_equal([[:respond_to?, :to_ary]], called, '[bug:6000]')
  561. end
  562. def test_implicit_respond_to_arity_3
  563. p = Object.new
  564. called = []
  565. p.singleton_class.class_eval do
  566. define_method(:respond_to?) do |a, b, c|
  567. called << [:respond_to?, a, b, c]
  568. false
  569. end
  570. end
  571. msg = 'respond_to? must accept 1 or 2 arguments (requires 3)'
  572. assert_raise_with_message(ArgumentError, msg, '[bug:6000]') do
  573. [[p]].flatten
  574. end
  575. end
  576. def test_method_missing_passed_block
  577. bug5731 = '[ruby-dev:44961]'
  578. c = Class.new do
  579. def method_missing(meth, *args) yield(meth, *args) end
  580. end
  581. a = c.new
  582. result = nil
  583. assert_nothing_raised(LocalJumpError, bug5731) do
  584. a.foo {|x| result = x}
  585. end
  586. assert_equal(:foo, result, bug5731)
  587. result = nil
  588. e = a.enum_for(:foo)
  589. assert_nothing_raised(LocalJumpError, bug5731) do
  590. e.each {|x| result = x}
  591. end
  592. assert_equal(:foo, result, bug5731)
  593. c = Class.new do
  594. def respond_to_missing?(id, priv)
  595. true
  596. end
  597. def method_missing(id, *args, &block)
  598. return block.call(:foo, *args)
  599. end
  600. end
  601. foo = c.new
  602. result = nil
  603. assert_nothing_raised(LocalJumpError, bug5731) do
  604. foo.foobar {|x| result = x}
  605. end
  606. assert_equal(:foo, result, bug5731)
  607. result = nil
  608. assert_nothing_raised(LocalJumpError, bug5731) do
  609. foo.enum_for(:foobar).each {|x| result = x}
  610. end
  611. assert_equal(:foo, result, bug5731)
  612. result = nil
  613. foobar = foo.method(:foobar)
  614. foobar.call {|x| result = x}
  615. assert_equal(:foo, result, bug5731)
  616. result = nil
  617. foobar = foo.method(:foobar)
  618. foobar.enum_for(:call).each {|x| result = x}
  619. assert_equal(:foo, result, bug5731)
  620. end
  621. def test_send_with_no_arguments
  622. assert_raise(ArgumentError) { 1.send }
  623. end
  624. def test_send_with_block
  625. x = :ng
  626. 1.send(:times) { x = :ok }
  627. assert_equal(:ok, x)
  628. x = :ok
  629. o = Object.new
  630. def o.inspect
  631. yield if block_given?
  632. super
  633. end
  634. begin
  635. nil.public_send(o) { x = :ng }
  636. rescue TypeError
  637. end
  638. assert_equal(:ok, x)
  639. end
  640. def test_public_send
  641. c = Class.new do
  642. def pub
  643. :ok
  644. end
  645. def invoke(m)
  646. public_send(m)
  647. end
  648. protected
  649. def prot
  650. :ng
  651. end
  652. private
  653. def priv
  654. :ng
  655. end
  656. end.new
  657. assert_equal(:ok, c.public_send(:pub))
  658. assert_raise(NoMethodError) {c.public_send(:priv)}
  659. assert_raise(NoMethodError) {c.public_send(:prot)}
  660. assert_raise(NoMethodError) {c.invoke(:priv)}
  661. bug7499 = '[ruby-core:50489]'
  662. assert_raise(NoMethodError, bug7499) {c.invoke(:prot)}
  663. end
  664. def test_no_superclass_method
  665. bug2312 = '[ruby-dev:39581]'
  666. o = Object.new
  667. e = assert_raise(NoMethodError) {
  668. o.method(:__send__).call(:never_defined_test_no_superclass_method)
  669. }
  670. m1 = e.message
  671. assert_no_match(/no superclass method/, m1, bug2312)
  672. e = assert_raise(NoMethodError) {
  673. o.method(:__send__).call(:never_defined_test_no_superclass_method)
  674. }
  675. assert_equal(m1, e.message, bug2312)
  676. e = assert_raise(NoMethodError) {
  677. o.never_defined_test_no_superclass_method
  678. }
  679. assert_equal(m1, e.message, bug2312)
  680. end
  681. def test_superclass_method
  682. bug2312 = '[ruby-dev:39581]'
  683. assert_in_out_err(["-e", "module Enumerable;undef min;end; (1..2).min{}"],
  684. "", [], /no superclass method/, bug2312)
  685. end
  686. def test_specific_eval_with_wrong_arguments
  687. assert_raise(ArgumentError) do
  688. 1.instance_eval("foo") { foo }
  689. end
  690. assert_raise(ArgumentError) do
  691. 1.instance_eval
  692. end
  693. assert_raise(ArgumentError) do
  694. 1.instance_eval("", 1, 1, 1)
  695. end
  696. end
  697. class InstanceExec
  698. INSTANCE_EXEC = 123
  699. end
  700. def test_instance_exec
  701. x = 1.instance_exec(42) {|a| self + a }
  702. assert_equal(43, x)
  703. x = "foo".instance_exec("bar") {|a| self + a }
  704. assert_equal("foobar", x)
  705. assert_raise(NameError) do
  706. InstanceExec.new.instance_exec { INSTANCE_EXEC }
  707. end
  708. end
  709. def test_extend
  710. assert_raise(ArgumentError) do
  711. 1.extend
  712. end
  713. end
  714. def test_to_s
  715. x = eval(<<-EOS)
  716. class ToS\u{3042}
  717. new.to_s
  718. end
  719. EOS
  720. assert_match(/\bToS\u{3042}:/, x)
  721. name = "X".freeze
  722. x = Object.new
  723. class<<x;self;end.class_eval {define_method(:to_s) {name}}
  724. assert_same(name, x.to_s)
  725. assert_equal("X", [x].join(""))
  726. end
  727. def test_inspect
  728. x = Object.new
  729. assert_match(/\A#<Object:0x\h+>\z/, x.inspect)
  730. x.instance_variable_set(:@ivar, :value)
  731. assert_match(/\A#<Object:0x\h+ @ivar=:value>\z/, x.inspect)
  732. x = Object.new
  733. x.instance_variable_set(:@recur, x)
  734. assert_match(/\A#<Object:0x\h+ @recur=#<Object:0x\h+ \.\.\.>>\z/, x.inspect)
  735. x = Object.new
  736. x.instance_variable_set(:@foo, "value")
  737. x.instance_variable_set(:@bar, 42)
  738. assert_match(/\A#<Object:0x\h+ (?:@foo="value", @bar=42|@bar=42, @foo="value")>\z/, x.inspect)
  739. # #inspect does not call #to_s anymore
  740. feature6130 = '[ruby-core:43238]'
  741. x = Object.new
  742. def x.to_s
  743. "to_s"
  744. end
  745. assert_match(/\A#<Object:0x\h+>\z/, x.inspect, feature6130)
  746. x = eval(<<-EOS)
  747. class Inspect\u{3042}
  748. new.inspect
  749. end
  750. EOS
  751. assert_match(/\bInspect\u{3042}:/, x)
  752. x = eval(<<-EOS)
  753. class Inspect\u{3042}
  754. def initialize
  755. @\u{3044} = 42
  756. end
  757. new
  758. end
  759. EOS
  760. assert_match(/\bInspect\u{3042}:.* @\u{3044}=42\b/, x.inspect)
  761. x.instance_variable_set("@\u{3046}".encode(Encoding::EUC_JP), 6)
  762. assert_match(/@\u{3046}=6\b/, x.inspect)
  763. end
  764. def test_singleton_methods
  765. assert_equal([], Object.new.singleton_methods)
  766. assert_equal([], Object.new.singleton_methods(false))
  767. c = Class.new
  768. def c.foo; end
  769. assert_equal([:foo], c.singleton_methods - [:yaml_tag])
  770. assert_equal([:foo], c.singleton_methods(false))
  771. assert_equal([], c.singleton_class.singleton_methods(false))
  772. c.singleton_class.singleton_class
  773. assert_equal([], c.singleton_class.singleton_methods(false))
  774. o = c.new.singleton_class
  775. assert_equal([:foo], o.singleton_methods - [:yaml_tag])
  776. assert_equal([], o.singleton_methods(false))
  777. o.singleton_class
  778. assert_equal([:foo], o.singleton_methods - [:yaml_tag])
  779. assert_equal([], o.singleton_methods(false))
  780. c.extend(Module.new{def bar; end})
  781. assert_equal([:bar, :foo], c.singleton_methods.sort - [:yaml_tag])
  782. assert_equal([:foo], c.singleton_methods(false))
  783. end
  784. def test_singleton_class
  785. x = Object.new
  786. xs = class << x; self; end
  787. assert_equal(xs, x.singleton_class)
  788. y = Object.new
  789. ys = y.singleton_class
  790. assert_equal(class << y; self; end, ys)
  791. assert_equal(NilClass, nil.singleton_class)
  792. assert_equal(TrueClass, true.singleton_class)
  793. assert_equal(FalseClass, false.singleton_class)
  794. assert_raise(TypeError) do
  795. 123.singleton_class
  796. end
  797. assert_raise(TypeError) do
  798. :foo.singleton_class
  799. end
  800. end
  801. def test_redef_method_missing
  802. bug5473 = '[ruby-core:40287]'
  803. ['ArgumentError.new("bug5473")', 'ArgumentError, "bug5473"', '"bug5473"'].each do |code|
  804. exc = code[/\A[A-Z]\w+/] || 'RuntimeError'
  805. assert_separately([], <<-SRC)
  806. $VERBOSE = nil
  807. class ::Object
  808. def method_missing(m, *a, &b)
  809. raise #{code}
  810. end
  811. end
  812. assert_raise_with_message(#{exc}, "bug5473", #{bug5473.dump}) {1.foo}
  813. SRC
  814. end
  815. end
  816. def assert_not_initialize_copy
  817. a = yield
  818. b = yield
  819. assert_nothing_raised("copy") {a.instance_eval {initialize_copy(b)}}
  820. c = a.dup.freeze
  821. assert_raise(FrozenError, "frozen") {c.instance_eval {initialize_copy(b)}}
  822. d = a.dup
  823. [a, b, c, d]
  824. end
  825. def test_bad_initialize_copy
  826. assert_not_initialize_copy {Object.new}
  827. assert_not_initialize_copy {[].to_enum}
  828. assert_not_initialize_copy {Enumerator::Generator.new {}}
  829. assert_not_initialize_copy {Enumerator::Yielder.new {}}
  830. assert_not_initialize_copy {File.stat(__FILE__)}
  831. assert_not_initialize_copy {open(__FILE__)}.each(&:close)
  832. assert_not_initialize_copy {ARGF.class.new}
  833. assert_not_initialize_copy {Random.new}
  834. assert_not_initialize_copy {//}
  835. assert_not_initialize_copy {/.*/.match("foo")}
  836. st = Struct.new(:foo)
  837. assert_not_initialize_copy {st.new}
  838. end
  839. def test_type_error_message
  840. _issue = "Bug #7539"
  841. assert_raise_with_message(TypeError, "can't convert Array into Integer") {Integer([42])}
  842. assert_raise_with_message(TypeError, 'no implicit conversion of Array into Integer') {[].first([42])}
  843. assert_raise_with_message(TypeError, "can't convert Array into Rational") {Rational([42])}
  844. end
  845. def test_copied_ivar_memory_leak
  846. bug10191 = '[ruby-core:64700] [Bug #10191]'
  847. assert_no_memory_leak([], <<-"end;", <<-"end;", bug10191, timeout: 60, limit: 1.8)
  848. def (a = Object.new).set; @v = nil; end
  849. num = 500_000
  850. end;
  851. num.times {a.clone.set}
  852. end;
  853. end
  854. def test_clone_object_should_not_be_old
  855. assert_normal_exit <<-EOS, '[Bug #13775]'
  856. b = proc { }
  857. 10.times do |i|
  858. b.clone
  859. GC.start
  860. end
  861. EOS
  862. end
  863. def test_matcher
  864. assert_warning(/deprecated Object#=~ is called on Object/) do
  865. assert_equal(Object.new =~ 42, nil)
  866. end
  867. assert_warning(/deprecated Object#=~ is called on Array/) do
  868. assert_equal([] =~ 42, nil)
  869. end
  870. end
  871. end