KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > automaton > ElementTest


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

17
18 package org.sablecc.sablecc.automaton;
19
20 import static org.junit.Assert.fail;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.sablecc.sablecc.alphabet.Realms;
25 import org.sablecc.sablecc.alphabet.Symbol;
26 import org.sablecc.sablecc.exception.InternalException;
27
28 public class ElementTest {
29
30     Partition<Integer JavaDoc> partition;
31
32     Dfa<Integer JavaDoc> dfa;
33
34     DfaState<Integer JavaDoc> startState;
35
36     Nfa<Integer JavaDoc> nfa;
37
38     Element<Integer JavaDoc> element;
39
40     @Before
41     public void setUp()
42             throws Exception JavaDoc {
43
44         this.nfa = new Nfa<Integer JavaDoc>(Realms.getInteger().createInterval(50))
45                 .zeroOrMore();
46
47         this.dfa = new Dfa<Integer JavaDoc>(this.nfa);
48
49         this.startState = this.dfa.getStartState();
50
51         this.partition = new Partition<Integer JavaDoc>(this.dfa);
52
53         this.element = this.partition.getElement(this.startState);
54
55     }
56
57     @SuppressWarnings JavaDoc("unused")
58     @Test
59     public void testElement() {
60
61         // Case with null Partition
62
Partition<Integer JavaDoc> nullPartition = null;
63         try {
64             Element element = new Element<Integer JavaDoc>(nullPartition,
65                     this.startState);
66             fail("partition may not be null");
67         }
68         catch (InternalException e) {
69             // Expected
70
}
71         // Case with null State
72
DfaState<Integer JavaDoc> nullState = null;
73         try {
74             Element element = new Element<Integer JavaDoc>(this.partition, nullState);
75             fail("state may not be null");
76         }
77         catch (InternalException e) {
78             // Expected
79
}
80
81         // Case with incorrect State
82
Dfa<Integer JavaDoc> newDfa = new Dfa<Integer JavaDoc>(new Nfa<Integer JavaDoc>(Realms
83                 .getInteger().createInterval(999)));
84
85         Partition newPartition = new Partition<Integer JavaDoc>(newDfa);
86
87         DfaState<Integer JavaDoc> newState = newDfa.getStartState();
88
89         try {
90             Element element = new Element<Integer JavaDoc>(this.partition, newState);
91             fail("invalid state");
92         }
93         catch (InternalException e) {
94             // Expected
95
}
96
97     }
98
99     @SuppressWarnings JavaDoc("unchecked")
100     @Test
101     public void testSetGroup() {
102
103         // Case with null Group
104
Group<Integer JavaDoc> nullGroup = null;
105         try {
106             this.element.setGroup(nullGroup);
107             fail("group may not be null");
108         }
109         catch (InternalException e) {
110             // Expected
111
}
112
113         // Case with invalid Group
114
Dfa<Integer JavaDoc> newDfa = new Dfa<Integer JavaDoc>(new Nfa<Integer JavaDoc>(Realms
115                 .getInteger().createInterval(999)));
116
117         Partition newPartition = new Partition<Integer JavaDoc>(newDfa);
118
119         Group<Integer JavaDoc> invalidGroup = new Group<Integer JavaDoc>(newPartition);
120
121         try {
122             this.element.setGroup(invalidGroup);
123             fail("the group is invalid");
124         }
125         catch (InternalException e) {
126             // Expected
127
}
128     }
129
130     @Test
131     public void testGetTarget() {
132
133         // Case with null Symbol
134
Symbol<Integer JavaDoc> nullSymbol = null;
135         try {
136             this.element.getTarget(nullSymbol);
137             fail("symbol may not be null");
138         }
139         catch (InternalException e) {
140             // Expected
141
}
142
143         // Case with invalid Symbol
144
Symbol<Integer JavaDoc> invalidSymbol = new Symbol<Integer JavaDoc>(Realms.getInteger()
145                 .createInterval(999));
146         try {
147             this.element.getTarget(invalidSymbol);
148             fail("the symbol is invalid");
149         }
150         catch (InternalException e) {
151             // Expected
152
}
153     }
154 }
155
Popular Tags