Skip to main content
summaryrefslogtreecommitdiffstats
blob: d53209866d5dfe70f8d06d7e6f00ea7c2df1d309 (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
package org.eclipse.xpand3.parser;

import java.util.List;

import junit.framework.TestCase;

import org.eclipse.xpand3.node.CompositeNode;
import org.eclipse.xpand3.node.LeafNode;
import org.eclipse.xpand3.node.LexedToken;
import org.eclipse.xpand3.node.Node;

public abstract class AbstractXpand3NodeParserTest extends TestCase {

	public static final char LG = '\u00AB';
	public static final char RG = '\u00BB';

	public static final String LGS = String.valueOf(LG);
	public static final String RGS = String.valueOf(RG);

	public AbstractXpand3NodeParserTest() {
		super();
	}

	public AbstractXpand3NodeParserTest(String name) {
		super(name);
	}

	protected CompositeNode checkIsRule(Node node, String ruleName,
			int numChildren) {
		assertTrue(node instanceof CompositeNode);
		assertEquals(ruleName, ((CompositeNode) node).getRule());
		assertEquals(numChildren, ((CompositeNode) node).getChildren().size());
		return (CompositeNode) node;
	}

	private LeafNode checkIsToken(Node node, String tokenText) {
		assertTrue(node instanceof LeafNode);
		LexedToken token = ((LeafNode) node).getToken();
		assertNotNull(token);
		assertEquals(tokenText, token.getText());
		return (LeafNode) node;
	}

	private Node getChild(Node parent, int index) {
		assertNotNull(parent);
		assertTrue(parent instanceof CompositeNode);
		List<Node> children = ((CompositeNode) parent).getChildren();
		assertTrue(children.size() > index);
		Node child = children.get(index);
		return child;
	}

	protected CompositeNode checkChildIsRule(Node parent, int index,
			String ruleName, int numChildren) {
		return checkIsRule(getChild(parent, index), ruleName, numChildren);
	}

	protected CompositeNode checkChildIsSimpleType(Node parent, int index,
			String identifierName) {
		CompositeNode simpleType = checkChildIsRule(parent, index,
				"r_simpleType", 1);
		checkChildIsIdentifier(simpleType, 0, identifierName);
		return simpleType;
	}

	protected CompositeNode checkChildIsCollectionType(Node parent, int index,
			String identifierName) {
		CompositeNode collectionType = checkChildIsRule(parent, index,
				"r_collectionType", 1);
		checkChildIsToken(collectionType, 0, identifierName);
		return collectionType;
	}

	protected CompositeNode checkChildIsIdentifier(Node parent, int index,
			String identifierName) {
		CompositeNode identifier = checkChildIsRule(parent, index,
				"r_identifier", 1);
		checkChildIsToken(identifier, 0, identifierName);
		return identifier;
	}

	protected CompositeNode checkChildIsScopedType(Node parent, int index,
			String... scopeName) {
		CompositeNode scopedType = checkChildIsRule(parent, index,
				"r_simpleType", 2 * scopeName.length - 1);
		checkChildIsIdentifier(scopedType, 0, scopeName[0]);
		for (int i = 1; i < scopeName.length; i++) {
			checkChildIsToken(scopedType, 2 * i - 1, "::");
			checkChildIsIdentifier(scopedType, 2 * i, scopeName[i]);
		}
		return scopedType;
	}

	protected CompositeNode checkChildIsNumberLiteral(Node parent, int index,
			String value) {
		CompositeNode numberLiteral = checkChildIsRule(parent, index,
				"r_numberLiteral", 1);
		checkChildIsToken(numberLiteral, 0, value);
		return numberLiteral;
	}

	protected CompositeNode checkChildIsStringLiteral(Node parent, int index,
			String value) {
		CompositeNode stringLiteral = checkChildIsRule(parent, index,
				"r_stringLiteral", 1);
		checkChildIsToken(stringLiteral, 0, value);
		return stringLiteral;
	}

	protected CompositeNode checkChildIsTrueLiteral(Node parent, int index) {
		CompositeNode booleanLiteral = checkChildIsRule(parent, index,
				"r_booleanLiteral", 1);
		checkChildIsToken(booleanLiteral, 0, "true");
		return booleanLiteral;
	}

	protected CompositeNode checkChildIsFalseLiteral(Node parent, int index) {
		CompositeNode booleanLiteral = checkChildIsRule(parent, index,
				"r_booleanLiteral", 1);
		checkChildIsToken(booleanLiteral, 0, "false");
		return booleanLiteral;
	}

	protected LeafNode checkChildIsToken(Node parent, int index,
			String tokenText) {
		return checkIsToken(getChild(parent, index), tokenText);
	}

	protected String tag(final String str) {
		return LG + str + RG;
	}

	protected CompositeNode checkChildIsSequenceText(Node parent, int index,
			String textWithoutGuillemots, int numChildren) {
		CompositeNode sequence = checkChildIsRule(parent, index, "r_sequence",
				numChildren);
		return checkChildIsText(sequence, 0, textWithoutGuillemots);
	}

	protected CompositeNode checkChildIsText(CompositeNode sequence, int index,
			String textWithoutGuillemots) {
		CompositeNode text = checkChildIsRule(sequence, index, "r_text", 1);
		checkChildIsToken(text, 0, RGS + textWithoutGuillemots + LGS);
		return sequence;
	}

}

Back to the top