aboutsummaryrefslogtreecommitdiffstats
path: root/org.jacoco.core.test/src/org/jacoco/core/internal/flow/MethodSanitizerTest.java
blob: 90bc881e4a00e59c24876c4e401ebca823206c51 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * 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:
 *    Marc R. Hoffmann - initial API and implementation
 *    
 *******************************************************************************/
package org.jacoco.core.internal.flow;

import static org.junit.Assert.assertEquals;

import org.jacoco.core.instr.MethodRecorder;
import org.junit.Before;
import org.junit.Test;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.MethodNode;

/**
 * Unit tests for {@link MethodSanitizer}.
 */
public class MethodSanitizerTest {

	private MethodNode actual;
	private MethodNode expected;

	private MethodVisitor sanitizer;

	@Before
	public void setup() {
		actual = new MethodNode(0, "test", "()V", null, null);
		expected = new MethodNode(0, "test", "()V", null, null);
		sanitizer = new MethodSanitizer(actual, 0, "test", "()V", null, null);
	}

	@Test
	public void testLocalVariablePositive() {
		Label l1 = new Label();
		Label l2 = new Label();
		sanitizer.visitCode();
		sanitizer.visitLabel(l1);
		sanitizer.visitInsn(Opcodes.RETURN);
		sanitizer.visitLabel(l2);
		sanitizer.visitLocalVariable("x", "I", null, l1, l2, 0);
		sanitizer.visitMaxs(0, 0);
		sanitizer.visitEnd();

		Label m1 = new Label();
		Label m2 = new Label();
		expected.visitLabel(m1);
		expected.visitInsn(Opcodes.RETURN);
		expected.visitLabel(m2);
		expected.visitLocalVariable("x", "I", null, m1, m2, 0);
		expected.visitMaxs(0, 0);
		expected.visitEnd();

		assertOutput();
	}

	@Test
	public void testLocalVariableNegative1() {
		Label l1 = new Label();
		Label l2 = new Label();
		sanitizer.visitCode();
		sanitizer.visitInsn(Opcodes.RETURN);
		sanitizer.visitLabel(l2);
		sanitizer.visitLocalVariable("x", "I", null, l1, l2, 0);
		sanitizer.visitMaxs(0, 0);
		sanitizer.visitEnd();

		Label m2 = new Label();
		expected.visitInsn(Opcodes.RETURN);
		expected.visitLabel(m2);
		expected.visitMaxs(0, 0);
		expected.visitEnd();

		assertOutput();
	}

	@Test
	public void testLocalVariableNegative2() {
		Label l1 = new Label();
		Label l2 = new Label();
		sanitizer.visitCode();
		sanitizer.visitLabel(l1);
		sanitizer.visitInsn(Opcodes.RETURN);
		sanitizer.visitLocalVariable("x", "I", null, l1, l2, 0);
		sanitizer.visitMaxs(0, 0);
		sanitizer.visitEnd();

		Label m1 = new Label();
		expected.visitLabel(m1);
		expected.visitInsn(Opcodes.RETURN);
		expected.visitMaxs(0, 0);
		expected.visitEnd();

		assertOutput();
	}

	@Test
	public void testLineNumberPositive() {
		Label l1 = new Label();
		sanitizer.visitCode();
		sanitizer.visitLabel(l1);
		sanitizer.visitLineNumber(15, l1);
		sanitizer.visitInsn(Opcodes.RETURN);
		sanitizer.visitMaxs(0, 0);
		sanitizer.visitEnd();

		Label m1 = new Label();
		expected.visitLabel(m1);
		expected.visitLineNumber(15, m1);
		expected.visitInsn(Opcodes.RETURN);
		expected.visitMaxs(0, 0);
		expected.visitEnd();

		assertOutput();
	}

	@Test
	public void testLineNumberNegative() {
		Label l1 = new Label();
		sanitizer.visitCode();
		sanitizer.visitLineNumber(15, l1);
		sanitizer.visitInsn(Opcodes.RETURN);
		sanitizer.visitMaxs(0, 0);
		sanitizer.visitEnd();

		expected.visitInsn(Opcodes.RETURN);
		expected.visitMaxs(0, 0);
		expected.visitEnd();

		assertOutput();
	}

	private void assertOutput() {
		assertEquals(dump(expected), dump(actual));
	}

	private MethodRecorder dump(MethodNode node) {
		MethodRecorder rec = new MethodRecorder();
		node.accept(rec.getVisitor());
		return rec;
	}
}