Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 228572df0d55c108cd6f9349ea3d1e98d8f48259 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*******************************************************************************
 * Copyright (c) 2005, 2012 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.model.tests;

import java.util.List;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IField;
import org.eclipse.cdt.core.model.IMethodDeclaration;
import org.eclipse.cdt.core.model.IStructure;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;

/**
 * @author bnicolle
 */
public class IStructureTests extends IntegratedCModelTest {
	/**
	 * @param name
	 */
	public IStructureTests(String name) {
		super(name);
	}

	/**
	 * @see org.eclipse.cdt.internal.core.model.IntegratedCModelTest
	 */
	@Override
	public String getSourcefileSubdir() {
		return "resources/cmodel/";
	}

	/**
	 * @see org.eclipse.cdt.internal.core.model.IntegratedCModelTest
	 */
	@Override
	public String getSourcefileResource() {
		return "IStructure.cpp";
	}

	/**
	 * @returns a test suite named after this class
	 *          containing all its public members named "test*"
	 */
	public static Test suite() {
		TestSuite suite = new TestSuite(IStructureTests.class.getName());

		// TODO check C-only behaviour using C_NATURE vs CC_NATURE

		// Interface tests:
		suite.addTest(new IStructureTests("testGetChildrenOfTypeStruct"));
		suite.addTest(new IStructureTests("testGetChildrenOfTypeClass")); // C++ only
		suite.addTest(new IStructureTests("testGetFields"));
		//Bug# 38985: solved. suite.addTest(new IStructureTests("testGetFieldsHack"));
		suite.addTest(new IStructureTests("testGetField"));
		suite.addTest(new IStructureTests("testGetMethods")); // C++ only
		//Bug# 38985: solved. suite.addTest(new IStructureTests("testGetMethodsHack")); // C++ only
		suite.addTest(new IStructureTests("testGetMethod")); // C++ only
		suite.addTest(new IStructureTests("testIsStruct"));
		suite.addTest(new IStructureTests("testIsClass")); // C++ only
		suite.addTest(new IStructureTests("testIsUnion"));
		suite.addTest(new IStructureTests("testIsAbstract")); // C++ only
		suite.addTest(new IStructureTests("testGetBaseTypes")); // C++ only
		suite.addTest(new IStructureTests("testGetAccessControl")); // C++ only

		// Language Specification tests:
		suite.addTest(new IStructureTests("testAnonymousStructObject"));
		suite.addTest(new IStructureTests("testInnerStruct"));

		return suite;
	}

	public void testGetChildrenOfTypeStruct() throws CModelException {
		ITranslationUnit tu = getTU();
		List arrayStructs = tu.getChildrenOfType(ICElement.C_STRUCT);
		String[] myExpectedStructs = { "testStruct1", "testStruct2", "testStruct3", "testStruct4NoSemicolon",
				/* 2 anonymous structs */ "", "", "testStruct7", "testStruct8" };
		assertEquals(myExpectedStructs.length, arrayStructs.size());
		for (int i = 0; i < myExpectedStructs.length; i++) {
			IStructure myIStruct = (IStructure) arrayStructs.get(i);
			assertNotNull("Failed on " + i, myIStruct);
			assertEquals(myExpectedStructs[i], myIStruct.getElementName());
		}
	}

	public void testGetChildrenOfTypeClass() throws CModelException {
		ITranslationUnit tu = getTU();
		List arrayClasses = tu.getChildrenOfType(ICElement.C_CLASS);
		String[] myExpectedClasses = { "testClass1", "testClass2NoSemicolon", "testClass3", "testClass4Abstract",
				"testClass5", "testClass6" };
		assertEquals(myExpectedClasses.length, arrayClasses.size());
		for (int i = 0; i < myExpectedClasses.length; i++) {
			IStructure myIStruct = (IStructure) arrayClasses.get(i);
			assertNotNull("Failed on " + i, myIStruct);
			assertEquals(myExpectedClasses[i], myIStruct.getElementName());
		}
	}

	public void testGetFields() throws CModelException {
		ITranslationUnit tu = getTU();
		List myArrayStructs = tu.getChildrenOfType(ICElement.C_STRUCT);
		IStructure myIStruct = (IStructure) myArrayStructs.get(0);
		IField[] myArrayIField = myIStruct.getFields();
		String[] myExpectedFields = { "m_field1", "m_field2", "m_field3", "m_field4", "m_field5", "m_field6", };
		assertEquals(myExpectedFields.length, myArrayIField.length);
		for (int i = 0; i < myArrayIField.length; i++) {
			assertNotNull("Failed on " + i, myArrayIField[i]);
			assertEquals("Failed on " + i, myExpectedFields[i], myArrayIField[i].getElementName());
		}
	}

	// TODO Bug# 38985: remove testGetFieldsHack()
	public void testGetFieldsHack() throws CModelException {
		ITranslationUnit tu = getTU();
		List myArrayStructs = tu.getChildrenOfType(ICElement.C_STRUCT);
		IStructure myIStruct = (IStructure) myArrayStructs.get(0);
		String[] myExpectedFields = { "m_field1", "m_field2", "m_field3", "m_field4", "m_field5", "m_field6", };
		List myArrayIField = myIStruct.getChildrenOfType(ICElement.C_FIELD);
		assertEquals(myExpectedFields.length, myArrayIField.size());
		for (int i = 0; i < myArrayIField.size(); i++) {
			IField myIField = (IField) myArrayIField.get(i);
			assertNotNull("Failed on " + i, myIField);
			assertEquals("Failed on " + i, myExpectedFields[i], myIField.getElementName());
		}
	}

	public void testGetField() throws CModelException {
		ITranslationUnit tu = getTU();
		List myArrayStructs = tu.getChildrenOfType(ICElement.C_STRUCT);
		IStructure myIStruct = (IStructure) myArrayStructs.get(0);
		String[] myExpectedFields = { "m_field1", "m_field2", "m_field3", "m_field4", "m_field5", "m_field6", };
		for (int i = 0; i < myExpectedFields.length; i++) {
			IField myIField = myIStruct.getField(myExpectedFields[i]);
			assertNotNull("Failed on " + i, myIField);
		}

		String[] myUnexpectedFields = { "m_field7", "m_field8", "m_field9", };
		for (int i = 0; i < myUnexpectedFields.length; i++) {
			IField myIField = myIStruct.getField(myUnexpectedFields[i]);
			assertNull("Failed on " + i, myIField);
		}
	}

	public void testGetMethods() throws CModelException {
		ITranslationUnit tu = getTU();
		List myArrayStructs = tu.getChildrenOfType(ICElement.C_STRUCT);
		IStructure myIStruct = (IStructure) myArrayStructs.get(0);
		IMethodDeclaration[] myArrayIMethod = myIStruct.getMethods();
		String[] myExpectedMethods = { "method1", "method2", "testStruct1", "~testStruct1" };
		assertEquals(myExpectedMethods.length, myArrayIMethod.length);
		for (int i = 0; i < myArrayIMethod.length; i++) {
			assertNotNull("Failed on " + i, myArrayIMethod[i]);
			assertEquals("Failed on " + i, myExpectedMethods[i], myArrayIMethod[i].getElementName());
		}
	}

	// TODO Bug# 38985: remove testGetMethodsHack()
	public void testGetMethodsHack() throws CModelException {
		ITranslationUnit tu = getTU();
		List myArrayStructs = tu.getChildrenOfType(ICElement.C_STRUCT);
		IStructure myIStruct = (IStructure) myArrayStructs.get(0);
		List myArrayIMethod = myIStruct.getChildrenOfType(ICElement.C_METHOD_DECLARATION);
		myArrayIMethod.addAll(myIStruct.getChildrenOfType(ICElement.C_METHOD));
		String[] myExpectedMethods = { "method1", "method2", "testStruct1", "~testStruct1" };
		assertEquals(myExpectedMethods.length, myArrayIMethod.size());
		for (int i = 0; i < myArrayIMethod.size(); i++) {
			IMethodDeclaration myIMethod = (IMethodDeclaration) myArrayIMethod.get(i);
			assertNotNull("Failed on " + i, myIMethod);
			assertEquals("Failed on " + i, myExpectedMethods[i], myIMethod.getElementName());
		}
	}

