KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > conditional > TestLexer


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.conditional;
16
17 import org.apache.hivemind.test.HiveMindTestCase;
18
19 /**
20  * Tests for {@link org.apache.hivemind.conditional.Lexer}.
21  *
22  * @author Howard M. Lewis Ship
23  * @since 1.1
24  */

25 public class TestLexer extends HiveMindTestCase
26 {
27     public void testKeywords()
28     {
29         Lexer l = new Lexer("and OR Not class Property");
30
31         assertSame(TokenType.AND, l.next().getType());
32         assertSame(TokenType.OR, l.next().getType());
33         assertSame(TokenType.NOT, l.next().getType());
34         assertSame(TokenType.CLASS, l.next().getType());
35         assertSame(TokenType.PROPERTY, l.next().getType());
36
37         assertNull(l.next());
38     }
39
40     public void testComplexSymbol()
41     {
42         Lexer l = new Lexer("property foo.bar-baz");
43
44         assertSame(TokenType.PROPERTY, l.next().getType());
45
46         Token t = l.next();
47
48         assertSame(TokenType.SYMBOL, t.getType());
49         assertEquals("foo.bar-baz", t.getValue());
50     }
51
52     public void testParens()
53     {
54         Lexer l = new Lexer("not (property foo)");
55         assertSame(TokenType.NOT, l.next().getType());
56         assertSame(TokenType.OPAREN, l.next().getType());
57         assertSame(TokenType.PROPERTY, l.next().getType());
58
59         Token t = l.next();
60
61         assertSame(TokenType.SYMBOL, t.getType());
62         assertEquals("foo", t.getValue());
63
64         assertSame(TokenType.CPAREN, l.next().getType());
65
66         assertNull(l.next());
67     }
68
69     public void testInvalidCharacter()
70     {
71         Lexer l = new Lexer("not[property foo]");
72
73         assertSame(TokenType.NOT, l.next().getType());
74
75         try
76         {
77             l.next();
78             unreachable();
79         }
80         catch (RuntimeException JavaDoc ex)
81         {
82             assertEquals(
83                     "Unexpected character '[' at position 4 of input string 'not[property foo]'.",
84                     ex.getMessage());
85         }
86     }
87
88     public void testSymbolAtEnd()
89     {
90         Lexer l = new Lexer("property foo.bar.Baz");
91         assertSame(TokenType.PROPERTY, l.next().getType());
92
93         Token t = l.next();
94
95         assertSame(TokenType.SYMBOL, t.getType());
96         assertEquals("foo.bar.Baz", t.getValue());
97
98         assertNull(l.next());
99     }
100 }
Popular Tags