aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java
diff options
context:
space:
mode:
authorGravatar Laurent Le Brun <laurentlb@google.com>2015-09-14 17:14:41 +0000
committerGravatar John Field <jfield@google.com>2015-09-15 20:25:30 +0000
commitcd2ad0f9e082d101c0e9664729a4d2632659361d (patch)
treef8eeafdc44eaa4a292103cb0af6107263ac5882d /src/test/java
parent097736ee9490e4ccf25cf56a201198f3a0388af7 (diff)
Fix and test SyntaxTreeVisitor
-- MOS_MIGRATED_REVID=103003770
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java
new file mode 100644
index 0000000000..72edba4765
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java
@@ -0,0 +1,82 @@
+// Copyright 2015 Google Inc. 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.devtools.build.lib.cmdline.PackageIdentifier;
+import com.google.devtools.build.lib.packages.CachingPackageLocator;
+import com.google.devtools.build.lib.testutil.Scratch;
+import com.google.devtools.build.lib.vfs.Path;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+
+/** Tests for @{code SyntaxTreeVisitor} */
+@RunWith(JUnit4.class)
+public class SyntaxTreeVisitorTest {
+
+ private Scratch scratch = new Scratch();
+
+ private class ScratchPathPackageLocator implements CachingPackageLocator {
+ @Override
+ public Path getBuildFileForPackage(PackageIdentifier packageName) {
+ return scratch.resolve(packageName.getPackageFragment()).getRelative("BUILD");
+ }
+ }
+
+ private CachingPackageLocator locator = new ScratchPathPackageLocator();
+
+ /** Parses the contents of the specified string and returns the AST. */
+ private BuildFileAST parse(String... lines) throws IOException {
+ Path file = scratch.file("/a/build/file/BUILD", lines);
+ return BuildFileAST.parseSkylarkFile(
+ file, null /* reporter */, locator, null /*validationEnvironment*/);
+ }
+
+ @Test
+ public void everyIdentifierAndParameterIsVisitedInOrder() throws IOException {
+ final List<String> idents = new ArrayList<>();
+ final List<String> params = new ArrayList<>();
+
+ class IdentVisitor extends SyntaxTreeVisitor {
+ @Override
+ public void visit(Identifier node) {
+ idents.add(node.getName());
+ }
+
+ @Override
+ public void visit(Parameter<?, ?> node) {
+ params.add(node.toString());
+ }
+ }
+
+ BuildFileAST ast =
+ parse(
+ "a = b",
+ "def c(p1, p2=4, **p3):",
+ " for d in e: f(g)",
+ " return h + i.j()");
+ IdentVisitor visitor = new IdentVisitor();
+ ast.accept(visitor);
+ assertThat(idents).containsExactly("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").inOrder();
+ assertThat(params).containsExactly("p1", "p2=4", "**p3").inOrder();
+ }
+}