	public void testGetMethod() throws CModelException {
		ITranslationUnit tu = getTU();
		List myArrayStructs = tu.getChildrenOfType(ICElement.C_STRUCT);
		IStructure myIStruct = (IStructure) myArrayStructs.get(0);
		String[] myExpectedMethods = { "method1", "method2", "testStruct1", "~testStruct1" };
		for (int i = 0; i < myExpectedMethods.length; i++) {
			IMethodDeclaration myIMethod = myIStruct.getMethod(myExpectedMethods[i]);
			assertNotNull("Failed on " + i, myIMethod);
		}

		String[] myUnexpectedMethods = { "method7", "method8", "method9", };
		for (int i = 0; i < myUnexpectedMethods.length; i++) {
			IMethodDeclaration myIMethod = myIStruct.getMethod(myUnexpectedMethods[i]);
			assertNull("Failed on " + i, myIMethod);
		}
	}

	public void testIsUnion() throws CModelException {
		ITranslationUnit tu = getTU();
		ICElement myElementUnion = null;
		ICElement myElementNonUnion = null;
		try {
			myElementUnion = tu.getElement("testUnion1");
			myElementNonUnion = tu.getElement("testStruct1");
		} catch (CModelException e) {
			assertNotNull("CModelException thrown", e);
		}
		assertNotNull(myElementUnion);
		assertTrue(myElementUnion.getElementType() == ICElement.C_UNION);
		IStructure myStructUnion = (IStructure) myElementUnion;
		assertNotNull(myStructUnion);
		assertTrue(myStructUnion.isUnion());

		assertNotNull(myElementNonUnion);
		assertTrue(myElementNonUnion.getElementType() != ICElement.C_UNION);
		IStructure myStructNonUnion = (IStructure) myElementNonUnion;
		assertNotNull(myStructNonUnion);
		assertFalse(myStructNonUnion.isUnion());
	}

	public void testIsStruct() throws CModelException {
		ITranslationUnit tu = getTU();
		ICElement myElementStruct = null;
		ICElement myElementNonStruct = null;
		try {
			myElementStruct = tu.getElement("testStruct1");
			myElementNonStruct = tu.getElement("testClass1");
		} catch (CModelException e) {
			assertNotNull("CModelException thrown", e);
		}
		assertNotNull(myElementStruct);
		assertTrue(myElementStruct.getElementType() == ICElement.C_STRUCT);
		IStructure myStructStruct = (IStructure) myElementStruct;
		assertNotNull(myStructStruct);
		assertTrue(myStructStruct.isStruct());

		assertNotNull(myElementNonStruct);
		assertTrue(myElementNonStruct.getElementType() != ICElement.C_STRUCT);
		IStructure myStructNonStruct = (IStructure) myElementNonStruct;
		assertNotNull(myStructNonStruct);
		assertFalse(myStructNonStruct.isStruct());
	}

	public void testIsClass() throws CModelException {
		ITranslationUnit tu = getTU();
		ICElement myElementClass = null;
		ICElement myElementNonClass = null;
		try {
			myElementClass = tu.getElement("testClass1");
			myElementNonClass = tu.getElement("testStruct1");
		} catch (CModelException e) {
			assertNotNull("CModelException thrown", e);
		}
		assertNotNull(myElementClass);
		assertTrue(myElementClass.getElementType() == ICElement.C_CLASS);
		IStructure myStructClass = (IStructure) myElementClass;
		assertNotNull(myStructClass);
		assertTrue(myStructClass.isClass());

		assertNotNull(myElementNonClass);
		assertTrue(myElementNonClass.getElementType() != ICElement.C_CLASS);
		IStructure myStructNonClass = (IStructure) myElementNonClass;
		assertNotNull(myStructNonClass);
		assertFalse(myStructNonClass.isClass());
	}

	public void testIsAbstract() throws CModelException {
		ITranslationUnit tu = getTU();
		ICElement myElementAbstract = null;
		ICElement myElementNonAbstract = null;
		try {
			myElementAbstract = tu.getElement("testClass4Abstract");
			myElementNonAbstract = tu.getElement("testClass1");
		} catch (CModelException e) {
			assertNotNull("CModelException thrown", e);
		}
		assertNotNull(myElementAbstract);
		assertTrue(myElementAbstract.getElementType() == ICElement.C_CLASS);
		IStructure myStructAbstract = (IStructure) myElementAbstract;
		assertNotNull(myStructAbstract);
		assertTrue(myStructAbstract.isAbstract());

		assertNotNull(myElementNonAbstract);
		assertTrue(myElementNonAbstract.getElementType() == ICElement.C_CLASS);
		IStructure myStructNonAbstract = (IStructure) myElementNonAbstract;
		assertNotNull(myStructNonAbstract);
		assertFalse(myStructNonAbstract.isAbstract());
	}

