Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 112 lines (96 sloc) 2.982 kb
957c2c9 Brian Shirai Standardize require lines.
brixen authored
1 require File.expand_path('../../../spec_helper', __FILE__)
2 require File.expand_path('../fixtures/classes', __FILE__)
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
3
4 describe "Array#compact" do
5 it "returns a copy of array with all nil elements removed" do
6 a = [1, 2, 4]
7 a.compact.should == [1, 2, 4]
8 a = [1, nil, 2, 4]
9 a.compact.should == [1, 2, 4]
10 a = [1, 2, 4, nil]
11 a.compact.should == [1, 2, 4]
12 a = [nil, 1, 2, 4]
13 a.compact.should == [1, 2, 4]
14 end
15
7f25d0c Yuki Yugui Sonoda puts the specs for Array on 1.8 and 1.9 together.
yugui authored
16 it "does not return self" do
17 a = [1, 2, 3]
18 a.compact.should_not equal(a)
19 end
20
bf8cf25 Paul Mucur Add coverage for 1.9.3 rb_ary_dup behaviour as of r30148 [ruby-core:3364...
mudge authored
21 ruby_version_is '' ... '1.9.3' do
22 it "keeps tainted status even if all elements are removed" do
23 a = [nil, nil]
24 a.taint
25 a.compact.tainted?.should be_true
26 end
7f25d0c Yuki Yugui Sonoda puts the specs for Array on 1.8 and 1.9 together.
yugui authored
27 end
28
bf8cf25 Paul Mucur Add coverage for 1.9.3 rb_ary_dup behaviour as of r30148 [ruby-core:3364...
mudge authored
29 ruby_version_is '1.9' ... '1.9.3' do
7f25d0c Yuki Yugui Sonoda puts the specs for Array on 1.8 and 1.9 together.
yugui authored
30 it "keeps untrusted status even if all elements are removed" do
31 a = [nil, nil]
32 a.untrust
33 a.compact.untrusted?.should be_true
34 end
35 end
bf8cf25 Paul Mucur Add coverage for 1.9.3 rb_ary_dup behaviour as of r30148 [ruby-core:3364...
mudge authored
36
37 ruby_version_is '' ... '1.9.3' do
38 it "returns subclass instance for Array subclasses" do
39 ArraySpecs::MyArray[1, 2, 3, nil].compact.should be_kind_of(ArraySpecs::MyArray)
40 end
41 end
42
43 ruby_version_is '1.9.3' do
44 it "does not return subclass instance for Array subclasses" do
45 ArraySpecs::MyArray[1, 2, 3, nil].compact.should be_kind_of(Array)
46 end
47
48 it "does not keep tainted status even if all elements are removed" do
49 a = [nil, nil]
50 a.taint
51 a.compact.tainted?.should be_false
52 end
53
54 it "does not keep untrusted status even if all elements are removed" do
55 a = [nil, nil]
56 a.untrust
57 a.compact.untrusted?.should be_false
58 end
59 end
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
60 end
61
62 describe "Array#compact!" do
63 it "removes all nil elements" do
64 a = ['a', nil, 'b', false, 'c']
3777b9e Arthur Schreiber Use 'obj.should equal(other_obj)' instead of 'obj.equal?(other_obj).shou...
arthurschreiber authored
65 a.compact!.should equal(a)
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
66 a.should == ["a", "b", false, "c"]
67 a = [nil, 'a', 'b', false, 'c']
3777b9e Arthur Schreiber Use 'obj.should equal(other_obj)' instead of 'obj.equal?(other_obj).shou...
arthurschreiber authored
68 a.compact!.should equal(a)
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
69 a.should == ["a", "b", false, "c"]
70 a = ['a', 'b', false, 'c', nil]
3777b9e Arthur Schreiber Use 'obj.should equal(other_obj)' instead of 'obj.equal?(other_obj).shou...
arthurschreiber authored
71 a.compact!.should equal(a)
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
72 a.should == ["a", "b", false, "c"]
73 end
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
74
7f25d0c Yuki Yugui Sonoda puts the specs for Array on 1.8 and 1.9 together.
yugui authored
75 it "returns self if some nil elements are removed" do
76 a = ['a', nil, 'b', false, 'c']
77 a.compact!.object_id.should == a.object_id
78 end
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
79
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
80 it "returns nil if there are no nil elements to remove" do
81 [1, 2, false, 3].compact!.should == nil
82 end
83
7f25d0c Yuki Yugui Sonoda puts the specs for Array on 1.8 and 1.9 together.
yugui authored
84 it "keeps tainted status even if all elements are removed" do
85 a = [nil, nil]
86 a.taint
87 a.compact!
88 a.tainted?.should be_true
89 end
90
91 ruby_version_is '1.9' do
92 it "keeps untrusted status even if all elements are removed" do
93 a = [nil, nil]
94 a.untrust
95 a.compact!
96 a.untrusted?.should be_true
97 end
98 end
99
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
100 ruby_version_is '' ... '1.9' do
101 it "raises a TypeError on a frozen array" do
102 lambda { ArraySpecs.frozen_array.compact! }.should raise_error(TypeError)
7f25d0c Yuki Yugui Sonoda puts the specs for Array on 1.8 and 1.9 together.
yugui authored
103 end
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
104 end
105
106 ruby_version_is '1.9' do
107 it "raises a RuntimeError on a frozen array" do
108 lambda { ArraySpecs.frozen_array.compact! }.should raise_error(RuntimeError)
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
109 end
110 end
111 end
Something went wrong with that request. Please try again.