aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build
diff options
context:
space:
mode:
authorGravatar cparsons <cparsons@google.com>2018-04-24 08:07:02 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-04-24 08:09:17 -0700
commit0e86686e3b1769213f70fef08047946d3d29db29 (patch)
tree9720596348f871070b9c7e651d4bd9bcc78c19f7 /src/test/java/com/google/devtools/build
parent451d2c9cd67d5d29e85784f403053f10b6e119ef (diff)
Introduce @SkylarkCallable.selfCall, to signify the containing class should be treated as a callable skylark object.
This will allow Skylark Provider objects to be better specified. For example, "JavaInfo" can have a fully-documented, fully-specified @SkylarkCallable method with selfCall=true to represent the method JavaInfo(), instead of being a subclass of BaseFunction and requiring a @SkylarkSignature annotation. There are no usages of this pattern introduced in this CL, and also no updates to docgen to support the new pattern. These will be introduced in another CL. RELNOTES: None. PiperOrigin-RevId: 194088227
Diffstat (limited to 'src/test/java/com/google/devtools/build')
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessorTest.java30
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/GoldenCase.java13
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/MultipleSelfCallMethods.java46
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SelfCallWithNoName.java31
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SelfCallWithStructField.java34
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java28
6 files changed, 182 insertions, 0 deletions
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 016fe30704..208e9a3e66 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
@@ -265,4 +265,34 @@ public final class SkylarkCallableProcessorTest {
"@SkylarkCallable annotated method has 3 parameters, but annotation declared "
+ "1 user-supplied parameters and 3 extra interpreter parameters.");
}
+
+ @Test
+ public void testSelfCallWithNoName() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("SelfCallWithNoName.java"))
+ .processedWith(new SkylarkCallableProcessor())
+ .failsToCompile()
+ .withErrorContaining(
+ "@SkylarkCallable-annotated methods with selfCall=true must have a name");
+ }
+
+ @Test
+ public void testSelfCallWithStructField() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("SelfCallWithStructField.java"))
+ .processedWith(new SkylarkCallableProcessor())
+ .failsToCompile()
+ .withErrorContaining(
+ "@SkylarkCallable-annotated methods with selfCall=true must have structField=false");
+ }
+
+ @Test
+ public void testMultipleSelfCallMethods() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("MultipleSelfCallMethods.java"))
+ .processedWith(new SkylarkCallableProcessor())
+ .failsToCompile()
+ .withErrorContaining(
+ "Containing class has more than one selfCall method defined.");
+ }
}
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 1b0e136fea..580be01a71 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
@@ -198,4 +198,17 @@ public class GoldenCase {
Environment environment) {
return "yar";
}
+
+ @SkylarkCallable(
+ name = "selfCallMethod",
+ selfCall = true,
+ parameters = {
+ @Param(name = "one", type = String.class, named = true),
+ @Param(name = "two", type = Integer.class, named = true),
+ },
+ documented = false
+ )
+ public Integer selfCallMethod(String one, Integer two) {
+ return 0;
+ }
}
diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/MultipleSelfCallMethods.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/MultipleSelfCallMethods.java
new file mode 100644
index 0000000000..674df67825
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/MultipleSelfCallMethods.java
@@ -0,0 +1,46 @@
+// 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.Param;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
+
+/**
+ * Test case for a class with multiple SkylarkCallable methods which have selfCall=true.
+ */
+public class MultipleSelfCallMethods {
+
+ @SkylarkCallable(
+ name = "selfCallMethod",
+ selfCall = true,
+ parameters = {
+ @Param(name = "one", type = String.class, named = true),
+ @Param(name = "two", type = Integer.class, named = true),
+ },
+ documented = false
+ )
+ public Integer selfCallMethod(String one, Integer two) {
+ return 0;
+ }
+
+ @SkylarkCallable(
+ name = "selfCallMethodTwo",
+ selfCall = true,
+ documented = false
+ )
+ public Integer selfCallMethodTwo() {
+ return 0;
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SelfCallWithNoName.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SelfCallWithNoName.java
new file mode 100644
index 0000000000..0469227ebc
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SelfCallWithNoName.java
@@ -0,0 +1,31 @@
+// 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 class with a SkylarkCallable method which has selfCall=true but no name.
+ */
+public class SelfCallWithNoName {
+
+ @SkylarkCallable(
+ selfCall = true,
+ documented = false
+ )
+ public Integer selfCallMethod() {
+ return 0;
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SelfCallWithStructField.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SelfCallWithStructField.java
new file mode 100644
index 0000000000..e096bf5810
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/SelfCallWithStructField.java
@@ -0,0 +1,34 @@
+// 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 class with a SkylarkCallable method which has selfCall=true and
+ * structField=true.
+ */
+public class SelfCallWithStructField {
+
+ @SkylarkCallable(
+ name = "selfCallMethod",
+ selfCall = true,
+ structField = true,
+ documented = false
+ )
+ public Integer selfCallMethod() {
+ return 0;
+ }
+}
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 f302535288..cb09f8d103 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
@@ -110,6 +110,15 @@ public class SkylarkEvaluationTest extends EvaluationTest {
@SkylarkModule(name = "Mock", doc = "")
static class Mock {
+ @SkylarkCallable(name = "MockFn", selfCall = true, documented = false,
+ parameters = {
+ @Param(name = "pos", positional = true, type = String.class),
+ }
+ )
+ public static String selfCall(String myName) {
+ return "I'm a mock named " + myName;
+ }
+
@SkylarkCallable(documented = false)
public static Integer valueOf(String str) {
return Integer.valueOf(str);
@@ -1203,6 +1212,25 @@ public class SkylarkEvaluationTest extends EvaluationTest {
}
@Test
+ public void testSelfCall() throws Exception {
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .setUp("v = mock('bestmock')")
+ .testLookup("v", "I'm a mock named bestmock");
+
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .setUp("mockfunction = mock", "v = mockfunction('bestmock')")
+ .testLookup("v", "I'm a mock named bestmock");
+
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .testIfErrorContains(
+ "expected string for 'pos' while calling MockFn but got int instead: 1",
+ "v = mock(1)");
+ }
+
+ @Test
public void testStructAccessAsFuncall() throws Exception {
foobar.configure(getClass().getDeclaredField("foobar").getAnnotation(SkylarkSignature.class));
new SkylarkTest()