1 /******************************************************************************* 2 * Copyright (c) 2000, 2008 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.text.tests; 15 16 import static org.junit.Assert.*; 17 18 import org.junit.After; 19 import org.junit.Before; 20 import org.junit.Test; 21 22 import org.eclipse.jface.text.ConfigurableLineTracker; 23 import org.eclipse.jface.text.GapTextStore; 24 import org.eclipse.jface.text.IRegion; 25 26 public class LineTrackerTest4 extends AbstractLineTrackerTest { 27 28 29 @Before setUp()30 public void setUp() { 31 fText= new GapTextStore(); 32 fTracker= new ConfigurableLineTracker(new String[] { "\r\n" }); 33 set("x\r\nx\r\nx\r\nx\r\nx\r\n"); 34 } 35 36 @After tearDown()37 public void tearDown() { 38 fTracker= null; 39 fText= null; 40 } 41 42 @Override getLineOffset(int line, int[] lines)43 protected int getLineOffset(int line, int[] lines) { 44 int offset= 0; 45 for (int i= 0; i < line; i++) 46 offset += (lines[i] + 2); 47 return offset; 48 } 49 50 @Test testEditScript1()51 public void testEditScript1() throws Exception { 52 checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); 53 54 replace(0, fText.getLength(), "x"); 55 checkLines(new int[] { 1 }); 56 57 replace(1, 0, "y"); 58 checkLines(new int[] { 2 }); 59 60 replace(2, 0, "z"); 61 checkLines(new int[] { 3 }); 62 63 replace(3, 0, "\r\n"); 64 checkLines(new int[] { 3, 0 }); 65 66 replace(5, 0, "x"); 67 checkLines(new int[] { 3, 1 }); 68 } 69 70 @Test testEmptyLines()71 public void testEmptyLines() throws Exception { 72 checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); 73 74 replace(0, 15, null); 75 checkLines(new int[] { 0 }); 76 77 replace(0, 0, "\r\n\r\n\r\n\r\n\r\n"); 78 checkLines(new int[] { 0, 0, 0, 0, 0, 0 }); 79 80 for (int i= 0; i < 10; i++) { 81 int no= fTracker.getLineNumberOfOffset(i); 82 double l= Math.floor(i / 2); 83 assertTrue("invalid line number " + no + " for position " + i + " should be " + l, l == no); 84 } 85 } 86 87 @Test testInsert1()88 public void testInsert1() throws Exception { 89 checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); 90 91 replace(4, 0, "yyyy"); 92 checkLines(new int[] { 1, 5, 1, 1, 1, 0 }); 93 94 replace(11, 0, "y\r\n"); 95 checkLines(new int[] { 1, 5, 2, 0, 1, 1, 0 }); 96 97 replace(14, 0, "y\r\n"); 98 checkLines(new int[] { 1, 5, 2, 1, 0, 1, 1, 0 }); 99 100 replace(17, 0, "y"); 101 checkLines(new int[] { 1, 5, 2, 1, 1, 1, 1, 0 }); 102 } 103 104 @Test testInsert2()105 public void testInsert2() throws Exception { 106 checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); 107 108 replace(4, 0, "yyyy"); 109 checkLines(new int[] { 1, 5, 1, 1, 1, 0 }); 110 111 replace(11, 0, "y\r\ny\r\ny"); 112 checkLines(new int[] { 1, 5, 2, 1, 1, 1, 1, 0 }); 113 } 114 115 @Test testLinesNumbers()116 public void testLinesNumbers() throws Exception { 117 checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); 118 119 replace(0, 15, "\r\na\r\nbb\r\nccc\r\ndddd\r\neeeee\r\n"); 120 checkLines(new int[] { 0, 1, 2, 3, 4, 5, 0 }); 121 122 int offset= 0; 123 for (int i= 0; i < 5; i++) { 124 for (int j= 0; j <= i; j++) { 125 int no= fTracker.getLineNumberOfOffset(offset + j); 126 assertTrue("invalid line number " + no + " reported instead of " + i, no == i); 127 } 128 offset+= (i + 2); 129 } 130 } 131 132 @Test testOffsets()133 public void testOffsets() throws Exception { 134 checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); 135 136 for (int i= 0; i < 5; i++) { 137 IRegion line= fTracker.getLineInformation(i); 138 int pos= line.getOffset() + line.getLength() + 1; 139 int offset= (3 * i) + 2; 140 assertTrue("invalid line end offset " + pos + " for line " + i + " should be " + offset, offset == pos); 141 } 142 143 for (int i= 0; i < 5; i++) { 144 int pos= fTracker.getLineOffset(i); 145 int offset= 3 * i; 146 assertTrue("invalid line start offset " + pos + " for line " + i + " should be " + offset, pos == offset); 147 } 148 149 for (int i= 0; i < 15; i++) { 150 int line= fTracker.getLineNumberOfOffset(i); 151 double l= Math.floor(i / 3); 152 assertTrue("invalid line number " + line + " for position " + i + " should be " + l, l == line); 153 } 154 155 int lastLine= fTracker.getLineNumberOfOffset(fText.getLength()); 156 assertTrue("invalid last line number " + lastLine, 5 == lastLine); 157 158 int offset= fTracker.getLineOffset(lastLine); 159 assertTrue("invalid last line start offset " + offset, fText.getLength() == offset); 160 161 int length= fTracker.getLineLength(lastLine); 162 assertTrue("invalid last line end offset " + (offset + length - 1), 0 == length); 163 } 164 165 @Test testRemove()166 public void testRemove() throws Exception { 167 checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); 168 169 replace(4, 2, null); 170 checkLines(new int[] { 1, 2, 1, 1, 0 }); 171 172 replace(8, 2, null); 173 checkLines(new int[] { 1, 2, 2, 0 }); 174 175 replace(4, 7, null); 176 checkLines(new int[] { 1, 1 }); 177 178 replace(0, 4, null); 179 checkLines(new int[] { 0 }); 180 } 181 182 @Test testReplace()183 public void testReplace() throws Exception { 184 checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); 185 186 replace(0, fText.getLength(), "\tx\r\n\tx\r\n\tx\r\n\tx\r\n\tx\r\n"); 187 checkLines(new int[] { 2, 2, 2, 2, 2, 0 }); 188 } 189 190 @Test testShiftLeft()191 public void testShiftLeft() throws Exception { 192 checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); 193 194 replace(0, fText.getLength(), "\tx\r\n\tx\r\n\tx\r\n\tx\r\n\tx\r\n"); 195 checkLines(new int[] { 2, 2, 2, 2, 2, 0 }); 196 197 for (int i= 0; i < 5; i++) { 198 int pos= fTracker.getLineOffset(i); 199 replace(pos, 1, null); 200 } 201 202 String txt= fText.get(0, fText.getLength()); 203 assertEquals("invalid text", "x\r\nx\r\nx\r\nx\r\nx\r\n", txt); 204 } 205 206 @Test testShiftRight()207 public void testShiftRight() throws Exception { 208 checkLines(new int[] { 1, 1, 1, 1, 1, 0 }); 209 210 for (int i= 0; i < 5; i++) { 211 int pos= fTracker.getLineOffset(i); 212 replace(pos, 0, "\t"); 213 } 214 215 checkLines(new int[] { 2, 2, 2, 2, 2, 0 }); 216 217 String txt= fText.get(0, fText.getLength()); 218 assertEquals("invalid text", "\tx\r\n\tx\r\n\tx\r\n\tx\r\n\tx\r\n", txt); 219 } 220 } 221