Skip to content

Commit

Permalink
Fix rubyspecs for Array#[]= accepting index or index, size
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Sep 20, 2013
1 parent 5ec2bdf commit 087cc6a
Show file tree
Hide file tree
Showing 4 changed files with 553 additions and 1 deletion.
29 changes: 28 additions & 1 deletion corelib/array.rb
Expand Up @@ -284,14 +284,41 @@ def [](index, length = undefined)
}
end

def []=(index, value)
def []=(index, value, extra = undefined)
%x{
var size = #{self}.length;
if (index < 0) {
index += size;
}
if (extra != null) {
if (value < 0) {
#{raise IndexError};
}
if (index > size) {
for (var i = size; index > i; i++) {
#{self}[i] = nil;
}
}
if (extra._isArray) {
#{self}.splice.apply(#{self}, [index, value].concat(extra));
}
else {
#{self}.splice(index, value, extra);
}
return extra;
}
if (index > size) {
for (var i = size; i < index; i++) {
#{self}[i] = nil;
}
}
return #{self}[index] = value;
}
end
Expand Down
25 changes: 25 additions & 0 deletions spec/filters/bugs/array/element_set.rb
@@ -0,0 +1,25 @@
opal_filter "Array#[]=" do
fails "Array#[]= does not call to_ary on rhs array subclasses for multi-element sets"
fails "Array#[]= calls to_ary on its rhs argument for multi-element sets"
fails "Array#[]= raises an IndexError when passed indexes out of bounds"
fails "Array#[]= tries to convert Range elements to Integers using #to_int with [m..n] and [m...n]"

fails "Array#[]= with [m..n] accepts Range subclasses"
fails "Array#[]= with [m..n] inserts the other section at m if m > n"
fails "Array#[]= with [m..n] replaces the section if m < 0 and n > 0"
fails "Array#[]= with [m..n] replaces the section if m and n < 0"
fails "Array#[]= with [m..n] replaces the section defined by range"
fails "Array#[]= with [m..n] just sets the section defined by range to nil if m and n < 0 and the rhs is nil"
fails "Array#[]= with [m..n] just sets the section defined by range to nil even if the rhs is nil"

fails "Array#[]= just inserts nil if the section defined by range has negative width and the rhs is nil"
fails "Array#[]= just inserts nil if the section defined by range is zero-width and the rhs is nil"
fails "Array#[]= inserts the given elements with [range] which the range has negative width"
fails "Array#[]= inserts the given elements with [range] which the range is zero-width"
fails "Array#[]= sets elements in the range arguments when passed ranges"
fails "Array#[]= checks frozen before attempting to coerce arguments"
fails "Array#[]= calls to_int on its start and length arguments"
fails "Array#[]= just sets the section defined by range to other even if other is nil"
fails "Array#[]= replaces the section defined by range with the given values"
fails "Array#[]= sets the section defined by range to other"
end
1 change: 1 addition & 0 deletions spec/filters/unsupported/frozen.rb
Expand Up @@ -29,4 +29,5 @@
fails "Array#compact! raises a RuntimeError on a frozen array"
fails "Array#clear raises a RuntimeError on a frozen array"
fails "Array#<< raises a RuntimeError on a frozen array"
fails "Array#[]= raises a RuntimeError on a frozen array"
end

0 comments on commit 087cc6a

Please sign in to comment.