Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1292ecf3d1843bb59293c614f733e0bdfebd20b7 (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
/*******************************************************************************
 * Copyright (c) 2000, 2019 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.text.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.TextUtilities;


/**
 * A test case for text utilities.
 */
public class TextUtilitiesTest {

	/**
	 * A document which is a copy of another document.
	 * Implementation uses old document state.
	 */
	private static class LazilyMirroredDocument extends Document {

		private final class DocumentListener implements IDocumentListener {
			@Override
			public void documentAboutToBeChanged(DocumentEvent event) { /* not used */ }
			@Override
			public void documentChanged(DocumentEvent event) {
				fEvents.add(event);
			}
		}

		/** The document listener. */
		private final DocumentListener fDocumentListener= new DocumentListener();

		/** The buffered events. */
		private final List<DocumentEvent> fEvents= new ArrayList<>();

		public LazilyMirroredDocument(IDocument document) {
			document.addDocumentListener(fDocumentListener);
		}

		private void flush() throws BadLocationException {
			DocumentEvent event= TextUtilities.mergeUnprocessedDocumentEvents(this, fEvents);
			if (event == null)
				return;

			replace(event.getOffset(), event.getLength(), event.getText());
			fEvents.clear();
		}

		/*
		 * Should override all other getXXX() methods as well, but it's sufficient for the test.
		 *
		 * @see org.eclipse.jface.text.IDocument#get()
		 */
		@Override
		public String get() {
			try {
				flush();
			} catch (BadLocationException e) {
				assertFalse(true);
			}
			return super.get();
		}
	}

	/**
	 * A document which is a copy of another document.
	 * Implementation uses new document state.
	 */
	private static class LazilyMirroredDocument2 extends Document {

		private final class DocumentListener implements IDocumentListener {
			@Override
			public void documentAboutToBeChanged(DocumentEvent event) { /* not used */ }
			@Override
			public void documentChanged(DocumentEvent event) {
				event= new DocumentEvent(event.getDocument(), event.getOffset(), event.getLength(), event.getText());
				fEvents.add(event);
			}
		}

		/** The document listener. */
		private final DocumentListener fDocumentListener= new DocumentListener();

		/** The buffered events. */
		private final List<DocumentEvent> fEvents= new ArrayList<>();

		public LazilyMirroredDocument2(IDocument document) {
			document.addDocumentListener(fDocumentListener);
		}

		private void flush() throws BadLocationException {
			DocumentEvent event= TextUtilities.mergeProcessedDocumentEvents(fEvents);
			if (event == null)
				return;

			replace(event.getOffset(), event.getLength(), event.getText());
			fEvents.clear();
		}

		/*
		 * Should override all other getXXX() methods as well, but it's sufficient for the test.
		 *
		 * @see org.eclipse.jface.text.IDocument#get()
		 */
		@Override
		public String get() {
			try {
				flush();
			} catch (BadLocationException e) {
				Assert.fail("bad implementation");
			}
			return super.get();
		}
	}


	private static DocumentEvent createRandomEvent(IDocument document, int maxLength, char character) {

		int index0= (int) (Math.random() * (maxLength + 1));
		int index1= (int) (Math.random() * (maxLength + 1));

		int offset= Math.min(index0, index1);
		int length= Math.max(index0, index1) - offset;

		int stringLength=  (int) (Math.random() * 10);
		StringBuilder buffer= new StringBuilder(stringLength);
		for (int i= 0; i < stringLength; ++i)
			buffer.append(character);

		return new DocumentEvent(document, offset, length, buffer.toString());
	}

	@Test
	public void testMergeEvents1() {
		IDocument reference= new Document();
		LazilyMirroredDocument testee= new LazilyMirroredDocument(reference);

		try {
			reference.replace(0, 0, "01234567890123");
			check(reference, testee);

			reference.replace(4, 3, "moo ");
			reference.replace(9, 2, "asd");
			check(reference, testee);

		} catch (BadLocationException e) {
			Assert.fail("bad location exception");
		}
	}

	@Test
	public void testMergeEvents() {
		IDocument reference= new Document();
		LazilyMirroredDocument testee= new LazilyMirroredDocument(reference);

		try {

			List<DocumentEvent> events= new ArrayList<>();
			int currentLength= 0;

			events.add(new DocumentEvent(reference, 0, 0, "foo bar goo haa"));
			events.add(new DocumentEvent(reference, 0, "foo bar goo haa".length(), "foo bar goo haa"));
			events.add(new DocumentEvent(reference, 4, 4, "xxxx"));
			events.add(new DocumentEvent(reference, 4, 4, "yyy"));
			events.add(new DocumentEvent(reference, 4, 3, "moo "));
			events.add(new DocumentEvent(reference, 9, 2, "asd"));
			events.add(new DocumentEvent(reference, 0, 2, "asd"));

			for (Iterator<DocumentEvent> iterator= events.iterator(); iterator.hasNext();) {
				DocumentEvent event= iterator.next();
				currentLength += event.getText().length() - event.getLength();
			}

			for (int i= 0; i < 500; i++) {
				char character= (char) (32 + i % 95);
				DocumentEvent event= createRandomEvent(reference, currentLength, character);
				currentLength += event.getText().length() - event.getLength();
				events.add(event);
			}

			for (Iterator<DocumentEvent> iterator= events.iterator(); iterator.hasNext();) {
				DocumentEvent event= iterator.next();

//				System.err.println(event.getOffset() + ", " + event.getLength() + ", [" + event.getText() + "]") ;

				reference.replace(event.getOffset(), event.getLength(), event.getText());
				if (Math.random() < 0.3) {
//					System.err.println("check");
					check(reference, testee);
//					System.err.println("length= " + reference.getLength());
				}
			}

			check(reference, testee);

//			System.out.println("[" + reference.get() + "]");
//			System.out.println("[" + testee.get() + "]");

		} catch (BadLocationException e) {
			Assert.fail("bad location exception");
		}
	}

	@Test
	public void testMergeEvents2() {
		IDocument reference= new Document();
		LazilyMirroredDocument2 testee= new LazilyMirroredDocument2(reference);

		try {

			List<DocumentEvent> events= new ArrayList<>();
			int currentLength= 0;

			events.add(new DocumentEvent(reference, 0, 0, "foo bar goo haa"));
			events.add(new DocumentEvent(reference, 0, "foo bar goo haa".length(), "foo bar goo haa"));
			events.add(new DocumentEvent(reference, 4, 4, "xxxx"));
			events.add(new DocumentEvent(reference, 4, 4, "yyy"));
			events.add(new DocumentEvent(reference, 4, 3, "moo "));
			events.add(new DocumentEvent(reference, 9, 2, "asd"));
			events.add(new DocumentEvent(reference, 0, 2, "asd"));

			for (Iterator<DocumentEvent> iterator= events.iterator(); iterator.hasNext();) {
				DocumentEvent event= iterator.next();
				currentLength += event.getText().length() - event.getLength();
			}

			for (int i= 0; i < 500; i++) {
				char character= (char) (32 + i % 95);
				DocumentEvent event= createRandomEvent(reference, currentLength, character);
				currentLength += event.getText().length() - event.getLength();
				events.add(event);
			}

			for (Iterator<DocumentEvent> iterator= events.iterator(); iterator.hasNext();) {
				DocumentEvent event= iterator.next();

				reference.replace(event.getOffset(), event.getLength(), event.getText());
				if (Math.random() < 0.3) {
					check(reference, testee);
				}
			}

			check(reference, testee);

		} catch (BadLocationException e) {
			Assert.fail("bad location exception");
		}
	}

	private static void check(IDocument reference, IDocument testee) {
		Assert.assertEquals(reference.get(), testee.get());
	}

	@Test
	@SuppressWarnings("deprecation")
	public void testIndexOf() {
		int[] result;
		result = TextUtilities.indexOf(new String[0], "xxxxxxxxxx", 0);
		assertEquals(-1, result[0]);
		assertEquals(-1, result[1]);

		result = TextUtilities.indexOf(new String[] { "a", "ab", "abc" }, "xxxxxxxxxx", 0);
		assertEquals(-1, result[0]);
		assertEquals(-1, result[1]);

		result = TextUtilities.indexOf(new String[] { "a", "ab", "abc" }, "foobarabcd", 0);
		assertEquals(4, result[0]);
		assertEquals(0, result[1]);

		result = TextUtilities.indexOf(new String[] { "ab", "ab" }, "foobarabcd", 0);
		assertEquals(6, result[0]);
		assertEquals(0, result[1]);

		result = TextUtilities.indexOf(new String[] { "", "ab", "abc" }, "foobarabcd", 0);
		assertEquals(6, result[0]);
		assertEquals(2, result[1]);

		result = TextUtilities.indexOf(new String[] { "arac", "", "fuu" }, "foobarabcd", 0);
		assertEquals(0, result[0]);
		assertEquals(1, result[1]);

		result = TextUtilities.indexOf(new String[] { "", "" }, "foobarabcd", 0);
		assertEquals(0, result[0]);
		assertEquals(1, result[1]);

		result = TextUtilities.indexOf(new String[] { "" }, "foobarabcd", 5);
		// looks strange that searching from offset 5 returns match offset 0 but that is
		// how it was implemented
		assertEquals(0, result[0]);
		assertEquals(0, result[1]);

		result = TextUtilities.indexOf(new String[] { "abc" }, "foobarabcd", -5);
		assertEquals(6, result[0]);
		assertEquals(0, result[1]);

		result = TextUtilities.indexOf(new String[] { "abc" }, "foobarabcd", 20);
		assertEquals(-1, result[0]);
		assertEquals(-1, result[1]);

		try {
			TextUtilities.indexOf(null, "foobarabcd", 0);
			fail("Exception not thrown");
		} catch (NullPointerException ex) {
			// expected
		}

		try {
			TextUtilities.indexOf(new String[] { "abc", null }, "foobarabcd", 0);
			fail("Exception not thrown");
		} catch (NullPointerException ex) {
			// expected
		}

		try {
			TextUtilities.indexOf(new String[] { "abc" }, null, 0);
			fail("Exception not thrown");
		} catch (NullPointerException ex) {
			// expected
		}
	}

	@Test
	public void testNextDelimiter() {
		Map.Entry<Integer, String> result;
		result = TextUtilities.nextDelimiter("abc\ndef", 0);
		assertEquals(3, result.getKey().intValue());
		assertEquals("\n", result.getValue());

		result = TextUtilities.nextDelimiter("abc\ndef", 5);
		assertEquals(-1, result.getKey().intValue());
		assertEquals(null, result.getValue());

		result = TextUtilities.nextDelimiter("abc\rdef\n123", 0);
		assertEquals(3, result.getKey().intValue());
		assertEquals("\r", result.getValue());

		result = TextUtilities.nextDelimiter("abc+\r\ndef\n123", 0);
		assertEquals(4, result.getKey().intValue());
		assertEquals("\r\n", result.getValue());

		result = TextUtilities.nextDelimiter("abc~>\r\r\ndef\n123", 0);
		assertEquals(5, result.getKey().intValue());
		assertEquals("\r", result.getValue());

		result = TextUtilities.nextDelimiter("\nabc~>\r\r\ndef\n123", 0);
		assertEquals(0, result.getKey().intValue());
		assertEquals("\n", result.getValue());

		result = TextUtilities.nextDelimiter("abc~>123\r\n", 0);
		assertEquals(8, result.getKey().intValue());
		assertEquals("\r\n", result.getValue());

		result = TextUtilities.nextDelimiter("abc~>\r\r\ndef\n123", 9);
		assertEquals(11, result.getKey().intValue());
		assertEquals("\n", result.getValue());

		result = TextUtilities.nextDelimiter("", 0);
		assertEquals(-1, result.getKey().intValue());
		assertEquals(null, result.getValue());

		result = TextUtilities.nextDelimiter("abc123", 0);
		assertEquals(-1, result.getKey().intValue());
		assertEquals(null, result.getValue());
	}
}

Back to the top