Skip to content
This repository
branch: master
Brian Shirai April 06, 2014
file 129 lines (102 sloc) 4.525 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
require File.expand_path('../../spec_helper', __FILE__)

describe "Splat operator" do
  describe "used to assign a splatted object to an object" do
    it "assigns an empty array when the splatted object is nil" do
      a = *nil; a.should == []
    end

    it "assigns an empty array when the splatted object is an empty array" do
      a = *[]; a.should == []
    end

    it "assigns the splatted object contained into an array when the splatted object doesn't respond to to_a" do
      a = *1; a.should == [1]
    end

    it "assigns the splatted object contained into an array when the splatted object is a result of 'obj || []'" do
      a = *(1 || []); a.should == [1]
    end

    it "assigns the returned value of to_a when the splatted object responds to to_a" do
      o = mock(Object)
      o.should_receive(:to_a).once.and_return(["foo"])
      a = *o; a.should == ["foo"]
    end

    it "assigns the object in a new array when it responds to to_a but to_a returns nil" do
      o = mock(Object)
      o.should_receive(:to_a).once.and_return(nil)
      a = *o; a.should == [o]
    end

    it "assigns an array with nil object if the content of the splatted array is nil" do
      a = *[nil]; a.should == [nil]
    end

    it "assings an array with an empty array when the splatted array contains an empty array" do
      a = *[[]]; a.should == [[]]
    end

    it "assigns an empty array when the content of the splatted array is an empty splatted array" do
      a = *[*[]]; a.should == []
    end

    it "assigns the second array when the content of the splatted array is a non empty splatted array" do
      a = *[*[1]]; a.should == [1]
    end

    it "assigns the second array when the splatted array contains a splatted array with more than one element" do
      a = *[*[1, 2]]; a.should == [1, 2]
    end
  end

  describe "used to assign an object to a splatted reference" do
    it "assigns an array with a nil object when the object is nil" do
      *a = nil; a.should == [nil]
    end

    it "assigns an array containing the object when the object is not an array" do
      *a = 1; a.should == [1]
    end

    it "assigns the object when the object is an array" do
      *a = []; a.should == []
      *a = [1]; a.should == [1]
      *a = [nil]; a.should == [nil]
      *a = [1,2]; a.should == [1,2]
    end

    it "assigns the splatted array when the object is an array that contains an splatted array" do
      *a = [*[]]; a.should == []
      *a = [*[1]]; a.should == [1]
      *a = [*[1,2]]; a.should == [1,2]
    end
  end

  describe "used to assign a splatted object to a splatted reference" do
    it "assigns an empty array when the splatted object is an empty array" do
      *a = *[]; a.should == []
    end

    it "assigns an array containing the splatted object when the splatted object is not an array" do
      *a = *1; a.should == [1]
    end

    it "assigns an array when the splatted object is an array" do
      *a = *[1,2]; a.should == [1,2]
      *a = *[1]; a.should == [1]
      *a = *[nil]; a.should == [nil]
    end

    it "assigns an empty array when the splatted object is an array that contains an empty splatted array" do
      *a = *[*[]]; a.should == []
      *a = *[*[1]]; a.should == [1]
      *a = *[*[1,2]]; a.should == [1,2]
    end

    it "assigns an empty array when the splatted object is nil" do
      *a = *nil; a.should == []
    end
  end

  describe "used to assign splatted objects to multiple block variables" do
    it "assigns nil to normal variables but empty array to references when the splatted object is nil" do
      a,b,*c = *nil; [a,b,c].should == [nil, nil, []]
    end

    it "assigns nil to normal variables but empty array to references when the splatted object is an empty array" do
      a,b,*c = *[]; [a,b,c].should == [nil, nil, []]
    end

    it "assigns the splatted object to the first variable and behaves like nil when the splatted object is not an array" do
      a,b,*c = *1; [a,b,c].should == [1, nil, []]
    end

    it "assigns array values to normal variables but arrays containing elements to references" do
      a,b,*c = *[1,2,3]; [a,b,c].should == [1,2,[3]]
    end

    it "assigns and empty array to the variable if the splatted object contains an empty array" do
      a,b,*c = *[[]]; [a,b,c].should == [[], nil, []]
    end

    it "assigns the values of a splatted array when the splatted object contains an splatted array" do
      a,b,*c = *[*[1,2,3]]; [a,b,c].should == [1,2,[3]]
    end
  end
end
Something went wrong with that request. Please try again.