diff options
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib')
8 files changed, 299 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD index 538cb8fd2c..cf66e2fe07 100644 --- a/src/test/java/com/google/devtools/build/lib/BUILD +++ b/src/test/java/com/google/devtools/build/lib/BUILD @@ -58,6 +58,7 @@ filegroup( "//src/test/java/com/google/devtools/build/lib/skyframe/serialization:srcs", "//src/test/java/com/google/devtools/build/lib/skyframe:srcs", "//src/test/java/com/google/devtools/build/lib/skylark:srcs", + "//src/test/java/com/google/devtools/build/lib/skylarkinterface/processor:srcs", ], visibility = ["//src:__pkg__"], ) 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 new file mode 100644 index 0000000000..d26c2d4c41 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/BUILD @@ -0,0 +1,29 @@ +licenses(["notice"]) # Apache 2.0 + +filegroup( + name = "srcs", + srcs = glob( + ["**"], + ), + visibility = [ + "//src/test/java/com/google/devtools/build/lib:__pkg__", + ], +) + +java_test( + name = "SkylarkCallableProcessorTest", + srcs = ["SkylarkCallableProcessorTest.java"], + resources = [":ProcessorTestFiles"], + deps = [ + "//src/main/java/com/google/devtools/build/lib/skylarkinterface/processor:annotation_preprocessor", + "//third_party:compile_testing", + "//third_party:guava", + "//third_party:junit4", + "//third_party:truth", + ], +) + +filegroup( + name = "ProcessorTestFiles", + srcs = glob(["testsources/*.java"]), +) 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 new file mode 100644 index 0000000000..3b2ce81bc3 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/SkylarkCallableProcessorTest.java @@ -0,0 +1,85 @@ +// 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; + +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.testing.compile.JavaFileObjects; +import javax.tools.JavaFileObject; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Unit tests for SkylarkCallableProcessor. + */ +@RunWith(JUnit4.class) +public final class SkylarkCallableProcessorTest { + + private static JavaFileObject getFile(String pathToFile) { + return JavaFileObjects.forResource(Resources.getResource( + SkylarkCallableProcessorTest.class, "testsources/" + pathToFile)); + } + + @Test + public void testGoldenCase() throws Exception { + assertAbout(javaSource()) + .that(getFile("GoldenCase.java")) + .processedWith(new SkylarkCallableProcessor()) + .compilesWithoutError(); + } + + @Test + public void testPrivateMethod() throws Exception { + assertAbout(javaSource()) + .that(getFile("PrivateMethod.java")) + .processedWith(new SkylarkCallableProcessor()) + .failsToCompile() + .withErrorContaining("@SkylarkCallable annotated methods must be public."); + } + + @Test + public void testStructFieldWithArguments() throws Exception { + assertAbout(javaSource()) + .that(getFile("StructFieldWithArguments.java")) + .processedWith(new SkylarkCallableProcessor()) + .failsToCompile() + .withErrorContaining( + "@SkylarkCallable annotated methods with structField=true must have zero arguments."); + } + + @Test + public void testArgumentMissing() throws Exception { + assertAbout(javaSource()) + .that(getFile("ArgumentMissing.java")) + .processedWith(new SkylarkCallableProcessor()) + .failsToCompile() + .withErrorContaining( + "@SkylarkCallable annotated method has 0 parameters, but annotation declared 1."); + } + + @Test + public void testTooManyArguments() throws Exception { + assertAbout(javaSource()) + .that(getFile("TooManyArguments.java")) + .processedWith(new SkylarkCallableProcessor()) + .failsToCompile() + .withErrorContaining( + "@SkylarkCallable annotated method has 2 parameters, but annotation declared 1."); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/ArgumentMissing.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/ArgumentMissing.java new file mode 100644 index 0000000000..f9face4b0b --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/ArgumentMissing.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.analysis.skylarkinterface.processor.testsources; + +import com.google.devtools.build.lib.skylarkinterface.Param; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; + +/** + * Test case for a SkylarkCallable method which has no arguments when the annotation indicates it + * should. + */ +public class ArgumentMissing { + + @SkylarkCallable( + name = "method_with_params", + doc = "", + parameters = { + @Param(name = "a_parameter", type = String.class, named = true), + }) + public String methodWithParams() { + return "bunny"; + } +} 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 new file mode 100644 index 0000000000..7682e57ce6 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/GoldenCase.java @@ -0,0 +1,58 @@ +// 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.analysis.skylarkinterface.processor.testsources; + +import com.google.devtools.build.lib.skylarkinterface.Param; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; + +/** + * Test source file verifying various proper uses of SkylarkCallable. + */ +public class GoldenCase { + + @SkylarkCallable( + name = "struct_field_method", + doc = "", + structField = true) + public String structFieldMethod() { + return "foo"; + } + + @SkylarkCallable( + name = "zero_arg_method", + doc = "") + public Integer zeroArgMethod() { + return 0; + } + + @SkylarkCallable( + name = "three_arg_method", + doc = "") + public String threeArgMethod(String one, Integer two, String three) { + return "bar"; + } + + @SkylarkCallable( + name = "three_arg_method_with_params", + 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), + }) + public String threeArgMethodWithParams(String one, Integer two, String three) { + return "baz"; + } +} diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/PrivateMethod.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/PrivateMethod.java new file mode 100644 index 0000000000..b0ae76a524 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/PrivateMethod.java @@ -0,0 +1,28 @@ +// 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.analysis.skylarkinterface.processor.testsources; + +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; + +/** + * Test case which verifies a method annotated with SkylarkCallable cannot be private. + */ +public class PrivateMethod { + + @SkylarkCallable(name = "private_method", doc = "A private method") + private String privateMethod() { + return "kitten"; + } +} diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/StructFieldWithArguments.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/StructFieldWithArguments.java new file mode 100644 index 0000000000..7f9a674ae8 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/StructFieldWithArguments.java @@ -0,0 +1,28 @@ +// 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.analysis.skylarkinterface.processor.testsources; + +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; + +/** + * Test case which verifies a struct field method cannot have arguments. + */ +public class StructFieldWithArguments { + + @SkylarkCallable(name = "struct_field_method", structField = true, doc = "A private method") + public String structFieldMethod(String foo) { + return "puppy"; + } +} diff --git a/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/TooManyArguments.java b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/TooManyArguments.java new file mode 100644 index 0000000000..476099c353 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/skylarkinterface/processor/testsources/TooManyArguments.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.analysis.skylarkinterface.processor.testsources; + +import com.google.devtools.build.lib.skylarkinterface.Param; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; + +/** + * Test case for a SkylarkCallable method which has more arguments than are declared by the + * annotation. + */ +public class TooManyArguments { + + @SkylarkCallable( + name = "method_with_too_many_arguments", + doc = "", + parameters = { + @Param(name = "parameter_one", type = String.class, named = true), + }) + public String methodWithTooManyArguments(String parameterOne, String parameterTwo) { + return "dolphin"; + } +} |