1 package org.codehaus.groovy.syntax;
2
3
4 import groovy.util.GroovyTestCase;
5 import org.codehaus.groovy.GroovyBugError;
6
7
8 public class AbstractTokenStreamTest
9 extends GroovyTestCase
10 {
11 public static class MockTokenStream
12 extends AbstractTokenStream
13 {
14 private int cur;
15 private int checkpoint;
16 private Token[] tokens;
17
18 public MockTokenStream(Token[] tokens)
19 {
20 this.tokens = tokens;
21 this.cur = 0;
22 }
23
24 public Token nextToken()
25 throws ReadException, SyntaxException
26 {
27 if ( this.cur >= this.tokens.length )
28 {
29 return null;
30 }
31
32 Token token = this.tokens[ this.cur ];
33
34 ++this.cur;
35
36 return token;
37 }
38
39 public void checkpoint() {
40 checkpoint = cur;
41 }
42
43 public void restore() {
44 cur = checkpoint;
45 }
46 }
47
48 public void testNextToken()
49 throws Exception
50 {
51 Token[] tokens = new Token[]
52 {
53 Token.newSymbol( "(", 1, 1 ),
54 Token.newSymbol( ")", 1, 2 )
55 };
56
57 MockTokenStream in = new MockTokenStream( tokens );
58
59
60 assertSame( tokens[0],
61 in.nextToken() );
62
63 assertSame( tokens[1],
64 in.nextToken() );
65
66 assertNull( in.nextToken() );
67
68 }
69
70 public void testLa()
71 throws Exception
72 {
73 Token[] tokens = new Token[]
74 {
75 Token.newSymbol( "(", 1, 1 ),
76 Token.newSymbol( ")", 1, 2 )
77 };
78
79 MockTokenStream in = new MockTokenStream( tokens );
80
81
82 assertSame( tokens[0],
83 in.la() );
84
85 assertSame( tokens[0],
86 in.la() );
87
88 assertSame( tokens[0],
89 in.la( 1 ) );
90
91 assertSame( tokens[0],
92 in.la( 1 ) );
93
94 assertSame( tokens[1],
95 in.la( 2 ) );
96
97 assertSame( tokens[1],
98 in.la( 2 ) );
99 }
100
101 public void testLaAndConsume()
102 throws Exception
103 {
104 Token[] tokens = new Token[]
105 {
106 Token.newSymbol( "(", 1, 1 ),
107 Token.newSymbol( ")", 1, 2 )
108 };
109
110 MockTokenStream in = new MockTokenStream( tokens );
111
112 assertSame( tokens[0],
113 in.la() );
114
115 assertSame( tokens[0],
116 in.la() );
117
118 assertSame( tokens[0],
119 in.la( 1 ) );
120
121 assertSame( tokens[0],
122 in.la( 1 ) );
123
124 assertSame( tokens[1],
125 in.la( 2 ) );
126
127 in.consume( Types.LEFT_PARENTHESIS );
128
129 assertSame( tokens[1],
130 in.la() );
131
132 assertSame( tokens[1],
133 in.la() );
134
135 assertSame( tokens[1],
136 in.la( 1 ) );
137
138 assertSame( tokens[1],
139 in.la( 1 ) );
140 }
141
142 public void testLaOutOfOrder()
143 throws Exception
144 {
145 Token[] tokens = new Token[]
146 {
147 Token.newIdentifier( "cheeseIt", 1, 1 ),
148 Token.newSymbol( "(", 1, 10 ),
149 Token.newSymbol( ")", 1, 11 )
150 };
151
152 MockTokenStream in = new MockTokenStream( tokens );
153
154 assertSame( tokens[2],
155 in.la( 3 ) );
156
157 assertSame( tokens[1],
158 in.la( 2 ) );
159
160 assertSame( tokens[0],
161 in.la( 1 ) );
162 }
163
164 public void testLaAtEnd()
165 throws Exception
166 {
167 Token[] tokens = new Token[]
168 {
169 };
170
171 MockTokenStream in = new MockTokenStream( tokens );
172
173 assertNull( in.la() );
174
175 assertNull( in.la() );
176
177 assertNull( in.la() );
178 }
179
180 /***
181 * this test is broken as we have a large look-ahead token buffer now
182 * to handle newlines. if we supported mid-stream consumption
183 * (e.g. consumeAtIndex(3) then we could avoid such a large buffer
184 */
185 public void DISABLED_testExhaustLookAhead()
186 throws Exception
187 {
188 Token[] tokens = new Token[]
189 {
190 Token.newSymbol( "(", 1, 1 ),
191 Token.newSymbol( ")", 1, 2 ),
192 Token.newSymbol( "[", 1, 3 ),
193 Token.newSymbol( "]", 1, 4 ),
194 Token.newSymbol( "{", 1, 5 ),
195 Token.newSymbol( "}", 1, 6 )
196 };
197
198 MockTokenStream in = new MockTokenStream( tokens );
199
200 assertSame( tokens[0],
201 in.la() );
202
203 assertSame( tokens[1],
204 in.la( 2 ) );
205
206 assertSame( tokens[2],
207 in.la( 3 ) );
208
209 assertSame( tokens[3],
210 in.la( 4 ) );
211
212 assertSame( tokens[4],
213 in.la( 5 ) );
214 try
215 {
216 in.la( 6 );
217 fail( "should have thrown GroovyBugError" );
218 }
219 catch (GroovyBugError e)
220 {
221
222
223
224 }
225 }
226
227 public void testTokenMismatch()
228 throws Exception
229 {
230 Token[] tokens = new Token[]
231 {
232 Token.newSymbol( "(", 1, 1 ),
233 Token.newSymbol( ")", 1, 2 )
234 };
235
236 MockTokenStream in = new MockTokenStream( tokens );
237
238 try
239 {
240 in.consume( Types.RIGHT_PARENTHESIS );
241 fail( "should have thrown TokenMismatchException" );
242 }
243 catch (TokenMismatchException e)
244 {
245
246
247 assertSame( tokens[0],
248 e.getUnexpectedToken() );
249
250 assertEquals( Types.RIGHT_PARENTHESIS,
251 e.getExpectedType() );
252 }
253 }
254 }