Better argument validation for core methods.

This commit is contained in:
Bob Nystrom
2014-01-03 10:47:26 -08:00
parent a89386b4e8
commit 058c2606b0
23 changed files with 58 additions and 87 deletions

View File

@ -23,10 +23,5 @@ var f = [1, 2, 3]
f.removeAt(-1)
IO.write(f) // expect: [1, 2]
// Out of bounds.
// TODO: Signal error in better way.
IO.write([1, 2, 3].removeAt(3)) // expect: null
IO.write([1, 2, 3].removeAt(-4)) // expect: null
// Return the removed value.
IO.write([3, 4, 5].removeAt(1)) // expect: 4

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a.removeAt(1.5) // expect runtime error: Index must be an integer.

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a.removeAt("2") // expect runtime error: Index must be a number.

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a.removeAt(4) // expect runtime error: Index out of bounds.

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a.removeAt(-5) // expect runtime error: Index out of bounds.

View File

@ -10,16 +10,3 @@ IO.write(list[-4]) // expect: a
IO.write(list[-3]) // expect: b
IO.write(list[-2]) // expect: c
IO.write(list[-1]) // expect: d
// Handle out of bounds.
// TODO: Should halt the fiber or raise an error somehow.
IO.write(list[4]) // expect: null
IO.write(list[-5]) // expect: null
// Handle wrong argument type.
// TODO: Should halt the fiber or raise an error somehow.
IO.write(list[true]) // expect: null
// Handle non-integer index.
// TODO: Should halt the fiber or raise an error somehow.
IO.write(list[1.5]) // expect: null

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a[1.5] // expect runtime error: Subscript must be an integer.

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a["2"] // expect runtime error: Subscript must be a number.

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a[1.5] = 1 // expect runtime error: Subscript must be an integer.

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a["2"] = 1 // expect runtime error: Subscript must be a number.

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a[4] = 1 // expect runtime error: Subscript out of bounds.

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a[-5] = 1 // expect runtime error: Subscript out of bounds.

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a[4] // expect runtime error: Subscript out of bounds.

View File

@ -0,0 +1,2 @@
var a = [1, 2, 3]
a[-5] // expect runtime error: Subscript out of bounds.

View File

@ -4,5 +4,3 @@ IO.write("something".contains("meth")) // expect: true
IO.write("something".contains("some")) // expect: true
IO.write("something".contains("ing")) // expect: true
IO.write("something".contains("math")) // expect: false
// TODO: Passing non-string as argument.

View File

@ -0,0 +1 @@
"foo".contains(1) // expect runtime error: Argument must be a string.

View File

@ -9,16 +9,3 @@ IO.write("abcd"[-4]) // expect: a
IO.write("abcd"[-3]) // expect: b
IO.write("abcd"[-2]) // expect: c
IO.write("abcd"[-1]) // expect: d
// Handle out of bounds.
// TODO: Should halt the fiber or raise an error somehow.
IO.write("abcd"[4]) // expect: null
IO.write("abcd"[-5]) // expect: null
// Handle wrong argument type.
// TODO: Should halt the fiber or raise an error somehow.
IO.write("abcd"[true]) // expect: null
// Handle non-integer index.
// TODO: Should halt the fiber or raise an error somehow.
IO.write("abcd"[1.5]) // expect: null

View File

@ -0,0 +1,2 @@
var a = "123"
a[1.5] // expect runtime error: Subscript must be an integer.

View File

@ -0,0 +1,2 @@
var a = "123"
a["2"] // expect runtime error: Subscript must be a number.

View File

@ -0,0 +1,2 @@
var a = "123"
a[4] // expect runtime error: Subscript out of bounds.

View File

@ -0,0 +1,2 @@
var a = "123"
a[-5] // expect runtime error: Subscript out of bounds.