Advertisement
ZaBlanc

Kata 9/1 Array Spec

Sep 1st, 2011
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.05 KB | None | 0 0
  1.  
  2. describe Array do
  3.   it "calculates intersections" do
  4.     a = [1, 1, 2, 3, 5]
  5.     b = [3, 4, 5]
  6.    
  7.     (a & b).should == [3, 5]
  8.   end
  9.  
  10.   it "can be multiplied" do
  11.     a = []
  12.     b = [1, 2]
  13.    
  14.     (a * 2).should == []
  15.     (b * 2).should == [1, 2, 1, 2]
  16.   end
  17.  
  18.   it "can be concatenated" do
  19.     a = [1, 2, 3]
  20.     b = [4, 5, 6]
  21.    
  22.     (a + b).should eq [1, 2, 3, 4, 5, 6]
  23.     (a << 4).should == [1, 2, 3, 4]
  24.     (a << [5]).should == [1, 2, 3, 4, [5]]
  25.    
  26.     b.concat([7, 8, 9]).should == [4, 5, 6, 7, 8, 9]
  27.   end
  28.  
  29.   it "can subtract values from itself" do
  30.     a = [1, 1, 2, 3, 5]
  31.     b = [1, 5]
  32.    
  33.     (a - b).should eq [2, 3]
  34.   end
  35.  
  36.   it "can be compared" do
  37.     a = [1, 2, 3]
  38.     b = []
  39.     c = [1, 2, 4]
  40.    
  41.     (a <=> b).should == 1
  42.     (a <=> c).should == -1
  43.     (b <=> c).should == -1
  44.   end
  45.  
  46.   it "can be indexed and sliced" do
  47.     a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  48.    
  49.     a[0].should == 1
  50.     a[0,2].should == [1, 2]
  51.     a[0..3].should == [1, 2, 3, 4]
  52.     a[-1].should == 10
  53.     a[-2].should == 9
  54.     a[12].should == nil
  55.     a[15, 2].should == nil
  56.    
  57.     a.at(2).should == 3
  58.    
  59.     a[1, 3] = []
  60.     a[0,4].should == [1, 5, 6, 7]
  61.   end
  62.  
  63.   it "can handle associations" do
  64.     a = ["john", 1, 2, 3]
  65.     b = ["bob", 4, 5, 6]
  66.     c = [a, b]
  67.    
  68.     c.assoc("bob")[1..-1].should == [4, 5, 6]
  69.   end
  70.  
  71.   it "can be cleared" do
  72.     a = [1, 2, 3]
  73.    
  74.     a.size.should == 3
  75.     a.clear
  76.     a.size.should == 0
  77.   end
  78.  
  79.   it "can map, or collect, a new array from itself" do
  80.     a = %w[ a b c d e]
  81.    
  82.     a.map { |i| i * 2 }.should == %w[aa bb cc dd ee]
  83.     a.collect { |i| i * 2 }.should == %w[aa bb cc dd ee]
  84.   end
  85.    
  86.   it "can calculate out permutations" do
  87.     a = [1, 2, 3]
  88.    
  89.     a.combination(1).to_a.should include([1])
  90.     a.combination(1).to_a.should include([2])
  91.     a.combination(1).to_a.should include([3])
  92.     a.combination(1).to_a.should == [[1], [2], [3]]
  93.   end
  94.  
  95.   it "can compact the nils out" do
  96.     a = [1, nil, nil, 2]
  97.    
  98.     a.compact.should == [1, 2]
  99.   end
  100.  
  101.   it "can calculate all sorts of counts" do
  102.     a = [1, 2, 4, 4, 5]
  103.    
  104.     a.count.should == 5
  105.     a.count(4).should == 2
  106.     a.count { |i| i % 2 == 0 }.should == 3
  107.   end
  108.  
  109.   it "can loop itself" do
  110.     a = [2, 4, 6]
  111.     b = 0
  112.    
  113.     a.cycle(2) { |i| b += 1}
  114.     b.should == 6
  115.   end
  116.  
  117.   it "can delete indexes" do
  118.     a = [1, 1, 2, 3, 5, 8, 13]
  119.     b = [1, 2, 3]
  120.     c = [1, 2, 3, 4, 5, 6]
  121.     d = [1, 2, 3, 4, 5]
  122.     e = d.dup
  123.    
  124.     a.delete(1).should == 1
  125.     a.delete(4) { |i| "none" }.should == "none"
  126.    
  127.     b.delete_at(1).should == 2
  128.     b.should == [1, 3]
  129.     b.delete_at(10).should == nil
  130.    
  131.     c.delete_if { |i| i % 3 == 0}.should == [1, 2, 4, 5]
  132.    
  133.     d.drop(2).should == [3, 4, 5]
  134.     e.drop_while { |i| i < 4}.should == [4, 5]
  135.   end
  136.  
  137.   it "iterates with each" do
  138.     a = [1, 3, 5, 7]
  139.     b = []
  140.     c = []
  141.    
  142.     a.each { |i| b << i}
  143.     a.each_index { |i| c << i }
  144.    
  145.     b.should == a
  146.     c.should == [0, 1, 2, 3]
  147.   end
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement