KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > test > state > StateLexerIncTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer.test.state;
21
22 import javax.swing.text.Document JavaDoc;
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.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
32 /**
33  * Test several simple lexer impls.
34  *
35  * @author mmetelka
36  */

37 public class StateLexerIncTest extends TestCase {
38     
39     public StateLexerIncTest(String JavaDoc testName) {
40         super(testName);
41     }
42     
43     protected void setUp() throws java.lang.Exception JavaDoc {
44     }
45
46     protected void tearDown() throws java.lang.Exception JavaDoc {
47     }
48
49     public void test() throws Exception JavaDoc {
50         Document JavaDoc doc = new ModificationTextDocument();
51         // Assign a language to the document
52
InputAttributes attrs = new InputAttributes();
53         doc.putProperty(InputAttributes.class, attrs);
54         doc.putProperty(Language.class, StateTokenId.language());
55         TokenHierarchy<?> hi = TokenHierarchy.get(doc);
56         TokenSequence<? extends TokenId> ts = hi.tokenSequence();
57         assertFalse(ts.moveNext());
58         
59         // Insert text into document
60
String JavaDoc text = "abc";
61         doc.insertString(0, text, null);
62
63         ts = hi.tokenSequence();
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         assertTrue(ts.moveNext());
69         LexerTestUtilities.assertTokenEquals(ts, StateTokenId.BMULTI, "b", 1);
70         assertEquals(LexerTestUtilities.state(ts), StateLexer.AFTER_B);
71         assertEquals(LexerTestUtilities.lookahead(ts), 1);
72         assertTrue(ts.moveNext());
73         LexerTestUtilities.assertTokenEquals(ts, StateTokenId.ERROR, "c", 2);
74         assertEquals(LexerTestUtilities.state(ts), null);
75         assertFalse(ts.moveNext());
76         
77         LexerTestUtilities.incCheck(doc, false);
78         
79         // Should relex "b" so restart state should be AFTER_A
80
attrs.setValue(StateTokenId.language(), "restartState", StateLexer.AFTER_A, true);
81         doc.insertString(2, "b", null);
82         
83     }
84
85 }
86
Popular Tags