From 25da19da81e9eaf06632349ad41ef9910940e33f Mon Sep 17 00:00:00 2001 From: vladmos Date: Thu, 4 May 2017 18:00:59 +0200 Subject: Implement a flag for extend-like behavior of the `+=` operator for lists Usage: --incompatible_list_plus_equals=true (the default value is false). PiperOrigin-RevId: 155084916 --- .../build/lib/syntax/SkylarkEvaluationTest.java | 67 ++++++++++++++++++++++ .../build/lib/syntax/util/EvaluationTestCase.java | 21 ++++--- 2 files changed, 80 insertions(+), 8 deletions(-) (limited to 'src/test/java/com/google/devtools/build') diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java index 848498b229..a71f656070 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java @@ -986,6 +986,73 @@ public class SkylarkEvaluationTest extends EvaluationTest { .testIfExactError("object of type 'string' has no field 'field'", "x = 'a'.field"); } + @Test + public void testPlusEqualsOnListCopying() throws Exception { + new SkylarkTest("--incompatible_list_plus_equals=false") + .setUp( + "def func():", + " l1 = [1, 2]", + " l2 = l1", + " l2 += [3, 4]", + " return l1, l2", + "lists = str(func())") + .testLookup("lists", "([1, 2], [1, 2, 3, 4])"); + } + + @Test + public void testPlusEqualsOnListMutating() throws Exception { + new SkylarkTest("--incompatible_list_plus_equals=true") + .setUp( + "def func():", + " l1 = [1, 2]", + " l2 = l1", + " l2 += [3, 4]", + " return l1, l2", + "lists = str(func())") + .testLookup("lists", "([1, 2, 3, 4], [1, 2, 3, 4])"); + + // The same but with += after an IndexExpression + new SkylarkTest("--incompatible_list_plus_equals=true") + .setUp( + "def func():", + " l = [1, 2]", + " d = {0: l}", + " d[0] += [3, 4]", + " return l, d[0]", + "lists = str(func())") + .testLookup("lists", "([1, 2, 3, 4], [1, 2, 3, 4])"); + } + + @Test + public void testPlusEqualsOnTuple() throws Exception { + new SkylarkTest("--incompatible_list_plus_equals=false") + .setUp( + "def func():", + " t1 = (1, 2)", + " t2 = t1", + " t2 += (3, 4)", + " return t1, t2", + "tuples = func()") + .testLookup("tuples", SkylarkList.Tuple.of( + SkylarkList.Tuple.of(1, 2), + SkylarkList.Tuple.of(1, 2, 3, 4) + )); + + // This behavior should remain the same regardless of the incompatible_list_plus_equals flag + new SkylarkTest("--incompatible_list_plus_equals=true") + .setUp( + "def func():", + " t1 = (1, 2)", + " t2 = t1", + " t2 += (3, 4)", + " return t1, t2", + "tuples = func()") + .testLookup("tuples", SkylarkList.Tuple.of( + SkylarkList.Tuple.of(1, 2), + SkylarkList.Tuple.of(1, 2, 3, 4) + )); + } + @Test public void testPlusEqualsOnDict() throws Exception { new SkylarkTest().setUp("def func():", diff --git a/src/test/java/com/google/devtools/build/lib/syntax/util/EvaluationTestCase.java b/src/test/java/com/google/devtools/build/lib/syntax/util/EvaluationTestCase.java index 0aa9b0a1e4..33ce2d8d09 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/util/EvaluationTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/util/EvaluationTestCase.java @@ -115,20 +115,21 @@ public class EvaluationTestCase { /** * Sets the specified {@code TestMode} and tries to create the appropriate {@code Environment} + * * @param testMode * @throws Exception */ - protected void setMode(TestMode testMode) throws Exception { + protected void setMode(TestMode testMode, String... skylarkOptions) throws Exception { this.testMode = testMode; - env = newEnvironment(); + env = newEnvironmentWithSkylarkOptions(skylarkOptions); } - protected void enableSkylarkMode() throws Exception { - setMode(TestMode.SKYLARK); + protected void enableSkylarkMode(String... skylarkOptions) throws Exception { + setMode(TestMode.SKYLARK, skylarkOptions); } - protected void enableBuildMode() throws Exception { - setMode(TestMode.BUILD); + protected void enableBuildMode(String... skylarkOptions) throws Exception { + setMode(TestMode.BUILD, skylarkOptions); } public EventHandler getEventHandler() { @@ -560,11 +561,15 @@ public class EvaluationTestCase { * A class that runs all tests in Skylark mode */ protected class SkylarkTest extends ModalTestCase { - public SkylarkTest() {} + private final String[] skylarkOptions; + + public SkylarkTest(String... skylarkOptions) { + this.skylarkOptions = skylarkOptions; + } @Override protected void run(Testable testable) throws Exception { - enableSkylarkMode(); + enableSkylarkMode(skylarkOptions); testable.run(); } } -- cgit v1.2.3