File: compact_spec.rb

package info (click to toggle)
jruby 9.1.17.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 71,608 kB
  • sloc: ruby: 505,916; java: 237,875; xml: 31,161; ansic: 7,152; yacc: 4,605; sh: 887; makefile: 108; jsp: 48; tcl: 40
file content (77 lines) | stat: -rw-r--r-- 2,076 bytes parent folder | download | duplicates (2)
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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

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.should_not equal(a)
  end

  it "does not return subclass instance for Array subclasses" do
    ArraySpecs::MyArray[1, 2, 3, nil].compact.should be_an_instance_of(Array)
  end

  it "does not keep tainted status even if all elements are removed" do
    a = [nil, nil]
    a.taint
    a.compact.tainted?.should be_false
  end

  it "does not keep untrusted status even if all elements are removed" do
    a = [nil, nil]
    a.untrust
    a.compact.untrusted?.should be_false
  end
end

describe "Array#compact!" do
  it "removes all nil elements" do
    a = ['a', nil, 'b', false, 'c']
    a.compact!.should equal(a)
    a.should == ["a", "b", false, "c"]
    a = [nil, 'a', 'b', false, 'c']
    a.compact!.should equal(a)
    a.should == ["a", "b", false, "c"]
    a = ['a', 'b', false, 'c', nil]
    a.compact!.should equal(a)
    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

  it "keeps tainted status even if all elements are removed" do
    a = [nil, nil]
    a.taint
    a.compact!
    a.tainted?.should be_true
  end

  it "keeps untrusted status even if all elements are removed" do
    a = [nil, nil]
    a.untrust
    a.compact!
    a.untrusted?.should be_true
  end

  it "raises a RuntimeError on a frozen array" do
    lambda { ArraySpecs.frozen_array.compact! }.should raise_error(RuntimeError)
  end
end