forked from ruby/spec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect.rb
More file actions
165 lines (143 loc) · 4.59 KB
/
collect.rb
File metadata and controls
165 lines (143 loc) · 4.59 KB
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
160
161
162
163
164
165
describe :array_collect, :shared => true do
it "returns a copy of array with each element replaced by the value returned by block" do
a = ['a', 'b', 'c', 'd']
b = a.send(@method) { |i| i + '!' }
b.should == ["a!", "b!", "c!", "d!"]
b.object_id.should_not == a.object_id
end
it "does not return subclass instance" do
ArraySpecs::MyArray[1, 2, 3].send(@method) { |x| x + 1 }.should be_an_instance_of(Array)
end
it "does not change self" do
a = ['a', 'b', 'c', 'd']
b = a.send(@method) { |i| i + '!' }
a.should == ['a', 'b', 'c', 'd']
end
it "returns the evaluated value of block if it broke in the block" do
a = ['a', 'b', 'c', 'd']
b = a.send(@method) {|i|
if i == 'c'
break 0
else
i + '!'
end
}
b.should == 0
end
ruby_version_is '' ... '1.9' do
it "returns a copy of self if no block given" do
a = [1, 2, 3]
copy = a.send(@method)
copy.should == a
copy.should_not equal(a)
end
end
ruby_version_is '1.9' do
it "returns an Enumerator when no block given" do
a = [1, 2, 3]
a.send(@method).should be_an_instance_of(enumerator_class)
end
end
it "does not copy tainted status" do
a = [1, 2, 3]
a.taint
a.send(@method){|x| x}.tainted?.should be_false
end
ruby_version_is '1.9' do
it "does not copy untrusted status" do
a = [1, 2, 3]
a.untrust
a.send(@method){|x| x}.untrusted?.should be_false
end
end
end
describe :array_collect_b, :shared => true do
it "replaces each element with the value returned by block" do
a = [7, 9, 3, 5]
a.send(@method) { |i| i - 1 }.should equal(a)
a.should == [6, 8, 2, 4]
end
it "returns self" do
a = [1, 2, 3, 4, 5]
b = a.send(@method) {|i| i+1 }
a.object_id.should == b.object_id
end
it "returns the evaluated value of block but its contents is partially modified, if it broke in the block" do
a = ['a', 'b', 'c', 'd']
b = a.send(@method) {|i|
if i == 'c'
break 0
else
i + '!'
end
}
b.should == 0
a.should == ['a!', 'b!', 'c', 'd']
end
ruby_version_is '' ... '1.8.7' do
it "raises LocalJumpError if no block given" do
a = [1, 2, 3]
lambda { a.send(@method) }.should raise_error(LocalJumpError)
end
end
ruby_version_is '1.8.7' do
it "returns an Enumerator when no block given, and the enumerator can modify the original array" do
a = [1, 2, 3]
enum = a.send(@method)
enum.should be_an_instance_of(enumerator_class)
enum.each{|i| "#{i}!" }
a.should == ["1!", "2!", "3!"]
end
end
it "keeps tainted status" do
a = [1, 2, 3]
a.taint
a.tainted?.should be_true
a.send(@method){|x| x}
a.tainted?.should be_true
end
ruby_version_is '1.9' do
it "keeps untrusted status" do
a = [1, 2, 3]
a.untrust
a.send(@method){|x| x}
a.untrusted?.should be_true
end
end
describe "when frozen" do
ruby_version_is '' ... '1.9' do
it "raises a TypeError" do
lambda { ArraySpecs.frozen_array.send(@method) {} }.should raise_error(TypeError)
end
it "raises a TypeError when empty" do
lambda { ArraySpecs.empty_frozen_array.send(@method) {} }.should raise_error(TypeError)
end
ruby_version_is '1.8.7' do
it "raises a TypeError when calling #each on the returned Enumerator" do
enumerator = ArraySpecs.frozen_array.send(@method)
lambda { enumerator.each {|x| x } }.should raise_error(TypeError)
end
it "raises a TypeError when calling #each on the returned Enumerator when empty" do
enumerator = ArraySpecs.empty_frozen_array.send(@method)
lambda { enumerator.each {|x| x } }.should raise_error(TypeError)
end
end
end
ruby_version_is '1.9' do
it "raises a RuntimeError" do
lambda { ArraySpecs.frozen_array.send(@method) {} }.should raise_error(RuntimeError)
end
it "raises a RuntimeError when empty" do
lambda { ArraySpecs.empty_frozen_array.send(@method) {} }.should raise_error(RuntimeError)
end
it "raises a RuntimeError when calling #each on the returned Enumerator" do
enumerator = ArraySpecs.frozen_array.send(@method)
lambda { enumerator.each {|x| x } }.should raise_error(RuntimeError)
end
it "raises a RuntimeError when calling #each on the returned Enumerator when empty" do
enumerator = ArraySpecs.empty_frozen_array.send(@method)
lambda { enumerator.each {|x| x } }.should raise_error(RuntimeError)
end
end
end
end