diff options
author | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2014-08-20 22:20:38 +0200 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2014-08-20 22:21:08 +0200 |
commit | 8ffcb7144775ba0da65304c15d952ba7a1cb7da6 (patch) | |
tree | 279e0c39916d3fc5bd2e423107b6bf4a439af753 /org.jacoco.core.test/src/org | |
parent | 4b5d99b1db89f9e87ff3d7625f5e1f1b96411a29 (diff) | |
download | jacoco-8ffcb7144775ba0da65304c15d952ba7a1cb7da6.tar.gz |
GitHub #232: Get code coverage for Java 8 lambda expressions
Diffstat (limited to 'org.jacoco.core.test/src/org')
4 files changed, 147 insertions, 0 deletions
diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/ClassAnalyzerTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/ClassAnalyzerTest.java index a1484ef9..45da7eec 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/ClassAnalyzerTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/ClassAnalyzerTest.java @@ -11,6 +11,14 @@ *******************************************************************************/ package org.jacoco.core.internal.analysis; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Collection; + +import org.jacoco.core.analysis.IMethodCoverage; +import org.jacoco.core.internal.flow.MethodProbesVisitor; import org.jacoco.core.internal.instr.InstrSupport; import org.junit.Before; import org.junit.Test; @@ -44,4 +52,48 @@ public class ClassAnalyzerTest { null, null); } + @Test + public void testMethodFilter_Empty() { + final MethodProbesVisitor mv = analyzer.visitMethod(0, "foo", "()V", + null, null); + mv.visitEnd(); + Collection<IMethodCoverage> methods = analyzer.getCoverage() + .getMethods(); + assertEquals(0, methods.size()); + } + + @Test + public void testMethodFilter_NonSynthetic() { + final MethodProbesVisitor mv = analyzer.visitMethod(0, "foo", "()V", + null, null); + mv.visitCode(); + mv.visitInsn(Opcodes.RETURN); + mv.visitEnd(); + Collection<IMethodCoverage> methods = analyzer.getCoverage() + .getMethods(); + assertEquals(1, methods.size()); + } + + @Test + public void testMethodFilter_Synthetic() { + final MethodProbesVisitor mv = analyzer.visitMethod( + Opcodes.ACC_SYNTHETIC, "foo", "()V", null, null); + assertNull(mv); + Collection<IMethodCoverage> methods = analyzer.getCoverage() + .getMethods(); + assertTrue(methods.isEmpty()); + } + + @Test + public void testMethodFilter_Lambda() { + final MethodProbesVisitor mv = analyzer.visitMethod( + Opcodes.ACC_SYNTHETIC, "lambda$1", "()V", null, null); + mv.visitCode(); + mv.visitInsn(Opcodes.RETURN); + mv.visitEnd(); + Collection<IMethodCoverage> methods = analyzer.getCoverage() + .getMethods(); + assertEquals(1, methods.size()); + } + } diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/java8/LambdaExpressionsTarget.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/java8/LambdaExpressionsTarget.java new file mode 100644 index 00000000..4271db2e --- /dev/null +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/java8/LambdaExpressionsTarget.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2009, 2014 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: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation.java8; + +import static org.jacoco.core.test.validation.targets.Stubs.exec; +import static org.jacoco.core.test.validation.targets.Stubs.noexec; +import static org.jacoco.core.test.validation.targets.Stubs.nop; + +/** + * This test target contains different lambda expressions. + */ +public class LambdaExpressionsTarget implements Runnable { + + @Override + public void run() { + + exec(() -> { + nop(); // $line-executedlambdabody$ + }); + + noexec(() -> { + nop(); // $line-notexecutedlambdabody$ + }); + + } + + public static void main(String[] args) { + new LambdaExpressionsTarget().run(); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/java8/LambdaExpressionsTest.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/java8/LambdaExpressionsTest.java new file mode 100644 index 00000000..d5520621 --- /dev/null +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/java8/LambdaExpressionsTest.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2009, 2014 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: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation.java8; + +import org.jacoco.core.analysis.ICounter; +import org.jacoco.core.test.validation.ValidationTestBase; +import org.junit.Test; + +/** + * Tests of basic Java control structures. + */ +public class LambdaExpressionsTest extends ValidationTestBase { + + public LambdaExpressionsTest() { + super(LambdaExpressionsTarget.class); + } + + @Override + protected void run(final Class<?> targetClass) throws Exception { + final Object instance = targetClass.newInstance(); + ((Runnable) instance).run(); + } + + @Test + public void testCoverageResult() { + + // Coverage of lambda bodies + assertLine("executedlambdabody", ICounter.FULLY_COVERED); + assertLine("notexecutedlambdabody", ICounter.NOT_COVERED); + + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Stubs.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Stubs.java index fdea7858..f5179c63 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Stubs.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Stubs.java @@ -104,4 +104,17 @@ public class Stubs { throw new StubException(); } + /** + * Directly executes the given runnable. + */ + public static void exec(Runnable task) { + task.run(); + } + + /** + * Never executes the given runnable. + */ + public static void noexec(Runnable task) { + } + } |