Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4eb934ae52de83fb7b040b7c7c204b1dceab2487 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2000, 2014 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core.tests.eval;

import junit.framework.Test;

import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.lookup.ProblemReasons;
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
import org.eclipse.jdt.internal.eval.GlobalVariable;
/**
 * Negative tests for variables. Only compilation problems should be reported in
 * these tests.
 */
@SuppressWarnings({ "rawtypes" })
public class NegativeVariableTest extends EvaluationTest implements ProblemSeverities, ProblemReasons {
/**
 * Creates a new NegativeVariableTest.
 */
public NegativeVariableTest(String name) {
	super(name);
}
public static Test suite() {
	return setupSuite(testClass());
}
public static Class testClass() {
	return NegativeVariableTest.class;
}
/**
 * Test a variable that has a problem in its initializer.
 */
public void testInitializerProblem() {
	// Problem in first variable
	GlobalVariable var = null;
	try {
		var = this.context.newVariable("int".toCharArray(), "i".toCharArray(), buildCharArray(new String[] {
			"(1 + 1) *",
			"(j + 2)"}));
		evaluateWithExpectedProblem(
			var,
			newProblem(IProblem.UnresolvedVariable, Error, 11, 11, 2)); // j cannot be resolved to a variable
	} finally {
		if (var != null) {
			this.context.deleteVariable(var);
		}
	}

	// Problem in second variable
	GlobalVariable var1 = null;
	GlobalVariable var2 = null;
	try {
		var1 = this.context.newVariable("Object".toCharArray(), "o".toCharArray(), "new Object()".toCharArray());
		var2 = this.context.newVariable("int".toCharArray(), "i".toCharArray(), buildCharArray(new String[] {
			"(1 + 1) *",
			"(1 ++ 2)"}));
		evaluateWithExpectedProblem(
			var2,
			newProblem(IProblem.InvalidUnaryExpression, Error, 11, 11, 2)); // Invalid argument to operation ++/--
	} finally {
		if (var1 != null) {
			this.context.deleteVariable(var1);
		}
		if (var2 != null) {
			this.context.deleteVariable(var2);
		}
	}

}
/**
 * Test a variable that has a problem in its name.
 * TODO (david) investigate why changes in enum recovery caused this test to fail
 */
public void _testInvalidName() {
	// Problem in first variable
	GlobalVariable var = null;
	try {
		var = this.context.newVariable("int".toCharArray(), "!@#$%^&*()_".toCharArray(), "1".toCharArray());
		evaluateWithExpectedProblem(
			var,
			newProblem(IProblem.ParsingErrorDeleteTokens, Error, 0, 9, 0)); // Syntax error, delete these tokens
	} finally {
		if (var != null) {
			this.context.deleteVariable(var);
		}
	}

	// Problem in second variable
	GlobalVariable var1 = null;
	GlobalVariable var2 = null;
	try {
		var1 = this.context.newVariable("String".toCharArray(), "foo".toCharArray(), "\"bar\"".toCharArray());
		var2 = this.context.newVariable("int".toCharArray(), "!@#$%^&*()_".toCharArray(), "1".toCharArray());
		evaluateWithExpectedProblem(
			var2,
			newProblem(IProblem.ParsingErrorDeleteTokens, Error, 0, 9, 0)); // Syntax error, delete these tokens
	} finally {
		if (var1 != null) {
			this.context.deleteVariable(var1);
		}
		if (var2 != null) {
			this.context.deleteVariable(var2);
		}
	}
}
/**
 * Test a variable that has a problem in its type declaration.
 */
public void testUnknownType() {
	// Problem in first variable
	GlobalVariable var = null;
	try {
		var = this.context.newVariable("foo.Bar".toCharArray(), "var".toCharArray(), null);
		evaluateWithExpectedProblem(
			var,
			newProblem(IProblem.UndefinedType, Error, 0, 2, -1)); // The type foo is undefined
	} finally {
		if (var != null) {
			this.context.deleteVariable(var);
		}
	}

	// Problem in second variable
	GlobalVariable var1 = null;
	GlobalVariable var2 = null;
	try {
		var1 = this.context.newVariable("int".toCharArray(), "x".toCharArray(), null);
		var2 = this.context.newVariable("foo.Bar".toCharArray(), "var".toCharArray(), null);
		evaluateWithExpectedProblem(
			var2,
			newProblem(IProblem.UndefinedType, Error, 0, 2, -1)); // The type foo is undefined
	} finally {
		if (var1 != null) {
			this.context.deleteVariable(var1);
		}
		if (var2 != null) {
			this.context.deleteVariable(var2);
		}
	}
}
}

Back to the top