aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build
diff options
context:
space:
mode:
authorGravatar vladmos <vladmos@google.com>2017-05-04 18:00:59 +0200
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2017-05-04 23:05:35 +0200
commit25da19da81e9eaf06632349ad41ef9910940e33f (patch)
treeb95a160f0145bd004e319f02d417a828481227ee /src/test/java/com/google/devtools/build
parent444b86319a901603a1030c484dac9f87501bad9d (diff)
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
Diffstat (limited to 'src/test/java/com/google/devtools/build')
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java67
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/util/EvaluationTestCase.java21
2 files changed, 80 insertions, 8 deletions
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
@@ -987,6 +987,73 @@ public class SkylarkEvaluationTest extends EvaluationTest {
}
@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():",
" d = {'a' : 1}",
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();
}
}