Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1212 lines (1004 sloc) 27.926 kb
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
require File.expand_path('../../spec_helper', __FILE__)
require File.expand_path('../fixtures/variables', __FILE__)

# TODO: partition these specs into distinct cases based on the
# real parsed forms, not the superficial code forms.
describe "Basic assignment" do
  it "allows the rhs to be assigned to the lhs" do
    a = nil
    a.should == nil
  end

  it "assigns nil to lhs when rhs is an empty expression" do
    a = ()
    a.should be_nil
  end

  it "assigns [] to lhs when rhs is an empty splat expression" do
    a = *()
    a.should == []
  end

  it "allows the assignment of the rhs to the lhs using the rhs splat operator" do
    a = *nil; a.should == []
    a = *1; a.should == [1]
    a = *[]; a.should == []
    a = *[1]; a.should == [1]
    a = *[nil]; a.should == [nil]
    a = *[[]]; a.should == [[]]
    a = *[1,2]; a.should == [1,2]
  end

  it "allows the assignment of the rhs to the lhs using the lhs splat operator" do
    * = 1,2 # Valid syntax, but pretty useless! Nothing to test
    *a = nil; a.should == [nil]
    *a = 1; a.should == [1]
    *a = []; a.should == []
    *a = [1]; a.should == [1]
    *a = [1,2]; a.should == [1,2]
  end

  it "allows the assignment of rhs to the lhs using the lhs and rhs splat operators simultaneously" do
    *a = *nil; a.should == []
    *a = *1; a.should == [1]
    *a = *[]; a.should == []
    *a = *[1]; a.should == [1]
    *a = *[nil]; a.should == [nil]
    *a = *[1,2]; a.should == [1,2]
  end

  it "sets unavailable values to nil" do
    ary = []
    a, b, c = ary

    a.should be_nil
    b.should be_nil
    c.should be_nil
  end

  it "sets the splat to an empty Array if there are no more values" do
    ary = []
    a, b, *c = ary

    a.should be_nil
    b.should be_nil
    c.should == []
  end

  it "allows multiple values to be assigned" do
    a,b,*c = nil; [a,b,c].should == [nil, nil, []]
    a,b,*c = 1; [a,b,c].should == [1, nil, []]
    a,b,*c = []; [a,b,c].should == [nil, nil, []]
    a,b,*c = [1]; [a,b,c].should == [1, nil, []]
    a,b,*c = [nil]; [a,b,c].should == [nil, nil, []]
    a,b,*c = [[]]; [a,b,c].should == [[], nil, []]
    a,b,*c = [1,2]; [a,b,c].should == [1,2,[]]

    a,b,*c = *nil; [a,b,c].should == [nil, nil, []]
    a,b,*c = *1; [a,b,c].should == [1, nil, []]
    a,b,*c = *[]; [a,b,c].should == [nil, nil, []]
    a,b,*c = *[1]; [a,b,c].should == [1, nil, []]
    a,b,*c = *[nil]; [a,b,c].should == [nil, nil, []]
    a,b,*c = *[[]]; [a,b,c].should == [[], nil, []]
    a,b,*c = *[1,2]; [a,b,c].should == [1,2,[]]
  end

  it "calls to_a on the given argument when using a splat" do
    a,b = *VariablesSpecs::ArrayLike.new([1,2]); [a,b].should == [1,2]
  end

  it "supports the {|r,| } form of block assignment" do
    f = lambda {|r,| r.should == []}
    f.call([], *[])

    f = lambda{|x,| x}
    f.call(42).should == 42
    f.call([42]).should == [42]
    f.call([[42]]).should == [[42]]
    f.call([42,55]).should == [42,55]
  end

  it "allows assignment through lambda" do
    f = lambda {|r,*l| r.should == []; l.should == [1]}
    f.call([], *[1])

    f = lambda{|x| x}
    f.call(42).should == 42
    f.call([42]).should == [42]
    f.call([[42]]).should == [[42]]
    f.call([42,55]).should == [42,55]

    f = lambda{|*x| x}
    f.call(42).should == [42]
    f.call([42]).should == [[42]]
    f.call([[42]]).should == [[[42]]]
    f.call([42,55]).should == [[42,55]]
    f.call(42,55).should == [42,55]
  end

  it "allows chained assignment" do
    (a = 1 + b = 2 + c = 4 + d = 8).should == 15
    d.should == 8
    c.should == 12
    b.should == 14
    a.should == 15
  end
