Skip to content

Commit c887648

Browse files
committedDec 25, 2013
Compliancy fixes for Array#shift
1 parent 100adab commit c887648

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed
 

‎opal/corelib/array.rb

+13-6
Original file line numberDiff line numberDiff line change
@@ -1237,13 +1237,20 @@ def select!(&block)
12371237
end
12381238

12391239
def shift(count = undefined)
1240-
%x{
1241-
if (self.length === 0) {
1242-
return nil;
1243-
}
1240+
if `count === undefined`
1241+
return if `self.length === 0`
1242+
return `self.shift()`
1243+
end
12441244

1245-
return count == null ? self.shift() : self.splice(0, count)
1246-
}
1245+
count = Opal.coerce_to count, Integer, :to_int
1246+
1247+
if `count < 0`
1248+
raise ArgumentError, 'negative array size'
1249+
end
1250+
1251+
return [] if `self.length === 0`
1252+
1253+
`self.splice(0, count)`
12471254
end
12481255

12491256
alias size length

‎spec/opal/filters/bugs/arity_check.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
opal_filter "Wrong arity check" do
2+
fails "Array#shift passed a number n as an argument raises an ArgumentError if more arguments are passed"
3+
end

‎spec/opal/filters/bugs/array.rb

-6
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,6 @@
172172
fails "Array#sample does not return the same value if the Array has unique values"
173173
fails "Array#sample returns at most the number of elements in the Array"
174174

175-
fails "Array#shift passed a number n as an argument raises an ArgumentError if more arguments are passed"
176-
fails "Array#shift passed a number n as an argument raises a TypeError when the passed n can be coerced to Integer"
177-
fails "Array#shift passed a number n as an argument tries to convert n to an Integer using #to_int"
178-
fails "Array#shift passed a number n as an argument raises an ArgumentError if n is negative"
179-
fails "Array#shift passed a number n as an argument returns a new empty array if there are no more elements"
180-
181175
fails "Array#shuffle uses given random generator"
182176
fails "Array#shuffle uses default random generator"
183177
fails "Array#shuffle attempts coercion via #to_hash"

0 commit comments

Comments
 (0)
Please sign in to comment.