1 19 20 package org.netbeans.lib.lexer.inc; 21 22 import javax.swing.text.Document ; 23 import org.netbeans.api.lexer.Language; 24 import org.netbeans.api.lexer.TokenChange; 25 import org.netbeans.api.lexer.TokenHierarchy; 26 import org.netbeans.api.lexer.TokenHierarchyEvent; 27 import org.netbeans.api.lexer.TokenHierarchyListener; 28 import org.netbeans.api.lexer.TokenId; 29 import org.netbeans.api.lexer.TokenSequence; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.lib.lexer.test.LexerTestUtilities; 32 import org.netbeans.lib.lexer.test.ModificationTextDocument; 33 import org.netbeans.lib.lexer.test.simple.SimplePlainTokenId; 34 import org.netbeans.lib.lexer.test.simple.SimpleTokenId; 35 import org.netbeans.spi.lexer.LanguageEmbedding; 36 37 42 public class TokenHierarchyEventTest extends NbTestCase { 43 44 public TokenHierarchyEventTest(String testName) { 45 super(testName); 46 } 47 48 protected void setUp() throws java.lang.Exception { 49 } 50 51 protected void tearDown() throws java.lang.Exception { 52 } 53 54 public void testCreateEmbedding() throws Exception { 55 Document doc = new ModificationTextDocument(); 56 String text = "abc def ghi"; 57 doc.insertString(0, text, null); 58 doc.putProperty(Language.class, SimpleTokenId.language()); 60 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 61 LexerTestUtilities.initLastTokenHierarchyEventListening(doc); 62 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 63 64 assertTrue(ts.moveNext()); 65 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0); 66 assertTrue(ts.moveNext()); 67 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.WHITESPACE, " ", 3); 68 assertTrue(ts.moveNext()); 69 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "def", 4); 70 assertTrue(ts.moveNext()); 71 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.WHITESPACE, " ", 7); 72 assertTrue(ts.moveNext()); 73 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ghi", 8); 74 assertFalse(ts.moveNext()); 75 76 doc.insertString(5, "x", null); 78 79 TokenHierarchyEvent evt = LexerTestUtilities.getLastTokenHierarchyEvent(doc); 81 assertNotNull(evt); 82 TokenChange<? extends TokenId> tc = evt.tokenChange(); 83 assertNotNull(tc); 84 assertEquals(2, tc.index()); 85 assertEquals(4, tc.offset()); 86 assertEquals(1, tc.addedTokenCount()); 87 assertEquals(1, tc.removedTokenCount()); 88 assertEquals(SimpleTokenId.language(), tc.language()); 89 assertEquals(0, tc.embeddedChangeCount()); 90 91 } 92 93 } 94 | Popular Tags |