aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/test/validation/StatementParserTest.java
blob: 7d200e0ccdbea605e3f40311bdda36a72537c9b0 (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
151
152
/*******************************************************************************
 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.core.test.validation;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jacoco.core.test.validation.StatementParser.IStatementVisitor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for {@link StatementParser}
 */
public class StatementParserTest {

	private IStatementVisitor visitor;

	private List<String> actualInvocations;
	private List<String> expectedInvocations;

	@Before
	public void setup() {
		actualInvocations = new ArrayList<String>();
		expectedInvocations = new ArrayList<String>();
		visitor = new IStatementVisitor() {
			public void visitInvocation(String ctx, String name,
					Object... args) {
				actualInvocations.add(invocationStr(ctx, name, args));
			}
		};
	}

	@After
	public void teardown() {
		assertEquals(expectedInvocations, actualInvocations);
	}

	@Test
	public void should_parse_empty_string() throws IOException {
		StatementParser.parse("", visitor, "Foo.java");
	}

	@Test
	public void should_parse_invocation_without_params() throws IOException {
		StatementParser.parse("run()", visitor, "Foo.java");
		expectInvocation("Foo.java", "run");
	}

	@Test
	public void should_parse_invocation_with_one_int_parameter()
			throws IOException {
		StatementParser.parse("ask(42)", visitor, "Foo.java");
		expectInvocation("Foo.java", "ask", Integer.valueOf(42));
	}

	@Test
	public void should_parse_invocation_with_one_string_parameter()
			throws IOException {
		StatementParser.parse("say(\"hello\")", visitor, "Foo.java");
		expectInvocation("Foo.java", "say", "hello");
	}

	@Test
	public void should_parse_invocation_with_two_parameters()
			throws IOException {
		StatementParser.parse("add(1000, 234)", visitor, "Foo.java");
		expectInvocation("Foo.java", "add", Integer.valueOf(1000),
				Integer.valueOf(234));
	}

	@Test
	public void should_parse_invocation_with_mixed_parameter_types()
			throws IOException {
		StatementParser.parse("mix(1, \"two\", 3)", visitor, "Foo.java");
		expectInvocation("Foo.java", "mix", Integer.valueOf(1), "two",
				Integer.valueOf(3));
	}

	@Test
	public void should_parse_multiple_invocations() throws IOException {
		StatementParser.parse("start() stop()", visitor, "Foo.java");
		expectInvocation("Foo.java", "start");
		expectInvocation("Foo.java", "stop");
	}

	@Test
	public void should_fail_when_parenthesis_is_missing() throws IOException {
		try {
			StatementParser.parse("bad(", visitor, "Foo.java");
			fail("exception expected");
		} catch (IOException e) {
			// expected
		}
	}

	@Test
	public void should_fail_when_argument1_is_missing() throws IOException {
		try {
			StatementParser.parse("bad(,2)", visitor, "Foo.java");
			fail("exception expected");
		} catch (IOException e) {
			// expected
		}
	}

	@Test
	public void should_fail_when_argument2_is_missing() throws IOException {
		try {
			StatementParser.parse("bad(1,)", visitor, "Foo.java");
			fail("exception expected");
		} catch (IOException e) {
			// expected
		}
	}

	@Test
	public void should_give_context_info_when_parsing_fails()
			throws IOException {
		try {
			StatementParser.parse("bad", visitor, "Foo.java:32");
			fail("exception expected");
		} catch (IOException e) {
			assertEquals("Invalid syntax (Foo.java:32)", e.getMessage());
		}
	}

	private void expectInvocation(String ctx, String name, Object... args) {
		expectedInvocations.add(invocationStr(ctx, name, args));
	}

	private String invocationStr(String ctx, String name, Object... args) {
		return String.format("%s:%s%s", ctx, name, Arrays.asList(args));
	}

}