end

describe "Assignment using expansion" do
  it "succeeds without conversion" do
    *x = (1..7).to_a
    x.should == [1, 2, 3, 4, 5, 6, 7]
  end
end

describe "Basic multiple assignment" do
  describe "with a single RHS value" do
    it "does not call #to_ary on an Array instance" do
      x = [1, 2]
      x.should_not_receive(:to_ary)

      a, b = x
      a.should == 1
      b.should == 2
    end

    it "does not call #to_a on an Array instance" do
      x = [1, 2]
      x.should_not_receive(:to_a)

      a, b = x
      a.should == 1
      b.should == 2
    end

    it "does not call #to_ary on an Array subclass instance" do
      x = VariablesSpecs::ArraySubclass.new [1, 2]
      x.should_not_receive(:to_ary)

      a, b = x
      a.should == 1
      b.should == 2
    end

    it "does not call #to_a on an Array subclass instance" do
      x = VariablesSpecs::ArraySubclass.new [1, 2]
      x.should_not_receive(:to_a)

      a, b = x
      a.should == 1
      b.should == 2
    end

    it "calls #to_ary on an object" do
      x = mock("single rhs value for masgn")
      x.should_receive(:to_ary).and_return([1, 2])

      a, b = x
      a.should == 1
      b.should == 2
    end

    it "does not call #to_a on an object if #to_ary is not defined" do
      x = mock("single rhs value for masgn")
      x.should_not_receive(:to_a)

      a, b = x
      a.should == x
      b.should be_nil
    end

    it "does not call #to_a on a String" do
      x = "one\ntwo"

      a, b = x
      a.should == x
      b.should be_nil
    end

    it "calls #to_ary on an object even if it's private" do
      a, b = VariablesSpecs::PrivateMethods.new
      a.should == 1
      b.should == 2
    end

    it "doesn't take in account #to_ary if it returns nil" do
      x = VariablesSpecs::ToAryNil.new

      a, b = x
      a.should == x
      b.should be_nil
    end
  end

  describe "with a splatted single RHS value" do
    it "does not call #to_ary on an Array instance" do
      x = [1, 2]
      x.should_not_receive(:to_ary)

      a, b = *x
      a.should == 1
      b.should == 2
    end

    it "does not call #to_a on an Array instance" do
      x = [1, 2]
      x.should_not_receive(:to_a)

      a, b = *x
      a.should == 1
      b.should == 2
    end

    it "does not call #to_ary on an Array subclass instance" do
      x = VariablesSpecs::ArraySubclass.new [1, 2]
      x.should_not_receive(:to_ary)

      a, b = *x
      a.should == 1
      b.should == 2
    end

    it "does not call #to_a on an Array subclass instance" do
      x = VariablesSpecs::ArraySubclass.new [1, 2]
      x.should_not_receive(:to_a)

      a, b = *x
      a.should == 1
      b.should == 2
    end

    it "calls #to_a on an object if #to_ary is not defined" do
      x = mock("single splatted rhs value for masgn")
      x.should_receive(:to_a).and_return([1, 2])

      a, b = *x
      a.should == 1
      b.should == 2
    end

    it "does not call #to_ary on an object" do
      x = mock("single splatted rhs value for masgn")
      x.should_not_receive(:to_ary)

      a, b = *x
      a.should == x
      b.should be_nil
    end

    it "does not call #to_a on a String" do
      x = "one\ntwo"

      a, b = *x
      a.should == x
      b.should be_nil
    end

    it "calls #to_a even if it's private" do
      x = VariablesSpecs::PrivateMethods.new

      a, b = *x
      a.should == 3
      b.should == 4
    end
  end
end

