mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 14:18:42 +01:00
Better argument validation for core methods.
This commit is contained in:
@ -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
|
||||
|
||||
2
test/list/remove_at_index_not_int.wren
Normal file
2
test/list/remove_at_index_not_int.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a.removeAt(1.5) // expect runtime error: Index must be an integer.
|
||||
2
test/list/remove_at_index_not_num.wren
Normal file
2
test/list/remove_at_index_not_num.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a.removeAt("2") // expect runtime error: Index must be a number.
|
||||
2
test/list/remove_at_index_too_large.wren
Normal file
2
test/list/remove_at_index_too_large.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a.removeAt(4) // expect runtime error: Index out of bounds.
|
||||
2
test/list/remove_at_index_too_small.wren
Normal file
2
test/list/remove_at_index_too_small.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a.removeAt(-5) // expect runtime error: Index out of bounds.
|
||||
@ -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
|
||||
|
||||
2
test/list/subscript_not_int.wren
Normal file
2
test/list/subscript_not_int.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a[1.5] // expect runtime error: Subscript must be an integer.
|
||||
2
test/list/subscript_not_num.wren
Normal file
2
test/list/subscript_not_num.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a["2"] // expect runtime error: Subscript must be a number.
|
||||
2
test/list/subscript_setter_not_int.wren
Normal file
2
test/list/subscript_setter_not_int.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a[1.5] = 1 // expect runtime error: Subscript must be an integer.
|
||||
2
test/list/subscript_setter_not_num.wren
Normal file
2
test/list/subscript_setter_not_num.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a["2"] = 1 // expect runtime error: Subscript must be a number.
|
||||
2
test/list/subscript_setter_too_large.wren
Normal file
2
test/list/subscript_setter_too_large.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a[4] = 1 // expect runtime error: Subscript out of bounds.
|
||||
2
test/list/subscript_setter_too_small.wren
Normal file
2
test/list/subscript_setter_too_small.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a[-5] = 1 // expect runtime error: Subscript out of bounds.
|
||||
2
test/list/subscript_too_large.wren
Normal file
2
test/list/subscript_too_large.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a[4] // expect runtime error: Subscript out of bounds.
|
||||
2
test/list/subscript_too_small.wren
Normal file
2
test/list/subscript_too_small.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = [1, 2, 3]
|
||||
a[-5] // expect runtime error: Subscript out of bounds.
|
||||
@ -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.
|
||||
|
||||
1
test/string/contains_argument_not_string.wren
Normal file
1
test/string/contains_argument_not_string.wren
Normal file
@ -0,0 +1 @@
|
||||
"foo".contains(1) // expect runtime error: Argument must be a string.
|
||||
@ -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
|
||||
|
||||
2
test/string/subscript_not_int.wren
Normal file
2
test/string/subscript_not_int.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = "123"
|
||||
a[1.5] // expect runtime error: Subscript must be an integer.
|
||||
2
test/string/subscript_not_num.wren
Normal file
2
test/string/subscript_not_num.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = "123"
|
||||
a["2"] // expect runtime error: Subscript must be a number.
|
||||
2
test/string/subscript_too_large.wren
Normal file
2
test/string/subscript_too_large.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = "123"
|
||||
a[4] // expect runtime error: Subscript out of bounds.
|
||||
2
test/string/subscript_too_small.wren
Normal file
2
test/string/subscript_too_small.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a = "123"
|
||||
a[-5] // expect runtime error: Subscript out of bounds.
|
||||
Reference in New Issue
Block a user