diff options
Diffstat (limited to 'org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilterTest.java')
-rw-r--r-- | org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilterTest.java | 101 |
1 files changed, 96 insertions, 5 deletions
diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilterTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilterTest.java index 9fc3b3da..308b866c 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilterTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilterTest.java @@ -1,9 +1,10 @@ /******************************************************************************* - * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html + * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors + * This program and the accompanying materials are made available under + * the terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 * * Contributors: * Evgeny Mandrikov - initial API and implementation @@ -30,6 +31,8 @@ public class KotlinUnsafeCastOperatorFilterTest extends FilterTestBase { @Test public void should_filter() { + context.classAnnotations + .add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC); final Label label = new Label(); m.visitInsn(Opcodes.DUP); @@ -49,4 +52,92 @@ public class KotlinUnsafeCastOperatorFilterTest extends FilterTestBase { assertIgnored(new Range(expectedFrom, expectedTo)); } + @Test + public void should_filter_Kotlin_1_4() { + context.classAnnotations + .add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC); + final Label label = new Label(); + + m.visitInsn(Opcodes.DUP); + m.visitJumpInsn(Opcodes.IFNONNULL, label); + final AbstractInsnNode expectedFrom = m.instructions.getLast(); + m.visitTypeInsn(Opcodes.NEW, "java/lang/NullPointerException"); + m.visitInsn(Opcodes.DUP); + m.visitLdcInsn("null cannot be cast to non-null type kotlin.String"); + m.visitMethodInsn(Opcodes.INVOKESPECIAL, + "java/lang/NullPointerException", "<init>", + "(Ljava/lang/String;)V", false); + m.visitInsn(Opcodes.ATHROW); + final AbstractInsnNode expectedTo = m.instructions.getLast(); + m.visitLabel(label); + + filter.filter(m, context, output); + + assertIgnored(new Range(expectedFrom, expectedTo)); + } + + /** + * For + * + * <pre> + * fun f(s: String?): String { + * return s as String + * } + * </pre> + * + * bytecode generated by Kotlin compiler version 1.4 is different from + * bytecode generated by version 1.5, unfortunately bytecode generated by + * later is the same as bytecode that both versions generate for + * + * <pre> + * fun f(s: String?): String { + * if (s == null) + * throw NullPointerException("null cannot be cast to non-null type kotlin.String") + * return s + * } + * </pre> + */ + @Test + public void should_filter_Kotlin_1_5() { + context.classAnnotations + .add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC); + + final Label label = new Label(); + m.visitJumpInsn(Opcodes.IFNONNULL, label); + final AbstractInsnNode expectedFrom = m.instructions.getLast(); + m.visitTypeInsn(Opcodes.NEW, "java/lang/NullPointerException"); + m.visitInsn(Opcodes.DUP); + m.visitLdcInsn("null cannot be cast to non-null type kotlin.String"); + m.visitMethodInsn(Opcodes.INVOKESPECIAL, + "java/lang/NullPointerException", "<init>", + "(Ljava/lang/String;)V", false); + m.visitInsn(Opcodes.ATHROW); + final AbstractInsnNode expectedTo = m.instructions.getLast(); + m.visitLabel(label); + m.visitVarInsn(Opcodes.ALOAD, 0); + + filter.filter(m, context, output); + + assertIgnored(new Range(expectedFrom, expectedTo)); + } + + @Test + public void should_not_filter_when_not_kotlin() { + m.visitInsn(Opcodes.DUP); + final Label label = new Label(); + m.visitJumpInsn(Opcodes.IFNONNULL, label); + m.visitTypeInsn(Opcodes.NEW, "java/lang/NullPointerException"); + m.visitInsn(Opcodes.DUP); + m.visitLdcInsn("null cannot be cast to non-null type kotlin.String"); + m.visitMethodInsn(Opcodes.INVOKESPECIAL, + "java/lang/NullPointerException", "<init>", + "(Ljava/lang/String;)V", false); + m.visitInsn(Opcodes.ATHROW); + m.visitLabel(label); + + filter.filter(m, context, output); + + assertIgnored(); + } + } |