|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2009, 2023 Mountainminds GmbH & Co. KG and Contributors |
| 3 | + * This program and the accompanying materials are made available under |
| 4 | + * the terms of the Eclipse Public License 2.0 which is available at |
| 5 | + * http://www.eclipse.org/legal/epl-2.0 |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Evgeny Mandrikov - initial API and implementation |
| 11 | + * |
| 12 | + *******************************************************************************/ |
| 13 | +package org.jacoco.core.test.validation.java20.targets; |
| 14 | + |
| 15 | +import static org.jacoco.core.test.validation.targets.Stubs.nop; |
| 16 | + |
| 17 | +/** |
| 18 | + * This target exercises Record Patterns |
| 19 | + * (<a href="https://openjdk.org/jeps/432">JEP 432</a>). |
| 20 | + */ |
| 21 | +public class RecordPatternsTarget { |
| 22 | + |
| 23 | + private record Point(int x, int y) { |
| 24 | + } |
| 25 | + |
| 26 | + private static void instanceofOperator(Object o) { |
| 27 | + if (o instanceof Point(int x,int y)) { // assertPartlyCovered(0, 2) |
| 28 | + nop(x + y); // assertFullyCovered() |
| 29 | + } // assertEmpty() |
| 30 | + } |
| 31 | + |
| 32 | + private static void switchStatement(Object p) { |
| 33 | + switch (p) { // assertFullyCovered(0, 2) |
| 34 | + case Point(int x, int y) -> nop(x + y); // assertFullyCovered() |
| 35 | + default -> nop(); // assertPartlyCovered() |
| 36 | + } // assertEmpty() |
| 37 | + } |
| 38 | + |
| 39 | + private static void enhancedForStatement(Point[] p) { |
| 40 | + for (Point(int x, int y) : p) { // assertPartlyCovered(2, 3) |
| 41 | + nop(x + y); // assertFullyCovered() |
| 42 | + } // assertEmpty() |
| 43 | + } |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + instanceofOperator(new Point(1, 2)); |
| 47 | + instanceofOperator(new Object()); |
| 48 | + |
| 49 | + switchStatement(new Point(1, 2)); |
| 50 | + switchStatement(new Object()); |
| 51 | + |
| 52 | + enhancedForStatement(new Point[] { new Point(1, 2) }); |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments