aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java
blob: 1ee0dff026189e7ffbd28d5e63c01a084f90899d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.syntax;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
import com.google.devtools.build.lib.syntax.SkylarkList.Tuple;
import com.google.devtools.build.lib.syntax.util.EvaluationTestCase;
import com.google.devtools.build.lib.testutil.TestMode;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Test of evaluation behavior.  (Implicitly uses lexer + parser.)
 */
@RunWith(JUnit4.class)
public class EvaluationTest extends EvaluationTestCase {

  @Before
  public final void setBuildMode() throws Exception {
    super.setMode(TestMode.BUILD);
  }

  /**
   * Creates a new instance of {@code ModalTestCase}.
   *
   * <p>If a test uses this method, it allows potential subclasses to run the very same test in a
   * different mode in subclasses
   */
  protected ModalTestCase newTest(String... skylarkOptions) {
    return new BuildTest(skylarkOptions);
  }

  @Test
  public void testExprs() throws Exception {
    newTest()
        .testStatement("'%sx' % 'foo' + 'bar1'", "fooxbar1")
        .testStatement("('%sx' % 'foo') + 'bar2'", "fooxbar2")
        .testStatement("'%sx' % ('foo' + 'bar3')", "foobar3x")
        .testStatement("123 + 456", 579)
        .testStatement("456 - 123", 333)
        .testStatement("8 % 3", 2)
        .testIfErrorContains("unsupported operand type(s) for %: 'int' and 'string'", "3 % 'foo'")
        .testStatement("-5", -5)
        .testIfErrorContains("unsupported operand type for -: 'string'", "-'foo'");
  }

  @Test
  public void testListExprs() throws Exception {
    newTest().testExactOrder("[1, 2, 3]", 1, 2, 3).testExactOrder("(1, 2, 3)", 1, 2, 3);
  }

  @Test
  public void testStringFormatMultipleArgs() throws Exception {
    newTest().testStatement("'%sY%s' % ('X', 'Z')", "XYZ");
  }

  @Test
  public void testConditionalExpressions() throws Exception {
    newTest()
        .testStatement("1 if True else 2", 1)
        .testStatement("1 if False else 2", 2)
        .testStatement("1 + 2 if 3 + 4 else 5 + 6", 3);

    setFailFast(false);
    parseExpression("1 if 2");
    assertContainsError(
        "missing else clause in conditional expression or semicolon before if");
  }

  @Test
  public void testListComparison() throws Exception {
    newTest()
        .testStatement("[] < [1]", true)
        .testStatement("[1] < [1, 1]", true)
        .testStatement("[1, 1] < [1, 2]", true)
        .testStatement("[1, 2] < [1, 2, 3]", true)
        .testStatement("[1, 2, 3] <= [1, 2, 3]", true)

        .testStatement("['a', 'b'] > ['a']", true)
        .testStatement("['a', 'b'] >= ['a']", true)
        .testStatement("['a', 'b'] < ['a']", false)
        .testStatement("['a', 'b'] <= ['a']", false)

        .testStatement("('a', 'b') > ('a', 'b')", false)
        .testStatement("('a', 'b') >= ('a', 'b')", true)
        .testStatement("('a', 'b') < ('a', 'b')", false)
        .testStatement("('a', 'b') <= ('a', 'b')", true)

        .testStatement("[[1, 1]] > [[1, 1], []]", false)
        .testStatement("[[1, 1]] < [[1, 1], []]", true);
  }

  @Test
  public void testSetComparison() throws Exception {
    newTest().testIfExactError("Cannot compare depsets", "depset([1, 2]) < depset([3, 4])");
  }

  @Test
  public void testSumFunction() throws Exception {
    BaseFunction sum = new BaseFunction("sum") {
      @Override
      public Object call(List<Object> args, Map<String, Object> kwargs,
          FuncallExpression ast, Environment env) {
        int sum = 0;
        for (Object arg : args) {
          sum += (Integer) arg;
        }
        return sum;
      }
    };

    newTest().update(sum.getName(), sum).testStatement("sum(1, 2, 3, 4, 5, 6)", 21)
        .testStatement("sum", sum).testStatement("sum(a=1, b=2)", 0);
  }

  @Test
  public void testNotCallInt() throws Exception {
    newTest().setUp("sum = 123456").testLookup("sum", 123456)
        .testIfExactError("'int' object is not callable", "sum(1, 2, 3, 4, 5, 6)")
        .testStatement("sum", 123456);
  }

  @Test
  public void testComplexFunctionCall() throws Exception {
    newTest().setUp("functions = [min, max]", "l = [1,2]")
        .testEval("(functions[0](l), functions[1](l))", "(1, 2)");
  }

  @Test
  public void testKeywordArgs() throws Exception {

    // This function returns the map of keyword arguments passed to it.
    BaseFunction kwargs = new BaseFunction("kwargs") {
      @Override
      public Object call(List<Object> args,
          final Map<String, Object> kwargs,
          FuncallExpression ast,
          Environment env) {
        return SkylarkDict.copyOf(env, kwargs);
      }
    };

    newTest()
        .update(kwargs.getName(), kwargs)
        .testEval(
            "kwargs(foo=1, bar='bar', wiz=[1,2,3]).items()",
            "[('foo', 1), ('bar', 'bar'), ('wiz', [1, 2, 3])]")
        .testEval(
            "kwargs(wiz=[1,2,3], bar='bar', foo=1).items()",
            "[('wiz', [1, 2, 3]), ('bar', 'bar'), ('foo', 1)]");
  }

  @Test
  public void testModulo() throws Exception {
    newTest()
        .testStatement("6 % 2", 0)
        .testStatement("6 % 4", 2)
        .testStatement("3 % 6", 3)
        .testStatement("7 % -4", -1)
        .testStatement("-7 % 4", 1)
        .testStatement("-7 % -4", -3)
        .testIfExactError("integer modulo by zero", "5 % 0");
  }

  @Test
  public void testMult() throws Exception {
    newTest()
        .testStatement("6 * 7", 42)
        .testStatement("3 * 'ab'", "ababab")
        .testStatement("0 * 'ab'", "")
        .testStatement("'1' + '0' * 5", "100000")
        .testStatement("'ab' * -4", "")
        .testStatement("-1 * ''", "");
  }

  @Test
  public void testSlashOperatorIsForbidden() throws Exception {
    newTest("--incompatible_disallow_slash_operator=true")
        .testIfErrorContains("The `/` operator has been removed.", "5 / 2");
  }

  @Test
  public void testDivision() throws Exception {
    newTest("--incompatible_disallow_slash_operator=false")
        .testStatement("6 / 2", 3)
        .testStatement("6 / 4", 1)
        .testStatement("3 / 6", 0)
        .testStatement("7 / -2", -4)
        .testStatement("-7 / 2", -4)
        .testStatement("-7 / -2", 3)
        .testStatement("2147483647 / 2", 1073741823)
        .testIfErrorContains("unsupported operand type(s) for /: 'string' and 'int'", "'str' / 2")
        .testIfExactError("integer division by zero", "5 / 0");
  }

  @Test
  public void testFloorDivision() throws Exception {
    newTest()
        .testStatement("6 // 2", 3)
        .testStatement("6 // 4", 1)
        .testStatement("3 // 6", 0)
        .testStatement("7 // -2", -4)
        .testStatement("-7 // 2", -4)
        .testStatement("-7 // -2", 3)
        .testStatement("2147483647 // 2", 1073741823)
        .testIfErrorContains("unsupported operand type(s) for /: 'string' and 'int'", "'str' / 2")
        .testIfExactError("integer division by zero", "5 // 0");
  }

  @Test
  public void testCheckedArithmetic() throws Exception {
    new SkylarkTest()
        .testIfErrorContains("integer overflow", "2000000000 + 2000000000")
        .testIfErrorContains("integer overflow", "1234567890 * 987654321")
        .testIfErrorContains("integer overflow", "- 2000000000 - 2000000000")

        // literal 2147483648 is not allowed, so we compute it
        .setUp("minint = - 2147483647 - 1")
        .testIfErrorContains("integer overflow", "-minint");
  }

  @Test
  public void testOperatorPrecedence() throws Exception {
    newTest()
        .testStatement("2 + 3 * 4", 14)
        .testStatement("2 + 3 / 4", 2)
        .testStatement("2 * 3 + 4 / -2", 4);
  }

  @Test
  public void testConcatStrings() throws Exception {
    newTest().testStatement("'foo' + 'bar'", "foobar");
  }

  @SuppressWarnings("unchecked")
  @Test
  public void testConcatLists() throws Exception {
    // TODO(fwe): cannot be handled by current testing suite
    // list
    Object x = eval("[1,2] + [3,4]");
    assertThat((Iterable<Object>) x).containsExactly(1, 2, 3, 4).inOrder();
    assertThat(x).isEqualTo(MutableList.of(env, 1, 2, 3, 4));
    assertThat(EvalUtils.isImmutable(x)).isFalse();

    // tuple
    x = eval("(1,2) + (3,4)");
    assertThat(x).isEqualTo(Tuple.of(1, 2, 3, 4));
    assertThat(EvalUtils.isImmutable(x)).isTrue();

    checkEvalError("unsupported operand type(s) for +: 'tuple' and 'list'",
        "(1,2) + [3,4]"); // list + tuple
  }

  @Test
  public void testListComprehensions() throws Exception {
    newTest()
        .testExactOrder("['foo/%s.java' % x for x in []]")
        .testExactOrder("['foo/%s.java' % y for y in ['bar', 'wiz', 'quux']]", "foo/bar.java",
            "foo/wiz.java", "foo/quux.java")
        .testExactOrder("['%s/%s.java' % (z, t) for z in ['foo', 'bar'] "
            + "for t in ['baz', 'wiz', 'quux']]",
            "foo/baz.java",
            "foo/wiz.java",
            "foo/quux.java",
            "bar/baz.java",
            "bar/wiz.java",
            "bar/quux.java")
        .testExactOrder("['%s/%s.java' % (b, b) for a in ['foo', 'bar'] "
            + "for b in ['baz', 'wiz', 'quux']]",
            "baz/baz.java",
            "wiz/wiz.java",
            "quux/quux.java",
            "baz/baz.java",
            "wiz/wiz.java",
            "quux/quux.java")
        .testExactOrder("['%s/%s.%s' % (c, d, e) for c in ['foo', 'bar'] "
            + "for d in ['baz', 'wiz', 'quux'] for e in ['java', 'cc']]",
            "foo/baz.java",
            "foo/baz.cc",
            "foo/wiz.java",
            "foo/wiz.cc",
            "foo/quux.java",
            "foo/quux.cc",
            "bar/baz.java",
            "bar/baz.cc",
            "bar/wiz.java",
            "bar/wiz.cc",
            "bar/quux.java",
            "bar/quux.cc")
        .testExactOrder("[i for i in (1, 2)]", 1, 2)
        .testExactOrder("[i for i in [2, 3] or [1, 2]]", 2, 3);
  }

  @Test
  public void testNestedListComprehensions() throws Exception {
    newTest().testExactOrder("li = [[1, 2], [3, 4]]\n" + "[j for i in li for j in i]", 1, 2,
        3, 4).testExactOrder("input = [['abc'], ['def', 'ghi']]\n"
        + "['%s %s' % (b, c) for a in input for b in a for c in b]",
        "abc a",
        "abc b",
        "abc c",
        "def d",
        "def e",
        "def f",
        "ghi g",
        "ghi h",
        "ghi i");
  }

  @Test
  public void testListComprehensionsMultipleVariables() throws Exception {
    newTest().testEval("[x + y for x, y in [(1, 2), (3, 4)]]", "[3, 7]")
        .testEval("[z + t for (z, t) in [[1, 2], [3, 4]]]", "[3, 7]");
  }

  @Test
  public void testListComprehensionsMultipleVariablesFail() throws Exception {
    newTest().testIfErrorContains(
        "assignment length mismatch: left-hand side has length 3, but right-hand side evaluates to "
            + "value of length 2",
        "[x + y for x, y, z in [(1, 2), (3, 4)]]").testIfExactError(
        "type 'int' is not a collection", "[x + y for x, y in (1, 2)]");
  }

  @Test
  public void testListComprehensionsWithFiltering() throws Exception {
    newTest()
        .setUp("range3 = [0, 1, 2]")
        .testEval("[a for a in (4, None, 2, None, 1) if a != None]", "[4, 2, 1]")
        .testEval("[b+c for b in [0, 1, 2] for c in [0, 1, 2] if b + c > 2]", "[3, 3, 4]")
        .testEval("[d+e for d in range3 if d % 2 == 1 for e in range3]", "[1, 2, 3]")
        .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 ]",
            "[[2, 6], [3, 8], [4, 6]]")
        .testEval("[h for h in [4, 2, 0, 1] if h]", "[4, 2, 1]");
  }

  @Test
  public void testListComprehensionDefinitionOrder() throws Exception {
    newTest().testIfErrorContains("name 'y' is not defined",
        "[x for x in (1, 2) if y for y in (3, 4)]");
  }

  @Test
  public void testTupleDestructuring() throws Exception {
    newTest()
        .setUp("a, b = 1, 2")
        .testLookup("a", 1)
        .testLookup("b", 2)
        .setUp("c, d = {'key1':2, 'key2':3}")
        .testLookup("c", "key1")
        .testLookup("d", "key2");
  }

  @Test
  public void testSingleTuple() throws Exception {
    newTest().setUp("(a,) = [1]").testLookup("a", 1);
  }

  @Test
  public void testHeterogeneousDict() throws Exception {
    newTest().setUp("d = {'str': 1, 2: 3}", "a = d['str']", "b = d[2]").testLookup("a", 1)
        .testLookup("b", 3);
  }

  @Test
  public void testAccessDictWithATupleKey() throws Exception {
    newTest().setUp("x = {(1, 2): 3}[1, 2]").testLookup("x", 3);
  }

  @Test
  public void testDictWithDuplicatedKey() throws Exception {
    new SkylarkTest()
        .testIfErrorContains(
            "Duplicated key \"str\" when creating dictionary", "{'str': 1, 'x': 2, 'str': 3}");
  }

  @Test
  public void testRecursiveTupleDestructuring() throws Exception {
    newTest()
        .setUp("((a, b), (c, d)) = [(1, 2), (3, 4)]")
        .testLookup("a", 1)
        .testLookup("b", 2)
        .testLookup("c", 3)
        .testLookup("d", 4);
  }

  @Test
  public void testListComprehensionAtTopLevel() throws Exception {
    // It is allowed to have a loop variable with the same name as a global variable.
    newTest().update("x", 42).setUp("y = [x + 1 for x in [1,2,3]]")
        .testExactOrder("y", 2, 3, 4);
  }

  @Test
  public void testDictComprehensions() throws Exception {
    newTest()
        .testStatement("{a : a for a in []}", Collections.emptyMap())
        .testStatement("{b : b for b in [1, 2]}", ImmutableMap.of(1, 1, 2, 2))
        .testStatement("{c : 'v_' + c for c in ['a', 'b']}",
            ImmutableMap.of("a", "v_a", "b", "v_b"))
        .testStatement("{'k_' + d : d for d in ['a', 'b']}",
            ImmutableMap.of("k_a", "a", "k_b", "b"))
        .testStatement("{'k_' + e : 'v_' + e for e in ['a', 'b']}",
            ImmutableMap.of("k_a", "v_a", "k_b", "v_b"))
        .testStatement("{x+y : x*y for x, y in [[2, 3]]}", ImmutableMap.of(5, 6));
  }

  @Test
  public void testDictComprehensionOnNonIterable() throws Exception {
    newTest().testIfExactError("type 'int' is not iterable", "{k : k for k in 3}");
  }

  @Test
  public void testDictComprehension_ManyClauses() throws Exception {
    new SkylarkTest().testStatement(
        "{x : x * y for x in range(1, 10) if x % 2 == 0 for y in range(1, 10) if y == x}",
        ImmutableMap.of(2, 4, 4, 16, 6, 36, 8, 64));
  }

  @Test
  public void testDictComprehensions_MultipleKey() throws Exception {
    newTest().testStatement("{x : x for x in [1, 2, 1]}", ImmutableMap.of(1, 1, 2, 2))
        .testStatement("{y : y for y in ['ab', 'c', 'a' + 'b']}",
            ImmutableMap.of("ab", "ab", "c", "c"));
  }

  @Test
  public void testListConcatenation() throws Exception {
    newTest()
        .testStatement("[1, 2] + [3, 4]", MutableList.of(env, 1, 2, 3, 4))
        .testStatement("(1, 2) + (3, 4)", Tuple.of(1, 2, 3, 4))
        .testIfExactError("unsupported operand type(s) for +: 'list' and 'tuple'",
            "[1, 2] + (3, 4)")
        .testIfExactError("unsupported operand type(s) for +: 'tuple' and 'list'",
            "(1, 2) + [3, 4]");
  }

  @Test
  public void testListMultiply() throws Exception {
    newTest()
        .testStatement("[1, 2, 3] * 1", MutableList.of(env, 1, 2, 3))
        .testStatement("[1, 2] * 2", MutableList.of(env, 1, 2, 1, 2))
        .testStatement("[1, 2] * 3", MutableList.of(env, 1, 2, 1, 2, 1, 2))
        .testStatement("[1, 2] * 4", MutableList.of(env, 1, 2, 1, 2, 1, 2, 1, 2))
        .testStatement("[8] * 5", MutableList.of(env, 8, 8, 8, 8, 8))
        .testStatement("[    ] * 10", MutableList.empty())
        .testStatement("[1, 2] * 0", MutableList.empty())
        .testStatement("[1, 2] * -4", MutableList.empty())
        .testStatement("2 * [1, 2]", MutableList.of(env, 1, 2, 1, 2))
        .testStatement("10 * []", MutableList.empty())
        .testStatement("0 * [1, 2]", MutableList.empty())
        .testStatement("-4 * [1, 2]", MutableList.empty());
  }

  @Test
  public void testTupleMultiply() throws Exception {
    newTest()
        .testStatement("(1, 2, 3) * 1", Tuple.of(1, 2, 3))
        .testStatement("(1, 2) * 2", Tuple.of(1, 2, 1, 2))
        .testStatement("(1, 2) * 3", Tuple.of(1, 2, 1, 2, 1, 2))
        .testStatement("(1, 2) * 4", Tuple.of(1, 2, 1, 2, 1, 2, 1, 2))
        .testStatement("(8,) * 5", Tuple.of(8, 8, 8, 8, 8))
        .testStatement("(    ) * 10", Tuple.empty())
        .testStatement("(1, 2) * 0", Tuple.empty())
        .testStatement("(1, 2) * -4", Tuple.empty())
        .testStatement("2 * (1, 2)", Tuple.of(1, 2, 1, 2))
        .testStatement("10 * ()", Tuple.empty())
        .testStatement("0 * (1, 2)", Tuple.empty())
        .testStatement("-4 * (1, 2)", Tuple.empty());
  }

  @SuppressWarnings("unchecked")
  @Test
  public void testSelectorListConcatenation() throws Exception {
    // TODO(fwe): cannot be handled by current testing suite
    SelectorList x = (SelectorList) eval("select({'foo': ['FOO'], 'bar': ['BAR']}) + []");
    List<Object> elements = x.getElements();
    assertThat(elements).hasSize(2);
    assertThat(elements.get(0)).isInstanceOf(SelectorValue.class);
    assertThat((Iterable<Object>) elements.get(1)).isEmpty();
  }

  @Test
  public void testAddSelectIncompatibleType() throws Exception {
    newTest()
        .testIfErrorContains(
            "'+' operator applied to incompatible types (select of list, int)",
            "select({'foo': ['FOO'], 'bar': ['BAR']}) + 1");
  }

  @Test
  public void testAddSelectIncompatibleType2() throws Exception {
    newTest()
        .testIfErrorContains(
            "'+' operator applied to incompatible types (select of list, select of int)",
            "select({'foo': ['FOO']}) + select({'bar': 2})");
  }

  @Test
  public void testListComprehensionFailsOnNonSequence() throws Exception {
    newTest().testIfErrorContains("type 'int' is not iterable", "[x + 1 for x in 123]");
  }

  @Test
  public void testListComprehensionOnString() throws Exception {
    newTest("--incompatible_string_is_not_iterable=false")
        .testExactOrder("[x for x in 'abc']", "a", "b", "c");
  }

  @Test
  public void testListComprehensionOnStringIsForbidden() throws Exception {
    newTest("--incompatible_string_is_not_iterable=true")
        .testIfErrorContains("type 'string' is not iterable", "[x for x in 'abc']");
  }

  @Test
  public void testInvalidAssignment() throws Exception {
    newTest().testIfErrorContains(
        "cannot assign to 'x + 1'", "x + 1 = 2");
  }

  @Test
  public void testListComprehensionOnDictionary() throws Exception {
    newTest().testExactOrder("val = ['var_' + n for n in {'a':1,'b':2}] ; val", "var_a", "var_b");
  }

  @Test
  public void testListComprehensionOnDictionaryCompositeExpression() throws Exception {
    new BuildTest()
        .setUp("d = {1:'a',2:'b'}", "l = [d[x] for x in d]")
        .testLookup("l", MutableList.of(env, "a", "b"));
  }

  @Test
  public void testListComprehensionUpdate() throws Exception {
    new BuildTest()
        .setUp("xs = [1, 2, 3]")
        .testIfErrorContains("trying to mutate a locked object",
            "[xs.append(4) for x in xs]");
  }

  @Test
  public void testNestedListComprehensionUpdate() throws Exception {
    new BuildTest()
        .setUp("xs = [1, 2, 3]")
        .testIfErrorContains("trying to mutate a locked object",
            "[xs.append(4) for x in xs for y in xs]");
  }

  @Test
  public void testListComprehensionUpdateInClause() throws Exception {
    new BuildTest()
        .setUp("xs = [1, 2, 3]")
        .testIfErrorContains("trying to mutate a locked object",
            // Use short-circuiting to produce valid output in the event
            // the exception is not raised.
            "[y for x in xs for y in (xs.append(4) or xs)]");
  }

  @Test
  public void testDictComprehensionUpdate() throws Exception {
    new BuildTest()
        .setUp("xs = {1:1, 2:2, 3:3}")
        .testIfErrorContains("trying to mutate a locked object",
            "[xs.popitem() for x in xs]");
  }

  @Test
  public void testInOperator() throws Exception {
    newTest()
        .testStatement("'b' in ['a', 'b']", Boolean.TRUE)
        .testStatement("'c' in ['a', 'b']", Boolean.FALSE)
        .testStatement("'b' in ('a', 'b')", Boolean.TRUE)
        .testStatement("'c' in ('a', 'b')", Boolean.FALSE)
        .testStatement("'b' in {'a' : 1, 'b' : 2}", Boolean.TRUE)
        .testStatement("'c' in {'a' : 1, 'b' : 2}", Boolean.FALSE)
        .testStatement("1 in {'a' : 1, 'b' : 2}", Boolean.FALSE)
        .testStatement("'b' in 'abc'", Boolean.TRUE)
        .testStatement("'d' in 'abc'", Boolean.FALSE);
  }

  @Test
  public void testNotInOperator() throws Exception {
    newTest()
        .testStatement("'b' not in ['a', 'b']", Boolean.FALSE)
        .testStatement("'c' not in ['a', 'b']", Boolean.TRUE)
        .testStatement("'b' not in ('a', 'b')", Boolean.FALSE)
        .testStatement("'c' not in ('a', 'b')", Boolean.TRUE)
        .testStatement("'b' not in {'a' : 1, 'b' : 2}", Boolean.FALSE)
        .testStatement("'c' not in {'a' : 1, 'b' : 2}", Boolean.TRUE)
        .testStatement("1 not in {'a' : 1, 'b' : 2}", Boolean.TRUE)
        .testStatement("'b' not in 'abc'", Boolean.FALSE)
        .testStatement("'d' not in 'abc'", Boolean.TRUE);
  }

  @Test
  public void testInFail() throws Exception {
    newTest()
        .testIfErrorContains(
            "'in <string>' requires string as left operand, not 'int'", "1 in '123'")
        .testIfErrorContains("'int' is not iterable. in operator only works on ", "'a' in 1");
  }

  @Test
  public void testInCompositeForPrecedence() throws Exception {
    newTest().testStatement("not 'a' in ['a'] or 0", 0);
  }

  private SkylarkValue createObjWithStr() {
    return new SkylarkValue() {
      @Override
      public void repr(SkylarkPrinter printer) {
        printer.append("<str marker>");
      }
    };
  }

  @Test
  public void testPercOnObject() throws Exception {
    newTest()
        .update("obj", createObjWithStr())
        .testStatement("'%s' % obj", "<str marker>");
    newTest()
        .update("unknown", new Object())
        .testStatement("'%s' % unknown", "<unknown object java.lang.Object>");
  }

  @Test
  public void testPercOnObjectList() throws Exception {
    newTest()
        .update("obj", createObjWithStr())
        .testStatement("'%s %s' % (obj, obj)", "<str marker> <str marker>");
    newTest()
        .update("unknown", new Object())
        .testStatement(
            "'%s %s' % (unknown, unknown)",
            "<unknown object java.lang.Object> <unknown object java.lang.Object>");
  }

  @Test
  public void testPercOnObjectInvalidFormat() throws Exception {
    newTest()
        .update("obj", createObjWithStr())
        .testIfExactError("invalid argument <str marker> for format pattern %d", "'%d' % obj");
  }

  @Test
  public void testDictKeys() throws Exception {
    newTest().testExactOrder("v = {'a': 1}.keys() + ['b', 'c'] ; v", "a", "b", "c");
  }

  @Test
  public void testDictKeysTooManyArgs() throws Exception {
    newTest().testIfExactError(
        "expected no more than 0 positional arguments, but got 1, "
            + "in method call keys(string) of 'dict'", "{'a': 1}.keys('abc')");
  }

  @Test
  public void testDictKeysTooManyKeyArgs() throws Exception {
    newTest().testIfExactError(
        "unexpected keyword 'arg', in method call keys(string arg) of 'dict'",
        "{'a': 1}.keys(arg='abc')");
  }

  @Test
  public void testDictKeysDuplicateKeyArgs() throws Exception {
    newTest().testIfExactError("duplicate keywords 'arg', 'k' in call to {\"a\": 1}.keys",
        "{'a': 1}.keys(arg='abc', arg='def', k=1, k=2)");
  }

  @Test
  public void testArgBothPosKey() throws Exception {
    newTest().testIfErrorContains(
        "got multiple values for keyword argument 'old', "
            + "in method call replace(string, string, int, string old) of 'string'",
        "'banana'.replace('a', 'o', 3, old='a')");
  }
}