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