feat: add validation to skip/take and rename split parameter for clarity

Add type and range checks to Sequence.skip() and Sequence.take() methods, aborting with "Count must be a non-negative integer." when count is not a non-negative integer. Rename split() parameter from 'delim' to 'delimiter' and update its error message from "Argument must be a non-empty string." to "Delimiter must be a non-empty string." for consistency. Remove old behavior allowing negative counts and add new test files for skip/take validation errors.
This commit is contained in:
Bob Nystrom
2017-03-15 14:22:44 +00:00
parent 38b92435bf
commit dad494093e
12 changed files with 52 additions and 28 deletions
+3 -6
View File
@@ -15,14 +15,11 @@ var test = TestSequence.new().skip(0)
System.print(test is Sequence) // expect: true
System.print(test) // expect: instance of SkipSequence
// Skipping 0 changes nothing
// Skipping 0 changes nothing.
System.print(test.toList) // expect: [1, 2, 3]
// Skipping 1 works
// Skipping 1 works.
System.print(test.skip(1).toList) // expect: [2, 3]
// Skipping more than length of sequence produces empty list
// Skipping more than length of sequence produces empty list.
System.print(test.skip(4).isEmpty) // expect: true
// Skipping less than 0 changes nothing
System.print(test.skip(-10).toList) // expect: [1, 2, 3]
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].skip(-1) // expect runtime error: Count must be a non-negative integer.
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].skip(1.2) // expect runtime error: Count must be a non-negative integer.
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].skip("s") // expect runtime error: Count must be a non-negative integer.
+3 -6
View File
@@ -15,14 +15,11 @@ var test = TestSequence.new().take(3)
System.print(test is Sequence) // expect: true
System.print(test) // expect: instance of TakeSequence
// Taking 0 produces empty list
// Taking 0 produces empty list.
System.print(test.take(0).isEmpty) // expect: true
// Taking 1 works
// Taking 1 works.
System.print(test.take(1).toList) // expect: [1]
// Taking more than length of sequence produces whole sequence
// Taking more than length of sequence produces whole sequence.
System.print(test.take(4).toList) // expect: [1, 2, 3]
// Taking less than 0 produces empty list
System.print(test.take(-10).isEmpty) // expect: true
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].take(-1) // expect runtime error: Count must be a non-negative integer.
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].take(1.2) // expect runtime error: Count must be a non-negative integer.
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].take("s") // expect runtime error: Count must be a non-negative integer.
@@ -1 +1 @@
"foo".split(1) // expect runtime error: Argument must be a non-empty string.
"foo".split(1) // expect runtime error: Delimiter must be a non-empty string.
+1 -1
View File
@@ -1 +1 @@
"foo".split("") // expect runtime error: Argument must be a non-empty string.
"foo".split("") // expect runtime error: Delimiter must be a non-empty string.