aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools
diff options
context:
space:
mode:
authorGravatar cparsons <cparsons@google.com>2018-03-07 11:01:17 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-07 11:03:38 -0800
commit5d446a7b8f784e9fc41e82a1b1fa941fe52cea31 (patch)
tree017273e8eb73558576f88f33c910bad9ad56e9f0 /src/test/java/com/google/devtools
parentf1013485d41efd8503f9d4f937e17d1b4bc91ed3 (diff)
Allow passing location, ast, and environment to @SkylarkCallable methods
RELNOTES: None. PiperOrigin-RevId: 188201686
Diffstat (limited to 'src/test/java/com/google/devtools')
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/BUILD2
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessorTest.java47
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/EnvironmentMissing.java29
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/GoldenCase.java46
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/LocationMissing.java35
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SkylarkInfoWrongOrder.java37
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java128
7 files changed, 319 insertions, 5 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/BUILD b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/BUILD
index d26c2d4c41..bf2eb60e4d 100644
--- a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/BUILD
@@ -15,6 +15,8 @@ java_test(
srcs = ["SkylarkCallableProcessorTest.java"],
resources = [":ProcessorTestFiles"],
deps = [
+ "//src/main/java/com/google/devtools/build/lib:events",
+ "//src/main/java/com/google/devtools/build/lib:syntax",
"//src/main/java/com/google/devtools/build/lib/skylarkinterface/processor:annotation_preprocessor",
"//third_party:compile_testing",
"//third_party:guava",
diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessorTest.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessorTest.java
index 3b2ce81bc3..074d52a8e9 100644
--- a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessorTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessorTest.java
@@ -18,9 +18,10 @@ import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
import com.google.common.io.Resources;
+import com.google.devtools.build.lib.events.Location;
+import com.google.devtools.build.lib.syntax.Environment;
import com.google.testing.compile.JavaFileObjects;
import javax.tools.JavaFileObject;
-
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -70,7 +71,45 @@ public final class SkylarkCallableProcessorTest {
.processedWith(new SkylarkCallableProcessor())
.failsToCompile()
.withErrorContaining(
- "@SkylarkCallable annotated method has 0 parameters, but annotation declared 1.");
+ "@SkylarkCallable annotated method has 0 parameters, "
+ + "but annotation declared 1 user-supplied parameters "
+ + "and 0 extra interpreter parameters.");
+ }
+
+ @Test
+ public void testEnvironmentMissing() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("EnvironmentMissing.java"))
+ .processedWith(new SkylarkCallableProcessor())
+ .failsToCompile()
+ .withErrorContaining(
+ "Expected parameter index 2 to be the "
+ + Environment.class.getCanonicalName()
+ + " type, matching useEnvironment, but was java.lang.String");
+ }
+
+ @Test
+ public void testLocationMissing() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("LocationMissing.java"))
+ .processedWith(new SkylarkCallableProcessor())
+ .failsToCompile()
+ .withErrorContaining(
+ "Expected parameter index 2 to be the "
+ + Location.class.getCanonicalName()
+ + " type, matching useLocation, but was java.lang.String");
+ }
+
+ @Test
+ public void testSkylarkInfoWrongOrder() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("SkylarkInfoWrongOrder.java"))
+ .processedWith(new SkylarkCallableProcessor())
+ .failsToCompile()
+ .withErrorContaining(
+ "Expected parameter index 3 to be the "
+ + Location.class.getCanonicalName()
+ + " type, matching useLocation, but was java.lang.Integer");
}
@Test
@@ -80,6 +119,8 @@ public final class SkylarkCallableProcessorTest {
.processedWith(new SkylarkCallableProcessor())
.failsToCompile()
.withErrorContaining(
- "@SkylarkCallable annotated method has 2 parameters, but annotation declared 1.");
+ "@SkylarkCallable annotated method has 2 parameters, "
+ + "but annotation declared 1 user-supplied parameters "
+ + "and 0 extra interpreter parameters.");
}
}
diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/EnvironmentMissing.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/EnvironmentMissing.java
new file mode 100644
index 0000000000..47bc416a47
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/EnvironmentMissing.java
@@ -0,0 +1,29 @@
+// Copyright 2018 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.skylarkinterface.processor.testsources;
+
+import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
+
+/**
+ * Test case for a SkylarkCallable method which does not have an appropriate Environment parameter
+ * despite having useEnvironment set.
+ */
+public class EnvironmentMissing {
+
+ @SkylarkCallable(name = "three_arg_method_missing_env", doc = "", useEnvironment = true)
+ public String threeArgMethod(String one, Integer two, String three) {
+ return "bar";
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/GoldenCase.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/GoldenCase.java
index f3dd513ef1..a1cfe5fa15 100644
--- a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/GoldenCase.java
+++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/GoldenCase.java
@@ -14,8 +14,11 @@
package com.google.devtools.build.lib.skylarkinterface.processor.testsources;
+import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.skylarkinterface.Param;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
+import com.google.devtools.build.lib.syntax.Environment;
+import com.google.devtools.build.lib.syntax.FuncallExpression;
/**
* Test source file verifying various proper uses of SkylarkCallable.
@@ -37,6 +40,22 @@ public class GoldenCase {
return 0;
}
+ @SkylarkCallable(name = "zero_arg_method_with_environment", doc = "", useEnvironment = true)
+ public Integer zeroArgMethod(Environment environment) {
+ return 0;
+ }
+
+ @SkylarkCallable(
+ name = "zero_arg_method_with_skylark_info",
+ doc = "",
+ useAst = true,
+ useLocation = true,
+ useEnvironment = true
+ )
+ public Integer zeroArgMethod(Location location, FuncallExpression ast, Environment environment) {
+ return 0;
+ }
+
@SkylarkCallable(
name = "three_arg_method",
doc = "")
@@ -44,6 +63,11 @@ public class GoldenCase {
return "bar";
}
+ @SkylarkCallable(name = "three_arg_method_with_ast", doc = "", useAst = true)
+ public String threeArgMethod(String one, Integer two, String three, FuncallExpression ast) {
+ return "bar";
+ }
+
@SkylarkCallable(
name = "three_arg_method_with_params",
doc = "",
@@ -55,4 +79,26 @@ public class GoldenCase {
public String threeArgMethodWithParams(String one, Integer two, String three) {
return "baz";
}
+
+ @SkylarkCallable(
+ name = "three_arg_method_with_params_and_info",
+ doc = "",
+ parameters = {
+ @Param(name = "one", type = String.class, named = true),
+ @Param(name = "two", type = Integer.class, named = true),
+ @Param(name = "three", type = String.class, named = true),
+ },
+ useAst = true,
+ useLocation = true,
+ useEnvironment = true
+ )
+ public String threeArgMethodWithParams(
+ String one,
+ Integer two,
+ String three,
+ Location location,
+ FuncallExpression ast,
+ Environment environment) {
+ return "baz";
+ }
}
diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/LocationMissing.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/LocationMissing.java
new file mode 100644
index 0000000000..6fd2ef3cb5
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/LocationMissing.java
@@ -0,0 +1,35 @@
+// Copyright 2018 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.skylarkinterface.processor.testsources;
+
+import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
+import com.google.devtools.build.lib.syntax.Environment;
+
+/**
+ * Test case for a SkylarkCallable method which does not have an appropriate Environment parameter
+ * despite having useLocation set.
+ */
+public class LocationMissing {
+
+ @SkylarkCallable(
+ name = "three_arg_method_missing_location",
+ doc = "",
+ useLocation = true,
+ useEnvironment = true
+ )
+ public String threeArgMethod(String one, Integer two, String three, Environment environment) {
+ return "bar";
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SkylarkInfoWrongOrder.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SkylarkInfoWrongOrder.java
new file mode 100644
index 0000000000..9e58070c67
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SkylarkInfoWrongOrder.java
@@ -0,0 +1,37 @@
+// Copyright 2018 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.skylarkinterface.processor.testsources;
+
+import com.google.devtools.build.lib.events.Location;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
+import com.google.devtools.build.lib.syntax.Environment;
+
+/**
+ * Test case for a SkylarkCallable method which specifies skylark-info parameters (for example
+ * Environment) before other parameters.
+ */
+public class SkylarkInfoWrongOrder {
+
+ @SkylarkCallable(
+ name = "three_arg_method_missing_location",
+ doc = "",
+ useLocation = true,
+ useEnvironment = true
+ )
+ public String threeArgMethod(
+ Location location, Environment environment, String one, Integer two, String three) {
+ return "bar";
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
index 9711dea3f5..8bb82f1279 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
@@ -233,12 +233,118 @@ public class SkylarkEvaluationTest extends EvaluationTest {
+ ", "
+ optionalNamed
+ ", "
- + nonNoneable.toString()
+ + nonNoneable
+ (noneable != Runtime.NONE ? ", " + noneable : "")
+ (multi != Runtime.NONE ? ", " + multi : "")
+ ")";
}
+ @SkylarkCallable(
+ name = "with_extra",
+ doc = "",
+ useLocation = true,
+ useAst = true,
+ useEnvironment = true
+ )
+ public String withExtraInterpreterParams(
+ Location location, FuncallExpression func, Environment env) {
+ return "with_extra("
+ + location.getStartLine()
+ + ", "
+ + func.getArguments().size()
+ + ", "
+ + env.isGlobal()
+ + ")";
+ }
+
+ @SkylarkCallable(
+ name = "with_params_and_extra",
+ doc = "",
+ mandatoryPositionals = 1,
+ parameters = {
+ @Param(name = "pos2", defaultValue = "False", type = Boolean.class),
+ @Param(
+ name = "posOrNamed",
+ defaultValue = "False",
+ type = Boolean.class,
+ positional = true,
+ named = true
+ ),
+ @Param(name = "named", type = Boolean.class, positional = false, named = true),
+ @Param(
+ name = "optionalNamed",
+ type = Boolean.class,
+ defaultValue = "False",
+ positional = false,
+ named = true
+ ),
+ @Param(
+ name = "nonNoneable",
+ type = Object.class,
+ defaultValue = "\"a\"",
+ positional = false,
+ named = true
+ ),
+ @Param(
+ name = "noneable",
+ type = Integer.class,
+ defaultValue = "None",
+ noneable = true,
+ positional = false,
+ named = true
+ ),
+ @Param(
+ name = "multi",
+ allowedTypes = {
+ @ParamType(type = String.class),
+ @ParamType(type = Integer.class),
+ @ParamType(type = SkylarkList.class, generic1 = Integer.class),
+ },
+ defaultValue = "None",
+ noneable = true,
+ positional = false,
+ named = true
+ )
+ },
+ useAst = true,
+ useLocation = true,
+ useEnvironment = true
+ )
+ public String withParamsAndExtraInterpreterParams(
+ Integer pos1,
+ boolean pos2,
+ boolean posOrNamed,
+ boolean named,
+ boolean optionalNamed,
+ Object nonNoneable,
+ Object noneable,
+ Object multi,
+ Location location,
+ FuncallExpression func,
+ Environment env) {
+ return "with_params_and_extra("
+ + pos1
+ + ", "
+ + pos2
+ + ", "
+ + posOrNamed
+ + ", "
+ + named
+ + ", "
+ + optionalNamed
+ + ", "
+ + nonNoneable
+ + (noneable != Runtime.NONE ? ", " + noneable : "")
+ + (multi != Runtime.NONE ? ", " + multi : "")
+ + ", "
+ + location.getStartLine()
+ + ", "
+ + func.getArguments().size()
+ + ", "
+ + env.isGlobal()
+ + ")";
+ }
+
@Override
public String toString() {
return "<mock>";
@@ -922,6 +1028,22 @@ public class SkylarkEvaluationTest extends EvaluationTest {
}
@Test
+ public void testJavaFunctionWithExtraInterpreterParams() throws Exception {
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .setUp("v = mock.with_extra()")
+ .testLookup("v", "with_extra(1, 0, true)");
+ }
+
+ @Test
+ public void testJavaFunctionWithParamsAndExtraInterpreterParams() throws Exception {
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .setUp("b = mock.with_params_and_extra(1, True, named=True)")
+ .testLookup("b", "with_params_and_extra(1, true, false, true, false, a, 1, 3, true)");
+ }
+
+ @Test
public void testStructAccessOfMethod() throws Exception {
new SkylarkTest()
.update("mock", new Mock())
@@ -1404,7 +1526,9 @@ public class SkylarkEvaluationTest extends EvaluationTest {
"struct_field_callable",
"value_of",
"voidfunc",
- "with_params");
+ "with_extra",
+ "with_params",
+ "with_params_and_extra");
}
@Test