blob: 83a55be2d8af7ff3e487e5d80aa07e92149e0b74 [file] [log] [blame]
Ulf Adams89f012d2015-02-26 13:39:28 +00001// Copyright 2014 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.syntax;
15
16import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +000017import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertFalse;
19import static org.junit.Assert.assertTrue;
Ulf Adams89f012d2015-02-26 13:39:28 +000020
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.ImmutableMap;
Florian Weikert28da3652015-07-01 14:52:30 +000023import com.google.devtools.build.lib.testutil.TestMode;
Ulf Adams89f012d2015-02-26 13:39:28 +000024
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +000025import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
28
Ulf Adams89f012d2015-02-26 13:39:28 +000029import java.util.Arrays;
30import java.util.Collections;
31import java.util.List;
32import java.util.Map;
33
34/**
35 * Test of evaluation behavior. (Implicitly uses lexer + parser.)
36 */
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +000037@RunWith(JUnit4.class)
Francois-Rene Rideau5f3e30c2015-04-10 19:08:39 +000038public class EvaluationTest extends EvaluationTestCase {
Florian Weikert28da3652015-07-01 14:52:30 +000039 public EvaluationTest() throws Exception {
40 super.setMode(TestMode.BUILD);
41 }
Ulf Adams89f012d2015-02-26 13:39:28 +000042
Florian Weikert28da3652015-07-01 14:52:30 +000043 /**
44 * Creates a new instance of {@code ModalTestCase}.
45 *
46 * <p>If a test uses this method, it allows potential subclasses to run the very same test in a
47 * different mode in subclasses
48 */
49 protected ModalTestCase newTest() {
50 return new BuildTest();
Ulf Adams89f012d2015-02-26 13:39:28 +000051 }
52
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +000053 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000054 public void testExprs() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +000055 newTest()
Francois-Rene Rideau0f7ba342015-08-31 16:16:21 +000056 .testStatement("'%sx' % 'foo' + 'bar1'", "fooxbar1")
57 .testStatement("('%sx' % 'foo') + 'bar2'", "fooxbar2")
58 .testStatement("'%sx' % ('foo' + 'bar3')", "foobar3x")
Florian Weikert28da3652015-07-01 14:52:30 +000059 .testStatement("123 + 456", 579)
60 .testStatement("456 - 123", 333)
61 .testStatement("8 % 3", 2)
62 .testIfErrorContains("unsupported operand type(s) for %: 'int' and 'string'", "3 % 'foo'");
Ulf Adams89f012d2015-02-26 13:39:28 +000063 }
64
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +000065 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000066 public void testListExprs() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +000067 newTest().testExactOrder("[1, 2, 3]", 1, 2, 3).testExactOrder("(1, 2, 3)", 1, 2, 3);
Ulf Adams89f012d2015-02-26 13:39:28 +000068 }
69
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +000070 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000071 public void testStringFormatMultipleArgs() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +000072 newTest().testStatement("'%sY%s' % ('X', 'Z')", "XYZ");
Ulf Adams89f012d2015-02-26 13:39:28 +000073 }
74
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +000075 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000076 public void testAndOr() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +000077 new BuildTest()
78 .testStatement("8 or 9", 8)
79 .testStatement("0 or 9", 9)
80 .testStatement("8 and 9", 9)
81 .testStatement("0 and 9", 0)
Ulf Adams89f012d2015-02-26 13:39:28 +000082
Florian Weikert28da3652015-07-01 14:52:30 +000083 .testStatement("1 and 2 or 3", 2)
84 .testStatement("0 and 2 or 3", 3)
85 .testStatement("1 and 0 or 3", 3)
Ulf Adams89f012d2015-02-26 13:39:28 +000086
Florian Weikert28da3652015-07-01 14:52:30 +000087 .testStatement("1 or 2 and 3", 1)
88 .testStatement("0 or 2 and 3", 3)
89 .testStatement("0 or 0 and 3", 0)
90 .testStatement("1 or 0 and 3", 1)
Ulf Adams89f012d2015-02-26 13:39:28 +000091
Francois-Rene Rideau0f7ba342015-08-31 16:16:21 +000092 .testStatement("None and 1", Runtime.NONE)
Florian Weikert28da3652015-07-01 14:52:30 +000093 .testStatement("\"\" or 9", 9)
94 .testStatement("\"abc\" or 9", "abc")
Francois-Rene Rideau0f7ba342015-08-31 16:16:21 +000095
Florian Weikert28da3652015-07-01 14:52:30 +000096 // check that 'foo' is not evaluated
97 .testStatement("8 or foo", 8)
98 .testStatement("0 and foo", 0);
Francois-Rene Rideau5f3e30c2015-04-10 19:08:39 +000099
Florian Weikert28da3652015-07-01 14:52:30 +0000100 new SkylarkTest()
101 .testIfErrorContains("name 'google' is not defined", "0 and google")
102 .testIfErrorContains("name 'google' is not defined", "8 or google");
Ulf Adams89f012d2015-02-26 13:39:28 +0000103 }
104
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000105 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000106 public void testNot() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000107 newTest().testStatement("not 1", false).testStatement("not ''", true);
Ulf Adams89f012d2015-02-26 13:39:28 +0000108 }
109
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000110 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000111 public void testNotWithLogicOperators() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000112 newTest()
113 .testStatement("not (0 and 0)", true)
114 .testStatement("not (1 or 0)", false)
Francois-Rene Rideau5f3e30c2015-04-10 19:08:39 +0000115
Florian Weikert28da3652015-07-01 14:52:30 +0000116 .testStatement("0 and not 0", 0)
117 .testStatement("not 0 and 0", 0)
Ulf Adams89f012d2015-02-26 13:39:28 +0000118
Florian Weikert28da3652015-07-01 14:52:30 +0000119 .testStatement("1 and not 0", true)
120 .testStatement("not 0 or 0", true)
Ulf Adams89f012d2015-02-26 13:39:28 +0000121
Florian Weikert28da3652015-07-01 14:52:30 +0000122 .testStatement("not 1 or 0", 0)
123 .testStatement("not 1 or 1", 1);
Ulf Adams89f012d2015-02-26 13:39:28 +0000124 }
125
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000126 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000127 public void testNotWithArithmeticOperators() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000128 newTest().testStatement("not 0 + 0", true).testStatement("not 2 - 1", false);
Ulf Adams89f012d2015-02-26 13:39:28 +0000129 }
130
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000131 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000132 public void testNotWithCollections() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000133 newTest().testStatement("not []", true).testStatement("not {'a' : 1}", false);
Ulf Adams89f012d2015-02-26 13:39:28 +0000134 }
135
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000136 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000137 public void testEquality() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000138 newTest()
139 .testStatement("1 == 1", true)
140 .testStatement("1 == 2", false)
141 .testStatement("'hello' == 'hel' + 'lo'", true)
142 .testStatement("'hello' == 'bye'", false)
143 .testStatement("None == None", true)
144 .testStatement("[1, 2] == [1, 2]", true)
145 .testStatement("[1, 2] == [2, 1]", false);
Ulf Adams89f012d2015-02-26 13:39:28 +0000146 }
147
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000148 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000149 public void testInequality() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000150 newTest()
151 .testStatement("1 != 1", false)
152 .testStatement("1 != 2", true)
153 .testStatement("'hello' != 'hel' + 'lo'", false)
154 .testStatement("'hello' != 'bye'", true)
155 .testStatement("[1, 2] != [1, 2]", false)
156 .testStatement("[1, 2] != [2, 1]", true);
Ulf Adams89f012d2015-02-26 13:39:28 +0000157 }
158
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000159 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000160 public void testEqualityPrecedence() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000161 newTest()
162 .testStatement("1 + 3 == 2 + 2", true)
163 .testStatement("not 1 == 2", true)
164 .testStatement("not 1 != 2", false)
165 .testStatement("2 and 3 == 3 or 1", true)
166 .testStatement("2 or 3 == 3 and 1", 2);
Ulf Adams89f012d2015-02-26 13:39:28 +0000167 }
168
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000169 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000170 public void testLessThan() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000171 newTest()
172 .testStatement("1 <= 1", true)
173 .testStatement("1 < 1", false)
174 .testStatement("'a' <= 'b'", true)
175 .testStatement("'c' < 'a'", false);
Ulf Adams89f012d2015-02-26 13:39:28 +0000176 }
177
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000178 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000179 public void testGreaterThan() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000180 newTest()
181 .testStatement("1 >= 1", true)
182 .testStatement("1 > 1", false)
183 .testStatement("'a' >= 'b'", false)
184 .testStatement("'c' > 'a'", true);
Ulf Adams89f012d2015-02-26 13:39:28 +0000185 }
186
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000187 @Test
Francois-Rene Rideau6fc5ee72015-03-12 20:55:17 +0000188 public void testConditionalExpressions() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000189 newTest()
190 .testStatement("1 if True else 2", 1)
191 .testStatement("1 if False else 2", 2)
192 .testStatement("1 + 2 if 3 + 4 else 5 + 6", 3);
Francois-Rene Rideau6fc5ee72015-03-12 20:55:17 +0000193
Francois-Rene Rideau5f3e30c2015-04-10 19:08:39 +0000194 setFailFast(false);
195 parseExpression("1 if 2");
196 assertContainsEvent(
Francois-Rene Rideau6fc5ee72015-03-12 20:55:17 +0000197 "missing else clause in conditional expression or semicolon before if");
Francois-Rene Rideau6fc5ee72015-03-12 20:55:17 +0000198 }
199
200 @Test
Laurent Le Brunac8aae82015-04-16 11:42:55 +0000201 public void testListComparison() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000202 newTest()
203 .testStatement("[] < [1]", true)
204 .testStatement("[1] < [1, 1]", true)
205 .testStatement("[1, 1] < [1, 2]", true)
206 .testStatement("[1, 2] < [1, 2, 3]", true)
207 .testStatement("[1, 2, 3] <= [1, 2, 3]", true)
Laurent Le Brunac8aae82015-04-16 11:42:55 +0000208
Florian Weikert28da3652015-07-01 14:52:30 +0000209 .testStatement("['a', 'b'] > ['a']", true)
210 .testStatement("['a', 'b'] >= ['a']", true)
211 .testStatement("['a', 'b'] < ['a']", false)
212 .testStatement("['a', 'b'] <= ['a']", false)
Laurent Le Brunac8aae82015-04-16 11:42:55 +0000213
Florian Weikert28da3652015-07-01 14:52:30 +0000214 .testStatement("('a', 'b') > ('a', 'b')", false)
215 .testStatement("('a', 'b') >= ('a', 'b')", true)
216 .testStatement("('a', 'b') < ('a', 'b')", false)
217 .testStatement("('a', 'b') <= ('a', 'b')", true)
Laurent Le Brunac8aae82015-04-16 11:42:55 +0000218
Florian Weikert28da3652015-07-01 14:52:30 +0000219 .testStatement("[[1, 1]] > [[1, 1], []]", false)
Florian Weikertf31b9472015-08-04 16:36:58 +0000220 .testStatement("[[1, 1]] < [[1, 1], []]", true);
Ulf Adams89f012d2015-02-26 13:39:28 +0000221 }
222
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000223 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000224 public void testSumFunction() throws Exception {
Francois-Rene Rideau76023b92015-04-17 15:31:59 +0000225 BaseFunction sum = new BaseFunction("sum") {
226 @Override
227 public Object call(List<Object> args, Map<String, Object> kwargs,
228 FuncallExpression ast, Environment env) {
229 int sum = 0;
230 for (Object arg : args) {
231 sum += (Integer) arg;
Ulf Adams89f012d2015-02-26 13:39:28 +0000232 }
Francois-Rene Rideau76023b92015-04-17 15:31:59 +0000233 return sum;
234 }
235 };
Ulf Adams89f012d2015-02-26 13:39:28 +0000236
Florian Weikert28da3652015-07-01 14:52:30 +0000237 newTest().update(sum.getName(), sum).testStatement("sum(1, 2, 3, 4, 5, 6)", 21)
238 .testStatement("sum", sum).testStatement("sum(a=1, b=2)", 0);
Francois-Rene Rideau5f3e30c2015-04-10 19:08:39 +0000239 }
Ulf Adams89f012d2015-02-26 13:39:28 +0000240
Francois-Rene Rideau5f3e30c2015-04-10 19:08:39 +0000241 @Test
242 public void testNotCallInt() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000243 newTest().setUp("sum = 123456").testLookup("sum", 123456)
244 .testIfExactError("'int' object is not callable", "sum(1, 2, 3, 4, 5, 6)")
245 .testStatement("sum", 123456);
Ulf Adams89f012d2015-02-26 13:39:28 +0000246 }
247
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000248 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000249 public void testKeywordArgs() throws Exception {
250
Francois-Rene Rideau76023b92015-04-17 15:31:59 +0000251 // This function returns the map of keyword arguments passed to it.
252 BaseFunction kwargs = new BaseFunction("kwargs") {
253 @Override
254 public Object call(List<Object> args,
255 final Map<String, Object> kwargs,
256 FuncallExpression ast,
257 Environment env) {
258 return kwargs;
259 }
260 };
Ulf Adams89f012d2015-02-26 13:39:28 +0000261
Florian Weikert28da3652015-07-01 14:52:30 +0000262 newTest()
263 .update(kwargs.getName(), kwargs)
264 .testEval(
265 "kwargs(foo=1, bar='bar', wiz=[1,2,3]).items()",
266 "[('bar', 'bar'), ('foo', 1), ('wiz', [1, 2, 3])]");
Ulf Adams89f012d2015-02-26 13:39:28 +0000267 }
268
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000269 @Test
Laurent Le Brunbd716742015-04-15 11:05:03 +0000270 public void testModulo() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000271 newTest()
272 .testStatement("6 % 2", 0)
273 .testStatement("6 % 4", 2)
274 .testStatement("3 % 6", 3)
275 .testStatement("7 % -4", -1)
276 .testStatement("-7 % 4", 1)
277 .testStatement("-7 % -4", -3)
278 .testIfExactError("integer modulo by zero", "5 % 0");
Laurent Le Brunbd716742015-04-15 11:05:03 +0000279 }
280
281 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000282 public void testMult() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000283 newTest()
284 .testStatement("6 * 7", 42)
285 .testStatement("3 * 'ab'", "ababab")
286 .testStatement("0 * 'ab'", "")
287 .testStatement("'1' + '0' * 5", "100000");
Ulf Adams89f012d2015-02-26 13:39:28 +0000288 }
289
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000290 @Test
Laurent Le Brun8a528262015-04-15 14:23:35 +0000291 public void testDivision() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000292 newTest()
293 .testStatement("6 / 2", 3)
294 .testStatement("6 / 4", 1)
295 .testStatement("3 / 6", 0)
296 .testStatement("7 / -2", -4)
297 .testStatement("-7 / 2", -4)
298 .testStatement("-7 / -2", 3)
299 .testStatement("2147483647 / 2", 1073741823)
Laurent Le Brun7bda87e2015-08-24 15:13:53 +0000300 .testIfErrorContains("unsupported operand type(s) for /: 'string' and 'int'", "'str' / 2")
Florian Weikert28da3652015-07-01 14:52:30 +0000301 .testIfExactError("integer division by zero", "5 / 0");
Laurent Le Brun8a528262015-04-15 14:23:35 +0000302 }
303
304 @Test
305 public void testOperatorPrecedence() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000306 newTest()
307 .testStatement("2 + 3 * 4", 14)
308 .testStatement("2 + 3 / 4", 2)
309 .testStatement("2 * 3 + 4 / -2", 4);
Laurent Le Brun8a528262015-04-15 14:23:35 +0000310 }
311
312 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000313 public void testConcatStrings() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000314 newTest().testStatement("'foo' + 'bar'", "foobar");
Ulf Adams89f012d2015-02-26 13:39:28 +0000315 }
316
Francois-Rene Rideau5f3e30c2015-04-10 19:08:39 +0000317 @SuppressWarnings("unchecked")
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000318 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000319 public void testConcatLists() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000320 // TODO(fwe): cannot be handled by current testing suite
Ulf Adams89f012d2015-02-26 13:39:28 +0000321 // list
322 Object x = eval("[1,2] + [3,4]");
Francois-Rene Rideau5f3e30c2015-04-10 19:08:39 +0000323 assertThat((Iterable<Object>) x).containsExactly(1, 2, 3, 4).inOrder();
Ulf Adams89f012d2015-02-26 13:39:28 +0000324 assertEquals(Arrays.asList(1, 2, 3, 4), x);
325 assertFalse(EvalUtils.isImmutable(x));
326
327 // tuple
328 x = eval("(1,2) + (3,4)");
329 assertEquals(Arrays.asList(1, 2, 3, 4), x);
330 assertTrue(EvalUtils.isImmutable(x));
331
Francois-Rene Rideau5f3e30c2015-04-10 19:08:39 +0000332 checkEvalError("can only concatenate List (not \"Tuple\") to List",
333 "(1,2) + [3,4]"); // list + tuple
Ulf Adams89f012d2015-02-26 13:39:28 +0000334 }
335
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000336 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000337 public void testListComprehensions() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000338 newTest()
339 .testExactOrder("['foo/%s.java' % x for x in []]")
340 .testExactOrder("['foo/%s.java' % y for y in ['bar', 'wiz', 'quux']]", "foo/bar.java",
341 "foo/wiz.java", "foo/quux.java")
342 .testExactOrder("['%s/%s.java' % (z, t) " + "for z in ['foo', 'bar'] "
343 + "for t in ['baz', 'wiz', 'quux']]",
344 "foo/baz.java",
345 "foo/wiz.java",
346 "foo/quux.java",
347 "bar/baz.java",
348 "bar/wiz.java",
349 "bar/quux.java")
350 .testExactOrder("['%s/%s.java' % (b, b) " + "for a in ['foo', 'bar'] "
351 + "for b in ['baz', 'wiz', 'quux']]",
352 "baz/baz.java",
353 "wiz/wiz.java",
354 "quux/quux.java",
355 "baz/baz.java",
356 "wiz/wiz.java",
357 "quux/quux.java")
358 .testExactOrder("['%s/%s.%s' % (c, d, e) " + "for c in ['foo', 'bar'] "
359 + "for d in ['baz', 'wiz', 'quux'] " + "for e in ['java', 'cc']]",
360 "foo/baz.java",
361 "foo/baz.cc",
362 "foo/wiz.java",
363 "foo/wiz.cc",
364 "foo/quux.java",
365 "foo/quux.cc",
366 "bar/baz.java",
367 "bar/baz.cc",
368 "bar/wiz.java",
369 "bar/wiz.cc",
370 "bar/quux.java",
371 "bar/quux.cc");
Ulf Adams89f012d2015-02-26 13:39:28 +0000372 }
373
Laurent Le Brun741824b2015-03-20 15:10:19 +0000374 @Test
Laurent Le Brun52021662015-05-18 09:28:26 +0000375 public void testNestedListComprehensions() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000376 newTest().testExactOrder("li = [[1, 2], [3, 4]]\n" + "[j for i in li for j in i]", 1, 2,
377 3, 4).testExactOrder("input = [['abc'], ['def', 'ghi']]\n"
378 + "['%s %s' % (b, c) for a in input for b in a for c in b]",
379 "abc a",
380 "abc b",
381 "abc c",
382 "def d",
383 "def e",
384 "def f",
385 "ghi g",
386 "ghi h",
387 "ghi i");
Laurent Le Brun52021662015-05-18 09:28:26 +0000388 }
389
390 @Test
Laurent Le Brun741824b2015-03-20 15:10:19 +0000391 public void testListComprehensionsMultipleVariables() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000392 newTest().testEval("[x + y for x, y in [(1, 2), (3, 4)]]", "[3, 7]").testEval(
393 "[z + t for (z, t) in [[1, 2], [3, 4]]]", "[3, 7]");
Laurent Le Brun741824b2015-03-20 15:10:19 +0000394 }
395
396 @Test
397 public void testListComprehensionsMultipleVariablesFail() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000398 newTest().testIfExactError("lvalue has length 3, but rvalue has has length 2",
399 "[x + y for x, y, z in [(1, 2), (3, 4)]]").testIfExactError(
400 "type 'int' is not a collection", "[x + y for x, y in (1, 2)]");
Laurent Le Brun741824b2015-03-20 15:10:19 +0000401 }
402
403 @Test
Laurent Le Brunb4c54742015-05-18 13:11:05 +0000404 public void testListComprehensionsWithFiltering() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000405 newTest()
406 .setUp("range3 = [0, 1, 2]")
407 .testEval("[a for a in (4, None, 2, None, 1) if a != None]", "[4, 2, 1]")
408 .testEval("[b+c for b in [0, 1, 2] for c in [0, 1, 2] if b + c > 2]", "[3, 3, 4]")
409 .testEval("[d+e for d in range3 if d % 2 == 1 for e in range3]", "[1, 2, 3]")
410 .testEval("[[f,g] for f in [0, 1, 2, 3, 4] if f for g in [5, 6, 7, 8] if f * g % 12 == 0 ]",
411 "[[2, 6], [3, 8], [4, 6]]")
412 .testEval("[h for h in [4, 2, 0, 1] if h]", "[4, 2, 1]");
Laurent Le Brunb4c54742015-05-18 13:11:05 +0000413 }
414
415 @Test
416 public void testListComprehensionDefinitionOrder() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000417 newTest().testIfErrorContains("name 'y' is not defined",
Laurent Le Brunb4c54742015-05-18 13:11:05 +0000418 "[x for x in (1, 2) if y for y in (3, 4)]");
419 }
420
421 @Test
Laurent Le Brun741824b2015-03-20 15:10:19 +0000422 public void testTupleDestructuring() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000423 newTest()
424 .setUp("a, b = 1, 2")
425 .testLookup("a", 1)
426 .testLookup("b", 2)
427 .setUp("c, d = {'key1':2, 'key2':3}")
428 .testLookup("c", "key1")
429 .testLookup("d", "key2");
Laurent Le Brun741824b2015-03-20 15:10:19 +0000430 }
431
432 @Test
Laurent Le Brun2e78d612015-04-15 09:06:46 +0000433 public void testHeterogeneousDict() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000434 newTest().setUp("d = {'str': 1, 2: 3}", "a = d['str']", "b = d[2]").testLookup("a", 1)
435 .testLookup("b", 3);
Laurent Le Brun2e78d612015-04-15 09:06:46 +0000436 }
437
438 @Test
Laurent Le Brun741824b2015-03-20 15:10:19 +0000439 public void testRecursiveTupleDestructuring() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000440 newTest()
441 .setUp("((a, b), (c, d)) = [(1, 2), (3, 4)]")
442 .testLookup("a", 1)
443 .testLookup("b", 2)
444 .testLookup("c", 3)
445 .testLookup("d", 4);
Laurent Le Brun741824b2015-03-20 15:10:19 +0000446 }
447
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000448 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000449 public void testListComprehensionModifiesGlobalEnv() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000450 new SkylarkTest().update("x", 42).testIfExactError("ERROR 1:1: Variable x is read only",
451 "[x + 1 for x in [1,2,3]]");
452 new BuildTest().update("x", 42).setUp("y =[x + 1 for x in [1,2,3]]")
453 .testExactOrder("y", 2, 3, 4).testLookup("x", 3); // (x is global)
Ulf Adams89f012d2015-02-26 13:39:28 +0000454 }
455
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000456 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000457 public void testDictComprehensions() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000458 newTest()
459 .testStatement("{a : a for a in []}", Collections.emptyMap())
460 .testStatement("{b : b for b in [1, 2]}", ImmutableMap.of(1, 1, 2, 2))
461 .testStatement("{c : 'v_' + c for c in ['a', 'b']}",
462 ImmutableMap.of("a", "v_a", "b", "v_b"))
463 .testStatement("{'k_' + d : d for d in ['a', 'b']}",
464 ImmutableMap.of("k_a", "a", "k_b", "b"))
465 .testStatement("{'k_' + e : 'v_' + e for e in ['a', 'b']}",
466 ImmutableMap.of("k_a", "v_a", "k_b", "v_b"))
467 .testStatement("{x+y : x*y for x, y in [[2, 3]]}", ImmutableMap.of(5, 6));
Ulf Adams89f012d2015-02-26 13:39:28 +0000468 }
469
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000470 @Test
Laurent Le Brun2e78d612015-04-15 09:06:46 +0000471 public void testDictComprehensionOnNonIterable() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000472 newTest().testIfExactError("type 'int' is not iterable", "{k : k for k in 3}");
Laurent Le Brun2e78d612015-04-15 09:06:46 +0000473 }
474
475 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000476 public void testDictComprehensions_MultipleKey() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000477 newTest().testStatement("{x : x for x in [1, 2, 1]}", ImmutableMap.of(1, 1, 2, 2))
478 .testStatement("{y : y for y in ['ab', 'c', 'a' + 'b']}",
479 ImmutableMap.of("ab", "ab", "c", "c"));
Ulf Adams89f012d2015-02-26 13:39:28 +0000480 }
481
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000482 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000483 public void testDictComprehensions_ToString() throws Exception {
Francois-Rene Rideau5f3e30c2015-04-10 19:08:39 +0000484 assertEquals("{x: x for x in [1, 2]}",
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000485 parseExpression("{x : x for x in [1, 2]}").toString());
Ulf Adams89f012d2015-02-26 13:39:28 +0000486 assertEquals("{x + 'a': x for x in [1, 2]}",
Francois-Rene Rideau89312fb2015-09-10 18:53:03 +0000487 parseExpression("{x + 'a' : x for x in [1, 2]}").toString());
Ulf Adams89f012d2015-02-26 13:39:28 +0000488 }
489
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000490 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000491 public void testListConcatenation() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000492 newTest()
493 .testStatement("[1, 2] + [3, 4]", Arrays.asList(1, 2, 3, 4))
494 .testStatement("(1, 2) + (3, 4)", ImmutableList.of(1, 2, 3, 4))
495 .testIfExactError("can only concatenate Tuple (not \"List\") to Tuple", "[1, 2] + (3, 4)")
496 .testIfExactError("can only concatenate List (not \"Tuple\") to List", "(1, 2) + [3, 4]");
Ulf Adams89f012d2015-02-26 13:39:28 +0000497 }
498
Greg Estrenb3dece02015-05-14 17:18:41 +0000499 @SuppressWarnings("unchecked")
500 @Test
501 public void testSelectorListConcatenation() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000502 // TODO(fwe): cannot be handled by current testing suite
Greg Estrenb3dece02015-05-14 17:18:41 +0000503 SelectorList x = (SelectorList) eval("select({'foo': ['FOO'], 'bar': ['BAR']}) + []");
504 List<Object> elements = x.getElements();
505 assertThat(elements.size()).isEqualTo(2);
506 assertThat(elements.get(0)).isInstanceOf(SelectorValue.class);
Florian Weikert28da3652015-07-01 14:52:30 +0000507 assertThat((Iterable<Object>) elements.get(1)).isEmpty();
Greg Estrenb3dece02015-05-14 17:18:41 +0000508 }
509
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000510 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000511 public void testListComprehensionFailsOnNonSequence() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000512 newTest().testIfErrorContains("type 'int' is not iterable", "[x + 1 for x in 123]");
Ulf Adams89f012d2015-02-26 13:39:28 +0000513 }
514
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000515 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000516 public void testListComprehensionOnString() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000517 newTest().testExactOrder("[x for x in 'abc']", "a", "b", "c");
Ulf Adams89f012d2015-02-26 13:39:28 +0000518 }
519
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000520 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000521 public void testInvalidAssignment() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000522 newTest().testIfErrorContains(
523 "can only assign to variables and tuples, not to 'x + 1'", "x + 1 = 2");
Ulf Adams89f012d2015-02-26 13:39:28 +0000524 }
525
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000526 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000527 public void testListComprehensionOnDictionary() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000528 newTest().testExactOrder("val = ['var_' + n for n in {'a':1,'b':2}] ; val", "var_a", "var_b");
Ulf Adams89f012d2015-02-26 13:39:28 +0000529 }
530
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000531 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000532 public void testListComprehensionOnDictionaryCompositeExpression() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000533 new BuildTest()
534 .setUp("d = {1:'a',2:'b'}", "l = [d[x] for x in d]")
535 .testLookup("l", ImmutableList.of("a", "b"));
Ulf Adams89f012d2015-02-26 13:39:28 +0000536 }
537
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000538 @Test
Laurent Le Brunab0ca1a2015-03-31 17:13:25 +0000539 public void testInOperator() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000540 newTest()
541 .testStatement("'b' in ['a', 'b']", Boolean.TRUE)
542 .testStatement("'c' in ['a', 'b']", Boolean.FALSE)
543 .testStatement("'b' in ('a', 'b')", Boolean.TRUE)
544 .testStatement("'c' in ('a', 'b')", Boolean.FALSE)
545 .testStatement("'b' in {'a' : 1, 'b' : 2}", Boolean.TRUE)
546 .testStatement("'c' in {'a' : 1, 'b' : 2}", Boolean.FALSE)
547 .testStatement("1 in {'a' : 1, 'b' : 2}", Boolean.FALSE)
548 .testStatement("'b' in 'abc'", Boolean.TRUE)
549 .testStatement("'d' in 'abc'", Boolean.FALSE);
Ulf Adams89f012d2015-02-26 13:39:28 +0000550 }
551
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000552 @Test
Laurent Le Brune3f4ed72015-05-08 14:47:26 +0000553 public void testNotInOperator() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000554 newTest()
555 .testStatement("'b' not in ['a', 'b']", Boolean.FALSE)
556 .testStatement("'c' not in ['a', 'b']", Boolean.TRUE)
557 .testStatement("'b' not in ('a', 'b')", Boolean.FALSE)
558 .testStatement("'c' not in ('a', 'b')", Boolean.TRUE)
559 .testStatement("'b' not in {'a' : 1, 'b' : 2}", Boolean.FALSE)
560 .testStatement("'c' not in {'a' : 1, 'b' : 2}", Boolean.TRUE)
561 .testStatement("1 not in {'a' : 1, 'b' : 2}", Boolean.TRUE)
562 .testStatement("'b' not in 'abc'", Boolean.FALSE)
563 .testStatement("'d' not in 'abc'", Boolean.TRUE);
Laurent Le Brune3f4ed72015-05-08 14:47:26 +0000564 }
565
566 @Test
Laurent Le Brunab0ca1a2015-03-31 17:13:25 +0000567 public void testInFail() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000568 newTest()
569 .testIfExactError(
570 "in operator only works on strings if the left operand is also a string", "1 in '123'")
571 .testIfExactError(
572 "in operator only works on lists, tuples, sets, dicts and strings", "'a' in 1");
Ulf Adams89f012d2015-02-26 13:39:28 +0000573 }
574
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000575 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000576 public void testInCompositeForPrecedence() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000577 newTest().testStatement("not 'a' in ['a'] or 0", 0);
Ulf Adams89f012d2015-02-26 13:39:28 +0000578 }
579
580 private Object createObjWithStr() {
581 return new Object() {
582 @Override
583 public String toString() {
584 return "str marker";
585 }
586 };
587 }
588
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000589 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000590 public void testPercOnObject() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000591 newTest().update("obj", createObjWithStr()).testStatement("'%s' % obj", "str marker");
Ulf Adams89f012d2015-02-26 13:39:28 +0000592 }
593
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000594 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000595 public void testPercOnObjectList() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000596 newTest().update("obj", createObjWithStr()).testStatement("'%s %s' % (obj, obj)",
597 "str marker str marker");
Ulf Adams89f012d2015-02-26 13:39:28 +0000598 }
599
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000600 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000601 public void testPercOnObjectInvalidFormat() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000602 newTest().update("obj", createObjWithStr()).testIfExactError(
603 "invalid argument str marker for format pattern %d", "'%d' % obj");
Ulf Adams89f012d2015-02-26 13:39:28 +0000604 }
605
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000606 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000607 public void testDictKeys() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000608 newTest().testExactOrder("v = {'a': 1}.keys() + ['b', 'c'] ; v", "a", "b", "c");
Ulf Adams89f012d2015-02-26 13:39:28 +0000609 }
610
Han-Wen Nienhuysccf19ea2015-02-27 15:53:24 +0000611 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +0000612 public void testDictKeysTooManyArgs() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000613 newTest().testIfExactError(
614 "too many (2) positional arguments in call to keys(self: dict)", "{'a': 1}.keys('abc')");
Francois-Rene Rideau76023b92015-04-17 15:31:59 +0000615 }
616
617 @Test
618 public void testDictKeysTooManyKeyArgs() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000619 newTest().testIfExactError("unexpected keyword 'arg' in call to keys(self: dict)",
Francois-Rene Rideau76023b92015-04-17 15:31:59 +0000620 "{'a': 1}.keys(arg='abc')");
621 }
622
623 @Test
624 public void testDictKeysDuplicateKeyArgs() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000625 newTest().testIfExactError("duplicate keywords 'arg', 'k' in call to keys",
Francois-Rene Rideau76023b92015-04-17 15:31:59 +0000626 "{'a': 1}.keys(arg='abc', arg='def', k=1, k=2)");
627 }
628
629 @Test
630 public void testArgBothPosKey() throws Exception {
Florian Weikert28da3652015-07-01 14:52:30 +0000631 newTest().testIfErrorContains(
632 "arguments 'old', 'new' passed both by position and by name "
Francois-Rene Rideau537a90b2015-04-22 06:47:31 +0000633 + "in call to replace(self: string, ",
634 "'banana'.replace('a', 'o', 3, old='a', new=4)");
Ulf Adams89f012d2015-02-26 13:39:28 +0000635 }
636}