Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e07a772edb9e17c7177523c386e322ed72da1f5b (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*******************************************************************************
 * Copyright (c) 2018 protos software gmbh (http://www.protos.de).
 * All rights reserved. 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.dctools.tests

import org.eclipse.etrice.dctools.fsm.ast.DCLanguage
import org.eclipse.etrice.dctools.fsm.ast.DCParser
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstArrayAccessNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstBracketNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstFeatureCallNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstIdentifierNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstMatchNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstOperationCallNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstOtherNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstPeriodNode
import org.eclipse.etrice.dctools.fsm.ast.util.DCAstPrinter
import org.junit.Before
import org.junit.Test

import static org.eclipse.etrice.dctools.tests.TestConstants.*
import static org.hamcrest.CoreMatchers.*
import static org.junit.Assert.*

class TestDCParser {
		
	DCParser parser
	
	@Before
	def void createParser() {
		parser = new DCParser(DCLanguage.C_LANGUAGE)
	}
	
	@Test
	def void testString() {
		testSingleOther("testString", TEST_STRING)
	}
	
	@Test
	def void testCComment() {
		testSingleOther("testCComment", TEST_C_COMMENT)
	}
	
	@Test
	def void testCComment2() {
		testSingleOther("testCComment2", TEST_C_COMMENT2)
	}
	
	@Test
	def void testCppComment() {
		testSingleOther("testCppComment", TEST_CPP_COMMENT)
	}
	
	@Test
	def void testEmptyRoundBracket() {
		testBracketExpression(TEST_EMPTY_ROUND_BRACKET, DCAstBracketNode.BracketType.ROUND)
	}
	
	@Test
	def void testEmptyCurlyBracket() {
		testBracketExpression(TEST_EMPTY_CURLY_BRACKET, DCAstBracketNode.BracketType.CURLY)
	}
	
	@Test
	def void testEmptySquareBracket() {
		testBracketExpression(TEST_EMPTY_SQUARE_BRACKET, DCAstBracketNode.BracketType.SQUARE)
	}
	
	@Test
	def void testRoundBracketWithContents() {
		testBracketExpression(TEST_EMPTY_ROUND_BRACKET_W_CONTENTS1, DCAstBracketNode.BracketType.ROUND, CONTENTS1)
	}
	
	@Test
	def void testNestedBrackets() {
		val result = parser.parse(TEST_NESTED_BRACKETS)
		assertNotNull(result)
		
		// we have a single bracket expression
		assertThat(result.children.size, is(1))
		var node = result.children.get(0)
		
		// it is a bracket node
		assertTrue(node instanceof DCAstBracketNode)
		assertTrue((node as DCAstBracketNode).type==DCAstBracketNode.BracketType.ROUND)
		
		// which has one child
		assertThat(node.children.size, is(1))
		node = node.children.get(0)
		
		// which is a match node
		assertTrue(node instanceof DCAstMatchNode)
		
		// which has three children
		assertThat(node.children.size, is(3))
		
		// first child is a comment
		node = node.children.get(0)
		assertTrue(node instanceof DCAstOtherNode)
		assertEquals((node as DCAstOtherNode).text, CONTENTS1)
		
		// third child is a comment
		node = node.parent.children.get(2)
		assertTrue(node instanceof DCAstOtherNode)
		assertEquals((node as DCAstOtherNode).text, CONTENTS3)
		
		// middle child is a bracket
		node = node.parent.children.get(1)
		assertTrue(node instanceof DCAstBracketNode)
		assertTrue((node as DCAstBracketNode).type==DCAstBracketNode.BracketType.ROUND)
		
		// which has one child
		assertThat(node.children.size, is(1))
		node = node.children.get(0)
		
		// which is a match node
		assertTrue(node instanceof DCAstMatchNode)
		
		// which has one child
		assertThat(node.children.size, is(1))
		node = node.children.get(0)
		
		// which is a comment
		assertTrue(node instanceof DCAstOtherNode)
		assertEquals((node as DCAstOtherNode).text, CONTENTS2)
		
		prettyPrint("testNestedBrackets", TEST_NESTED_BRACKETS, result, 7, 8)
	}
	
	@Test
	def void testIdentifier() {
		val result = parser.parse(TEST_IDENTIFIER)
		assertNotNull(result)
		
		// we have a feature
		assertThat(result.children.size, is(1))
		var node = result.children.get(0)
		assertTrue(node instanceof DCAstFeatureCallNode)
		
		// it is an identifier node
		node = node.children.get(0)
		assertTrue(node instanceof DCAstIdentifierNode)
		assertThat((node as DCAstIdentifierNode).id, is(TEST_IDENTIFIER))
		
		prettyPrint("testIdentifier", TEST_IDENTIFIER, result, 1, 3)
	}
	
	@Test
	def void testArrayAccess() {
		val result = parser.parse(TEST_ARRAY_ACCESS)
		assertNotNull(result)
		
		// we have a feature
		assertThat(result.children.size, is(1))
		var node = result.children.get(0)
		assertTrue(node instanceof DCAstFeatureCallNode)
		
		// with an array access
		assertThat(node.children.size, is(1))
		node = node.children.get(0)
		assertTrue(node instanceof DCAstArrayAccessNode)

		// with two children		
		assertThat(node.children.size, is(2))
		
		// an identifier
		node = node.children.get(0)
		assertTrue(node instanceof DCAstIdentifierNode)
		
		// and a bracket
		node = node.parent.children.get(1)
		assertTrue(node instanceof DCAstBracketNode)
		
		prettyPrint("testArrayAccess", TEST_ARRAY_ACCESS, result, 3, 6)
	}
	
	@Test
	def void testOperationCall() {
		val result = parser.parse(TEST_OPERATION_CALL)
		assertNotNull(result)
		
		// we have a feature
		assertThat(result.children.size, is(1))
		var node = result.children.get(0)
		assertTrue(node instanceof DCAstFeatureCallNode)
		
		// with an operation call
		assertThat(node.children.size, is(1))
		node = node.children.get(0)
		assertTrue(node instanceof DCAstOperationCallNode)

		// with two children		
		assertThat(node.children.size, is(2))
		
		// an identifier
		node = node.children.get(0)
		assertTrue(node instanceof DCAstIdentifierNode)
		
		// and a bracket
		node = node.parent.children.get(1)
		assertTrue(node instanceof DCAstBracketNode)
		
		prettyPrint("testOperationCall", TEST_OPERATION_CALL, result, 3, 6)
	}
	
	@Test
	def void testMemberAccess() {
		val result = parser.parse(TEST_MEMBER_ACCESS)
		assertNotNull(result)
		
		// we have a feature
		assertThat(result.children.size, is(1))
		var node = result.children.get(0)
		assertTrue(node instanceof DCAstFeatureCallNode)
		
		// with identifier period identifier
		assertThat(node.children.size, is(3))
		
		// identifier node
		node = node.children.get(0)
		assertTrue(node instanceof DCAstIdentifierNode)
		
		// period node
		node = node.parent.children.get(1)
		assertTrue(node instanceof DCAstPeriodNode)
		
		// identifier node
		node = node.parent.children.get(2)
		assertTrue(node instanceof DCAstIdentifierNode)
		
		prettyPrint("testMemberAccess", TEST_MEMBER_ACCESS, result, 3, 5)
	}
	
	@Test
	def void testArrayMemberAccess() {
		val result = parser.parse(TEST_ARRAY_MEMBER_ACCESS)
		assertNotNull(result)
		
		prettyPrint("testArrayMemberAccess", TEST_ARRAY_MEMBER_ACCESS, result, 8, 11)
	}
	
	@Test
	def void testArrayMemberAccessAndCall() {
		val result = parser.parse(TEST_ARRAY_MEMBER_ACCESS_AND_CALL)
		assertNotNull(result)
		
		prettyPrint("testArrayMemberAccessAndCall", TEST_ARRAY_MEMBER_ACCESS_AND_CALL, result, 12, 16)
	}
	
	@Test
	def void testSpaciousArrayMemberAccessAndCall() {
		val result = parser.parse(TEST_SPACIOUS_ARRAY_MEMBER_ACCESS_AND_CALL)
		assertNotNull(result)
		
		prettyPrint("testSpacyArrayMemberAccessAndCall", TEST_SPACIOUS_ARRAY_MEMBER_ACCESS_AND_CALL, result, 23, 27)
	}
	
	@Test
	def void testCode() {
		val result = parser.parse(TEST_CODE)
		assertNotNull(result)
		
		prettyPrint("testCode", TEST_CODE, result, 20, 29)
	}
	
	@Test
	def void testCode1() {
		val result = parser.parse(TEST_CODE1)
		assertNotNull(result)
		
		prettyPrint("testCode", TEST_CODE1, result, 15, 20)
	}
	
	@Test
	def void testCode2() {
		val result = parser.parse(TEST_CODE2)
		assertNotNull(result)
		
		prettyPrint("testCode2", TEST_CODE2, result, 59, 76)
	}
	
	@Test
	def void testCode3() {
		val result = parser.parse(TEST_CODE3)
		assertNotNull(result)
		
		prettyPrint("testCode3", TEST_CODE3, result, 69, 81)
	}
	
	@Test
	def void testUnclosedString() {
		val result = parser.parse(TEST_UNCLOSED_STRING)
		assertNotNull(result)
		
		prettyPrint("testUnclosedString", TEST_UNCLOSED_STRING, result, 1, 2)
	}
	
	@Test
	def void testUnclosedNestedBrackets() {
		val result = parser.parse(TEST_UNCLOSED_NESTED_BRACKETS)
		assertNotNull(result)
		
		prettyPrint("testUnclosedNestedBrackets", TEST_UNCLOSED_NESTED_BRACKETS, result, 6, 7)
	}
	
	@Test
	def void testIncompleteFeatureCall() {
		val result = parser.parse(TEST_INCOMPLETE_FEATURE_CALL)
		assertNotNull(result)
		
		prettyPrint("testIncompleteFeatureCall", TEST_INCOMPLETE_FEATURE_CALL, result, 4, 6)
	}
	
	/*
	 * helper methods
	 */
	private def prettyPrint(String method, String orig, DCAstMatchNode node, int nTokes, int nNodes) {
		val printer = new DCAstPrinter
		node.visit(printer)
		
		println("<<<" + method)
		println("tree: ")
		node.print
		println(nNodes + " nodes, " + nTokes + " tokens")
		println("orig: '" + orig + "'")
		println("ast:  '" + printer.text + "'")
		println(method + ">>>")
		
		assertEquals(orig, printer.text)
		assertThat(node.totallyReadTokens, is(nTokes))
		assertThat(node.countNodes, is(nNodes))
	}
	
	private def void testBracketExpression(String test, DCAstBracketNode.BracketType bt) {
		testBracketExpression(test, bt, null)
	}
	
	private def void testBracketExpression(String test, DCAstBracketNode.BracketType bt, String contents) {
		val result = parser.parse(test)
		assertNotNull(result)
		
		// we have only a bracket expression
		assertThat(result.children.size, is(1))
		var node = result.children.get(0)
		
		// it is a bracket node
		assertTrue(node instanceof DCAstBracketNode)
		assertTrue((node as DCAstBracketNode).type==bt)
		
		// which has one child
		assertThat(node.children.size, is(1))
		node = node.children.get(0)
		
		// which is a match node
		assertTrue(node instanceof DCAstMatchNode)
		
		if (contents===null) {
			// which is empty
			assertThat(node.children.size, is(0))
		}
		else {
			// which has a single sub node
			assertThat(node.children.size, is(1))
			
			// which is Other with contents
			testOther(node.children.get(0), contents)
		}
	}
	
	private def testSingleOther(String method, String test) {
		val result = parser.parse(test)
		assertNotNull(result)
		
		prettyPrint(method, test, result, 1, 2)
		
		// only one Other expression
		assertThat(result.children.size, is(1))
		
		testOther(result.children.get(0), test)
	}
	
	private def testOther(DCAstNode node, String contents) {
		// which is an Other node
		assertTrue(node instanceof DCAstOtherNode)
		
		// with the test text as contents
		assertEquals((node as DCAstOtherNode).text, contents)
	}
	
	private def int countNodes(DCAstNode node) {
		var sum = 1
		
		for (child : node.children) {
			sum += child.countNodes
		}
		
		return sum
	}
}

Back to the top