001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.text.tests;
011:
012: import junit.framework.TestCase;
013:
014: import org.eclipse.jface.text.BadLocationException;
015: import org.eclipse.jface.text.ConfigurableLineTracker;
016: import org.eclipse.jface.text.ILineTracker;
017: import org.eclipse.jface.text.IRegion;
018: import org.eclipse.jface.text.ITextStore;
019:
020: /**
021: * Correctness tests for {@link ITextStore} implementations.
022: *
023: * @since 3.3
024: */
025: public abstract class TextStoreTest extends TestCase {
026:
027: private ITextStore fTextStore;
028: private ILineTracker fTracker;
029:
030: protected void setUp() {
031:
032: fTextStore = createTextStore();
033: fTracker = createTracker();
034: set("x\nx\nx\nx\nx\n");
035: }
036:
037: protected ILineTracker createTracker() {
038: return new ConfigurableLineTracker(new String[] { "\n" });
039: }
040:
041: protected final void replace(int offset, int length, String text)
042: throws BadLocationException {
043: fTextStore.replace(offset, length, text);
044: fTracker.replace(offset, length, text);
045: }
046:
047: protected final void set(String text) {
048: fTextStore.set(text);
049: fTracker.set(text);
050: }
051:
052: abstract protected ITextStore createTextStore();
053:
054: protected void tearDown() {
055: fTextStore = null;
056: fTracker = null;
057: }
058:
059: public void testGet1() throws Exception {
060: set("xxxxx");
061:
062: String[] expected = { "xyxxxx", "xyxyxxx", "xyxyxyxx",
063: "xyxyxyxyx", "xyxyxyxyxy" };
064:
065: for (int i = 1; i < 5; i++) {
066: replace(2 * i - 1, 0, "y");
067: String txt = fTextStore.get(0, fTextStore.getLength());
068: assertEquals(expected[i - 1], txt);
069: }
070:
071: }
072:
073: public void testGet2() throws Exception {
074: set("xxxxx");
075:
076: String[] expected = { "yxxxxx", "yxyxxxx", "yxyxyxxx",
077: "yxyxyxyxx", "yxyxyxyxyx" };
078:
079: for (int i = 1; i < 5; i++) {
080: replace(2 * (i - 1), 0, "y");
081: String txt = fTextStore.get(0, fTextStore.getLength());
082: assertEquals(expected[i - 1], txt);
083: }
084:
085: }
086:
087: public void testEditScript1() throws Exception {
088: replace(0, fTextStore.getLength(), "x");
089: assertTextStoreContents("x");
090:
091: replace(1, 0, "y");
092: assertTextStoreContents("xy");
093:
094: replace(2, 0, "z");
095: assertTextStoreContents("xyz");
096:
097: replace(3, 0, "\n");
098: assertTextStoreContents("xyz\n");
099:
100: replace(4, 0, "x");
101: assertTextStoreContents("xyz\nx");
102: }
103:
104: private void assertTextStoreContents(String expected) {
105: assertEquals(expected, fTextStore
106: .get(0, fTextStore.getLength()));
107: for (int i = 0; i < fTextStore.getLength(); i++)
108: assertEquals(expected.charAt(i), fTextStore.get(i));
109:
110: try {
111: fTextStore.get(fTextStore.getLength());
112: fail();
113: } catch (IndexOutOfBoundsException e) {
114: }
115: }
116:
117: public void testEmptyLines() throws Exception {
118:
119: replace(0, 10, null);
120: assertTextStoreContents("");
121:
122: replace(0, 0, "\n\n\n\n\n");
123: assertTextStoreContents("\n\n\n\n\n");
124: }
125:
126: public void testInsert1() throws Exception {
127:
128: replace(3, 0, "yyyy");
129: assertTextStoreContents("x\nxyyyy\nx\nx\nx\n");
130:
131: replace(9, 0, "y\n");
132: assertTextStoreContents("x\nxyyyy\nxy\n\nx\nx\n");
133:
134: replace(11, 0, "y\n");
135: assertTextStoreContents("x\nxyyyy\nxy\ny\n\nx\nx\n");
136:
137: replace(13, 0, "y");
138: assertTextStoreContents("x\nxyyyy\nxy\ny\ny\nx\nx\n");
139:
140: replace(11, 5, "y\nxyz");
141: assertTextStoreContents("x\nxyyyy\nxy\ny\nxyz\nx\n");
142: }
143:
144: public void testInsert2() throws Exception {
145: replace(3, 0, "yyyy");
146: assertTextStoreContents("x\nxyyyy\nx\nx\nx\n");
147:
148: replace(9, 0, "y\ny\ny");
149: assertTextStoreContents("x\nxyyyy\nxy\ny\ny\nx\nx\n");
150: }
151:
152: public void testLinesNumbers() throws Exception {
153: replace(0, 10, "\na\nbb\nccc\ndddd\neeeee\n");
154: assertTextStoreContents("\na\nbb\nccc\ndddd\neeeee\n");
155:
156: int offset = 0;
157: for (int i = 0; i < 5; i++) {
158: for (int j = 0; j < i; j++) {
159: int no = fTracker.getLineNumberOfOffset(offset + j);
160: assertTrue("invalid line number " + no
161: + " reported instead of " + i, no == i);
162: }
163: offset += (i + 1);
164: }
165: }
166:
167: public void testOffsets() throws Exception {
168: for (int i = 0; i < 5; i++) {
169: IRegion line = fTracker.getLineInformation(i);
170: int pos = line.getOffset() + line.getLength();
171: int offset = (2 * i) + 1;
172: assertTrue("invalid line end offset " + pos + " for line "
173: + i + " should be " + offset, offset == pos);
174: }
175:
176: for (int i = 0; i < 5; i++) {
177: int pos = fTracker.getLineOffset(i);
178: int offset = 2 * i;
179: assertTrue("invalid line start offset " + pos
180: + " for line " + i + " should be " + offset,
181: pos == offset);
182: }
183:
184: for (int i = 0; i < 10; i++) {
185: int line = fTracker.getLineNumberOfOffset(i);
186: double l = Math.floor(i / 2);
187: assertTrue("invalid line number " + line + " for position "
188: + i + " should be " + l, l == line);
189: }
190: }
191:
192: public void testRemove() throws Exception {
193: replace(3, 1, null);
194: assertTextStoreContents("x\nxx\nx\nx\n");
195:
196: replace(6, 1, null);
197: assertTextStoreContents("x\nxx\nxx\n");
198:
199: replace(3, 5, null);
200: assertTextStoreContents("x\nx");
201:
202: replace(0, 3, null);
203: assertTextStoreContents("");
204: }
205:
206: public void testReplace() throws Exception {
207: replace(0, fTextStore.getLength(), "\tx\n\tx\n\tx\n\tx\n\tx\n");
208: assertTextStoreContents("\tx\n\tx\n\tx\n\tx\n\tx\n");
209: }
210:
211: public void testReplace2() throws Exception {
212: replace(0, fTextStore.getLength(), "x");
213: assertTextStoreContents("x");
214:
215: replace(0, fTextStore.getLength(), "x\nx\nx\n");
216: assertTextStoreContents("x\nx\nx\n");
217: }
218:
219: public void testReplace3() throws Exception {
220: replace(1, 1, "\n");
221: assertTextStoreContents("x\nx\nx\nx\nx\n");
222: }
223:
224: public void testReplace4() throws Exception {
225: int lines = fTracker.getNumberOfLines();
226: IRegion previous = fTracker.getLineInformation(0);
227: for (int i = 1; i < lines; i++) {
228: int lastLineEnd = previous.getOffset()
229: + previous.getLength();
230: int lineStart = fTracker.getLineInformation(i).getOffset();
231: replace(lastLineEnd, lineStart - lastLineEnd, "\n");
232: assertTextStoreContents("x\nx\nx\nx\nx\n");
233: previous = fTracker.getLineInformation(i);
234: }
235: }
236:
237: public void testShiftLeft() throws Exception {
238: replace(0, fTextStore.getLength(), "\tx\n\tx\n\tx\n\tx\n\tx\n");
239: assertTextStoreContents("\tx\n\tx\n\tx\n\tx\n\tx\n");
240:
241: for (int i = 0; i < 5; i++) {
242: int pos = fTracker.getLineOffset(i);
243: replace(pos, 1, null);
244: }
245:
246: assertTextStoreContents("x\nx\nx\nx\nx\n");
247: }
248:
249: public void testShiftRight() throws Exception {
250: for (int i = 0; i < 5; i++) {
251: int pos = fTracker.getLineOffset(i);
252: replace(pos, 0, "\t");
253: }
254:
255: assertTextStoreContents("\tx\n\tx\n\tx\n\tx\n\tx\n");
256: }
257:
258: public void testDeleteEmptyLine() throws Exception {
259: set("x\nx\n\nx\n\n");
260: assertTextStoreContents("x\nx\n\nx\n\n");
261:
262: String[] expected = { "", "x\n", "x\nx\n", "x\nx\n\n",
263: "x\nx\n\nx\n", "x\nx\n\nx\n\n", };
264:
265: for (int line = fTracker.getNumberOfLines() - 1; line >= 0; line--) {
266: int offset = fTracker.getLineOffset(line);
267: int length = fTracker.getLineLength(line);
268: replace(offset, length, null);
269: assertTextStoreContents(expected[line]);
270: }
271:
272: }
273:
274: public void testDeleteLines() throws Exception {
275: String content = "";
276: for (int i = 0; i < 50; i++) {
277: content += "x\nx\n\nx\n\n";
278: set(content);
279:
280: String expected = content;
281: int lines = fTracker.getNumberOfLines();
282: for (int line = 0; line < lines; line++) {
283: int offset = fTracker.getLineOffset(0);
284: int length = fTracker.getLineLength(0);
285: replace(offset, length, null);
286: expected = expected.substring(length);
287: assertTextStoreContents(expected);
288: }
289: }
290: content = "";
291: for (int i = 0; i < 50; i++) {
292: content += "x\nx\n\nx\n\n";
293: set(content);
294:
295: String expected = content;
296: int lines = fTracker.getNumberOfLines();
297: for (int line = lines - 1; line >= 0; line--) {
298: int offset = fTracker.getLineOffset(line);
299: int length = fTracker.getLineLength(line);
300: replace(offset, length, null);
301: expected = expected.substring(0, expected.length()
302: - length);
303: assertTextStoreContents(expected);
304: }
305: }
306: }
307:
308: public void testDeleteLines2() throws Exception {
309: String content = "";
310: for (int i = 0; i < 50; i++) {
311: content += "xxxxxxxxxxxxxx";
312: set(content);
313:
314: String expected = content;
315: int lines = fTracker.getNumberOfLines();
316: for (int line = 0; line < lines; line++) {
317: int offset = fTracker.getLineOffset(0);
318: int length = fTracker.getLineLength(0);
319: replace(offset, length, null);
320: expected = expected.substring(length);
321: assertTextStoreContents(expected);
322: }
323: }
324: content = "";
325: for (int i = 0; i < 50; i++) {
326: content += "xxxxxxxxxxxxxx";
327: set(content);
328:
329: String expected = content;
330: int lines = fTracker.getNumberOfLines();
331: for (int line = lines - 1; line >= 0; line--) {
332: int offset = fTracker.getLineOffset(line);
333: int length = fTracker.getLineLength(line);
334: replace(offset, length, null);
335: expected = expected.substring(0, expected.length()
336: - length);
337: assertTextStoreContents(expected);
338: }
339: }
340: }
341:
342: public void testSet() throws Exception {
343: String content = "";
344: for (int i = 0; i < 35; i++) {
345: int[] lenghts = new int[i + 1];
346: for (int j = 0; j < i + 1; j++)
347: lenghts[j] = j;
348: for (int j = 0; j < i; j++)
349: content += "x";
350:
351: set(content);
352: assertTextStoreContents(content);
353:
354: content += "\n";
355: }
356: }
357:
358: public void testFunnyLastLineCompatibility() throws Exception {
359: /* empty last line */
360: set("x\n");
361: assertTextStoreContents("x\n");
362: int[] offsets = { 0, 2 };
363: int[] lengths = { 1, 0 };
364:
365: assertEquals("invalid number of lines, ", lengths.length,
366: fTracker.getNumberOfLines());
367: assertEquals("invalid number of lines, ", lengths.length,
368: fTracker.getNumberOfLines(0, fTextStore.getLength()));
369: for (int i = 0; i < lengths.length; i++) {
370: IRegion line = fTracker.getLineInformation(i);
371: assertEquals("line: " + i, lengths[i], line.getLength());
372: assertEquals("line: " + i, offsets[i], line.getOffset());
373: }
374: try {
375: fTracker.getLineInformation(lengths.length);
376: fail();
377: } catch (Exception e) {
378: }
379:
380: try {
381: fTracker
382: .getLineInformationOfOffset(offsets[offsets.length] + 1);
383: fail();
384: } catch (Exception e) {
385: }
386:
387: /* phantom last line when the last line is not empty */
388: set("x\nx");
389: assertTextStoreContents("x\nx");
390: offsets = new int[] { 0, 2, 3 };
391: lengths = new int[] { 1, 1, 0 };
392: assertEquals("invalid number of lines, ",
393: lengths.length - 1 /* !!!! */, fTracker
394: .getNumberOfLines());
395: assertEquals("invalid number of lines, ",
396: lengths.length - 1 /* !!!! */, fTracker
397: .getNumberOfLines(0, fTextStore.getLength()));
398: for (int i = 0; i < lengths.length; i++) {
399: IRegion line = fTracker.getLineInformation(i);
400: int len = lengths[i];
401: int offset = offsets[i];
402: assertEquals("length of line: " + i, len, line.getLength());
403: assertEquals("offset of line: " + i, offset, line
404: .getOffset());
405:
406: line = fTracker.getLineInformationOfOffset(offset);
407: if (i == lengths.length - 1) { // phantom line cannot be queried by offset
408: len = lengths[i - 1];
409: offset = offsets[i - 1];
410: }
411: assertEquals("length of line: " + i, len, line.getLength());
412: assertEquals("offset of line: " + i, offset, line
413: .getOffset());
414: }
415:
416: try {
417: fTracker.getLineInformation(lengths.length);
418: fail();
419: } catch (Exception e) {
420: }
421:
422: try {
423: fTracker
424: .getLineInformationOfOffset(offsets[offsets.length] + 1);
425: fail();
426: } catch (Exception e) {
427: }
428:
429: }
430:
431: }
|