describe "Assigning multiple values" do
  it "allows parallel assignment" do
    a, b = 1, 2
    a.should == 1
    b.should == 2

    a, = 1,2
    a.should == 1
  end

  it "allows safe parallel swapping" do
    a, b = 1, 2
    a, b = b, a
    a.should == 2
    b.should == 1
  end

  not_compliant_on :rubinius do
    it "returns the rhs values used for assignment as an array" do
      x = begin; a, b, c = 1, 2, 3; end
      x.should == [1,2,3]
    end
  end

  it "wraps a single value in an Array if it's not already one" do
    *a = 1
    a.should == [1]

    b = [1]
    *a = b
    a.should == b
  end

  it "evaluates rhs left-to-right" do
    a = VariablesSpecs::ParAsgn.new
    d, e ,f = a.inc, a.inc, a.inc
    d.should == 1
    e.should == 2
    f.should == 3
  end

  it "supports parallel assignment to lhs args via object.method=" do
    a = VariablesSpecs::ParAsgn.new
    a.x, b = 1, 2

    a.x.should == 1
    b.should == 2

    c = VariablesSpecs::ParAsgn.new
    c.x, a.x = a.x, b

    c.x.should == 1
    a.x.should == 2
  end

  it "supports parallel assignment to lhs args using []=" do
    a = [1,2,3]
    a[3], b = 4,5

    a.should == [1,2,3,4]
    b.should == 5
  end

  it "bundles remaining values to an array when using the splat operator" do
    a, *b = 1, 2, 3
    a.should == 1
    b.should == [2, 3]

    *a = 1, 2, 3
    a.should == [1, 2, 3]

    *a = 4
    a.should == [4]

    *a = nil
    a.should == [nil]

    a, = *[1]
    a.should == 1
  end

  it "calls #to_ary on RHS arg if the corresponding LHS var is a splat" do
    x = VariablesSpecs::ParAsgn.new

    a,(*b),c = 5,x
    a.should == 5
    b.should == x.to_ary
    c.should == nil
  end

  it "allows complex parallel assignment" do
    a, (b, c), d = 1, [2, 3], 4
    a.should == 1
    b.should == 2
    c.should == 3
    d.should == 4

    x, (y, z) = 1, 2, 3
    [x,y,z].should == [1,2,nil]
    x, (y, z) = 1, [2,3]
    [x,y,z].should == [1,2,3]
    x, (y, z) = 1, [2]
    [x,y,z].should == [1,2,nil]

    a,(b,c,*d),(e,f),*g = 0,[1,2,3,4],[5,6],7,8
    a.should == 0
    b.should == 1
    c.should == 2
    d.should == [3,4]
    e.should == 5
    f.should == 6
    g.should == [7,8]

    x = VariablesSpecs::ParAsgn.new
    a,(b,c,*d),(e,f),*g = 0,x,[5,6],7,8
    a.should == 0
    b.should == 1
    c.should == 2
    d.should == [3,4]
    e.should == 5
    f.should == 6
    g.should == [7,8]
  end

  it "allows a lhs arg to be used in another lhs args parallel assignment" do
    c = [4,5,6]
    a,b,c[a] = 1,2,3
    a.should == 1
    b.should == 2
    c.should == [4,3,6]

    c[a],b,a = 7,8,9
    a.should == 9
    b.should == 8
    c.should == [4,7,6]
  end
end

describe "Conditional assignment" do
  it "assigns the lhs if previously unassigned" do
    a=[]
    a[0] ||= "bar"
    a[0].should == "bar"

    h={}
    h["foo"] ||= "bar"
    h["foo"].should == "bar"

    h["foo".to_sym] ||= "bar"
    h["foo".to_sym].should == "bar"

    aa = 5
    aa ||= 25
    aa.should == 5

    bb ||= 25
    bb.should == 25

    cc &&=33
    cc.should == nil

    cc = 5
    cc &&=44
    cc.should == 44
  end

  it "checks for class variable definition before fetching its value" do
    class VariableSpecCVarSpec
      @@cvarspec ||= 5
      @@cvarspec.should == 5
    end
  end
end

