Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d5a78555f6e1f2f4f58886f7b7e861fc97148b6c (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**********************************************************************
 * Copyright (c) 2002,2003 Rational Software Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: 
 * IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.core.parser.tests;

import java.io.StringReader;
import java.util.Iterator;

import junit.framework.TestCase;

import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IQuickParseCallback;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserFactoryError;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.ast.ASTNotImplementedException;
import org.eclipse.cdt.core.parser.ast.IASTCompilationUnit;
import org.eclipse.cdt.core.parser.ast.IASTDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTFunction;
import org.eclipse.cdt.core.parser.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTSimpleTypeSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTTypedefDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTVariable;
import org.eclipse.cdt.internal.core.parser.ParserException;

/**
 * @author jcamelon
 *
 */
public class BaseASTTest extends TestCase
{
	public BaseASTTest( String a )
	{
		super( a );
	}
	
	protected IQuickParseCallback quickParseCallback; 
	protected IParser parser; 
	
	protected IASTCompilationUnit parse( String code, boolean quick, boolean throwExceptionOnError, ParserLanguage lang ) throws ParserException, ParserFactoryError
	{
		ParserMode mode = quick ? ParserMode.QUICK_PARSE : ParserMode.COMPLETE_PARSE; 
		quickParseCallback = ParserFactory.createQuickParseCallback(); 
		parser = ParserFactory.createParser( ParserFactory.createScanner( new StringReader( code ), "code", new ScannerInfo(), mode, lang, quickParseCallback, new NullLogService()), quickParseCallback, mode, lang, null );
		if( ! parser.parse() && throwExceptionOnError )
			throw new ParserException("Parse failure");
		return quickParseCallback.getCompilationUnit(); 		
	}
	
	
	protected IASTCompilationUnit parse( String code, boolean quick, boolean throwExceptionOnError ) throws ParserException, ParserFactoryError
	{
		return parse( code, quick, throwExceptionOnError, ParserLanguage.CPP );
	}
	
	protected IASTCompilationUnit parse( String code )throws ParserException, ParserFactoryError
	{
		return parse( code, true, true );
	}
	
	protected IASTCompilationUnit fullParse( String code ) throws ParserException, ParserFactoryError
	{
		return parse( code, false, true );
	}
	
	protected IASTDeclaration assertSoleDeclaration( String code ) throws ParserException, ParserFactoryError
	{
		return assertSoleDeclaration( code, ParserLanguage.CPP );
	}	
	
	protected IASTDeclaration assertSoleDeclaration( String code, ParserLanguage language ) throws ParserException, ParserFactoryError
	{
		Iterator declarationIter = null;
        try
        {
            declarationIter = parse(code, true, true, language).getDeclarations();
        }
        catch (ASTNotImplementedException e1)
        {
            // TODO Auto-generated catch block
        }

        assertNotNull( declarationIter );
		assertTrue( declarationIter.hasNext() );
		IASTDeclaration returnValue = (IASTDeclaration)declarationIter.next();
		assertFalse( declarationIter.hasNext() );
		return returnValue;
	}
	
	public void assertCodeFailsParse( String code )
	{
		assertCodeFailsParse( code, true, true, ParserLanguage.CPP ); 
	}
	
	public void assertCodeFailsParse(String code, boolean quick, boolean throwOnError, ParserLanguage CPP ) {
		boolean testPassed = false;
		try {
			parse(code, quick, throwOnError, CPP );
			testPassed = true;
			fail( "We should not reach this point");
		} catch (Throwable e) {
			if (!(e instanceof ParserException))
				fail("Unexpected Error: " + e.getMessage());
		}
		if (testPassed)
			fail("The expected error did not occur.");
	}

	public void assertCodeFailsFullParse(String code) {
		boolean testPassed = false;
		try {
			fullParse(code);
			testPassed = true;
			fail( "We should not reach this point");
		} catch (Throwable e) {
			if (!(e instanceof ParserException))
				fail("Unexpected Error: " + e.getMessage());
		}
		if (testPassed)
			fail("The expected error did not occur.");
	}
	
    protected void assertSimpleReturnType(IASTFunction function, IASTSimpleTypeSpecifier.Type type)
    {
    	assertEquals( ((IASTSimpleTypeSpecifier)function.getReturnType().getTypeSpecifier()).getType(), type ); 
    }

	protected void assertSimpleType(IASTTypedefDeclaration variable, IASTSimpleTypeSpecifier.Type type)
	{
		assertEquals( ((IASTSimpleTypeSpecifier)variable.getAbstractDeclarator().getTypeSpecifier()).getType(), type ); 
	}

    
	protected void assertSimpleType(IASTVariable variable, IASTSimpleTypeSpecifier.Type type)
	{
		assertEquals( ((IASTSimpleTypeSpecifier)variable.getAbstractDeclaration().getTypeSpecifier()).getType(), type ); 
	}
	
	protected void assertParameterSimpleType(IASTParameterDeclaration variable, IASTSimpleTypeSpecifier.Type type)
	{
		assertEquals( ((IASTSimpleTypeSpecifier)variable.getTypeSpecifier()).getType(), type ); 
	}

    protected void failedAsExpected()
    {
        assertFalse( "The expected error did not occur.", false );
    }

    protected void assertNotReached()
    {
        fail( "We should not reach this point");
    }

    protected void assertQualifiedName(String [] fromAST, String [] theTruth)
    {
    	assertNotNull( fromAST );
    	assertNotNull( theTruth );
    	assertEquals( fromAST.length, theTruth.length );
    	for( int i = 0; i < fromAST.length; ++i )
    	{
    		assertEquals( fromAST[i], theTruth[i]);
    	}
    }

}

Back to the top