-
Notifications
You must be signed in to change notification settings - Fork 349
/
LineMapperTest.java
122 lines (97 loc) · 4.36 KB
/
LineMapperTest.java
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
package org.pitest.coverage.codeassist;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.pitest.classinfo.ClassByteArraySource;
import org.pitest.classinfo.ClassName;
import org.pitest.coverage.BlockLocation;
import org.pitest.coverage.LineMap;
import org.pitest.coverage.analysis.LineMapper;
import java.util.Optional;
import org.pitest.mutationtest.engine.Location;
import com.example.coverage.execute.samples.simple.LastLineOfContructorCheck;
import com.example.coverage.execute.samples.simple.OneBlock;
import com.example.coverage.execute.samples.simple.ThreeBlocks;
import com.example.coverage.execute.samples.simple.ThreeMultiLineBlocks;
@RunWith(MockitoJUnitRunner.class)
public class LineMapperTest {
@Mock
ClassByteArraySource source;
@Test
public void shouldMapAllLinesWhenMethodContainsSingleBlock() throws Exception {
final Map<BlockLocation, Set<Integer>> actual = analyse(OneBlock.class);
final Location l = Location.location(ClassName.fromClass(OneBlock.class),
"foo", "()I");
final BlockLocation bl = new BlockLocation(l, 0);
assertThat(actual.get(bl)).containsOnly(5);
}
@Test
public void shouldMapAllLinesWhenMethodContainsThreeBlocks() throws Exception {
final Map<BlockLocation, Set<Integer>> actual = analyse(ThreeBlocks.class);
final Location l = Location.location(ClassName.fromClass(ThreeBlocks.class),
"foo", "(I)I");
assertThat(actual.get(BlockLocation.blockLocation(l, 0))).containsOnly(5);
assertThat(actual.get(BlockLocation.blockLocation(l, 1))).containsOnly(6);
assertThat(actual.get(BlockLocation.blockLocation(l, 2))).containsOnly(8);
}
@Test
public void shouldMapAllLinesWhenMethodContainsThreeMultiLineBlocks()
throws Exception {
final Map<BlockLocation, Set<Integer>> actual = analyse(ThreeMultiLineBlocks.class);
final Location l = Location.location(
ClassName.fromClass(ThreeMultiLineBlocks.class),
"foo", "(I)I");
assertThat(actual.get(BlockLocation.blockLocation(l, 0))).contains(5, 6);
assertThat(actual.get(BlockLocation.blockLocation(l, 1))).contains(7);
assertThat(actual.get(BlockLocation.blockLocation(l, 2))).contains(8,9);
assertThat(actual.get(BlockLocation.blockLocation(l, 3))).contains(10);
assertThat(actual.get(BlockLocation.blockLocation(l, 4))).contains(12,13);
assertThat(actual.get(BlockLocation.blockLocation(l, 5))).contains(14);
}
@Test
public void shouldMapLinesWhenLinesSpanBlocks() throws Exception {
final Map<BlockLocation, Set<Integer>> actual = analyse(com.example.LineNumbersSpanBlocks.class);
final Location l = Location.location(
ClassName.fromClass(com.example.LineNumbersSpanBlocks.class),
"foo", "(I)I");
assertThat(actual.get(BlockLocation.blockLocation(l, 2))).containsOnly(12);
}
@Test
public void shouldIncludeLastLinesConstructorsInBlock() throws Exception {
final Map<BlockLocation, Set<Integer>> actual = analyse(LastLineOfContructorCheck.class);
final Location l = Location.location(
ClassName.fromClass(LastLineOfContructorCheck.class),
"<init>", "()V");
assertThat(actual.get(BlockLocation.blockLocation(l, 1))).contains(6);
}
@Test
public void shouldI() throws Exception {
final Map<BlockLocation, Set<Integer>> actual = analyse(ThreeBlocks2.class);
final Location l = Location.location(ClassName.fromClass(ThreeBlocks2.class),
"foo", "(I)I");
assertThat(actual.get(BlockLocation.blockLocation(l, 0))).containsOnly(107);
assertThat(actual.get(BlockLocation.blockLocation(l, 1))).containsOnly(108);
assertThat(actual.get(BlockLocation.blockLocation(l, 2))).containsOnly(110);
}
static class ThreeBlocks2 {
int foo(int i) {
if (i > 30) {
return 1;
}
return 2;
}
}
private Map<BlockLocation, Set<Integer>> analyse(Class<?> clazz)
throws ClassNotFoundException {
when(this.source.getBytes(anyString())).thenReturn(
Optional.ofNullable(ClassUtils.classAsBytes(clazz)));
final LineMap testee = new LineMapper(this.source);
return testee.mapLines(ClassName.fromClass(clazz));
}
}