describe "Unconditional operator assignment 'var op= expr'" do
  it "is equivalent to 'var = var op expr'" do
    x = 13
    (x += 5).should == 18
    x.should == 18

    x = 17
    (x -= 11).should == 6
    x.should == 6

    x = 2
    (x *= 5).should == 10
    x.should == 10

    x = 36
    (x /= 9).should == 4
    x.should == 4

    x = 23
    (x %= 5).should == 3
    x.should == 3
    (x %= 3).should == 0
    x.should == 0

    x = 2
    (x **= 3).should == 8
    x.should == 8

    x = 4
    (x |= 3).should == 7
    x.should == 7
    (x |= 4).should == 7
    x.should == 7

    x = 6
    (x &= 3).should == 2
    x.should == 2
    (x &= 4).should == 0
    x.should == 0

    # XOR
    x = 2
    (x ^= 3).should == 1
    x.should == 1
    (x ^= 4).should == 5
    x.should == 5

    # Bit-shift left
    x = 17
    (x <<= 3).should == 136
    x.should == 136

    # Bit-shift right
    x = 5
    (x >>= 1).should == 2
    x.should == 2
  end
end

describe "Conditional operator assignment 'var op= expr'" do
  it "assigns the lhs if its truthiness is false for ||, true for &&" do
    x = nil
    (x ||= 17).should == 17
    x.should == 17
    (x ||= 2).should == 17
    x.should == 17

    x = false
    (x &&= true).should == false
    x.should == false
    (x &&= false).should == false
    x.should == false
    x = true
    (x &&= true).should == true
    x.should == true
    (x &&= false).should == false
    x.should == false
  end

  it "may not assign at all, depending on the truthiness of lhs" do
    Object.new.instance_eval do
      @falsey = false
      @truthy = true
      freeze
      lambda{ @truthy ||= 42 }.should_not raise_error
      lambda{ @falsey &&= 42 }.should_not raise_error
    end
  end

  it "uses short-circuit arg evaluation" do
    x = 8
    y = VariablesSpecs::OpAsgn.new
    (x ||= y.do_side_effect).should == 8
    y.side_effect.should == nil

    x = nil
    (x &&= y.do_side_effect).should == nil
    y.side_effect.should == nil

    y.a = 5
    (x ||= y.do_side_effect).should == 5
    y.side_effect.should == true
  end
end

describe "Unconditional operator assignment 'obj.meth op= expr'" do
  it "is equivalent to 'obj.meth = obj.meth op expr'" do
    @x = VariablesSpecs::OpAsgn.new
    @x.a = 13
    (@x.a += 5).should == 18
    @x.a.should == 18

    @x.a = 17
    (@x.a -= 11).should == 6
    @x.a.should == 6

    @x.a = 2
    (@x.a *= 5).should == 10
    @x.a.should == 10

    @x.a = 36
    (@x.a /= 9).should == 4
    @x.a.should == 4

    @x.a = 23
    (@x.a %= 5).should == 3
    @x.a.should == 3
    (@x.a %= 3).should == 0
    @x.a.should == 0

    @x.a = 2
    (@x.a **= 3).should == 8
    @x.a.should == 8

    @x.a = 4
    (@x.a |= 3).should == 7
    @x.a.should == 7
    (@x.a |= 4).should == 7
    @x.a.should == 7

    @x.a = 6
    (@x.a &= 3).should == 2
    @x.a.should == 2
    (@x.a &= 4).should == 0
    @x.a.should == 0

    # XOR
    @x.a = 2
    (@x.a ^= 3).should == 1
    @x.a.should == 1
    (@x.a ^= 4).should == 5
    @x.a.should == 5

    @x.a = 17
    (@x.a <<= 3).should == 136
    @x.a.should == 136

    @x.a = 5
    (@x.a >>= 1).should == 2
    @x.a.should == 2
  end
end

