Skip to content

Commit

Permalink
Added validations to list.pop (issue #854)
Browse files Browse the repository at this point in the history
Added more autotests for list.pop and list.remove
  • Loading branch information
JennaSys committed Jul 28, 2024
1 parent 3021550 commit 240738e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,37 @@ def aTest6(test_list):
def aTest7(test_list):
test_list[7:0:0] = ['x', 'y', 'z'] # ValueError: slice step cannot be zero
autoTester.check('zero step slice', autoTester.expectException(lambda: aTest7(aList)))

aList = ['a', 'b', 'c']
aList.remove('b')
autoTester.check(aList)
aList.remove('a')
autoTester.check(aList)
autoTester.check('not in list', autoTester.expectException(lambda: aList.remove('d')))
autoTester.check(aList)
aList.remove('c')
autoTester.check(aList)
autoTester.check('not in list', autoTester.expectException(lambda: aList.remove('c')))
autoTester.check(aList)

aList = ['a', 'b', 'c', 'd', 'e', 'f']
aList.pop(2)
autoTester.check(aList)
aList.pop(0)
autoTester.check(aList)
aList.pop(-3)
autoTester.check(aList)
aList.pop(-1)
autoTester.check(aList)
autoTester.check('out of range', autoTester.expectException(lambda: aList.pop(-3)))
autoTester.check('out of range', autoTester.expectException(lambda: aList.pop(3)))
aList.pop()
autoTester.check(aList)
aList.pop()
autoTester.check(aList)
autoTester.check('empty list', autoTester.expectException(lambda: aList.pop()))

# Check pop of empty list (issue 854)
autoTester.check('empty list', autoTester.expectException(lambda: aList.pop(-1)))
autoTester.check('empty list', autoTester.expectException(lambda: aList.pop(0)))
autoTester.check('empty list', autoTester.expectException(lambda: aList.pop(1)))
15 changes: 11 additions & 4 deletions transcrypt/modules/org/transcrypt/__builtin__.js
Original file line number Diff line number Diff line change
Expand Up @@ -1150,8 +1150,8 @@ Array.prototype.insert = function (index, element) {

Array.prototype.remove = function (element) {
let index = this.indexOf (element);
if (index == -1) {
throw ValueError ("list.remove(x): x not in list", new Error ());
if (index === -1) {
throw ValueError("list.remove(x): x not in list", new Error ());
}
this.splice (index, 1);
};
Expand All @@ -1161,11 +1161,18 @@ Array.prototype.index = function (element) {
};

Array.prototype.py_pop = function (index) {
if (index == undefined) {
if(this.length === 0){
throw IndexError("pop from empty list", new Error())
}
if (index === undefined) {
return this.pop (); // Remove last element
}
else {
return this.splice (index, 1) [0];
const idx = index < 0 ? this.length + index : index
if(this[idx] === undefined){
throw IndexError("pop index out of range", new Error())
}
return this.splice (idx, 1) [0];
}
};

Expand Down

0 comments on commit 240738e

Please sign in to comment.