1 19 20 package org.netbeans.lib.lexer.test.inc; 21 22 import javax.swing.text.Document ; 23 import junit.framework.TestCase; 24 import org.netbeans.api.lexer.Language; 25 import org.netbeans.api.lexer.TokenHierarchy; 26 import org.netbeans.api.lexer.TokenId; 27 import org.netbeans.api.lexer.TokenSequence; 28 import org.netbeans.lib.lexer.test.LexerTestUtilities; 29 import org.netbeans.lib.lexer.test.ModificationTextDocument; 30 import org.netbeans.lib.lexer.test.simple.SimpleTokenId; 31 32 37 public class TokenHierarchySnapshotTest extends TestCase { 38 39 public TokenHierarchySnapshotTest(String testName) { 40 super(testName); 41 } 42 43 protected void setUp() throws java.lang.Exception { 44 } 45 46 protected void tearDown() throws java.lang.Exception { 47 } 48 49 public void testSnapshot() throws Exception { 50 Document doc = new ModificationTextDocument(); 51 doc.putProperty(Language.class, SimpleTokenId.language()); 53 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 54 assertNotNull("Null token hierarchy for document", hi); 55 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 56 assertFalse(ts.moveNext()); 57 58 String text = "abc+def-xyz"; 60 doc.insertString(0, text, null); 61 62 ts = hi.tokenSequence(); 63 assertTrue(ts.moveNext()); 64 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "abc", 0); 65 assertTrue(ts.moveNext()); 66 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.PLUS, "+", 3); 67 assertTrue(ts.moveNext()); 68 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "def", 4); 69 assertTrue(ts.moveNext()); 70 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.MINUS, "-", 7); 71 assertTrue(ts.moveNext()); 72 LexerTestUtilities.assertTokenEquals(ts, SimpleTokenId.IDENTIFIER, "xyz", 8); 73 assertFalse(ts.moveNext()); 74 LexerTestUtilities.incCheck(doc, false); 75 76 String hi1text = doc.getText(0, doc.getLength()); 78 TokenHierarchy<?> hi1 = TokenHierarchy.create(hi1text, SimpleTokenId.language()); 79 TokenHierarchy<?> snapshot1 = hi.createSnapshot(); 80 assertEquals(snapshot1.snapshotOf(), hi); 81 assertFalse(snapshot1.isSnapshotReleased()); 82 83 ts = snapshot1.tokenSequence(); 85 assertEquals(0, ts.moveIndex(0)); 86 assertTrue(ts.moveNext()); 87 88 LexerTestUtilities.assertTokenSequencesEqual(hi1.tokenSequence(), hi1, 89 snapshot1.tokenSequence(), snapshot1, false); 90 91 doc.insertString(4, "+", null); 92 93 assertEquals(0, ts.moveIndex(0)); 95 assertTrue(ts.moveNext()); 96 assertNotNull(ts.token()); 97 98 ts = snapshot1.tokenSequence(); 100 101 LexerTestUtilities.assertTokenSequencesEqual(hi1.tokenSequence(), hi1, 102 snapshot1.tokenSequence(), snapshot1, true); 103 104 String hi2text = doc.getText(0, doc.getLength()); 106 TokenHierarchy<?> hi2 = TokenHierarchy.create(hi2text, SimpleTokenId.language()); 107 TokenHierarchy<?> snapshot2 = hi.createSnapshot(); 108 assertEquals(snapshot2.snapshotOf(), hi); 109 110 ts = snapshot2.tokenSequence(); 112 assertEquals(0, ts.moveIndex(0)); 113 assertTrue(ts.moveNext()); 114 115 LexerTestUtilities.assertTokenSequencesEqual(hi2.tokenSequence(), hi2, 116 snapshot2.tokenSequence(), snapshot2, false); 117 118 doc.remove(8, 1); 119 120 LexerTestUtilities.assertTokenSequencesEqual(hi1.tokenSequence(), hi1, 121 snapshot1.tokenSequence(), snapshot1, false); 122 LexerTestUtilities.assertTokenSequencesEqual(hi2.tokenSequence(), hi2, 123 snapshot2.tokenSequence(), snapshot2, false); 124 } 125 126 } 127 | Popular Tags |