Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 77a07cecb047a660d787837c54e52cb181fafb6b (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
/*
 * Copyright (c) 2013, 2015 QNX Software Systems 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
 */
package org.eclipse.cdt.qt.tests;

import org.eclipse.cdt.core.dom.ast.IASTCompletionNode;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.qt.ui.assist.QPropertyExpansion;
import org.eclipse.cdt.ui.text.contentassist.ICEditorContentAssistInvocationContext;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.ui.IEditorPart;

import junit.framework.TestCase;

public class QtContentAssistantTests extends TestCase {

	public void testQPropertyProposals() throws Exception {

		String decl = "Q_PROPERTY( type name READ accessor WRITE modifier )";
		int atEnd = decl.length();
		int atRParen = decl.indexOf(')');
		int afterModifier = decl.indexOf("modifier") + "modifier".length();
		int afterWRITE = decl.indexOf("WRITE") + "WRITE".length();
		int inModifier = decl.indexOf("modifier") + 3;
		int inWRITE = decl.indexOf("WRITE") + 3;

		IDocument doc = new Document(decl);

		// The expansion is not applicable when invoked after the closing paren
		QPropertyExpansion exp = QPropertyExpansion.create(new Context(doc, atEnd));
		assertNull(exp);

		exp = QPropertyExpansion.create(new Context(doc, atRParen));
		assertNotNull(exp);
		assertNull(exp.getCurrIdentifier());
		assertEquals("modifier", exp.getPrevIdentifier());

		exp = QPropertyExpansion.create(new Context(doc, afterModifier));
		assertNotNull(exp);
		assertEquals("modifier", exp.getCurrIdentifier());
		assertEquals("WRITE", exp.getPrevIdentifier());

		exp = QPropertyExpansion.create(new Context(doc, afterWRITE));
		assertNotNull(exp);
		assertEquals("WRITE", exp.getCurrIdentifier());
		assertEquals("accessor", exp.getPrevIdentifier());

		exp = QPropertyExpansion.create(new Context(doc, inModifier));
		assertNotNull(exp);
		assertEquals("modifier", exp.getCurrIdentifier());
		assertEquals("WRITE", exp.getPrevIdentifier());

		exp = QPropertyExpansion.create(new Context(doc, inWRITE));
		assertNotNull(exp);
		assertEquals("WRITE", exp.getCurrIdentifier());
		assertEquals("accessor", exp.getPrevIdentifier());
	}

	public void testQPropertyWithoutLeadingWhitespace() throws Exception {

		String decl = "Q_PROPERTY(type name READ accessor )";
		int atRParen = decl.indexOf(')');
		IDocument doc = new Document(decl);

		// The expansion should be created even when there is no leading whitesapce in the
		// expansion parameter.
		QPropertyExpansion exp = QPropertyExpansion.create(new Context(doc, atRParen));
		assertNotNull(exp);
	}

	public void testQPropertyPrefixes() throws Exception {
		String decl = "Q_PROPERTY( type name READ accessor WRITE  )";
		int len = decl.length();
		IDocument doc = new Document(decl);

		// The expansion is not applicable when invoked after the closing paren
		QPropertyExpansion atEnd = QPropertyExpansion.create(new Context(doc, len));
		assertNull(atEnd);

		QPropertyExpansion inWS = QPropertyExpansion.create(new Context(doc, len - 2));
		assertNotNull(inWS);
		assertNull(inWS.getPrefix());
		assertNull(inWS.getCurrIdentifier());
		assertEquals("WRITE", inWS.getPrevIdentifier());

		QPropertyExpansion afterWRITE = QPropertyExpansion.create(new Context(doc, len - 3));
		assertNotNull(afterWRITE);
		assertEquals("WRITE", afterWRITE.getPrefix());
		assertEquals("WRITE", afterWRITE.getCurrIdentifier());
		assertEquals("accessor", afterWRITE.getPrevIdentifier());

		QPropertyExpansion inWRITE_e = QPropertyExpansion.create(new Context(doc, len - 4));
		assertNotNull(inWRITE_e);
		assertEquals("WRIT", inWRITE_e.getPrefix());
		assertEquals("WRITE", inWRITE_e.getCurrIdentifier());
		assertEquals("accessor", inWRITE_e.getPrevIdentifier());

		QPropertyExpansion inWRITE_b = QPropertyExpansion.create(new Context(doc, len - 6));
		assertNotNull(inWRITE_b);
		assertEquals("WR", inWRITE_b.getPrefix());
		assertEquals("WRITE", inWRITE_b.getCurrIdentifier());
		assertEquals("accessor", inWRITE_b.getPrevIdentifier());

		QPropertyExpansion startWRITE = QPropertyExpansion.create(new Context(doc, len - 8));
		assertNotNull(startWRITE);
		assertNull(startWRITE.getPrefix());
		assertNull(startWRITE.getCurrIdentifier());
		assertEquals("accessor", startWRITE.getPrevIdentifier());
	}

	// This implements only the parts that are known to be used in the QPropertyExpansion
	// implementation.
	private static class Context implements ICEditorContentAssistInvocationContext {

		private final IDocument doc;
		private final int contextOffset;
		private final int invokedOffset;

		public Context(IDocument doc, int invoked) {
			this.doc = doc;
			this.contextOffset = doc.get().indexOf('(') + 1;
			this.invokedOffset = invoked;
		}

		@Override
		public int getInvocationOffset() {
			return invokedOffset;
		}

		@Override
		public int getContextInformationOffset() {
			return contextOffset;
		}

		@Override
		public IDocument getDocument() {
			return doc;
		}

		@Override
		public boolean isContextInformationStyle() {
			return false;
		}

		@Override
		public ITextViewer getViewer() {
			return null;
		}

		@Override
		public ITranslationUnit getTranslationUnit() {
			return null;
		}

		@Override
		public ICProject getProject() {
			return null;
		}

		@Override
		public int getParseOffset() {
			return 0;
		}

		@Override
		public IEditorPart getEditor() {
			return null;
		}

		@Override
		public IASTCompletionNode getCompletionNode() {
			return null;
		}

		@Override
		public CharSequence computeIdentifierPrefix() throws BadLocationException {
			return null;
		}
	};
}

Back to the top