1 19 20 package org.netbeans.lib.lexer.test.inc; 21 22 import java.util.ConcurrentModificationException ; 23 import javax.swing.text.Document ; 24 import junit.framework.TestCase; 25 import org.netbeans.api.lexer.Language; 26 import org.netbeans.api.lexer.TokenHierarchy; 27 import org.netbeans.api.lexer.TokenId; 28 import org.netbeans.api.lexer.TokenSequence; 29 import org.netbeans.lib.lexer.test.LexerTestUtilities; 30 import org.netbeans.lib.lexer.test.ModificationTextDocument; 31 import org.netbeans.lib.lexer.test.simple.SimpleTokenId; 32 33 38 public class TokenListUpdaterTest extends TestCase { 39 40 public TokenListUpdaterTest(String testName) { 41 super(testName); 42 } 43 44 protected void setUp() throws java.lang.Exception { 45 } 46 47 protected void tearDown() throws java.lang.Exception { 48 } 49 50 public void testInsertUnfinishedLexing() throws Exception { 51 Document doc = new ModificationTextDocument(); 52 String text = "abc+uv-xy"; 54 doc.insertString(0, text, null); 55 56 doc.putProperty(Language.class, SimpleTokenId.language()); 57 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 58 assertNotNull("Null token hierarchy for document", hi); 59 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 60 assertTrue(ts.moveNext()); 61 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0); 62 assertTrue(ts.moveNext()); 63 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 3); 64 65 doc.insertString(3, "x", null); 67 try { 68 ts.moveNext(); 69 fail("Should not get there"); 70 } catch (ConcurrentModificationException e) { 71 } 73 74 ts = hi.tokenSequence(); 75 assertTrue(ts.moveNext()); 76 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abcx", 0); 77 assertTrue(ts.moveNext()); 78 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 4); 79 assertTrue(ts.moveNext()); 80 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "uv", 5); 81 assertTrue(ts.moveNext()); 82 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 7); 83 assertTrue(ts.moveNext()); 84 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "xy", 8); 85 assertFalse(ts.moveNext()); 86 } 87 88 public void testRemoveUnfinishedLexingZeroLookaheadToken() throws Exception { 89 Document doc = new ModificationTextDocument(); 90 String text = "a+b"; 92 doc.insertString(0, text, null); 93 94 doc.putProperty(Language.class, SimpleTokenId.language()); 95 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 96 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 97 assertTrue(ts.moveNext()); 98 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0); 99 assertTrue(ts.moveNext()); 100 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1); 101 102 doc.remove(1, 1); 104 try { 105 ts.moveNext(); 106 fail("Should not get there"); 107 } catch (ConcurrentModificationException e) { 108 } 110 111 ts = hi.tokenSequence(); 112 assertTrue(ts.moveNext()); 113 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ab", 0); 114 assertFalse(ts.moveNext()); 115 } 116 117 public void testRemoveUnfinishedLexingRightAfterLastToken() throws Exception { 118 Document doc = new ModificationTextDocument(); 119 String text = "a+b"; 121 doc.insertString(0, text, null); 122 123 doc.putProperty(Language.class, SimpleTokenId.language()); 124 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 125 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 126 assertTrue(ts.moveNext()); 127 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0); 128 129 doc.remove(1, 1); 131 try { 132 ts.moveNext(); 133 fail("Should not get there"); 134 } catch (ConcurrentModificationException e) { 135 } 137 138 ts = hi.tokenSequence(); 139 assertTrue(ts.moveNext()); 140 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ab", 0); 141 assertFalse(ts.moveNext()); 142 } 143 144 public void testRemoveUnfinishedLexingAfterLastToken() throws Exception { 145 Document doc = new ModificationTextDocument(); 146 String text = "a+b+"; 148 doc.insertString(0, text, null); 149 150 doc.putProperty(Language.class, SimpleTokenId.language()); 151 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 152 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 153 assertTrue(ts.moveNext()); 154 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0); 155 156 doc.remove(2, 1); 158 try { 159 ts.moveNext(); 160 fail("Should not get there"); 161 } catch (ConcurrentModificationException e) { 162 } 164 165 ts = hi.tokenSequence(); 166 assertTrue(ts.moveNext()); 167 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0); 168 assertTrue(ts.moveNext()); 169 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1); 170 assertTrue(ts.moveNext()); 171 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 2); 172 assertFalse(ts.moveNext()); 173 } 174 175 public void testReadAllInsertAtEnd() throws Exception { 176 Document doc = new ModificationTextDocument(); 177 String text = "a+"; 179 doc.insertString(0, text, null); 180 181 doc.putProperty(Language.class, SimpleTokenId.language()); 182 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 183 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 184 assertTrue(ts.moveNext()); 185 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0); 186 assertTrue(ts.moveNext()); 187 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1); 188 189 doc.insertString(2, "-", null); 191 try { 192 ts.moveNext(); 193 fail("Should not get there"); 194 } catch (ConcurrentModificationException e) { 195 } 197 198 ts = hi.tokenSequence(); 199 assertTrue(ts.moveNext()); 200 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0); 201 assertTrue(ts.moveNext()); 202 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1); 203 assertTrue(ts.moveNext()); 204 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 2); 205 assertFalse(ts.moveNext()); 206 } 207 208 public void testReadOneInsertAtEnd() throws Exception { 209 Document doc = new ModificationTextDocument(); 210 String text = "a+"; 212 doc.insertString(0, text, null); 213 214 doc.putProperty(Language.class, SimpleTokenId.language()); 215 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 216 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 217 assertTrue(ts.moveNext()); 218 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0); 219 220 doc.insertString(2, "-", null); 222 try { 223 ts.moveNext(); 224 fail("Should not get there"); 225 } catch (ConcurrentModificationException e) { 226 } 228 229 ts = hi.tokenSequence(); 230 assertTrue(ts.moveNext()); 231 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0); 232 assertTrue(ts.moveNext()); 233 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1); 234 assertTrue(ts.moveNext()); 235 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 2); 236 assertFalse(ts.moveNext()); 237 } 238 239 public void testReadNoneInsertAtEnd() throws Exception { 240 Document doc = new ModificationTextDocument(); 241 String text = "a+"; 243 doc.insertString(0, text, null); 244 245 doc.putProperty(Language.class, SimpleTokenId.language()); 246 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 247 248 doc.insertString(2, "-", null); 250 251 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 252 assertTrue(ts.moveNext()); 253 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "a", 0); 254 assertTrue(ts.moveNext()); 255 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 1); 256 assertTrue(ts.moveNext()); 257 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 2); 258 assertFalse(ts.moveNext()); 259 } 260 261 } 262 | Popular Tags |