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
|
/*******************************************************************************
* Copyright (c) 2011 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 v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* CONTRIBUTORS:
* Henrik Rentz-Reichert (initial contribution)
*
*******************************************************************************/
package org.eclipse.etrice.dctools.tests
import org.eclipse.etrice.dctools.fsm.ast.DCParser
import org.junit.Before
import org.eclipse.etrice.dctools.fsm.ast.DCLanguage
import static org.eclipse.etrice.dctools.tests.TestConstants.*
import org.junit.Test
import org.eclipse.etrice.dctools.fsm.ast.DCNodeAtOffset
import static org.junit.Assert.*
import static org.hamcrest.CoreMatchers.*
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstOtherNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstIdentifierNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstBracketNode
class TestDCNodeAtOffset {
DCParser parser
@Before
def void createParser() {
parser = new DCParser(DCLanguage.C_LANGUAGE)
}
@Test
def void testString() {
val ast = parser.parse(TEST_STRING)
val result = DCNodeAtOffset.find(ast, 3)
assertNotNull(result)
assertTrue(result instanceof DCAstOtherNode)
assertThat(result.text, is(TEST_STRING))
}
@Test
def void testMemberAccess() {
val ast = parser.parse(TEST_MEMBER_ACCESS)
var result = DCNodeAtOffset.find(ast, 8)
assertNotNull(result)
assertTrue(result instanceof DCAstIdentifierNode)
assertThat(result.text, is("member"))
result = DCNodeAtOffset.find(ast, 4)
assertNotNull(result)
assertTrue(result instanceof DCAstIdentifierNode)
assertThat(result.text, is("field"))
}
@Test
def void testArrayMemberAccess() {
val ast = parser.parse(TEST_ARRAY_MEMBER_ACCESS)
var result = DCNodeAtOffset.find(ast, 14)
assertNotNull(result)
assertTrue(result instanceof DCAstIdentifierNode)
assertThat(result.text, is("member"))
result = DCNodeAtOffset.find(ast, 4)
assertNotNull(result)
assertTrue(result instanceof DCAstIdentifierNode)
assertThat(result.text, is("field"))
result = DCNodeAtOffset.find(ast, 7)
assertNotNull(result)
assertTrue(result instanceof DCAstOtherNode)
assertThat(result.text, is("+"))
}
@Test
def void testCode() {
val ast = parser.parse(TEST_CODE)
var result = DCNodeAtOffset.find(ast, 16)
assertNotNull(result)
assertTrue(result instanceof DCAstBracketNode)
if (result instanceof DCAstBracketNode) {
assertThat(result.text, is("()"))
assertThat(result.begin, is(15))
assertThat(result.end, is(16))
assertThat(result.posClose, is(16))
}
}
@Test
def void testCode2() {
val ast = parser.parse(TEST_CODE2)
// note: because line breaks differ on UNIX and Windows we can't use a constant offset here
val pos = TEST_CODE2.indexOf("456")+2
var result = DCNodeAtOffset.find(ast, pos)
assertNotNull(result)
assertTrue(result instanceof DCAstOtherNode)
assertThat(result.text, is("456"))
}
}
|