Skip to content

Commit ba205fb

Browse files
committedSep 23, 2013
Fix some array methods and enable relevant specs
1 parent 50bb47e commit ba205fb

File tree

3 files changed

+42
-52
lines changed

3 files changed

+42
-52
lines changed
 

‎corelib/array.rb

+21-21
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ def *(other)
123123
if (typeof(other) === 'string') {
124124
return #{self}.join(other);
125125
}
126+
if (other < 0) {
127+
#{raise ArgumentError};
128+
}
126129
127130
var result = [];
128131
@@ -448,24 +451,6 @@ def concat(other)
448451
self
449452
end
450453

451-
def count(object = undefined)
452-
%x{
453-
if (object == null) {
454-
return #{self}.length;
455-
}
456-
457-
var result = 0;
458-
459-
for (var i = 0, length = #{self}.length; i < length; i++) {
460-
if (#{`#{self}[i]` == object}) {
461-
result++;
462-
}
463-
}
464-
465-
return result;
466-
}
467-
end
468-
469454
def delete(object)
470455
%x{
471456
var original = #{self}.length;
@@ -523,7 +508,13 @@ def delete_if(&block)
523508
end
524509

525510
def drop(number)
526-
`#{self}.slice(number)`
511+
%x{
512+
if (number < 0) {
513+
#{raise ArgumentError}
514+
}
515+
516+
return #{self}.slice(number);
517+
}
527518
end
528519

529520
alias dup clone
@@ -691,6 +682,9 @@ def index(object=undefined, &block)
691682
}
692683
}
693684
}
685+
else {
686+
return #{enum_for :index};
687+
}
694688
695689
return nil;
696690
}
@@ -821,7 +815,7 @@ def pop(count = undefined)
821815
}
822816
823817
if (count < 0) {
824-
#{ raise "negative count given" };
818+
#{ raise ArgumentError, "negative count given" };
825819
}
826820
827821
return count > length ? #{self}.splice(0, #{self}.length) : #{self}.splice(length - count, length);
@@ -1087,7 +1081,13 @@ def sort!(&block)
10871081
end
10881082

10891083
def take(count)
1090-
`#{self}.slice(0, count)`
1084+
%x{
1085+
if (count < 0) {
1086+
#{raise ArgumentError};
1087+
}
1088+
1089+
return #{self}.slice(0, count);
1090+
}
10911091
end
10921092

10931093
def take_while(&block)

‎spec/filters/bugs/array.rb

-31
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
fails "Array#clone copies singleton methods"
66
fails "Array#clone creates a new array containing all elements or the original"
7-
fails "Array#clone returns an Array or a subclass instance"
87

98
fails "Array#collect! returns an Enumerator when no block given, and the enumerator can modify the original array"
109

@@ -23,15 +22,11 @@
2322
fails "Array#<=> returns +1 if the arrays have same length and a pair of corresponding elements returns +1 for <=>"
2423
fails "Array#<=> properly handles recursive arrays"
2524
fails "Array#<=> tries to convert the passed argument to an Array using #to_ary"
26-
fails "Array#<=> does not call #to_ary on Array subclasses"
2725
fails "Array#<=> returns nil when the argument is not array-like"
2826

2927
fails "Array#concat tries to convert the passed argument to an Array using #to_ary"
30-
fails "Array#concat does not call #to_ary on Array subclasses"
3128
fails "Array#concat is not infected by the other"
3229

33-
fails "Array#count returns the number of element for which the block evaluates to true"
34-
3530
fails "Array#delete_at tries to convert the passed argument to an Integer using #to_int"
3631

3732
fails "Array#delete_if returns an Enumerator if no block given, and the enumerator can modify the original array"
@@ -43,12 +38,8 @@
4338
fails "Array#drop_while removes elements from the start of the array until the block returns nil"
4439
fails "Array#drop_while removes elements from the start of the array while the block evaluates to true"
4540

46-
fails "Array#drop raises an ArgumentError if the number of elements specified is negative"
47-
4841
fails "Array#dup creates a new array containing all elements or the original"
49-
fails "Array#dup returns an Array or a subclass instance"
5042

51-
fails "Array#[]= does not call to_ary on rhs array subclasses for multi-element sets"
5243
fails "Array#[]= calls to_ary on its rhs argument for multi-element sets"
5344
fails "Array#[]= raises an IndexError when passed indexes out of bounds"
5445
fails "Array#[]= tries to convert Range elements to Integers using #to_int with [m..n] and [m...n]"
@@ -65,7 +56,6 @@
6556