describe "Conditional operator assignment 'obj.meth op= expr'" do
  it "is equivalent to 'obj.meth op obj.meth = expr'" do
    @x = VariablesSpecs::OpAsgn.new
    @x.a = nil
    (@x.a ||= 17).should == 17
    @x.a.should == 17
    (@x.a ||= 2).should == 17
    @x.a.should == 17

    @x.a = false
    (@x.a &&= true).should == false
    @x.a.should == false
    (@x.a &&= false).should == false
    @x.a.should == false
    @x.a = true
    (@x.a &&= true).should == true
    @x.a.should == true
    (@x.a &&= false).should == false
    @x.a.should == false
  end

  it "may not assign at all, depending on the truthiness of lhs" do
    m = mock("object")
    m.should_receive(:foo).and_return(:truthy)
    m.should_not_receive(:foo=)
    m.foo ||= 42

    m.should_receive(:bar).and_return(false)
    m.should_not_receive(:bar=)
    m.bar &&= 42
  end

  it "uses short-circuit arg evaluation" do
    x = 8
    y = VariablesSpecs::OpAsgn.new
    (x ||= y.do_side_effect).should == 8
    y.side_effect.should == nil

    x = nil
    (x &&= y.do_side_effect).should == nil
    y.side_effect.should == nil

    y.a = 5
    (x ||= y.do_side_effect).should == 5
    y.side_effect.should == true
  end
end

describe "Operator assignment 'obj.meth op= expr'" do
  it "evaluates lhs one time" do
    x = VariablesSpecs::OpAsgn.new
    x.a = 5
    (x.do_more_side_effects.a += 5).should == 15
    x.a.should == 15

    x.a = 5
    (x.do_more_side_effects.a -= 4).should == 6
    x.a.should == 6

    x.a = 2
    (x.do_more_side_effects.a *= 5).should == 35
    x.a.should == 35

    x.a = 31
    (x.do_more_side_effects.a /= 9).should == 4
    x.a.should == 4

    x.a = 18
    (x.do_more_side_effects.a %= 5).should == 3
    x.a.should == 3

    x.a = 0
    (x.do_more_side_effects.a **= 3).should == 125
    x.a.should == 125

    x.a = -1
    (x.do_more_side_effects.a |= 3).should == 7
    x.a.should == 7

    x.a = 1
    (x.do_more_side_effects.a &= 3).should == 2
    x.a.should == 2

    # XOR
    x.a = -3
    (x.do_more_side_effects.a ^= 3).should == 1
    x.a.should == 1

    x.a = 12
    (x.do_more_side_effects.a <<= 3).should == 136
    x.a.should == 136

    x.a = 0
    (x.do_more_side_effects.a >>= 1).should == 2
    x.a.should == 2

    x.a = nil
    x.b = 0
    (x.do_bool_side_effects.a ||= 17).should == 17
    x.a.should == 17
    x.b.should == 1

    x.a = false
    x.b = 0
    (x.do_bool_side_effects.a &&= true).should == false
    x.a.should == false
    x.b.should == 1
    (x.do_bool_side_effects.a &&= false).should == false
    x.a.should == false
    x.b.should == 2
    x.a = true
    x.b = 0
    (x.do_bool_side_effects.a &&= true).should == true
    x.a.should == true
    x.b.should == 1
    (x.do_bool_side_effects.a &&= false).should == false
    x.a.should == false
    x.b.should == 2
  end
end

describe "Unconditional operator assignment 'obj[idx] op= expr'" do
  it "is equivalent to 'obj[idx] = obj[idx] op expr'" do
    x = [2,13,7]
    (x[1] += 5).should == 18
    x.should == [2,18,7]

    x = [17,6]
    (x[0] -= 11).should == 6
    x.should == [6,6]

    x = [nil,2,28]
    (x[2] *= 2).should == 56
    x.should == [nil,2,56]

    x = [3,9,36]
    (x[2] /= x[1]).should == 4
    x.should == [3,9,4]

    x = [23,4]
    (x[0] %= 5).should == 3
    x.should == [3,4]
    (x[0] %= 3).should == 0
    x.should == [0,4]

    x = [1,2,3]
    (x[1] **= 3).should == 8
    x.should == [1,8,3]

    x = [4,5,nil]
    (x[0] |= 3).should == 7
    x.should == [7,5,nil]
    (x[0] |= 4).should == 7
    x.should == [7,5,nil]

    x = [3,6,9]
    (x[1] &= 3).should == 2
    x.should == [3,2,9]
    (x[1] &= 4).should == 0
    x.should == [3,0,9]

    # XOR
    x = [0,1,2]
    (x[2] ^= 3).should == 1
    x.should == [0,1,1]
    (x[2] ^= 4).should == 5
    x.should == [0,1,5]

    x = [17]
    (x[0] <<= 3).should == 136
    x.should == [136]

    x = [nil,5,8]
    (x[1] >>= 1).should == 2
    x.should == [nil,2,8]
  end
