diff options
author | 2017-02-21 15:30:54 +0000 | |
---|---|---|
committer | 2017-02-21 16:09:02 +0000 | |
commit | 23ba4a8416e3de077ff85cf59b95ccefe8ef3892 (patch) | |
tree | bb8efc9ecfbdc54d56c4023b69d167cc532fabfe /src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java | |
parent | 3da16e3b4e8be4d4c4a6eae80365a85851e3253d (diff) |
Augmented assignments don't evaluate lhs multiple times
--
PiperOrigin-RevId: 148092963
MOS_MIGRATED_REVID=148092963
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java | 43 |
1 files changed, 43 insertions, 0 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 0a701e0e5c..6353c1270e 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 @@ -909,6 +909,49 @@ public class SkylarkEvaluationTest extends EvaluationTest { } @Test + public void testAugmentedAssignmentHasNoSideEffects() throws Exception { + new SkylarkTest().setUp( + "counter = [0]", + "value = [1, 2]", + "", + "def f():", + " counter[0] = counter[0] + 1", + " return value", + "", + "f()[1] += 1") // `f()` should be called only once here + .testLookup("counter", MutableList.of(env, 1)); + } + + @Test + public void testAugmentedAssignmentNotAllowedForListLiterals() throws Exception { + new SkylarkTest().testIfErrorContains("Cannot perform augment assignment on a list literal", + "def f(a, b):", + " [a, b] += []", + "f(1, 2)"); + } + + @Test + public void testAssignmentEvaluationOrder() throws Exception { + new SkylarkTest().setUp( + "ordinary = []", + "augmented = []", + "value = [1, 2]", + "", + "def f(record):", + " record.append('f')", + " return value", + "", + "def g(record):", + " record.append('g')", + " return value", + "", + "f(ordinary)[0] = g(ordinary)[1]", + "f(augmented)[0] += g(augmented)[1]") + .testLookup("ordinary", MutableList.of(env, "g", "f")) // This order is consistent + .testLookup("augmented", MutableList.of(env, "f", "g")); // with Python + } + + @Test public void testStaticDirectJavaCall() throws Exception { new SkylarkTest().update("Mock", Mock.class).setUp("val = Mock.value_of('8')") .testLookup("val", 8); |