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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Array#uniq" do
it "returns an array with no duplicates" do
["a", "a", "b", "b", "c"].uniq.should == ["a", "b", "c"]
end
ruby_bug "#", "1.8.6.277" do
it "properly handles recursive arrays" do
empty = ArraySpecs.empty_recursive_array
empty.uniq.should == [empty]
array = ArraySpecs.recursive_array
array.uniq.should == [1, 'two', 3.0, array]
end
end
it "uses eql? semantics" do
[1.0, 1].uniq.should == [1.0, 1]
end
it "compares elements first with hash" do
# Can't use should_receive because it uses hash internally
x = mock('0')
def x.hash() 0 end
y = mock('0')
def y.hash() 0 end
[x, y].uniq.should == [x, y]
end
it "does not compare elements with different hash codes via eql?" do
# Can't use should_receive because it uses hash and eql? internally
x = mock('0')
def x.eql?(o) raise("Shouldn't receive eql?") end
y = mock('1')
def y.eql?(o) raise("Shouldn't receive eql?") end
def x.hash() 0 end
def y.hash() 1 end
[x, y].uniq.should == [x, y]
end
it "compares elements with matching hash codes with #eql?" do
# Can't use should_receive because it uses hash and eql? internally
a = Array.new(2) do
obj = mock('0')
def obj.hash()
# It's undefined whether the impl does a[0].eql?(a[1]) or
# a[1].eql?(a[0]) so we taint both.
def self.eql?(o) taint; o.taint; false; end
return 0
end
obj
end
a.uniq.should == a
a[0].tainted?.should == true
a[1].tainted?.should == true
a = Array.new(2) do
obj = mock('0')
def obj.hash()
# It's undefined whether the impl does a[0].eql?(a[1]) or
# a[1].eql?(a[0]) so we taint both.
def self.eql?(o) taint; o.taint; true; end
return 0
end
obj
end
a.uniq.size.should == 1
a[0].tainted?.should == true
a[1].tainted?.should == true
end
ruby_version_is "1.9" do
it "compares elements based on the value returned from the block" do
a = [1, 2, 3, 4]
a.uniq { |x| x >= 2 ? 1 : 0 }.should == [1, 2]
end
end
it "returns subclass instance on Array subclasses" do
ArraySpecs::MyArray[1, 2, 3].uniq.should be_an_instance_of(ArraySpecs::MyArray)
end
end
describe "Array#uniq!" do
it "modifies the array in place" do
a = [ "a", "a", "b", "b", "c" ]
a.uniq!
a.should == ["a", "b", "c"]
end
it "returns self" do
a = [ "a", "a", "b", "b", "c" ]
a.should equal(a.uniq!)
end
ruby_bug "#", "1.8.6.277" do
it "properly handles recursive arrays" do
empty = ArraySpecs.empty_recursive_array
empty_dup = empty.dup
empty.uniq!
empty.should == empty_dup
array = ArraySpecs.recursive_array
expected = array[0..3]
array.uniq!
array.should == expected
end
end
it "returns nil if no changes are made to the array" do
[ "a", "b", "c" ].uniq!.should == nil
end
ruby_version_is ""..."1.9" do
it "raises a TypeError on a frozen array when the array is modified" do
dup_ary = [1, 1, 2]
dup_ary.freeze
lambda { dup_ary.uniq! }.should raise_error(TypeError)
end
it "does not raise an exception on a frozen array when the array would not be modified" do
ArraySpecs.frozen_array.uniq!.should be_nil
end
end
ruby_version_is "1.9" do
it "raises a RuntimeError on a frozen array when the array is modified" do
dup_ary = [1, 1, 2]
dup_ary.freeze
lambda { dup_ary.uniq! }.should raise_error(RuntimeError)
end
# see [ruby-core:23666]
it "raises a RuntimeError on a frozen array when the array would not be modified" do
lambda { ArraySpecs.frozen_array.uniq!}.should raise_error(RuntimeError)
lambda { ArraySpecs.empty_frozen_array.uniq!}.should raise_error(RuntimeError)
end
it "doesn't yield to the block on a frozen array" do
lambda { ArraySpecs.frozen_array.uniq!{ raise RangeError, "shouldn't yield"}}.should raise_error(RuntimeError)
end
it "compares elements based on the value returned from the block" do
a = [1, 2, 3, 4]
a.uniq! { |x| x >= 2 ? 1 : 0 }.should == [1, 2]
end
end
end
|