end

describe "Conditional operator assignment 'obj[idx] op= expr'" do
  it "is equivalent to 'obj[idx] op obj[idx] = expr'" do
    x = [1,nil,12]
    (x[1] ||= 17).should == 17
    x.should == [1,17,12]
    (x[1] ||= 2).should == 17
    x.should == [1,17,12]

    x = [true, false, false]
    (x[1] &&= true).should == false
    x.should == [true, false, false]
    (x[1] &&= false).should == false
    x.should == [true, false, false]
    (x[0] &&= true).should == true
    x.should == [true, false, false]
    (x[0] &&= false).should == false
    x.should == [false, false, false]
  end

  it "may not assign at all, depending on the truthiness of lhs" do
    m = mock("object")
    m.should_receive(:[]).and_return(:truthy)
    m.should_not_receive(:[]=)
    m[:foo] ||= 42

    m = mock("object")
    m.should_receive(:[]).and_return(false)
    m.should_not_receive(:[]=)
    m[:bar] &&= 42
  end

  it "uses short-circuit arg evaluation" do
    x = 8
    y = VariablesSpecs::OpAsgn.new
    (x ||= y.do_side_effect).should == 8
    y.side_effect.should == nil

    x = nil
    (x &&= y.do_side_effect).should == nil
    y.side_effect.should == nil

    y.a = 5
    (x ||= y.do_side_effect).should == 5
    y.side_effect.should == true
  end
end

describe "Operator assignment 'obj[idx] op= expr'" do
  class ArrayWithDefaultIndex < Array
    def [](index=nil)
      super(index || 0)
    end

    def []=(first_arg, second_arg=nil)
      if second_arg
        index = fist_arg
        value = second_arg
      else
        index = 0
        value = first_arg
      end

      super(index, value)
    end
  end

  it "handles empty index (idx) arguments" do
    array = ArrayWithDefaultIndex.new
    array << 1
    (array[] += 5).should == 6
  end

  it "handles complex index (idx) arguments" do
    x = [1,2,3,4]
    (x[0,2] += [5]).should == [1,2,5]
    x.should == [1,2,5,3,4]
    (x[0,2] += [3,4]).should == [1,2,3,4]
    x.should == [1,2,3,4,5,3,4]

    (x[2..3] += [8]).should == [3,4,8]
    x.should == [1,2,3,4,8,5,3,4]

    y = VariablesSpecs::OpAsgn.new
    y.a = 1
    (x[y.do_side_effect] *= 2).should == 4
    x.should == [1,4,3,4,8,5,3,4]

    h = {'key1' => 23, 'key2' => 'val'}
    (h['key1'] %= 5).should == 3
    (h['key2'] += 'ue').should == 'value'
    h.should == {'key1' => 3, 'key2' => 'value'}
  end

  it "handles empty splat index (idx) arguments" do
    array = ArrayWithDefaultIndex.new
    array << 5
    splat_index = []

    (array[*splat_index] += 5).should == 10
    array.should== [10]
  end

  it "handles single splat index (idx) arguments" do
    array = [1,2,3,4]
    splat_index = [0]

    (array[*splat_index] += 5).should == 6
    array.should == [6,2,3,4]
  end

  it "handles multiple splat index (idx) arguments" do
    array = [1,2,3,4]
    splat_index = [0,2]

    (array[*splat_index] += [5]).should == [1,2,5]
    array.should == [1,2,5,3,4]
  end

  it "handles splat index (idx) arguments with normal arguments" do
    array = [1,2,3,4]
    splat_index = [2]

    (array[0, *splat_index] += [5]).should == [1,2,5]
    array.should == [1,2,5,3,4]
  end

  # This example fails on 1.9 because of bug #2050
  it "returns result of rhs not result of []=" do
    a = VariablesSpecs::Hashalike.new

    (a[123] = 2).should == 2
    (a[123] += 2).should == 125
    (a[123] -= 2).should == 121
    (a[123] *= 2).should == 246
    # Guard against the Mathn library
    # TODO: Make these specs not rely on specific behaviour / result values
    # by using mocks.
    conflicts_with :Prime do
      (a[123] /= 2).should == 61
    end
    (a[123] %= 2).should == 1
    (a[123] **= 2).should == 15129
    (a[123] |= 2).should == 123
    (a[123] &= 2).should == 2
    (a[123] ^= 2).should == 121
    (a[123] <<= 2).should == 492
    (a[123] >>= 2).should == 30
    (a[123] ||= 2).should == 123
    (a[nil] ||= 2).should == 2
    (a[123] &&= 2).should == 2
    (a[nil] &&= 2).should == nil
  end
