diff options
author | Evgeny Mandrikov <138671+Godin@users.noreply.github.com> | 2020-01-07 10:05:04 +0100 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2020-01-07 10:05:04 +0100 |
commit | fbd009930c38344d08fcfe966165afa11412bf25 (patch) | |
tree | 37a301db931157090595c57c02fd44ba47790685 /org.jacoco.core.test/src | |
parent | 6bcce6972b8939d7925c4b4d3df785d9a7b00007 (diff) | |
download | jacoco-fbd009930c38344d08fcfe966165afa11412bf25.tar.gz |
Add filter for Records (#990)
Diffstat (limited to 'org.jacoco.core.test/src')
-rw-r--r-- | org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/RecordsFilterTest.java | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/RecordsFilterTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/RecordsFilterTest.java new file mode 100644 index 00000000..b16778cd --- /dev/null +++ b/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/RecordsFilterTest.java @@ -0,0 +1,175 @@ +/******************************************************************************* + * Copyright (c) 2009, 2020 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 + * + *******************************************************************************/ +package org.jacoco.core.internal.analysis.filter; + +import org.jacoco.core.internal.instr.InstrSupport; +import org.junit.Test; +import org.objectweb.asm.Handle; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.MethodNode; + +/** + * Unit tests for {@link RecordsFilter}. + */ +public class RecordsFilterTest extends FilterTestBase { + + private final RecordsFilter filter = new RecordsFilter(); + + @Test + public void should_filter_generated_toString_method() { + context.superClassName = "java/lang/Record"; + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, + "toString", "()Ljava/lang/String;", null, null); + m.visitVarInsn(Opcodes.ALOAD, 0); + m.visitInvokeDynamicInsn("toString", "(LPoint;)Ljava/lang/String;", + new Handle(Opcodes.H_INVOKESTATIC, + "java/lang/runtime/ObjectMethods", "bootstrap", + "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava/lang/Object;", + false)); + m.visitInsn(Opcodes.ARETURN); + + filter.filter(m, context, output); + + assertMethodIgnored(m); + } + + @Test + public void should_not_filter_custom_toString_method() { + context.superClassName = "java/lang/Record"; + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, + "toString", "()Ljava/lang/String;", null, null); + m.visitLdcInsn(""); + m.visitInsn(Opcodes.ARETURN); + + filter.filter(m, context, output); + + assertIgnored(); + } + + @Test + public void should_not_filter_non_toString_method() { + context.superClassName = "java/lang/Record"; + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, + "toString", "()V", null, null); + m.visitInsn(Opcodes.NOP); + + filter.filter(m, context, output); + + assertIgnored(); + } + + @Test + public void should_filter_generated_hashCode_method() { + context.superClassName = "java/lang/Record"; + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, + "hashCode", "()I", null, null); + m.visitVarInsn(Opcodes.ALOAD, 0); + m.visitInvokeDynamicInsn("hashCode", "(LPoint;)I", new Handle( + Opcodes.H_INVOKESTATIC, "java/lang/runtime/ObjectMethods", + "bootstrap", + "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava/lang/Object;", + false)); + m.visitInsn(Opcodes.IRETURN); + + filter.filter(m, context, output); + + assertMethodIgnored(m); + } + + @Test + public void should_not_filter_custom_hashCode_method() { + context.superClassName = "java/lang/Record"; + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, + "hashCode", "()I", null, null); + m.visitInsn(Opcodes.ICONST_0); + m.visitInsn(Opcodes.IRETURN); + + filter.filter(m, context, output); + + assertIgnored(); + } + + @Test + public void should_not_filter_non_hashCode_method() { + context.superClassName = "java/lang/Record"; + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, + "hashCode", "()V", null, null); + m.visitInsn(Opcodes.NOP); + + filter.filter(m, context, output); + + assertIgnored(); + } + + @Test + public void should_filter_generated_equals_method() { + context.superClassName = "java/lang/Record"; + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, + "equals", "(Ljava/lang/Object;)Z", null, null); + m.visitVarInsn(Opcodes.ALOAD, 0); + m.visitVarInsn(Opcodes.ALOAD, 1); + m.visitInvokeDynamicInsn("equals", "(LPoint;Ljava/lang/Object;)Z", + new Handle(Opcodes.H_INVOKESTATIC, + "java/lang/runtime/ObjectMethods", "bootstrap", + "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava/lang/Object;", + false)); + m.visitInsn(Opcodes.IRETURN); + + filter.filter(m, context, output); + + assertMethodIgnored(m); + } + + @Test + public void should_not_filter_custom_equals_method() { + context.superClassName = "java/lang/Record"; + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, + "equals", "(Ljava/lang/Object;)Z", null, null); + m.visitInsn(Opcodes.ICONST_0); + m.visitInsn(Opcodes.IRETURN); + + filter.filter(m, context, output); + + assertIgnored(); + } + + @Test + public void should_not_filter_non_equals_method() { + context.superClassName = "java/lang/Record"; + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, + "equals", "()V", null, null); + m.visitInsn(Opcodes.NOP); + + filter.filter(m, context, output); + + assertIgnored(); + } + + @Test + public void should_not_filter_non_records() { + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, + "toString", "()Ljava/lang/String;", null, null); + m.visitVarInsn(Opcodes.ALOAD, 0); + m.visitInvokeDynamicInsn("toString", "(LPoint;)Ljava/lang/String;", + new Handle(Opcodes.H_INVOKESTATIC, + "java/lang/runtime/ObjectMethods", "bootstrap", + "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava/lang/Object;", + false)); + m.visitInsn(Opcodes.ARETURN); + + filter.filter(m, context, output); + + assertIgnored(); + } + +} |