Skip to content

Commit

Permalink
Compliancy fixes for Array#shift
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Dec 25, 2013
1 parent 100adab commit c887648
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
19 changes: 13 additions & 6 deletions opal/corelib/array.rb
Expand Up @@ -1237,13 +1237,20 @@ def select!(&block)
end

def shift(count = undefined)
%x{
if (self.length === 0) {
return nil;
}
if `count === undefined`
return if `self.length === 0`
return `self.shift()`
end

return count == null ? self.shift() : self.splice(0, count)
}
count = Opal.coerce_to count, Integer, :to_int

if `count < 0`
raise ArgumentError, 'negative array size'
end

return [] if `self.length === 0`

`self.splice(0, count)`
end

alias size length
Expand Down
3 changes: 3 additions & 0 deletions spec/opal/filters/bugs/arity_check.rb
@@ -0,0 +1,3 @@
opal_filter "Wrong arity check" do
fails "Array#shift passed a number n as an argument raises an ArgumentError if more arguments are passed"
end
6 changes: 0 additions & 6 deletions spec/opal/filters/bugs/array.rb
Expand Up @@ -172,12 +172,6 @@
fails "Array#sample does not return the same value if the Array has unique values"
fails "Array#sample returns at most the number of elements in the Array"

fails "Array#shift passed a number n as an argument raises an ArgumentError if more arguments are passed"
fails "Array#shift passed a number n as an argument raises a TypeError when the passed n can be coerced to Integer"
fails "Array#shift passed a number n as an argument tries to convert n to an Integer using #to_int"
fails "Array#shift passed a number n as an argument raises an ArgumentError if n is negative"
fails "Array#shift passed a number n as an argument returns a new empty array if there are no more elements"

fails "Array#shuffle uses given random generator"
fails "Array#shuffle uses default random generator"
fails "Array#shuffle attempts coercion via #to_hash"
Expand Down

0 comments on commit c887648

Please sign in to comment.