KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > EmbeddedTokenListTest


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 package org.netbeans.lib.lexer;
20
21 import javax.swing.text.Document JavaDoc;
22 import javax.swing.text.PlainDocument JavaDoc;
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 /**
33  *
34  * @author Jan Lahoda
35  */

36 public class EmbeddedTokenListTest extends TestCase {
37     
38     public EmbeddedTokenListTest(String JavaDoc testName) {
39         super(testName);
40     }
41
42     protected void setUp() throws Exception JavaDoc {
43     }
44
45     public void testUpdateStartOffset() throws Exception JavaDoc {
46         Document JavaDoc d = new PlainDocument JavaDoc();
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 JavaDoc {
91         Document JavaDoc d = new PlainDocument JavaDoc();
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