1 19 20 package org.netbeans.lib.lexer.test.state; 21 22 import javax.swing.text.Document ; 23 import junit.framework.TestCase; 24 import org.netbeans.api.lexer.InputAttributes; 25 import org.netbeans.api.lexer.Language; 26 import org.netbeans.api.lexer.LanguagePath; 27 import org.netbeans.api.lexer.TokenHierarchy; 28 import org.netbeans.api.lexer.TokenId; 29 import org.netbeans.api.lexer.TokenSequence; 30 import org.netbeans.lib.lexer.test.LexerTestUtilities; 31 import org.netbeans.lib.lexer.test.ModificationTextDocument; 32 33 38 public class InvalidLexerOperationTest extends TestCase { 39 40 public InvalidLexerOperationTest(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 testEarlyNullToken() throws Exception { 51 Document doc = new ModificationTextDocument(); 52 InputAttributes attrs = new InputAttributes(); 54 doc.putProperty(InputAttributes.class, attrs); 55 56 String text = "abc"; 58 doc.insertString(0, text, null); 59 doc.putProperty(Language.class, StateTokenId.language()); 61 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 62 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 63 64 assertTrue(ts.moveNext()); 65 LexerTestUtilities.assertTokenEquals(ts, StateTokenId.A, "a", 0); 66 assertEquals(LexerTestUtilities.lookahead(ts), 0); 67 assertEquals(LexerTestUtilities.state(ts), StateLexer.AFTER_A); 68 69 attrs.setValue(StateTokenId.language(), "returnNullToken", Boolean.TRUE, true); 70 try { 71 assertTrue(ts.moveNext()); 73 fail("IllegalStateException not thrown when null token returned before input end."); 74 } catch (IllegalStateException e) { 75 } 77 } 78 79 public void testBatchLexerRelease() throws Exception { 80 String text = "ab"; 81 InputAttributes attrs = new InputAttributes(); 82 TokenHierarchy<?> hi = TokenHierarchy.create(text, false, StateTokenId.language(), 83 null, attrs); 84 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 85 assertTrue(ts.moveNext()); 86 LexerTestUtilities.assertTokenEquals(ts, StateTokenId.A, "a", 0); 87 assertTrue(ts.moveNext()); 88 LanguagePath lp = LanguagePath.get(StateTokenId.language()); 89 assertFalse(Boolean.TRUE.equals(attrs.getValue(lp, "lexerRelease"))); 90 LexerTestUtilities.assertTokenEquals(ts, StateTokenId.BMULTI, "b", 1); 91 assertFalse(ts.moveNext()); 92 assertTrue(Boolean.TRUE.equals(attrs.getValue(lp, "lexerRelease"))); 93 94 } 95 96 public void testIncLexerRelease() throws Exception { 97 Document doc = new ModificationTextDocument(); 98 InputAttributes attrs = new InputAttributes(); 100 doc.putProperty(InputAttributes.class, attrs); 101 102 String text = "ab"; 104 doc.insertString(0, text, null); 105 doc.putProperty(Language.class, StateTokenId.language()); 107 TokenHierarchy<?> hi = TokenHierarchy.get(doc); 108 TokenSequence<? extends TokenId> ts = hi.tokenSequence(); 109 110 assertTrue(ts.moveNext()); 111 LexerTestUtilities.assertTokenEquals(ts, StateTokenId.A, "a", 0); 112 LanguagePath lp = LanguagePath.get(StateTokenId.language()); 113 assertFalse(Boolean.TRUE.equals(attrs.getValue(lp, "lexerRelease"))); 114 assertTrue(ts.moveNext()); 115 LexerTestUtilities.assertTokenEquals(ts, StateTokenId.BMULTI, "b", 1); 116 assertFalse(ts.moveNext()); 117 assertTrue(Boolean.TRUE.equals(attrs.getValue(lp, "lexerRelease"))); 118 attrs.setValue(lp, "lexerRelease", Boolean.FALSE, false); 119 120 doc.insertString(1, "b", null); 122 assertTrue(Boolean.TRUE.equals(attrs.getValue(lp, "lexerRelease"))); 123 } 124 125 } 126 | Popular Tags |