Sha256: 37ec52368a43cbf6dcc2fd3914ba6bd57e0686b74dbf04602d757df317193d3d

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

describe "Array#compact" do
  it "returns a copy of array with all nil elements removed" do
    a = [1, 2, 4]
    a.compact.should == [1, 2, 4]
    a = [1, nil, 2, 4]
    a.compact.should == [1, 2, 4]
    a = [1, 2, 4, nil]
    a.compact.should == [1, 2, 4]
    a = [nil, 1, 2, 4]
    a.compact.should == [1, 2, 4]
  end
  
  it "does not return self" do
    a = [1, 2, 3]
    a.compact.object_id.should_not == a.object_id
  end
end

describe "Array#compact!" do
  it "removes all nil elements" do
    a = ['a', nil, 'b', false, 'c']
    a.compact!.object_id.should == a.object_id
    a.should == ['a', 'b', false, 'c']
    a = [nil, 'a', 'b', false, 'c']
    a.compact!.object_id.should == a.object_id
    a.should == ['a', 'b', false, 'c']
    a = ['a', 'b', false, 'c', nil]
    a.compact!.object_id.should == a.object_id
    a.should == ['a', 'b', false, 'c']
  end
  
  it "returns self if some nil elements are removed" do
    a = ['a', nil, 'b', false, 'c']
    a.compact!.object_id.should == a.object_id
  end
  
  it "returns nil if there are no nil elements to remove" do
    [1, 2, false, 3].compact!.should == nil
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opal-0.3.2 gems/core/spec/core/array/compact_spec.rb
opal-0.3.1 gems/core/spec/core/array/compact_spec.rb
opal-0.3.0 gems/core/spec/core/array/compact_spec.rb