1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*******************************************************************************
* 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
*
* Contributors:
* Evgeny Mandrikov - initial API and implementation
*
*******************************************************************************/
package org.jacoco.core.internal.analysis.filter;
import java.util.HashSet;
import java.util.Set;
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.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodNode;
/**
* Unit tests for {@link KotlinWhenFilter}.
*/
public class KotlinWhenFilterTest extends FilterTestBase {
private final KotlinWhenFilter filter = new KotlinWhenFilter();
private final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"name", "()V", null, null);
@Test
public void should_filter_implicit_else() {
final Label label = new Label();
final Range range1 = new Range();
m.visitInsn(Opcodes.NOP);
m.visitJumpInsn(Opcodes.IFEQ, label);
range1.fromInclusive = m.instructions.getLast();
range1.toInclusive = m.instructions.getLast();
m.visitInsn(Opcodes.NOP);
final Range range2 = new Range();
m.visitLabel(label);
range2.fromInclusive = m.instructions.getLast();
m.visitTypeInsn(Opcodes.NEW, "kotlin/NoWhenBranchMatchedException");
m.visitInsn(Opcodes.DUP);
m.visitMethodInsn(Opcodes.INVOKESPECIAL,
"kotlin/NoWhenBranchMatchedException", "<init>", "()V", false);
m.visitInsn(Opcodes.ATHROW);
range2.toInclusive = m.instructions.getLast();
filter.filter(m, context, output);
assertIgnored(range1, range2);
assertNoReplacedBranches();
}
@Test
public void should_not_filter_explicit_else() {
final Label label = new Label();
m.visitInsn(Opcodes.NOP);
m.visitJumpInsn(Opcodes.IFEQ, label);
m.visitInsn(Opcodes.NOP);
m.visitLabel(label);
m.visitTypeInsn(Opcodes.NEW, "kotlin/NoWhenBranchMatchedException");
m.visitInsn(Opcodes.DUP);
m.visitMethodInsn(Opcodes.INVOKESPECIAL,
"kotlin/NoWhenBranchMatchedException", "<init>", "()V", false);
m.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Throwable");
m.visitInsn(Opcodes.ATHROW);
filter.filter(m, context, output);
assertIgnored();
assertNoReplacedBranches();
}
@Test
public void should_filter_implicit_default() {
final Label case1 = new Label();
final Label caseDefault = new Label();
final Label after = new Label();
m.visitInsn(Opcodes.NOP);
m.visitTableSwitchInsn(0, 0, caseDefault, case1);
final AbstractInsnNode switchNode = m.instructions.getLast();
final Set<AbstractInsnNode> newTargets = new HashSet<AbstractInsnNode>();
m.visitLabel(case1);
m.visitInsn(Opcodes.ICONST_1);
newTargets.add(m.instructions.getLast());
m.visitJumpInsn(Opcodes.GOTO, after);
final Range range1 = new Range();
m.visitLabel(caseDefault);
range1.fromInclusive = m.instructions.getLast();
m.visitTypeInsn(Opcodes.NEW, "kotlin/NoWhenBranchMatchedException");
m.visitInsn(Opcodes.DUP);
m.visitMethodInsn(Opcodes.INVOKESPECIAL,
"kotlin/NoWhenBranchMatchedException", "<init>", "()V", false);
m.visitInsn(Opcodes.ATHROW);
range1.toInclusive = m.instructions.getLast();
m.visitLabel(after);
filter.filter(m, context, output);
assertIgnored(range1);
assertReplacedBranches(switchNode, newTargets);
}
}
|