6657
fails "Array#eql? returns false if any corresponding elements are not #eql?"
6758
fails "Array#eql? ignores array class differences"
68-
fails "Array#eql? does not call #to_ary on Array subclasses"
6959
fails "Array#eql? does not call #to_ary on its argument"
7060
fails "Array#eql? handles well recursive arrays"
7161
fails "Array#eql? returns false immediately when sizes of the arrays differ"
@@ -74,7 +64,6 @@
7464
fails "Array#== returns true if corresponding elements are #=="
7565
fails "Array#== returns false if any corresponding elements are not #=="
7666
fails "Array#== compares with an equivalent Array-like object using #to_ary"
77-
fails "Array#== does not call #to_ary on Array subclasses"
7867
fails "Array#== does not call #to_ary on its argument"
7968
fails "Array#== handles well recursive arrays"
8069
fails "Array#== returns false immediately when sizes of the arrays differ"
@@ -88,7 +77,6 @@
8877
fails "Array#flatten does not call flatten on elements"
8978
fails "Array#flatten raises an ArgumentError on recursive arrays"
9079
fails "Array#flatten flattens any element which responds to #to_ary, using the return value of said method"
91-
fails "Array#flatten returns subclass instance for Array subclasses"
9280
fails "Array#flatten with a non-Array object in the Array ignores the return value of #to_ary if it is nil"
9381
fails "Array#flatten with a non-Array object in the Array raises a TypeError if the return value of #to_ary is not an Array"
9482
fails "Array#flatten raises a TypeError when the passed Object can't be converted to an Integer"
@@ -110,11 +98,9 @@
11098
fails "Array#initialize with (size, object=nil) calls #to_int to convert the size argument to an Integer when object is given"
11199
fails "Array#initialize with (size, object=nil) raises an ArgumentError if size is too large"
112100
fails "Array#initialize with (size, object=nil) sets the array to size and fills with the object"
113-
fails "Array#initialize with (array) does not call #to_ary on instances of Array or subclasses of Array"
114101
fails "Array#initialize with (array) calls #to_ary to convert the value to an array"
115102
fails "Array#initialize raises a RuntimeError on frozen arrays"
116103
fails "Array#initialize preserves the object's identity even when changing its value"
117-
fails "Array#initialize is called on subclasses"
118104
fails "Array#initialize is private"
119105

120106
fails "Array#insert tries to convert the passed position argument to an Integer using #to_int"
@@ -134,7 +120,6 @@
134120
fails "Array#& determines equivalence between elements in the sense of eql?"
135121

136122
fails "Array#index returns the index of the first element == to object"
137-
fails "Array#index given no argument and no block produces an Enumerator"
138123

139124
fails "Array#inspect calls inspect on its elements and joins the results with commas"
140125

@@ -152,8 +137,6 @@
152137
fails "Array#- removes an item identified as equivalent via #hash and #eql?"
153138
fails "Array#- tries to convert the passed arguments to Arrays using #to_ary"
154139

155-
fails "Array#* with an integer with a subclass of Array returns a subclass instance"
156-
fails "Array#* with an integer raises an ArgumentError when passed a negative integer"
157140
fails "Array#* raises a TypeError is the passed argument is nil"
158141
fails "Array#* converts the passed argument to a String rather than an Integer"
159142
fails "Array#* raises a TypeError if the argument can neither be converted to a string nor an integer"
@@ -170,7 +153,6 @@
170153

171154
fails "Array.new with (size, object=nil) raises an ArgumentError if size is too large"
172155
fails "Array.new with (array) calls #to_ary to convert the value to an array"
173-
fails "Array.new with (array) does not call #to_ary on instances of Array or subclasses of Array"
174156
fails "Array.new with (size, object=nil) calls #to_int to convert the size argument to an Integer when object is given"
175157
fails "Array.new with (size, object=nil) calls #to_int to convert the size argument to an Integer when object is not given"
176158
fails "Array.new with (size, object=nil) raises a TypeError if the size argument is not an Integer type"
@@ -180,12 +162,10 @@
180162
fails "Array#pop passed a number n as an argument raises an ArgumentError if more arguments are passed"
181163
fails "Array#pop passed a number n as an argument raises a TypeError when the passed n can be coerced to Integer"
182164
fails "Array#pop passed a number n as an argument tries to convert n to an Integer using #to_int"
183-
fails "Array#pop passed a number n as an argument raises an ArgumentError if n is negative"
184165