end

describe "Single assignment" do
  it "Assignment does not modify the lhs, it reassigns its reference" do
    a = 'Foobar'
    b = a
    b = 'Bazquux'
    a.should == 'Foobar'
    b.should == 'Bazquux'
  end

  it "Assignment does not copy the object being assigned, just creates a new reference to it" do
    a = []
    b = a
    b << 1
    a.should == [1]
  end

  it "If rhs has multiple arguments, lhs becomes an Array of them" do
    a = 1, 2, 3
    a.should == [1, 2, 3]

    a = 1, (), 3
    a.should == [1, nil, 3]
  end
end

describe "Multiple assignment without grouping or splatting" do
  it "An equal number of arguments on lhs and rhs assigns positionally" do
    a, b, c, d = 1, 2, 3, 4
    a.should == 1
    b.should == 2
    c.should == 3
    d.should == 4
  end

  it "If rhs has too few arguments, the missing ones on lhs are assigned nil" do
    a, b, c = 1, 2
    a.should == 1
    b.should == 2
    c.should == nil
  end

  it "If rhs has too many arguments, the extra ones are silently not assigned anywhere" do
    a, b = 1, 2, 3
    a.should == 1
    b.should == 2
  end

  it "The assignments are done in parallel so that lhs and rhs are independent of eachother without copying" do
    o_of_a, o_of_b = mock('a'), mock('b')
    a, b = o_of_a, o_of_b
    a, b = b, a
    a.should equal(o_of_b)
    b.should equal(o_of_a)
  end
end

describe "Multiple assignments with splats" do
  it "* on the lhs collects all parameters from its position onwards as an Array or an empty Array" do
    a, *b = 1, 2
    c, *d = 1
    e, *f = 1, 2, 3
    g, *h = 1, [2, 3]
    *i = 1, [2,3]
    *k = 1,2,3

    a.should == 1
    b.should == [2]
    c.should == 1
    d.should == []
    e.should == 1
    f.should == [2, 3]
    g.should == 1
    h.should == [[2, 3]]
    i.should == [1, [2, 3]]
    k.should == [1,2,3]
  end

  it "* on the LHS returns the Array on the RHS without enclosing it in an Array" do
    *j = [1,2,3]
    j.should == [1,2,3]
  end
end

