Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2673bea6d5fdef4d11ff2d1de1c1fb0d8a0971fe (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
/*******************************************************************************
 * Copyright (c) 2005, 2008 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.jface.text.tests.rules;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import org.eclipse.swt.SWT;

import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.PatternRule;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WordRule;

/**
 * @since 3.3
 */
public class WordRuleTest {


	private static class SimpleWordDetector implements IWordDetector {
		@Override
		public boolean isWordStart(char c) {
			return !Character.isWhitespace(c);
		}

		@Override
		public boolean isWordPart(char c) {
			return !Character.isWhitespace(c);
		}
	}


	/*
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=163116
	 */
	@Test
	public void testBug163116() throws Exception {
		IWordDetector detector= new IWordDetector() {

			@Override
			public boolean isWordPart(char c) {
				return true;
			}

			@Override
			public boolean isWordStart(char c) {
				return true;
			}

		};

		WordRule rule= new WordRule(detector, new Token(this));

		RuleBasedScanner scanner= new RuleBasedScanner();
		scanner.setRules(new IRule[] { rule });
		scanner.setRange(new Document(), 0, 0);

		IToken token= null;
		int i= 0;
		while (token != Token.EOF && i++ < 1000)
			token= scanner.nextToken();

		assertTrue(i < 1000);

	}

	/*
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=144355
	 */
	@Test
	public void testBug144355() throws Exception {
		IWordDetector detector= new SimpleWordDetector();

		String defaultTokenString= "defaultToken";
		Token defaultToken= new Token(defaultTokenString);

		String testTokenStringNormal= "TestTokenString";
		String testTokenStringDifferentCapitalization= "TestTOKENString";
		String testTokenStringCompletelyDifferent= "XXX";
		Token normalToken= new Token(testTokenStringNormal);

		WordRule rule= new WordRule(detector, defaultToken, true);
		rule.addWord(testTokenStringNormal, normalToken);

		// scenario 1
		// pre: pass in a normal string ("TestTokenString")
		// post: expect the normal token to be returned
		RuleBasedScanner scanner= new RuleBasedScanner();
		scanner.setRules(new IRule[] {rule});
		scanner.setRange(new Document(testTokenStringNormal), 0, testTokenStringNormal.length());
		assertTrue(scanner.nextToken().getData().equals(testTokenStringNormal));

		// scenario 2
		// pre: pass in a normal string but different capitalization ("TestTOKENString")
		// post: expect the normal token to be returned
		scanner= new RuleBasedScanner();
		scanner.setRules(new IRule[] {rule});
		scanner.setRange(new Document(testTokenStringDifferentCapitalization), 0, testTokenStringDifferentCapitalization.length());
		assertTrue(scanner.nextToken().getData().equals(testTokenStringNormal));

		// scenario 3
		// pre: pass in a completely different string ("XXX")
		// post: expect the default token to be returned because the string can't be matched
		scanner= new RuleBasedScanner();
		scanner.setRules(new IRule[] {rule});
		scanner.setRange(new Document(testTokenStringCompletelyDifferent), 0, testTokenStringCompletelyDifferent.length());
		assertTrue(scanner.nextToken().getData().equals(defaultTokenString));

		WordRule ruleWithoutIgnoreCase= new WordRule(detector, defaultToken);
		ruleWithoutIgnoreCase.addWord(testTokenStringNormal, normalToken);

		// scenario 4
		// pre: pass in a normal string ("TestTokenString")
		// post: expect the normal token to be returned
		scanner= new RuleBasedScanner();
		scanner.setRules(new IRule[] {ruleWithoutIgnoreCase});
		scanner.setRange(new Document(testTokenStringNormal), 0, testTokenStringNormal.length());
		assertTrue(scanner.nextToken().getData().equals(testTokenStringNormal));

		// scenario 5
		// pre: pass in a normal string but different capitalization ("TestTOKENString")
		// post: expect the default token to be returned
		scanner= new RuleBasedScanner();
		scanner.setRules(new IRule[] {ruleWithoutIgnoreCase});
		scanner.setRange(new Document(testTokenStringDifferentCapitalization), 0, testTokenStringDifferentCapitalization.length());
		assertTrue(scanner.nextToken().getData().equals(defaultTokenString));
	}

	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=175712
	@Test
	public void testBug175712_1() throws Exception {
		IRule[] rules= new IRule[2];

		IToken stepToken= new Token(new TextAttribute(null, null, SWT.BOLD));
		PatternRule stepRule= new PatternRule("(((", ")", stepToken, (char) 0,false);
		stepRule.setColumnConstraint(-1);
		rules[1]= stepRule;

		IToken titleToken= new Token(new TextAttribute(null, null, SWT.BOLD));
		WordRule wordRule= new WordRule(new SimpleWordDetector());
		wordRule.addWord("((", titleToken);
		rules[0]= wordRule;

		IDocument document= new Document("((( \n((\n- Cheese\n- Wine");
		RuleBasedScanner scanner= new RuleBasedScanner();
		scanner.setRules(rules);
		scanner.setRange(document, 0, document.getLength());

		IToken defaultToken= new Token(this);
		scanner.setDefaultReturnToken(defaultToken);

		IToken token= scanner.nextToken();
		assertSame(defaultToken, token);

		token= scanner.nextToken();
		assertSame(defaultToken, token);

		token= scanner.nextToken();
		assertSame(defaultToken, token);

		token= scanner.nextToken();
		assertSame(titleToken, token);

	}

	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=175712
	@Test
	public void testBug175712_2() throws Exception {
		IRule[] rules= new IRule[2];

		IToken stepToken= new Token(new TextAttribute(null, null, SWT.BOLD));
		PatternRule stepRule= new PatternRule("(((", ")", stepToken, (char) 0,false);
		stepRule.setColumnConstraint(-1);
		rules[1]= stepRule;

		IToken titleToken= new Token(new TextAttribute(null, null, SWT.BOLD));
		WordRule wordRule= new WordRule(new SimpleWordDetector());
		wordRule.addWord("((", titleToken);
		rules[0]= wordRule;

		IDocument document= new Document("((\n((\n- Cheese\n- Wine");
		RuleBasedScanner scanner= new RuleBasedScanner();
		scanner.setRules(rules);
		scanner.setRange(document, 0, document.getLength());

		IToken defaultToken= new Token(this);
		scanner.setDefaultReturnToken(defaultToken);

		IToken token= scanner.nextToken();
		assertSame(titleToken, token);

	}


}

Back to the top