-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add filter for assert statement #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...coco.core.test.validation.java5/src/org/jacoco/core/test/validation/java5/AssertTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2018 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 | ||
* | ||
* Contributors: | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.test.validation.java5; | ||
|
||
import org.jacoco.core.analysis.ICounter; | ||
import org.jacoco.core.test.validation.ValidationTestB 8000 ase; | ||
import org.jacoco.core.test.validation.java5.targets.AssertTarget; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test of filtering of a bytecode that is generated for an assert statement. | ||
*/ | ||
public class AssertTest extends ValidationTestBase { | ||
|
||
public AssertTest() { | ||
super(AssertTarget.class); | ||
} | ||
|
||
@Test | ||
public void clinit() { | ||
assertMethod("<clinit>", ICounter.FULLY_COVERED); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...test.validation.java5/src/org/jacoco/core/test/validation/java5/targets/AssertTarget.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2018 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 | ||
* | ||
* Contributors: | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.test.validation.java5.targets; | ||
|
||
public class AssertTarget { | ||
|
||
public static boolean b = true; | ||
|
||
public static void simple() { | ||
assert b; // assertPartlyCovered(1, 1) | ||
} | ||
|
||
public static void message() { | ||
assert b : "m"; // assertPartlyCovered(1, 1) | ||
} | ||
|
||
public static class SimpleClinit { | ||
static { | ||
assert b; // assertPartlyCovered(1, 1) | ||
} | ||
|
||
public static void init() { | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
simple(); | ||
message(); | ||
SimpleClinit.init(); | ||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/AssertFilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2018 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 | ||
* | ||
* Contributors: | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.internal.analysis.filter; | ||
|
||
import org.jacoco.core.internal.instr.InstrSupport; | ||
import org.junit.Test; | ||
import org.objectweb.asm.Label; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
public class AssertFilterTest extends FilterTestBase { | ||
|
||
private final IFilter filter = new AssertFilter(); | ||
|
||
@Test | ||
public void should_filter_initialize() { | ||
final Label disable = new Label(); | ||
final Label init = new Label(); | ||
|
||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"<clinit>", "()V", null, null); | ||
m.visitLdcInsn(Type.getObjectType("Foo")); | ||
final AbstractInsnNode fromInclusive = m.instructions.getLast(); | ||
|
||
m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", | ||
"desiredAssertionStatus", "()Z", false); | ||
m.visitJumpInsn(Opcodes.IFNE, disable); | ||
m.visitInsn(Opcodes.ICONST_1); | ||
m.visitJumpInsn(Opcodes.GOTO, init); | ||
|
||
m.visitLabel(disable); | ||
m.visitInsn(Opcodes.ICONST_0); | ||
|
||
m.visitLabel(init); | ||
m.visitFieldInsn(Opcodes.PUTSTATIC, "Foo", "$assertionsDisabled", "Z"); | ||
final AbstractInsnNode toInclusive = m.instructions.getLast(); | ||
|
||
filter.filter(m, context, output); | ||
|
||
assertIgnored(new Range(fromInclusive, toInclusive)); | ||
} | ||
|
||
@Test | ||
public void should_filter_assert() { | ||
final Label disabled = new Label(); | ||
|
||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"m", "(Z)V", null, null); | ||
m.visitFieldInsn(Opcodes.GETSTATIC, "Foo", "$assertionsDisabled", "Z"); | ||
final AbstractInsnNode fromInclusive = m.instructions.getLast(); | ||
|
||
m.visitJumpInsn(Opcodes.IFNE, disabled); | ||
final AbstractInsnNode toInclusive = m.instructions.getLast(); | ||
|
||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitJumpInsn(Opcodes.IFNE, disabled); | ||
m.visitTypeInsn(Opcodes.NEW, "java/lang/AssertionError"); | ||
m.visitInsn(Opcodes.DUP); | ||
m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/AssertionError", | ||
"<init>", "()V", false); | ||
m.visitInsn(Opcodes.ATHROW); | ||
m.visitLabel(disabled); | ||
|
||
filter.filter(m, context, output); | ||
|
||
assertIgnored(new Range(fromInclusive, toInclusive)); | ||
} | ||
|
||
@Test | ||
public void should_filter_assert_message() { | ||
final Label disabled = new Label(); | ||
|
||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"m", "(Z)V", null, null); | ||
m.visitFieldInsn(Opcodes.GETSTATIC, "Foo", "$assertionsDisabled", "Z"); | ||
final AbstractInsnNode fromInclusive = m.instructions.getLast(); | ||
|
||
m.visitJumpInsn(Opcodes.IFNE, disabled); | ||
final AbstractInsnNode toInclusive = m.instructions.getLast(); | ||
|
||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitJumpInsn(Opcodes.IFNE, disabled); | ||
m.visitTypeInsn(Opcodes.NEW, "java/lang/AssertionError"); | ||
m.visitInsn(Opcodes.DUP); | ||
m.visitLdcInsn("m"); | ||
m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/AssertionError", | ||
"<init>", "(Ljava/lang/Object;)V", false); | ||
m.visitInsn(Opcodes.ATHROW); | ||
m.visitLabel(disabled); | ||
|
||
filter.filter(m, context, output); | ||
|
||
assertIgnored(new Range(fromInclusive, toInclusive)); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bjkail I started working on getting this done - wanted to mention your contribution in our changelog and realized that you didn't wrote name in headers. So wondering if this was on purpose or just was forgotten? is there any IP/legal issues?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't recall, I probably copied from somewhere, cleared it out, and then just didn't bother to fill in my name :-). There are no IP/legal issues.