	// IInheritance
	public void testGetBaseTypes() throws CModelException {
		ITranslationUnit tu = getTU();
		ICElement myElementDerived = null;
		String[] myBaseTypes = null;
		try {
			myElementDerived = tu.getElement("testClass5"); // throws
			assertNotNull(myElementDerived);
			assertTrue(myElementDerived.getElementType() == ICElement.C_CLASS);
			IStructure myStructDerived = (IStructure) myElementDerived;
			assertNotNull(myStructDerived);
			myBaseTypes = myStructDerived.getSuperClassesNames();
		} catch (CModelException e) {
			assertNotNull("CModelException thrown", e);
		}

		String[] myExpectedBaseTypes = { "testClass1", "testClass3", "testClass4Abstract" };
		assertEquals(myExpectedBaseTypes.length, myBaseTypes.length);
		for (int i = 0; i < myBaseTypes.length; i++) {
			assertEquals("Failed on " + i, myExpectedBaseTypes[i], myBaseTypes[i]);
		}
	}

	// IInheritance
	public void testGetAccessControl() throws CModelException {
		ITranslationUnit tu = getTU();
		ICElement myElementDerived = null;
		String[] myBaseTypes = null;
		try {
			myElementDerived = tu.getElement("testClass5"); // throws
			assertNotNull(myElementDerived);
			assertTrue(myElementDerived.getElementType() == ICElement.C_CLASS);
			IStructure myStructDerived = (IStructure) myElementDerived;
			assertNotNull(myStructDerived);
			myBaseTypes = myStructDerived.getSuperClassesNames();

			ASTAccessVisibility[] myExpectedAccessControl = {
					// TODO #38986: expect appropriate access control tags
					ASTAccessVisibility.PUBLIC, ASTAccessVisibility.PROTECTED, ASTAccessVisibility.PRIVATE };
			assertEquals(myExpectedAccessControl.length, myBaseTypes.length);
			for (int i = 0; i < myBaseTypes.length; i++) {
				ASTAccessVisibility myAccessControl = myStructDerived.getSuperClassAccess(myBaseTypes[i]);
				assertEquals("Failed on " + i, myExpectedAccessControl[i], myAccessControl);
			}
		} catch (CModelException e) {
			assertNotNull("CModelException thrown", e);
		}
	}

	// getStructureInfo
	public void testGetStructureInfo() {
	}

	// TODO: Not tested; Bug# 38958. public void testGetInitializer()
	// TODO: Not tested; Bug# 38958. public void testGetTypeName()
	// TODO: Not tested; Bug# 38958. public void testIsConst()
	// TODO: Not tested; Bug# 38958. public void testIsStatic()
	// TODO: Not tested; Bug# 38958. public void testIsVolatile()
	// TODO: Not tested; Bug# 38958. public void testGetAccessControl_Void()

	//
	// Language Specification Tests
	//

	public void testAnonymousStructObject() throws CModelException {
		ITranslationUnit tu = getTU();
		ICElement myElement = null;
		try {
			myElement = tu.getElement("testAnonymousStructObject1");
		} catch (CModelException e) {
			assertNotNull("CModelException thrown", e);
		}
		assertNotNull(myElement);
		assertEquals(ICElement.C_VARIABLE, myElement.getElementType());
	}

	public void testInnerStruct() throws CModelException {
		ITranslationUnit tu = getTU();
		ICElement myElement = null;
		try {
			myElement = tu.getElement("testStruct8");
		} catch (CModelException e) {
			assertNotNull("CModelException thrown", e);
		}
		assertNotNull(myElement);
		IStructure myIStruct = (IStructure) myElement;
		assertNotNull(myIStruct);

		String[] myExpectedInnerStructs = { "testStruct9Inner", "testStruct10Inner" };
		List myInnerStructs = myIStruct.getChildrenOfType(ICElement.C_STRUCT);
		assertEquals(myExpectedInnerStructs.length, myInnerStructs.size());
		for (int i = 0; i < myExpectedInnerStructs.length; i++) {
			IStructure myInnerStruct = (IStructure) myInnerStructs.get(i);
			assertNotNull("Failed on " + i, myInnerStruct);
			assertEquals("Failed on " + i, myExpectedInnerStructs[i], myInnerStruct.getElementName());
		}
	}
}

Back to the top