Build language: Fix evaluation of nested list comprehensions.
--
MOS_MIGRATED_REVID=93869549
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java
index 9d83155..b410fa0 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java
@@ -362,6 +362,21 @@
}
@Test
+ public void testNestedListComprehensions() throws Exception {
+ assertThat((Iterable<?>) eval(
+ "li = [[1, 2], [3, 4]]\n"
+ + "[j for i in li for j in i]"))
+ .containsExactly(1, 2, 3, 4).inOrder();
+
+ assertThat((Iterable<?>) eval(
+ "input = [['abc'], ['def', 'ghi']]\n"
+ + "['%s %s' % (b, c) for a in input for b in a for c in b]"))
+ .containsExactly(
+ "abc a", "abc b", "abc c", "def d", "def e", "def f", "ghi g", "ghi h", "ghi i")
+ .inOrder();
+ }
+
+ @Test
public void testListComprehensionsMultipleVariables() throws Exception {
assertThat(eval("[x + y for x, y in [(1, 2), (3, 4)]]").toString())
.isEqualTo("[3, 7]");
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java b/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
index 613a648..e5d4815 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
@@ -605,20 +605,32 @@
}
@Test
+ public void testListComprehensionEmptyList() throws Exception {
+ List<ListComprehension.Clause> clauses = ((ListComprehension) parseExpression(
+ "['foo/%s.java' % x for x in []]")).getClauses();
+ assertThat(clauses).hasSize(1);
+ assertThat(clauses.get(0).getExpression().toString()).isEqualTo("[]");
+ assertThat(clauses.get(0).getLValue().getExpression().toString()).isEqualTo("x");
+ }
+
+ @Test
public void testListComprehension() throws Exception {
- ListComprehension list =
- (ListComprehension) parseExpression(
- "['foo/%s.java' % x "
- + "for x in []]");
- assertThat(list.getLists()).hasSize(1);
+ List<ListComprehension.Clause> clauses = ((ListComprehension) parseExpression(
+ "['foo/%s.java' % x for x in ['bar', 'wiz', 'quux']]")).getClauses();
+ assertThat(clauses).hasSize(1);
+ assertThat(clauses.get(0).getLValue().getExpression().toString()).isEqualTo("x");
+ assertThat(clauses.get(0).getExpression()).isInstanceOf(ListLiteral.class);
+ }
- list = (ListComprehension) parseExpression("['foo/%s.java' % x "
- + "for x in ['bar', 'wiz', 'quux']]");
- assertThat(list.getLists()).hasSize(1);
-
- list = (ListComprehension) parseExpression("['%s/%s.java' % (x, y) "
- + "for x in ['foo', 'bar'] for y in ['baz', 'wiz', 'quux']]");
- assertThat(list.getLists()).hasSize(2);
+ @Test
+ public void testForForListComprehension() throws Exception {
+ List<ListComprehension.Clause> clauses = ((ListComprehension) parseExpression(
+ "['%s/%s.java' % (x, y) for x in ['foo', 'bar'] for y in list]")).getClauses();
+ assertThat(clauses).hasSize(2);
+ assertThat(clauses.get(0).getLValue().getExpression().toString()).isEqualTo("x");
+ assertThat(clauses.get(0).getExpression()).isInstanceOf(ListLiteral.class);
+ assertThat(clauses.get(1).getLValue().getExpression().toString()).isEqualTo("y");
+ assertThat(clauses.get(1).getExpression()).isInstanceOf(Ident.class);
}
@Test