1 19 package org.netbeans.lib.lexer; 20 21 import javax.swing.text.Document ; 22 import javax.swing.text.PlainDocument ; 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.simple.SimpleJavadocTokenId; 30 import org.netbeans.lib.lexer.test.simple.SimpleTokenId; 31 32 36 public class EmbeddedTokenListTest extends TestCase { 37 38 public EmbeddedTokenListTest(String testName) { 39 super(testName); 40 } 41 42 protected void setUp() throws Exception { 43 } 44 45 public void testUpdateStartOffset() throws Exception { 46 Document d = new PlainDocument (); 47 48 d.putProperty(Language.class, SimpleTokenId.language()); 49 50 d.insertString(0, "ident ident /** @see X */", null); 51 52 TokenHierarchy<?> h = TokenHierarchy.get(d); 53 TokenSequence<? extends TokenId> ts = h.tokenSequence(); 54 55 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ident"); 56 assertEquals(0, ts.offset()); 57 58 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.WHITESPACE, " "); 59 assertEquals(5, ts.offset()); 60 61 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ident"); 62 assertEquals(6, ts.offset()); 63 64 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.WHITESPACE, " "); 65 assertEquals(11, ts.offset()); 66 67 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.JAVADOC_COMMENT, "/** @see X */"); 68 assertEquals(12, ts.offset()); 69 70 TokenSequence<? extends TokenId> inner = ts.embedded(); 71 72 assertNotNull(inner); 73 74 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.OTHER_TEXT, " "); 75 assertEquals(15, inner.offset()); 76 77 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.TAG, "@see"); 78 assertEquals(16, inner.offset()); 79 80 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.OTHER_TEXT, " "); 81 assertEquals(20, inner.offset()); 82 83 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.IDENT, "X"); 84 assertEquals(21, inner.offset()); 85 86 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.OTHER_TEXT, " "); 87 assertEquals(22, inner.offset()); 88 } 89 90 public void testSnapshots() throws Exception { 91 Document d = new PlainDocument (); 92 93 d.putProperty(Language.class, SimpleTokenId.language()); 94 95 d.insertString(0, "ident ident /** @see X */", null); 96 97 TokenHierarchy<?> h = TokenHierarchy.get(d); 98 TokenSequence<? extends TokenId> ts = h.tokenSequence(); 99 100 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ident"); 101 assertEquals(0, ts.offset()); 102 103 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.WHITESPACE, " "); 104 assertEquals(5, ts.offset()); 105 106 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ident"); 107 assertEquals(6, ts.offset()); 108 109 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.WHITESPACE, " "); 110 assertEquals(11, ts.offset()); 111 112 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.JAVADOC_COMMENT, "/** @see X */"); 113 assertEquals(12, ts.offset()); 114 115 TokenSequence<? extends TokenId> inner = ts.embedded(); 116 117 assertNotNull(inner); 118 119 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.OTHER_TEXT, " "); 120 assertEquals(15, inner.offset()); 121 122 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.TAG, "@see"); 123 assertEquals(16, inner.offset()); 124 125 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.OTHER_TEXT, " "); 126 assertEquals(20, inner.offset()); 127 128 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.IDENT, "X"); 129 assertEquals(21, inner.offset()); 130 131 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.OTHER_TEXT, " "); 132 assertEquals(22, inner.offset()); 133 134 135 h = TokenHierarchy.get(d).createSnapshot(); 136 ts = h.tokenSequence(); 137 138 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ident"); 139 assertEquals(0, ts.offset()); 140 141 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.WHITESPACE, " "); 142 assertEquals(5, ts.offset()); 143 144 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.IDENTIFIER, "ident"); 145 assertEquals(6, ts.offset()); 146 147 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.WHITESPACE, " "); 148 assertEquals(11, ts.offset()); 149 150 LexerTestUtilities.assertNextTokenEquals(ts, SimpleTokenId.JAVADOC_COMMENT, "/** @see X */"); 151 assertEquals(12, ts.offset()); 152 153 inner = ts.embedded(); 154 155 assertNotNull(inner); 156 157 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.OTHER_TEXT, " "); 158 assertEquals(15, inner.offset()); 159 160 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.TAG, "@see"); 161 assertEquals(16, inner.offset()); 162 163 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.OTHER_TEXT, " "); 164 assertEquals(20, inner.offset()); 165 166 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.IDENT, "X"); 167 assertEquals(21, inner.offset()); 168 169 LexerTestUtilities.assertNextTokenEquals(inner, SimpleJavadocTokenId.OTHER_TEXT, " "); 170 assertEquals(22, inner.offset()); 171 } 172 } 173 | Popular Tags |