describe "Multiple assignments with grouping" do
  it "A group on the lhs is considered one position and treats its corresponding rhs position like an Array" do
    a, (b, c), d = 1, 2, 3, 4
    e, (f, g), h = 1, [2, 3, 4], 5
    i, (j, k), l = 1, 2, 3
    a.should == 1
    b.should == 2
    c.should == nil
    d.should == 3
    e.should == 1
    f.should == 2
    g.should == 3
    h.should == 5
    i.should == 1
    j.should == 2
    k.should == nil
    l.should == 3
  end

  it "supports multiple levels of nested groupings" do
    a,(b,(c,d)) = 1,[2,[3,4]]
    a.should == 1
    b.should == 2
    c.should == 3
    d.should == 4

    a,(b,(c,d)) = [1,[2,[3,4]]]
    a.should == 1
    b.should == 2
    c.should == 3
    d.should == 4

    x = [1,[2,[3,4]]]
    a,(b,(c,d)) = x
    a.should == 1
    b.should == 2
    c.should == 3
    d.should == 4
  end

  it "rhs cannot use parameter grouping, it is a syntax error" do
    lambda { eval '(a, b) = (1, 2)' }.should raise_error(SyntaxError)
  end
end

# TODO: merge the following two describe blocks and partition the specs
# into distinct cases.
describe "Multiple assignment" do
  not_compliant_on :rubinius do
    it "has the proper return value" do
      (a,b,*c = *[5,6,7,8,9,10]).should == [5,6,7,8,9,10]
      (d,e = VariablesSpecs.reverse_foo(4,3)).should == [3,4]
      (f,g,h = VariablesSpecs.reverse_foo(6,7)).should == [7,6]
      (i,*j = *[5,6,7]).should == [5,6,7]
      (k,*l = [5,6,7]).should == [5,6,7]
      a.should == 5
      b.should == 6
      c.should == [7,8,9,10]
      d.should == 3
      e.should == 4
      f.should == 7
      g.should == 6
      h.should == nil
      i.should == 5
      j.should == [6,7]
      k.should == 5
      l.should == [6,7]
    end
  end

  # TODO: write Rubinius versions
end

# For now, masgn is deliberately non-compliant with MRI wrt the return val from an masgn.
# Rubinius returns true as the result of the assignment, but MRI returns an array
# containing all the elements on the rhs. As this result is never used, the cost
# of creating and then discarding this array is avoided
describe "Multiple assignment, array-style" do
  it "returns an array of all rhs values" do
    (a,b = 5,6,7).should == [5,6,7]
    a.should == 5
    b.should == 6

    (c,d,*e = 99,8).should == [99,8]
    c.should == 99
    d.should == 8
    e.should == []

    (f,g,h = 99,8).should == [99,8]
    f.should == 99
    g.should == 8
    h.should == nil
  end
end

describe "Scope of variables" do
  it "instance variables not overwritten by local variable in each block" do

    class ScopeVariables
      attr_accessor :v

      def initialize
        @v = ['a', 'b', 'c']
      end

      def check_access
        v.should == ['a', 'b', 'c']
        self.v.should == ['a', 'b', 'c']
      end

      def check_local_variable
        v = nil
        self.v.should == ['a', 'b', 'c']
      end

      def check_each_block
        self.v.each { |v|
          # Don't actually do anything
        }
        self.v.should == ['a', 'b', 'c']
        v.should == ['a', 'b', 'c']
        self.v.object_id.should == v.object_id
      end
    end # Class ScopeVariables

    instance = ScopeVariables.new()
    instance.check_access
    instance.check_local_variable
    instance.check_each_block
  end
end

describe "A local variable in a #define_method scope" do
  ruby_bug '#1322', '1.8.7.228' do
    it "shares the lexical scope containing the call to #define_method" do
      # We need a new scope to reproduce this bug.
      handle = mock("handle for containing scope method")

      def handle.produce_bug
        local = 1

        klass = Class.new
        klass.send :define_method, :set_local do |arg|
          lambda { local = 2 }.call
        end

        # We must call with at least one argument to reproduce the bug.
        klass.new.set_local(nil)

        local
      end

      handle.produce_bug.should == 2
    end
  end

  describe "Multiple assignments with splats" do
    it "* on the LHS has to be applied to any parameter" do
      a, *b, c = 1, 2, 3
      a.should == 1
      b.should == [2]
      c.should == 3
    end
  end

  describe "Chained assignments with method call without parenthesis" do
    it "assigns the result of the method call" do
      a = b = VariablesSpecs::Chain.without_parenthesis 1
      a.should == 1
      b.should == 1
    end
  end
end
Something went wrong with that request. Please try again.