1
/*******************************************************************************
2
* Copyright (c) 2006, 2009 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.core.lrparser.tests;
13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.Comparator;
16
import java.util.List;
18
import junit.framework.TestCase;
19
import junit.framework.TestSuite;
21
import org.eclipse.cdt.core.dom.ast.IASTCompletionNode;
22
import org.eclipse.cdt.core.dom.ast.IASTName;
23
import org.eclipse.cdt.core.dom.ast.IBinding;
24
import org.eclipse.cdt.core.dom.ast.IEnumerator;
25
import org.eclipse.cdt.core.dom.ast.IField;
26
import org.eclipse.cdt.core.dom.ast.ITypedef;
27
import org.eclipse.cdt.core.dom.ast.IVariable;
28
import org.eclipse.cdt.core.dom.lrparser.gnu.GCCLanguage;
29
import org.eclipse.cdt.core.model.ILanguage;
33
* Reuse the completion parse tests from the old parser for now.
35
@SuppressWarnings("nls")
36
public class LRCompletionParseTest extends TestCase {
38
public static TestSuite suite() {
39
return new TestSuite(LRCompletionParseTest.class);
43
public LRCompletionParseTest() { }
44
public LRCompletionParseTest(String name) { super(name); }
47
protected IASTCompletionNode parse(String code, int offset) throws Exception {
48
return ParseHelper.getCompletionNode(code, getCLanguage(), offset);
52
private static final Comparator<IBinding> BINDING_COMPARATOR = new Comparator<IBinding>() {
53
public int compare(IBinding b1, IBinding b2) {
54
return b1.getName().compareTo(b2.getName());
59
public static IBinding[] getBindings(IASTName[] names) {
60
List<IBinding> bindings = new ArrayList<IBinding>();
62
for(IASTName name : names) {
63
if(name.getTranslationUnit() == null)
66
for(IBinding binding : name.getCompletionContext().findBindings(name, true))
67
bindings.add(binding);
70
Collections.sort(bindings, BINDING_COMPARATOR);
71
return bindings.toArray(new IBinding[bindings.size()]);
75
protected ILanguage getCLanguage() {
76
return GCCLanguage.getDefault();
80
// First steal tests from CompletionParseTest
83
public void testCompletionStructField() throws Exception {
95
int index = code.indexOf( "d.a" );
97
IASTCompletionNode node = parse( code, index + 3 );
98
assertNotNull( node );
100
String prefix = node.getPrefix();
101
assertNotNull( prefix );
102
assertEquals( prefix, "a" );
104
IASTName[] names = node.getNames();
105
assertEquals(1, names.length);
107
IBinding[] bindings = getBindings(names);
109
assertEquals(2, bindings.length);
110
assertEquals("aField1", ((IField)bindings[0]).getName());
111
assertEquals("aField2", ((IField)bindings[1]).getName());
114
public void testCompletionStructFieldPointer() throws Exception {
121
"int volume( struct Cube * pCube ) { " +
124
IASTCompletionNode node = parse( code, code.indexOf("SP"));
126
IASTName[] names = node.getNames();
127
assertEquals(1, names.length);
129
IBinding[] bindings = getBindings(names);
131
assertEquals(3, bindings.length);
132
assertEquals("nHeight", ((IField)bindings[0]).getName());
133
assertEquals("nLen", ((IField)bindings[1]).getName());
134
assertEquals("nWidth", ((IField)bindings[2]).getName());
138
public void testCompletionParametersAsLocalVariables() throws Exception{
140
"int foo( int aParameter ){" +
142
" if( aLocal != 0 ){" +
143
" int aBlockLocal;" +
146
int index = code.indexOf( " a " );
148
IASTCompletionNode node = parse( code, index + 2 );
149
assertNotNull( node );
151
assertEquals("a", node.getPrefix()); //$NON-NLS-1$
153
IASTName[] names = node.getNames();
154
assertEquals(2, names.length);
156
IBinding[] bindings = getBindings(names);
158
assertEquals(3, bindings.length);
159
assertEquals("aBlockLocal", ((IVariable)bindings[0]).getName());
160
assertEquals("aLocal", ((IVariable)bindings[1]).getName());
161
assertEquals("aParameter", ((IVariable)bindings[2]).getName());
165
public void testCompletionTypedef() throws Exception {
167
"typedef int Int; " +
170
int index = code.indexOf( "SP" );
172
IASTCompletionNode node = parse( code, index );
175
IASTName[] names = node.getNames();
176
assertEquals(1, names.length);
178
assertEquals("In", node.getPrefix());
180
IBinding[] bindings = getBindings(names);
182
assertEquals(1, bindings.length);
183
assertEquals("Int", ((ITypedef)bindings[0]).getName());
186
public void testCompletion() throws Exception {
188
"#define GL_T 0x2001\n" +
189
"#define GL_TRUE 0x1\n" +
190
"typedef unsigned char GLboolean;\n" +
191
"static GLboolean should_rotate = GL_T";
193
int index = code.indexOf("= GL_T");
195
IASTCompletionNode node = parse( code, index + 6);
198
assertEquals("GL_T", node.getPrefix()); //$NON-NLS-1$
200
IASTName[] names = node.getNames();
201
assertEquals(1, names.length);
204
public void testCompletionInTypeDef() throws Exception {
206
"struct A { int name; }; \n" +
207
"typedef struct A * PA; \n" +
213
int index = code.indexOf("SP"); //$NON-NLS-1$
215
IASTCompletionNode node = parse( code, index );
216
assertNotNull( node );
219
IASTName[] names = node.getNames();
220
assertEquals(1, names.length);
222
IBinding[] bindings = getBindings(names);
224
assertEquals(1, bindings.length);
225
assertEquals("name", ((IField)bindings[0]).getName());
229
public void _testCompletionFunctionCall() throws Exception {
235
"const A * foo(){} \n" +
240
int index = code.indexOf( "SP" );
242
IASTCompletionNode node = parse( code, index );
243
assertNotNull( node );
245
IASTName[] names = node.getNames();
246
assertEquals(1, names.length);
248
IBinding[] bindings = getBindings(names);
250
assertEquals(2, bindings.length);
251
assertEquals("f2", ((IField)bindings[0]).getName());
252
assertEquals("f4", ((IField)bindings[1]).getName());
256
public void _testCompletionSizeof() throws Exception {
262
int index = code.indexOf( "of(bl" );
264
IASTCompletionNode node = parse( code, index + 5);
265
assertNotNull( node );
267
IASTName[] names = node.getNames();
268
assertEquals(1, names.length);
270
IBinding[] bindings = getBindings(names);
272
assertEquals(1, bindings.length);
273
assertEquals("blah", ((IVariable)bindings[0]).getName());
277
public void testCompletionForLoop() throws Exception {
280
" int biSizeImage = 5;\n" +
281
"for (int i = 0; i < bi ";
283
int index = code.indexOf("< bi");
285
IASTCompletionNode node = parse( code, index + 4);
286
assertNotNull( node );
288
IASTName[] names = node.getNames();
289
assertEquals(1, names.length);
291
IBinding[] bindings = getBindings(names);
293
assertEquals(1, bindings.length);
294
assertEquals("biSizeImage", ((IVariable)bindings[0]).getName());
299
public void testCompletionStructPointer() throws Exception {
301
" struct Temp { char * total; };" +
302
" int f(struct Temp * t) {" +
305
int index = code.indexOf("= t->");
307
IASTCompletionNode node = parse( code, index + 5);
308
assertNotNull( node );
310
IASTName[] names = node.getNames();
311
assertEquals(1, names.length);
313
IBinding[] bindings = getBindings(names);
315
assertEquals(1, bindings.length);
316
assertEquals("total", ((IVariable)bindings[0]).getName());
320
public void testCompletionEnum() throws Exception {
322
"typedef int DWORD;\n" +
323
"typedef char BYTE;\n" +
324
"#define MAKEFOURCC(ch0, ch1, ch2, ch3) \\\n" +
325
"((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \\\n" +
326
"((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 ))\n" +
329
"blah2 = MAKEFOURCC('a', 'b', 'c', 'd'),\n" +
334
int index = code.indexOf("= bl");
336
IASTCompletionNode node = parse( code, index + 4);
337
assertNotNull( node );
339
IASTName[] names = node.getNames();
340
assertEquals(1, names.length);
342
IBinding[] bindings = getBindings(names);
344
assertEquals(3, bindings.length);
345
assertEquals("blah1", ((IEnumerator)bindings[0]).getName());
346
assertEquals("blah2", ((IEnumerator)bindings[1]).getName());
347
assertEquals("blah3", ((IEnumerator)bindings[2]).getName());
351
public void testCompletionStructArray() throws Exception {
353
"struct packet { int a; int b; };\n" +
354
"struct packet buffer[5];\n" +
355
"int main(int argc, char **argv) {\n" +
358
int index = code.indexOf("[2].");
360
IASTCompletionNode node = parse( code, index + 4);
361
assertNotNull( node );
363
IASTName[] names = node.getNames();
364
assertEquals(1, names.length);
366
IBinding[] bindings = getBindings(names);
368
assertEquals(2, bindings.length);
369
assertEquals("a", ((IField)bindings[0]).getName());
370
assertEquals("b", ((IField)bindings[1]).getName());
374
public void testCompletionPreprocessorDirective() throws Exception {
375
IASTCompletionNode node = parse("#", 1);
376
assertNotNull( node );
378
IASTName[] names = node.getNames();
379
assertEquals(1, names.length);
381
//assertEquals("#", node.getPrefix());
384
public void testCompletionPreprocessorMacro() throws Exception {
386
"#define AMACRO 99 \n" +
391
int index = code.indexOf("= A");
393
IASTCompletionNode node = parse( code, index + 3);
394
assertNotNull( node );
396
IASTName[] names = node.getNames();
397
assertEquals(1, names.length);
398
assertEquals("A", node.getPrefix());
402
public void testCompletionInsidePreprocessorDirective() throws Exception {
404
"#define MAC1 99 \n" +
405
"#define MAC2 99 \n" +
408
int index = code.length();
410
IASTCompletionNode node = parse( code, index );
411
assertNotNull( node );
413
assertEquals("MA", node.getPrefix());