Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 1 | // Copyright (c) 2021, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 5 | package records; |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 6 | |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 7 | import static com.android.tools.r8.DiagnosticsMatcher.diagnosticType; |
| 8 | import static com.android.tools.r8.utils.codeinspector.Matchers.isAbsent; |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 9 | import static com.android.tools.r8.utils.codeinspector.Matchers.isPresentIf; |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 10 | import static org.hamcrest.MatcherAssert.assertThat; |
| 11 | import static org.junit.Assert.assertThrows; |
| 12 | import static org.junit.Assert.assertTrue; |
| 13 | |
| 14 | import com.android.tools.r8.CompilationFailedException; |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 15 | import com.android.tools.r8.D8TestBuilder; |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 16 | import com.android.tools.r8.D8TestCompileResult; |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 17 | import com.android.tools.r8.GlobalSyntheticsTestingConsumer; |
Clément Béra | 528ce25 | 2022-06-28 15:05:59 +0200 | [diff] [blame] | 18 | import com.android.tools.r8.OutputMode; |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 19 | import com.android.tools.r8.TestBase; |
| 20 | import com.android.tools.r8.TestParameters; |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 21 | import com.android.tools.r8.TestParametersCollection; |
| 22 | import com.android.tools.r8.errors.DuplicateTypesDiagnostic; |
| 23 | import com.android.tools.r8.errors.MissingGlobalSyntheticsConsumerDiagnostic; |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 24 | import com.android.tools.r8.utils.StringUtils; |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 25 | import com.android.tools.r8.utils.codeinspector.CodeInspector; |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 26 | import java.nio.file.Path; |
Clément Béra | 528ce25 | 2022-06-28 15:05:59 +0200 | [diff] [blame] | 27 | import org.junit.Assume; |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.junit.runners.Parameterized; |
| 31 | |
| 32 | @RunWith(Parameterized.class) |
| 33 | public class RecordMergeTest extends TestBase { |
| 34 | |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 35 | private static final String EXPECTED_RESULT_1 = |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 36 | StringUtils.lines("BobX", "43", "FelixX", "-1", "print", "Bob43", "extra"); |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 37 | |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 38 | private static final String EXPECTED_RESULT_2 = |
Christoffer Quist Adamsen | 2f14e24 | 2023-06-29 11:04:22 +0200 | [diff] [blame] | 39 | StringUtils.lines( |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 40 | "Jane Doe", "42", "true", "true", "true", "false", "false", "false", "false"); |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 41 | |
| 42 | private final TestParameters parameters; |
| 43 | |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 44 | public RecordMergeTest(TestParameters parameters) { |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 45 | this.parameters = parameters; |
| 46 | } |
| 47 | |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 48 | @Parameterized.Parameters(name = "{0}") |
| 49 | public static TestParametersCollection data() { |
| 50 | return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build(); |
| 51 | } |
| 52 | |
| 53 | @Test |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 54 | public void testNoGlobalSyntheticsConsumer() throws Exception { |
| 55 | D8TestBuilder builder = |
| 56 | testForD8(parameters.getBackend()) |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 57 | .addStrippedOuter(getClass()) |
| 58 | .addProgramClassesAndInnerClasses(RecordWithMembers.class) |
| 59 | .addClasspathClassesAndInnerClasses(SimpleRecord.class) |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 60 | .setMinApi(parameters) |
| 61 | .setIntermediate(true); |
Clément Béra | d8769c2 | 2024-08-15 08:43:58 +0200 | [diff] [blame] | 62 | if (isRecordsFullyDesugaredForD8(parameters)) { |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 63 | assertThrows( |
| 64 | CompilationFailedException.class, |
| 65 | () -> |
| 66 | builder.compileWithExpectedDiagnostics( |
| 67 | diagnostics -> |
| 68 | diagnostics |
| 69 | .assertOnlyErrors() |
| 70 | .assertErrorsMatch( |
| 71 | diagnosticType(MissingGlobalSyntheticsConsumerDiagnostic.class)))); |
Søren Gjesse | fb2b4ba | 2023-11-21 09:11:14 +0100 | [diff] [blame] | 72 | } else { |
| 73 | builder.compile(); |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 74 | } |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testMergeDesugaredInputs() throws Exception { |
Clément Béra | 528ce25 | 2022-06-28 15:05:59 +0200 | [diff] [blame] | 79 | testMergeDesugaredInputsDexPerClass(false); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testMergeDesugaredInputsDexPerClass() throws Exception { |
| 84 | Assume.assumeTrue("CF is already run from the other test", parameters.isDexRuntime()); |
| 85 | testMergeDesugaredInputsDexPerClass(true); |
| 86 | } |
| 87 | |
| 88 | private void testMergeDesugaredInputsDexPerClass(boolean filePerClass) throws Exception { |
Ian Zerny | e736dec | 2022-05-05 20:07:45 +0200 | [diff] [blame] | 89 | GlobalSyntheticsTestingConsumer globals1 = new GlobalSyntheticsTestingConsumer(); |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 90 | Path output1 = |
| 91 | testForD8(parameters.getBackend()) |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 92 | .addStrippedOuter(getClass()) |
| 93 | .addProgramClassesAndInnerClasses(RecordWithMembers.class) |
| 94 | .addClasspathClassesAndInnerClasses(SimpleRecord.class) |
Christoffer Quist Adamsen | d4d9360 | 2023-02-21 15:28:42 +0100 | [diff] [blame] | 95 | .setMinApi(parameters) |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 96 | .setIntermediate(true) |
Clément Béra | 528ce25 | 2022-06-28 15:05:59 +0200 | [diff] [blame] | 97 | .applyIf( |
| 98 | filePerClass && !parameters.isCfRuntime(), |
| 99 | b -> b.setOutputMode(OutputMode.DexFilePerClassFile)) |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 100 | .apply(b -> b.getBuilder().setGlobalSyntheticsConsumer(globals1)) |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 101 | .compile() |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 102 | .inspect(this::assertDoesNotHaveRecordTag) |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 103 | .writeToZip(); |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 104 | |
Ian Zerny | e736dec | 2022-05-05 20:07:45 +0200 | [diff] [blame] | 105 | GlobalSyntheticsTestingConsumer globals2 = new GlobalSyntheticsTestingConsumer(); |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 106 | Path output2 = |
| 107 | testForD8(parameters.getBackend()) |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 108 | .addProgramClassesAndInnerClasses(SimpleRecord.class) |
| 109 | .addClasspathClassesAndInnerClasses(getClass()) |
Christoffer Quist Adamsen | d4d9360 | 2023-02-21 15:28:42 +0100 | [diff] [blame] | 110 | .setMinApi(parameters) |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 111 | .setIntermediate(true) |
Clément Béra | 528ce25 | 2022-06-28 15:05:59 +0200 | [diff] [blame] | 112 | .applyIf( |
| 113 | filePerClass && !parameters.isCfRuntime(), |
| 114 | b -> b.setOutputMode(OutputMode.DexFilePerClassFile)) |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 115 | .apply(b -> b.getBuilder().setGlobalSyntheticsConsumer(globals2)) |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 116 | .compile() |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 117 | .inspect(this::assertDoesNotHaveRecordTag) |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 118 | .writeToZip(); |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 119 | |
Clément Béra | d8769c2 | 2024-08-15 08:43:58 +0200 | [diff] [blame] | 120 | assertTrue(isRecordsFullyDesugaredForD8(parameters) ^ !globals1.hasGlobals()); |
| 121 | assertTrue(isRecordsFullyDesugaredForD8(parameters) ^ !globals2.hasGlobals()); |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 122 | |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 123 | D8TestCompileResult result = |
| 124 | testForD8(parameters.getBackend()) |
| 125 | .addProgramFiles(output1, output2) |
Ian Zerny | e736dec | 2022-05-05 20:07:45 +0200 | [diff] [blame] | 126 | .apply( |
| 127 | b -> |
| 128 | b.getBuilder() |
Clément Béra | 528ce25 | 2022-06-28 15:05:59 +0200 | [diff] [blame] | 129 | .addGlobalSyntheticsResourceProviders(globals1.getProviders()) |
| 130 | .addGlobalSyntheticsResourceProviders(globals2.getProviders())) |
Christoffer Quist Adamsen | d4d9360 | 2023-02-21 15:28:42 +0100 | [diff] [blame] | 131 | .setMinApi(parameters) |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 132 | .compile() |
| 133 | .inspect(this::assertHasRecordTag); |
| 134 | |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 135 | result |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 136 | .run(parameters.getRuntime(), RecordWithMembers.class) |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 137 | .applyIf( |
Clément Béra | d8769c2 | 2024-08-15 08:43:58 +0200 | [diff] [blame] | 138 | isRecordsFullyDesugaredForD8(parameters) |
Søren Gjesse | fb2b4ba | 2023-11-21 09:11:14 +0100 | [diff] [blame] | 139 | || runtimeWithRecordsSupport(parameters.getRuntime()), |
| 140 | r -> r.assertSuccessWithOutput(EXPECTED_RESULT_1), |
| 141 | r -> r.assertFailureWithErrorThatThrows(NoClassDefFoundError.class)); |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 142 | result |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 143 | .run(parameters.getRuntime(), SimpleRecord.class) |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 144 | .applyIf( |
Clément Béra | d8769c2 | 2024-08-15 08:43:58 +0200 | [diff] [blame] | 145 | isRecordsFullyDesugaredForD8(parameters) |
Søren Gjesse | fb2b4ba | 2023-11-21 09:11:14 +0100 | [diff] [blame] | 146 | || runtimeWithRecordsSupport(parameters.getRuntime()), |
| 147 | r -> r.assertSuccessWithOutput(EXPECTED_RESULT_2), |
| 148 | r -> r.assertFailureWithErrorThatThrows(NoClassDefFoundError.class)); |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testMergeDesugaredAndNonDesugaredInputs() throws Exception { |
Ian Zerny | e736dec | 2022-05-05 20:07:45 +0200 | [diff] [blame] | 153 | GlobalSyntheticsTestingConsumer globals1 = new GlobalSyntheticsTestingConsumer(); |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 154 | Path output1 = |
| 155 | testForD8(parameters.getBackend()) |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 156 | .addProgramClassesAndInnerClasses(RecordWithMembers.class) |
| 157 | .addClasspathClasses(getClass()) |
| 158 | .addClasspathClassesAndInnerClasses(SimpleRecord.class) |
Christoffer Quist Adamsen | d4d9360 | 2023-02-21 15:28:42 +0100 | [diff] [blame] | 159 | .setMinApi(parameters) |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 160 | .setIntermediate(true) |
| 161 | .apply(b -> b.getBuilder().setGlobalSyntheticsConsumer(globals1)) |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 162 | .compile() |
| 163 | .writeToZip(); |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 164 | |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 165 | D8TestCompileResult result = |
| 166 | testForD8(parameters.getBackend()) |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 167 | .addStrippedOuter(getClass()) |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 168 | .addProgramFiles(output1) |
Ian Zerny | e736dec | 2022-05-05 20:07:45 +0200 | [diff] [blame] | 169 | .apply( |
| 170 | b -> b.getBuilder().addGlobalSyntheticsResourceProviders(globals1.getProviders())) |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 171 | .addProgramClassesAndInnerClasses(SimpleRecord.class) |
| 172 | .addClasspathClassesAndInnerClasses(getClass()) |
Christoffer Quist Adamsen | d4d9360 | 2023-02-21 15:28:42 +0100 | [diff] [blame] | 173 | .setMinApi(parameters) |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 174 | .compile(); |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 175 | result |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 176 | .run(parameters.getRuntime(), RecordWithMembers.class) |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 177 | .applyIf( |
Clément Béra | d8769c2 | 2024-08-15 08:43:58 +0200 | [diff] [blame] | 178 | isRecordsFullyDesugaredForD8(parameters) |
Søren Gjesse | fb2b4ba | 2023-11-21 09:11:14 +0100 | [diff] [blame] | 179 | || runtimeWithRecordsSupport(parameters.getRuntime()), |
| 180 | r -> r.assertSuccessWithOutput(EXPECTED_RESULT_1), |
| 181 | r -> r.assertFailureWithErrorThatThrows(NoClassDefFoundError.class)); |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 182 | result |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 183 | .run(parameters.getRuntime(), SimpleRecord.class) |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 184 | .applyIf( |
Clément Béra | d8769c2 | 2024-08-15 08:43:58 +0200 | [diff] [blame] | 185 | isRecordsFullyDesugaredForD8(parameters) |
Søren Gjesse | fb2b4ba | 2023-11-21 09:11:14 +0100 | [diff] [blame] | 186 | || runtimeWithRecordsSupport(parameters.getRuntime()), |
| 187 | r -> r.assertSuccessWithOutput(EXPECTED_RESULT_2), |
| 188 | r -> r.assertFailureWithErrorThatThrows(NoClassDefFoundError.class)); |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 189 | } |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 190 | |
| 191 | @Test |
| 192 | public void testMergeNonIntermediates() throws Exception { |
| 193 | Path output1 = |
| 194 | testForD8(parameters.getBackend()) |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 195 | .addStrippedOuter(getClass()) |
| 196 | .addProgramClassesAndInnerClasses(RecordWithMembers.class) |
| 197 | .addClasspathClassesAndInnerClasses(SimpleRecord.class) |
Christoffer Quist Adamsen | d4d9360 | 2023-02-21 15:28:42 +0100 | [diff] [blame] | 198 | .setMinApi(parameters) |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 199 | .compile() |
| 200 | .inspect(this::assertHasRecordTag) |
| 201 | .writeToZip(); |
| 202 | |
| 203 | Path output2 = |
| 204 | testForD8(parameters.getBackend()) |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 205 | .addProgramClassesAndInnerClasses(SimpleRecord.class) |
| 206 | .addClasspathClassesAndInnerClasses(getClass()) |
Christoffer Quist Adamsen | d4d9360 | 2023-02-21 15:28:42 +0100 | [diff] [blame] | 207 | .setMinApi(parameters) |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 208 | .compile() |
| 209 | .inspect(this::assertHasRecordTag) |
| 210 | .writeToZip(); |
| 211 | |
Clément Béra | d8769c2 | 2024-08-15 08:43:58 +0200 | [diff] [blame] | 212 | if (!isRecordsFullyDesugaredForD8(parameters)) { |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 213 | D8TestCompileResult result = |
| 214 | testForD8(parameters.getBackend()) |
| 215 | .addProgramFiles(output1, output2) |
| 216 | .setMinApi(parameters) |
| 217 | .compile(); |
| 218 | result |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 219 | .run(parameters.getRuntime(), RecordWithMembers.class) |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 220 | .applyIf( |
Clément Béra | d8769c2 | 2024-08-15 08:43:58 +0200 | [diff] [blame] | 221 | isRecordsFullyDesugaredForD8(parameters) |
Søren Gjesse | fb2b4ba | 2023-11-21 09:11:14 +0100 | [diff] [blame] | 222 | || runtimeWithRecordsSupport(parameters.getRuntime()), |
| 223 | r -> r.assertSuccessWithOutput(EXPECTED_RESULT_1), |
| 224 | r -> r.assertFailureWithErrorThatThrows(NoClassDefFoundError.class)); |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 225 | result |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 226 | .run(parameters.getRuntime(), SimpleRecord.class) |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 227 | .applyIf( |
Clément Béra | d8769c2 | 2024-08-15 08:43:58 +0200 | [diff] [blame] | 228 | isRecordsFullyDesugaredForD8(parameters) |
Søren Gjesse | fb2b4ba | 2023-11-21 09:11:14 +0100 | [diff] [blame] | 229 | || runtimeWithRecordsSupport(parameters.getRuntime()), |
| 230 | r -> r.assertSuccessWithOutput(EXPECTED_RESULT_2), |
| 231 | r -> r.assertFailureWithErrorThatThrows(NoClassDefFoundError.class)); |
Søren Gjesse | 12a9892 | 2023-09-11 10:24:49 +0200 | [diff] [blame] | 232 | } else { |
| 233 | assertThrows( |
| 234 | CompilationFailedException.class, |
| 235 | () -> |
| 236 | testForD8(parameters.getBackend()) |
| 237 | .addProgramFiles(output1, output2) |
| 238 | .setMinApi(parameters) |
| 239 | .compileWithExpectedDiagnostics( |
| 240 | diagnostics -> |
| 241 | diagnostics |
| 242 | .assertOnlyErrors() |
| 243 | .assertErrorsMatch(diagnosticType(DuplicateTypesDiagnostic.class)))); |
| 244 | } |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | private void assertHasRecordTag(CodeInspector inspector) { |
| 248 | // Note: this should be asserting on record tag. |
Søren Gjesse | fb2b4ba | 2023-11-21 09:11:14 +0100 | [diff] [blame] | 249 | assertThat( |
Clément Béra | d8769c2 | 2024-08-15 08:43:58 +0200 | [diff] [blame] | 250 | inspector.clazz("java.lang.Record"), isPresentIf(isRecordsFullyDesugaredForD8(parameters))); |
Ian Zerny | ef26714 | 2022-03-28 12:40:03 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private void assertDoesNotHaveRecordTag(CodeInspector inspector) { |
| 254 | // Note: this should be asserting on record tag. |
| 255 | assertThat(inspector.clazz("java.lang.Record"), isAbsent()); |
| 256 | } |
Clément Béra | 0f1f4f7 | 2024-09-11 10:21:14 +0200 | [diff] [blame] | 257 | |
| 258 | public class RecordWithMembers { |
| 259 | record PersonWithConstructors(String name, int age) { |
| 260 | |
| 261 | public PersonWithConstructors(String name, int age) { |
| 262 | this.name = name + "X"; |
| 263 | this.age = age; |
| 264 | } |
| 265 | |
| 266 | public PersonWithConstructors(String name) { |
| 267 | this(name, -1); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | record PersonWithMethods(String name, int age) { |
| 272 | public static void staticPrint() { |
| 273 | System.out.println("print"); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public String toString() { |
| 278 | return name + age; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | record PersonWithFields(String name, int age) { |
| 283 | |
| 284 | // Extra instance fields are not allowed on records. |
| 285 | public static String globalName; |
| 286 | } |
| 287 | |
| 288 | public static void main(String[] args) { |
| 289 | personWithConstructorTest(); |
| 290 | personWithMethodsTest(); |
| 291 | personWithFieldsTest(); |
| 292 | } |
| 293 | |
| 294 | private static void personWithConstructorTest() { |
| 295 | PersonWithConstructors bob = new PersonWithConstructors("Bob", 43); |
| 296 | System.out.println(bob.name()); |
| 297 | System.out.println(bob.age()); |
| 298 | PersonWithConstructors felix = new PersonWithConstructors("Felix"); |
| 299 | System.out.println(felix.name()); |
| 300 | System.out.println(felix.age()); |
| 301 | } |
| 302 | |
| 303 | private static void personWithMethodsTest() { |
| 304 | PersonWithMethods.staticPrint(); |
| 305 | PersonWithMethods bob = new PersonWithMethods("Bob", 43); |
| 306 | System.out.println(bob.toString()); |
| 307 | } |
| 308 | |
| 309 | private static void personWithFieldsTest() { |
| 310 | PersonWithFields.globalName = "extra"; |
| 311 | System.out.println(PersonWithFields.globalName); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | public class SimpleRecord { |
| 316 | |
| 317 | record Person(String name, int age) {} |
| 318 | |
| 319 | public static void main(String[] args) { |
| 320 | Person janeDoe = new Person("Jane Doe", 42); |
| 321 | System.out.println(janeDoe.name()); |
| 322 | System.out.println(janeDoe.age()); |
| 323 | |
| 324 | // Test equals with self. |
| 325 | System.out.println(janeDoe.equals(janeDoe)); |
| 326 | |
| 327 | // Test equals with structurally equals Person. |
| 328 | Person otherJaneDoe = new Person("Jane Doe", 42); |
| 329 | System.out.println(janeDoe.equals(otherJaneDoe)); |
| 330 | System.out.println(otherJaneDoe.equals(janeDoe)); |
| 331 | |
| 332 | // Test equals with not-structually equals Person. |
| 333 | Person johnDoe = new Person("John Doe", 42); |
| 334 | System.out.println(janeDoe.equals(johnDoe)); |
| 335 | System.out.println(johnDoe.equals(janeDoe)); |
| 336 | |
| 337 | // Test equals with Object and null. |
| 338 | System.out.println(janeDoe.equals(new Object())); |
| 339 | System.out.println(janeDoe.equals(null)); |
| 340 | } |
| 341 | } |
Clément Béra | 8eca109 | 2021-03-15 14:41:02 +0000 | [diff] [blame] | 342 | } |