diff options
author | Adrian Roos <roosa@google.com> | 2022-12-05 21:11:42 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-12-05 21:11:42 +0000 |
commit | 210c7d017424c9cf50cdb5fda52ca31febc92a13 (patch) | |
tree | 0baa92251e9c2c2d2522d953021138893ae84133 /javatests/com/google/turbine/processing | |
parent | 0a1bffd725f80e576b850263ee5ba54d7ecd7c58 (diff) | |
parent | 8742e9d19f14650ce3154e48a681476ea4c8fe1d (diff) | |
download | turbine-210c7d017424c9cf50cdb5fda52ca31febc92a13.tar.gz |
Merge remote-tracking branch 'aosp/upstream-main' into aosp/master am: 1317f29ad0 am: 8742e9d19fandroid-u-beta-1-gpl
Original change: https://android-review.googlesource.com/c/platform/external/turbine/+/2327873
Change-Id: Ied4766e3be049c753596ae8f2f221e2e1a59a7ab
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
Diffstat (limited to 'javatests/com/google/turbine/processing')
5 files changed, 148 insertions, 36 deletions
diff --git a/javatests/com/google/turbine/processing/AbstractTurbineTypesBiFunctionTest.java b/javatests/com/google/turbine/processing/AbstractTurbineTypesBiFunctionTest.java new file mode 100644 index 0000000..e00673d --- /dev/null +++ b/javatests/com/google/turbine/processing/AbstractTurbineTypesBiFunctionTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2022 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.turbine.processing; + +import static com.google.common.truth.Truth.assertWithMessage; + +import javax.lang.model.element.Element; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.util.Types; + +/** + * A combo test for {@link TurbineTypes} that compares the behaviour of bifunctions like {@link + * Types#asMemberOf(DeclaredType, Element)} with javac's implementation. + */ +abstract class AbstractTurbineTypesBiFunctionTest<T> extends AbstractTurbineTypesTest { + + final String testDescription; + final TypesBiFunctionInput javacInput; + final TypesBiFunctionInput turbineInput; + + public AbstractTurbineTypesBiFunctionTest( + String testDescription, TypesBiFunctionInput javacInput, TypesBiFunctionInput turbineInput) { + this.testDescription = testDescription; + this.javacInput = javacInput; + this.turbineInput = turbineInput; + } + + protected void test(String symbol, TypeBiFunction<T> predicate) { + assertWithMessage("%s = %s", javacInput.format(symbol), turbineInput.format(symbol)) + .that(turbineInput.apply(predicate)) + .isEqualTo(javacInput.apply(predicate)); + } +} diff --git a/javatests/com/google/turbine/processing/AbstractTurbineTypesBiPredicateTest.java b/javatests/com/google/turbine/processing/AbstractTurbineTypesBiPredicateTest.java index 6ea6e72..08891eb 100644 --- a/javatests/com/google/turbine/processing/AbstractTurbineTypesBiPredicateTest.java +++ b/javatests/com/google/turbine/processing/AbstractTurbineTypesBiPredicateTest.java @@ -16,8 +16,6 @@ package com.google.turbine.processing; -import static com.google.common.truth.Truth.assertWithMessage; - import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Types; @@ -25,22 +23,10 @@ import javax.lang.model.util.Types; * A combo test for {@link TurbineTypes} that compares the behaviour of bipredicates like {@link * Types#isSubtype(TypeMirror, TypeMirror)} with javac's implementation. */ -abstract class AbstractTurbineTypesBiPredicateTest extends AbstractTurbineTypesTest { - - final String testDescription; - final TypesBiFunctionInput javacInput; - final TypesBiFunctionInput turbineInput; - +abstract class AbstractTurbineTypesBiPredicateTest + extends AbstractTurbineTypesBiFunctionTest<Boolean> { public AbstractTurbineTypesBiPredicateTest( String testDescription, TypesBiFunctionInput javacInput, TypesBiFunctionInput turbineInput) { - this.testDescription = testDescription; - this.javacInput = javacInput; - this.turbineInput = turbineInput; - } - - protected void test(String symbol, TypeBiPredicate predicate) { - assertWithMessage("%s = %s", javacInput.format(symbol), turbineInput.format(symbol)) - .that(turbineInput.apply(predicate)) - .isEqualTo(javacInput.apply(predicate)); + super(testDescription, javacInput, turbineInput); } } diff --git a/javatests/com/google/turbine/processing/AbstractTurbineTypesTest.java b/javatests/com/google/turbine/processing/AbstractTurbineTypesTest.java index 7d8d479..02df1ec 100644 --- a/javatests/com/google/turbine/processing/AbstractTurbineTypesTest.java +++ b/javatests/com/google/turbine/processing/AbstractTurbineTypesTest.java @@ -31,6 +31,7 @@ import com.google.common.collect.ListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import com.google.common.collect.MultimapBuilder; +import com.google.common.collect.ObjectArrays; import com.google.common.collect.Streams; import com.google.turbine.binder.Binder; import com.google.turbine.binder.Binder.BindingResult; @@ -93,8 +94,8 @@ class AbstractTurbineTypesTest { } } - protected interface TypeBiPredicate { - boolean apply(Types types, TypeMirror a, TypeMirror b); + protected interface TypeBiFunction<T> { + T apply(Types types, TypeMirror a, TypeMirror b); } static class TypesBiFunctionInput { @@ -108,8 +109,8 @@ class AbstractTurbineTypesTest { this.rhs = rhs; } - boolean apply(TypeBiPredicate predicate) { - return predicate.apply(types, lhs, rhs); + <T> T apply(TypeBiFunction<T> function) { + return function.apply(types, lhs, rhs); } String format(String symbol) { @@ -232,18 +233,23 @@ class AbstractTurbineTypesTest { "Float", "Double", }, - // type annotations - { - "@A List<@B Integer>", - "@A List", - "@A int @B []", - "@A List<@A int @B []>", - "Map.@A Entry<@B Integer, @C Number>", - }, }; + + // type annotations + List<String> annotatedTypes = new ArrayList<>(); + annotatedTypes.add("@A int @B []"); + // The string representation of these types changed in JDK 19, see JDK-8281238 + if (Runtime.version().feature() >= 19) { + annotatedTypes.add("@A List<@B Integer>"); + annotatedTypes.add("@A List"); + annotatedTypes.add("@A List<@A int @B []>"); + annotatedTypes.add("Map.@A Entry<@B Integer, @C Number>"); + } + List<String> files = new ArrayList<>(); AtomicInteger idx = new AtomicInteger(); - for (String[] group : types) { + for (String[] group : + ObjectArrays.<String[]>concat(annotatedTypes.toArray(new String[0]), types)) { StringBuilder sb = new StringBuilder(); Joiner.on('\n') .appendTo( diff --git a/javatests/com/google/turbine/processing/TurbineFilerTest.java b/javatests/com/google/turbine/processing/TurbineFilerTest.java index 83dcc70..96c325b 100644 --- a/javatests/com/google/turbine/processing/TurbineFilerTest.java +++ b/javatests/com/google/turbine/processing/TurbineFilerTest.java @@ -23,7 +23,6 @@ import static org.junit.Assert.assertThrows; import com.google.common.base.Function; import com.google.common.base.Supplier; -import com.google.common.io.ByteStreams; import com.google.common.io.CharStreams; import com.google.turbine.diag.SourceFile; import java.io.FileNotFoundException; @@ -127,8 +126,7 @@ public class TurbineFilerTest { Collection<SourceFile> unused = filer.finishRound(); FileObject output = filer.getResource(StandardLocation.SOURCE_OUTPUT, "com.foo", "Bar.java"); - assertThat(new String(ByteStreams.toByteArray(output.openInputStream()), UTF_8)) - .isEqualTo("hello"); + assertThat(new String(output.openInputStream().readAllBytes(), UTF_8)).isEqualTo("hello"); assertThat(output.getCharContent(false).toString()).isEqualTo("hello"); assertThat(CharStreams.toString(output.openReader(true))).isEqualTo("hello"); } @@ -142,8 +140,7 @@ public class TurbineFilerTest { Collection<SourceFile> unused = filer.finishRound(); FileObject output = filer.getResource(StandardLocation.CLASS_OUTPUT, "com.foo", "Baz.class"); - assertThat(new String(ByteStreams.toByteArray(output.openInputStream()), UTF_8)) - .isEqualTo("goodbye"); + assertThat(new String(output.openInputStream().readAllBytes(), UTF_8)).isEqualTo("goodbye"); assertThat(output.getCharContent(false).toString()).isEqualTo("goodbye"); assertThat(CharStreams.toString(output.openReader(true))).isEqualTo("goodbye"); } @@ -153,7 +150,7 @@ public class TurbineFilerTest { FileObject resource = filer.getResource(StandardLocation.ANNOTATION_PROCESSOR_PATH, "META-INF", "MANIFEST.MF"); - assertThat(new String(ByteStreams.toByteArray(resource.openInputStream()), UTF_8)) + assertThat(new String(resource.openInputStream().readAllBytes(), UTF_8)) .contains("Manifest-Version:"); assertThat(CharStreams.toString(resource.openReader(true))).contains("Manifest-Version:"); assertThat(resource.getCharContent(false).toString()).contains("Manifest-Version:"); diff --git a/javatests/com/google/turbine/processing/TurbineTypesAsMemberOfTest.java b/javatests/com/google/turbine/processing/TurbineTypesAsMemberOfTest.java new file mode 100644 index 0000000..1a368c9 --- /dev/null +++ b/javatests/com/google/turbine/processing/TurbineTypesAsMemberOfTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2022 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.turbine.processing; + +import static com.google.common.truth.TruthJUnit.assume; +import static org.junit.Assert.assertThrows; + +import javax.lang.model.element.Element; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.type.TypeVariable; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class TurbineTypesAsMemberOfTest extends AbstractTurbineTypesBiFunctionTest<String> { + + @Parameters(name = "{index}: {0}") + public static Iterable<Object[]> parameters() throws Exception { + return binaryParameters(); + } + + public TurbineTypesAsMemberOfTest( + String testDescription, TypesBiFunctionInput javacInput, TypesBiFunctionInput turbineInput) { + super(testDescription, javacInput, turbineInput); + } + + @Test + public void asMemberOf() { + assume().that(javacInput.lhs.getKind()).isEqualTo(TypeKind.DECLARED); + assume().that(javacInput.rhs.getKind()).isAnyOf(TypeKind.TYPEVAR, TypeKind.DECLARED); + + TypeBiFunction<String> predicate = + (types, lhs, rhs) -> types.asMemberOf((DeclaredType) lhs, element(rhs)).toString(); + + try { + String unused = javacInput.apply(predicate); + } catch (IllegalArgumentException e) { + assertThrows( + turbineInput.format("asMemberOf"), + IllegalArgumentException.class, + () -> turbineInput.apply(predicate)); + return; + } + + test("asMemberOf", predicate); + } + + private static Element element(TypeMirror rhs) { + switch (rhs.getKind()) { + case TYPEVAR: + return ((TypeVariable) rhs).asElement(); + case DECLARED: + return ((DeclaredType) rhs).asElement(); + default: + throw new AssertionError(rhs.getKind()); + } + } +} |