185166
fails "Array#rassoc does not check the last element in each contained but speficically the second"
186167
fails "Array#rassoc calls elem == obj on the second element of each contained array"
187168

188-
fails "Array#replace does not call #to_ary on Array subclasses"
189169
fails "Array#replace tries to convert the passed argument to an Array using #to_ary"
190170

191171
fails "Array#rindex rechecks the array size during iteration"
@@ -213,12 +193,6 @@
213193

214194
fails "Array#shuffle! returns the same values, in a usually different order"
215195

216-
fails "Array#slice with a subclass of Array returns a subclass instance with [-n...-m]"
217-
fails "Array#slice with a subclass of Array returns a subclass instance with [-n..-m]"
218-
fails "Array#slice with a subclass of Array returns a subclass instance with [n...m]"
219-
fails "Array#slice with a subclass of Array returns a subclass instance with [n..m]"
220-
fails "Array#slice with a subclass of Array returns a subclass instance with [-n, m]"
221-
fails "Array#slice with a subclass of Array returns a subclass instance with [n, m]"
222196
fails "Array#slice raises a RangeError when the length is out of range of Fixnum"
223197
fails "Array#slice raises a RangeError when the start index is out of range of Fixnum"
224198
fails "Array#slice returns nil if range start is not in the array with [m..n]"
@@ -232,10 +206,6 @@
232206
fails "Array#slice! removes and return elements in range"
233207
fails "Array#slice! calls to_int on start and length arguments"
234208

235-
fails "Array#take raises an ArgumentError when the argument is negative"
236-
237-
fails "Array#to_a does not return subclass instance on Array subclasses"
238-
239209
fails "Array#to_s calls inspect on its elements and joins the results with commas"
240210

241211
fails "Array#transpose raises a TypeError if the passed Argument does not respond to #to_ary"
@@ -250,7 +220,6 @@
250220
fails "Array#uniq compares elements based on the value returned from the block"
251221
fails "Array#uniq compares elements with matching hash codes with #eql?"
252222
fails "Array#uniq uses eql? semantics"
253-
fails "Array#uniq returns subclass instance on Array subclasses"
254223

255224
fails "Array#uniq! compares elements based on the value returned from the block"
256225
fails "Array#uniq! properly handles recursive arrays"

‎spec/filters/unsupported/array_subclasses.rb

+21
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,25 @@
33
fails "Array[] is a synonym for .[]"
44
fails "Array.new returns an instance of a subclass"
55
fails "Array#values_at does not return subclass instance on Array subclasses"
6+
fails "Array#to_a does not return subclass instance on Array subclasses"
7+
fails "Array#uniq returns subclass instance on Array subclasses"
8+
fails "Array#clone returns an Array or a subclass instance"
9+
fails "Array#<=> does not call #to_ary on Array subclasses"
10+
fails "Array#concat does not call #to_ary on Array subclasses"
11+
fails "Array#dup returns an Array or a subclass instance"
12+
fails "Array#[]= does not call to_ary on rhs array subclasses for multi-element sets"
13+
fails "Array#eql? does not call #to_ary on Array subclasses"
14+
fails "Array#== does not call #to_ary on Array subclasses"
15+
fails "Array#flatten returns subclass instance for Array subclasses"
16+
fails "Array#initialize with (array) does not call #to_ary on instances of Array or subclasses of Array"
17+
fails "Array#initialize is called on subclasses"
18+
fails "Array#* with an integer with a subclass of Array returns a subclass instance"
19+
fails "Array.new with (array) does not call #to_ary on instances of Array or subclasses of Array"
20+
fails "Array#replace does not call #to_ary on Array subclasses"
21+
fails "Array#slice with a subclass of Array returns a subclass instance with [-n...-m]"
22+
fails "Array#slice with a subclass of Array returns a subclass instance with [-n..-m]"
23+
fails "Array#slice with a subclass of Array returns a subclass instance with [n...m]"
24+
fails "Array#slice with a subclass of Array returns a subclass instance with [n..m]"
25+
fails "Array#slice with a subclass of Array returns a subclass instance with [-n, m]"
26+
fails "Array#slice with a subclass of Array returns a subclass instance with [n, m]"
627
end

0 commit comments

Comments
 (0)
Please sign in to comment.