diff options
Diffstat (limited to 'src/test/java/com/google')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java | 34 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java | 110 |
2 files changed, 126 insertions, 18 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java index b70e1f6819..ea81adb35d 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java @@ -1028,8 +1028,8 @@ public class MethodLibraryTest extends EvaluationTestCase { @Test public void testListSlice_WrongType() throws Exception { new BothModesTest() - .testIfExactError("'a' is not a valid int", "'123'['a'::]") - .testIfExactError("'b' is not a valid int", "'123'[:'b':]"); + .testIfExactError("Slice start must be an integer, not 'a'", "'123'['a'::]") + .testIfExactError("Slice end must be an integer, not 'b'", "'123'[:'b':]"); } @Test @@ -1074,7 +1074,7 @@ public class MethodLibraryTest extends EvaluationTestCase { @Test public void testListSliceStep_InvalidStep() throws Exception { - String msg = "slice step cannot be zero"; + String msg = "Slice step cannot be zero"; new BothModesTest() .testIfExactError(msg, "[1, 2, 3][::0]") .testIfExactError(msg, "[1, 2, 3][1::0]") @@ -1098,7 +1098,7 @@ public class MethodLibraryTest extends EvaluationTestCase { .testEval("(1, 2, 3, 4, 5)[3:1:-1]", "(4, 3)") .testEval("(1, 2, 3, 4, 5)[::-2]", "(5, 3, 1)") .testEval("(1, 2, 3, 4, 5)[::-10]", "(5,)") - .testIfExactError("slice step cannot be zero", "(1, 2, 3)[1::0]"); + .testIfExactError("Slice step cannot be zero", "(1, 2, 3)[1::0]"); } @Test @@ -1145,7 +1145,7 @@ public class MethodLibraryTest extends EvaluationTestCase { public void testListAccessBadIndex() throws Exception { new BothModesTest() .testIfErrorContains( - "List indices must be integers, not string", + "Indices must be integers, not string", "[[1], [2]]['a']"); } @@ -1177,9 +1177,9 @@ public class MethodLibraryTest extends EvaluationTestCase { @Test public void testStringIndexingOutOfRange() throws Exception { new BothModesTest() - .testIfErrorContains("List index out of range", "'abcdef'[10]") - .testIfErrorContains("List index out of range", "'abcdef'[-11]") - .testIfErrorContains("List index out of range", "'abcdef'[42]"); + .testIfErrorContains("Index out of range", "'abcdef'[10]") + .testIfErrorContains("Index out of range", "'abcdef'[-11]") + .testIfErrorContains("Index out of range", "'abcdef'[42]"); } @Test @@ -1196,8 +1196,8 @@ public class MethodLibraryTest extends EvaluationTestCase { @Test public void testStringSlice_WrongType() throws Exception { new BothModesTest() - .testIfExactError("'a' is not a valid int", "'123'['a'::]") - .testIfExactError("'b' is not a valid int", "'123'[:'b':]"); + .testIfExactError("Slice start must be an integer, not 'a'", "'123'['a'::]") + .testIfExactError("Slice end must be an integer, not 'b'", "'123'[:'b':]"); } @Test @@ -1242,7 +1242,7 @@ public class MethodLibraryTest extends EvaluationTestCase { @Test public void testStringSliceStep_InvalidStep() throws Exception { - String msg = "slice step cannot be zero"; + String msg = "Slice step cannot be zero"; new BothModesTest() .testIfExactError(msg, "'123'[::0]") .testIfExactError(msg, "'123'[1::0]") @@ -1484,15 +1484,15 @@ public class MethodLibraryTest extends EvaluationTestCase { public void testListIndexOutOfRange() throws Exception { new BothModesTest() .testIfErrorContains( - "List index out of range (index is 3, but list has 3 elements)", "[0, 1, 2][3]") + "Index out of range (index is 3, but sequence has 3 elements)", "[0, 1, 2][3]") .testIfErrorContains( - "List index out of range (index is -4, but list has 3 elements)", "[0, 1, 2][-4]") + "Index out of range (index is -4, but sequence has 3 elements)", "[0, 1, 2][-4]") .testIfErrorContains( - "List index out of range (index is -2, but list has 1 elements)", "[0][-2]") + "Index out of range (index is -2, but sequence has 1 elements)", "[0][-2]") .testIfErrorContains( - "List index out of range (index is 1, but list has 1 elements)", "[0][1]") + "Index out of range (index is 1, but sequence has 1 elements)", "[0][1]") .testIfErrorContains( - "List index out of range (index is 1, but list has 0 elements)", "[][1]"); + "Index out of range (index is 1, but sequence has 0 elements)", "[][1]"); } @Test @@ -1611,7 +1611,7 @@ public class MethodLibraryTest extends EvaluationTestCase { .testLookup("ret", 3); new BothModesTest() .testIfErrorContains( - "List index out of range (index is 3, but list has 2 elements)", "[1, 2].pop(3)"); + "Index out of range (index is 3, but sequence has 2 elements)", "[1, 2].pop(3)"); new BothModesTest().testIfErrorContains("Type tuple has no function pop()", "(1, 2).pop()"); } diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java index 00a4d14d3e..d6fa9fdab8 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java @@ -29,11 +29,119 @@ import org.junit.runners.JUnit4; public class SkylarkListTest extends EvaluationTestCase { @Test - public void testListIndex() throws Exception { + public void testIndex() throws Exception { eval("l = [1, '2', 3]"); assertThat(eval("l[0]")).isEqualTo(1); assertThat(eval("l[1]")).isEqualTo("2"); assertThat(eval("l[2]")).isEqualTo(3); + + eval("t = (1, '2', 3)"); + assertThat(eval("t[0]")).isEqualTo(1); + assertThat(eval("t[1]")).isEqualTo("2"); + assertThat(eval("t[2]")).isEqualTo(3); + } + + @Test + public void testIndexOutOfBounds() throws Exception { + checkEvalError("Index out of range (index is 3, but sequence has 3 elements)", + "['a', 'b', 'c'][3]"); + checkEvalError("Index out of range (index is 10, but sequence has 3 elements)", + "['a', 'b', 'c'][10]"); + checkEvalError("Index out of range (index is 0, but sequence has 0 elements)", + "[][0]"); + } + + @Test + public void testNegativeIndices() throws Exception { + eval("l = ['a', 'b', 'c']"); + assertThat(eval("l[0]")).isEqualTo("a"); + assertThat(eval("l[-1]")).isEqualTo("c"); + assertThat(eval("l[-2]")).isEqualTo("b"); + assertThat(eval("l[-3]")).isEqualTo("a"); + checkEvalError("Index out of range (index is -4, but sequence has 3 elements)", + "l[-4]"); + checkEvalError("Index out of range (index is -1, but sequence has 0 elements)", + "[][-1]"); + } + + @SuppressWarnings("unchecked") + private SkylarkList<Object> listEval(String... input) throws Exception { + return (SkylarkList<Object>) eval(input); + } + + @Test + public void testSlice() throws Exception { + eval("l = ['a', 'b', 'c']"); + assertThat(listEval("l[0:3]")).containsExactly("a", "b", "c").inOrder(); + assertThat(listEval("l[0:2]")).containsExactly("a", "b").inOrder(); + assertThat(listEval("l[0:1]")).containsExactly("a").inOrder(); + assertThat(listEval("l[0:0]")).isEmpty(); + assertThat(listEval("l[1:3]")).containsExactly("b", "c").inOrder(); + assertThat(listEval("l[2:3]")).containsExactly("c").inOrder(); + assertThat(listEval("l[3:3]")).isEmpty(); + assertThat(listEval("l[2:1]")).isEmpty(); + assertThat(listEval("l[3:0]")).isEmpty(); + + eval("t = ('a', 'b', 'c')"); + assertThat(listEval("t[0:3]")).containsExactly("a", "b", "c").inOrder(); + assertThat(listEval("t[1:2]")).containsExactly("b").inOrder(); + } + + @Test + public void testSliceDefault() throws Exception { + eval("l = ['a', 'b', 'c']"); + assertThat(listEval("l[:]")).containsExactly("a", "b", "c").inOrder(); + assertThat(listEval("l[:2]")).containsExactly("a", "b").inOrder(); + assertThat(listEval("l[2:]")).containsExactly("c").inOrder(); + } + + @Test + public void testSliceNegative() throws Exception { + eval("l = ['a', 'b', 'c']"); + assertThat(listEval("l[-2:-1]")).containsExactly("b").inOrder(); + assertThat(listEval("l[-2:]")).containsExactly("b", "c").inOrder(); + assertThat(listEval("l[0:-1]")).containsExactly("a", "b").inOrder(); + assertThat(listEval("l[-1:1]")).isEmpty(); + } + + @Test + public void testSliceBounds() throws Exception { + eval("l = ['a', 'b', 'c']"); + assertThat(listEval("l[0:5]")).containsExactly("a", "b", "c").inOrder(); + assertThat(listEval("l[-10:2]")).containsExactly("a", "b").inOrder(); + assertThat(listEval("l[3:10]")).isEmpty(); + assertThat(listEval("l[-10:-9]")).isEmpty(); + } + + @Test + public void testSliceSkip() throws Exception { + eval("l = ['a', 'b', 'c', 'd', 'e', 'f', 'g']"); + assertThat(listEval("l[0:6:2]")).containsExactly("a", "c", "e").inOrder(); + assertThat(listEval("l[0:7:2]")).containsExactly("a", "c", "e", "g").inOrder(); + assertThat(listEval("l[0:10:2]")).containsExactly("a", "c", "e", "g").inOrder(); + assertThat(listEval("l[-6:10:2]")).containsExactly("b", "d", "f").inOrder(); + assertThat(listEval("l[1:5:3]")).containsExactly("b", "e").inOrder(); + assertThat(listEval("l[-10:3:2]")).containsExactly("a", "c").inOrder(); + assertThat(listEval("l[-10:10:1]")).containsExactly( + "a", "b", "c", "d", "e", "f", "g").inOrder(); + } + + @Test + public void testSliceNegativeSkip() throws Exception { + eval("l = ['a', 'b', 'c', 'd', 'e', 'f', 'g']"); + assertThat(listEval("l[5:2:-1]")).containsExactly("f", "e", "d").inOrder(); + assertThat(listEval("l[5:2:-2]")).containsExactly("f", "d").inOrder(); + assertThat(listEval("l[5:3:-2]")).containsExactly("f").inOrder(); + assertThat(listEval("l[6::-4]")).containsExactly("g", "c").inOrder(); + assertThat(listEval("l[7::-4]")).containsExactly("g", "c").inOrder(); + assertThat(listEval("l[-1::-4]")).containsExactly("g", "c").inOrder(); + assertThat(listEval("l[-1:-10:-4]")).containsExactly("g", "c").inOrder(); + assertThat(listEval("l[-1:-3:-4]")).containsExactly("g").inOrder(); + assertThat(listEval("l[2:5:-1]")).isEmpty(); + assertThat(listEval("l[-10:5:-1]")).isEmpty(); + assertThat(listEval("l[1:-8:-1]")).containsExactly("b", "a").inOrder(); + + checkEvalError("Slice step cannot be zero", "l[2:5:0]"); } @Test |