Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- describe Array do
- it "calculates intersections" do
- a = [1, 1, 2, 3, 5]
- b = [3, 4, 5]
- (a & b).should == [3, 5]
- end
- it "can be multiplied" do
- a = []
- b = [1, 2]
- (a * 2).should == []
- (b * 2).should == [1, 2, 1, 2]
- end
- it "can be concatenated" do
- a = [1, 2, 3]
- b = [4, 5, 6]
- (a + b).should eq [1, 2, 3, 4, 5, 6]
- (a << 4).should == [1, 2, 3, 4]
- (a << [5]).should == [1, 2, 3, 4, [5]]
- b.concat([7, 8, 9]).should == [4, 5, 6, 7, 8, 9]
- end
- it "can subtract values from itself" do
- a = [1, 1, 2, 3, 5]
- b = [1, 5]
- (a - b).should eq [2, 3]
- end
- it "can be compared" do
- a = [1, 2, 3]
- b = []
- c = [1, 2, 4]
- (a <=> b).should == 1
- (a <=> c).should == -1
- (b <=> c).should == -1
- end
- it "can be indexed and sliced" do
- a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- a[0].should == 1
- a[0,2].should == [1, 2]
- a[0..3].should == [1, 2, 3, 4]
- a[-1].should == 10
- a[-2].should == 9
- a[12].should == nil
- a[15, 2].should == nil
- a.at(2).should == 3
- a[1, 3] = []
- a[0,4].should == [1, 5, 6, 7]
- end
- it "can handle associations" do
- a = ["john", 1, 2, 3]
- b = ["bob", 4, 5, 6]
- c = [a, b]
- c.assoc("bob")[1..-1].should == [4, 5, 6]
- end
- it "can be cleared" do
- a = [1, 2, 3]
- a.size.should == 3
- a.clear
- a.size.should == 0
- end
- it "can map, or collect, a new array from itself" do
- a = %w[ a b c d e]
- a.map { |i| i * 2 }.should == %w[aa bb cc dd ee]
- a.collect { |i| i * 2 }.should == %w[aa bb cc dd ee]
- end
- it "can calculate out permutations" do
- a = [1, 2, 3]
- a.combination(1).to_a.should include([1])
- a.combination(1).to_a.should include([2])
- a.combination(1).to_a.should include([3])
- a.combination(1).to_a.should == [[1], [2], [3]]
- end
- it "can compact the nils out" do
- a = [1, nil, nil, 2]
- a.compact.should == [1, 2]
- end
- it "can calculate all sorts of counts" do
- a = [1, 2, 4, 4, 5]
- a.count.should == 5
- a.count(4).should == 2
- a.count { |i| i % 2 == 0 }.should == 3
- end
- it "can loop itself" do
- a = [2, 4, 6]
- b = 0
- a.cycle(2) { |i| b += 1}
- b.should == 6
- end
- it "can delete indexes" do
- a = [1, 1, 2, 3, 5, 8, 13]
- b = [1, 2, 3]
- c = [1, 2, 3, 4, 5, 6]
- d = [1, 2, 3, 4, 5]
- e = d.dup
- a.delete(1).should == 1
- a.delete(4) { |i| "none" }.should == "none"
- b.delete_at(1).should == 2
- b.should == [1, 3]
- b.delete_at(10).should == nil
- c.delete_if { |i| i % 3 == 0}.should == [1, 2, 4, 5]
- d.drop(2).should == [3, 4, 5]
- e.drop_while { |i| i < 4}.should == [4, 5]
- end
- it "iterates with each" do
- a = [1, 3, 5, 7]
- b = []
- c = []
- a.each { |i| b << i}
- a.each_index { |i| c << i }
- b.should == a
- c.should == [0, 1, 2, 3]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement