blob: 69bede701a918c1c6cf90259808c0d4da958715c [file] [log] [blame]
Dmitry Lomov950310f2017-03-01 17:45:12 +00001// Copyright 2006 The Bazel Authors. 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 com.google.common.truth.Truth;
17import java.util.Arrays;
18import org.junit.Test;
19import org.junit.runner.RunWith;
20import org.junit.runners.JUnit4;
21
22/** A test for {@link LValue#boundNames()}. */
23@RunWith(JUnit4.class)
24public class LValueBoundNamesTest {
25
26 @Test
27 public void simpleAssignment() {
28 assertBoundNames("x = 1", "x");
29 }
30
31 @Test
32 public void listAssignment() {
33 assertBoundNames("x, y = 1", "x", "y");
34 }
35
36 @Test
37 public void complexListAssignment() {
38 assertBoundNames("x, [y] = 1", "x", "y");
39 }
40
41 @Test
42 public void arrayElementAssignment() {
43 assertBoundNames("x[1] = 1");
44 }
45
46 @Test
47 public void complexListAssignment2() {
48 assertBoundNames("[[x], y], [z, w[1]] = 1", "x", "y", "z");
49 }
50
51 private static void assertBoundNames(String assignement, String... boundNames) {
52 BuildFileAST buildFileAST = BuildFileAST
53 .parseSkylarkString(Environment.FAIL_FAST_HANDLER, assignement);
54 LValue lValue = ((AssignmentStatement) buildFileAST.getStatements().get(0)).getLValue();
55 Truth.assertThat(lValue.boundNames()).containsExactlyElementsIn(Arrays.asList(boundNames));
56 }
57}