001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.jjs.test;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: /**
021: * Tests enum functionality.
022: */
023: public class EnumsTest extends GWTTestCase {
024:
025: enum Basic {
026: A, B, C
027: }
028:
029: enum Complex {
030: A("X"), B("Y"), C("Z");
031:
032: String value;
033:
034: Complex(String value) {
035: this .value = value;
036: }
037:
038: public String value() {
039: return value;
040: }
041: }
042:
043: enum Subclassing {
044: A {
045: @Override
046: public String value() {
047: return "X";
048: }
049: },
050: B {
051: @Override
052: public String value() {
053: return "Y";
054: }
055: },
056: C {
057: @Override
058: public String value() {
059: return "Z";
060: }
061: };
062:
063: public abstract String value();
064: }
065:
066: public String getModuleName() {
067: return "com.google.gwt.dev.jjs.CompilerSuite";
068: }
069:
070: public void testCompareTo() {
071: assertTrue(Basic.A.compareTo(Basic.valueOf("A")) == 0);
072: assertTrue(Basic.B.compareTo(Basic.A) > 0);
073: assertTrue(Basic.A.compareTo(Basic.B) < 0);
074:
075: assertTrue(Complex.A.compareTo(Complex.valueOf("A")) == 0);
076: assertTrue(Complex.B.compareTo(Complex.A) > 0);
077: assertTrue(Complex.A.compareTo(Complex.B) < 0);
078:
079: assertTrue(Subclassing.A.compareTo(Subclassing.valueOf("A")) == 0);
080: assertTrue(Subclassing.B.compareTo(Subclassing.A) > 0);
081: assertTrue(Subclassing.A.compareTo(Subclassing.B) < 0);
082: }
083:
084: public void testField() {
085: assertEquals("X", Complex.A.value);
086: assertEquals("Y", Complex.B.value);
087: assertEquals("Z", Complex.C.value);
088: }
089:
090: public void testGetDeclaringClass() {
091: assertEquals(Basic.class, Basic.A.getDeclaringClass());
092: assertEquals(Complex.class, Complex.A.getDeclaringClass());
093: assertEquals(Subclassing.class, Subclassing.A
094: .getDeclaringClass());
095: }
096:
097: public void testMethod() {
098: assertEquals("X", Complex.A.value());
099: assertEquals("Y", Complex.B.value());
100: assertEquals("Z", Complex.C.value());
101:
102: assertEquals("X", Subclassing.A.value());
103: assertEquals("Y", Subclassing.B.value());
104: assertEquals("Z", Subclassing.C.value());
105: }
106:
107: public void testName() {
108: assertEquals("A", Basic.A.name());
109: assertEquals("B", Basic.B.name());
110: assertEquals("C", Basic.C.name());
111:
112: assertEquals("A", Complex.A.name());
113: assertEquals("B", Complex.B.name());
114: assertEquals("C", Complex.C.name());
115:
116: assertEquals("A", Subclassing.A.name());
117: assertEquals("B", Subclassing.B.name());
118: assertEquals("C", Subclassing.C.name());
119: }
120:
121: public void testOrdinals() {
122: assertEquals(0, Basic.A.ordinal());
123: assertEquals(1, Basic.B.ordinal());
124: assertEquals(2, Basic.C.ordinal());
125:
126: assertEquals(0, Complex.A.ordinal());
127: assertEquals(1, Complex.B.ordinal());
128: assertEquals(2, Complex.C.ordinal());
129:
130: assertEquals(0, Subclassing.A.ordinal());
131: assertEquals(1, Subclassing.B.ordinal());
132: assertEquals(2, Subclassing.C.ordinal());
133: }
134:
135: public void testSwitch() {
136: switch (Basic.A) {
137: case A:
138: break;
139: case B:
140: fail("Switch failed");
141: break;
142: case C:
143: fail("Switch failed");
144: break;
145: default:
146: fail("Switch failed");
147: break;
148: }
149: switch (Complex.B) {
150: case A:
151: fail("Switch failed");
152: break;
153: case B:
154: break;
155: case C:
156: fail("Switch failed");
157: break;
158: default:
159: fail("Switch failed");
160: break;
161: }
162: switch (Subclassing.C) {
163: case A:
164: fail("Switch failed");
165: break;
166: case B:
167: fail("Switch failed");
168: break;
169: default:
170: break;
171: }
172: }
173:
174: public void testValueOf() {
175: assertEquals(Basic.A, Basic.valueOf("A"));
176: assertEquals(Basic.B, Basic.valueOf("B"));
177: assertEquals(Basic.C, Basic.valueOf("C"));
178: try {
179: Basic.valueOf("D");
180: fail("Basic.valueOf(\"D\") -- expected IllegalArgumentException");
181: } catch (IllegalArgumentException e) {
182: }
183:
184: assertEquals(Complex.A, Complex.valueOf("A"));
185: assertEquals(Complex.B, Complex.valueOf("B"));
186: assertEquals(Complex.C, Complex.valueOf("C"));
187: try {
188: Complex.valueOf("D");
189: fail("Complex.valueOf(\"D\") -- expected IllegalArgumentException");
190: } catch (IllegalArgumentException e) {
191: }
192:
193: assertEquals(Subclassing.A, Subclassing.valueOf("A"));
194: assertEquals(Subclassing.B, Subclassing.valueOf("B"));
195: assertEquals(Subclassing.C, Subclassing.valueOf("C"));
196: try {
197: Subclassing.valueOf("D");
198: fail("Subclassing.valueOf(\"D\") -- expected IllegalArgumentException");
199: } catch (IllegalArgumentException e) {
200: }
201: }
202:
203: public void testValues() {
204: Basic[] simples = Basic.values();
205: assertEquals(3, simples.length);
206: assertEquals(Basic.A, simples[0]);
207: assertEquals(Basic.B, simples[1]);
208: assertEquals(Basic.C, simples[2]);
209:
210: Complex[] complexes = Complex.values();
211: assertEquals(3, complexes.length);
212: assertEquals(Complex.A, complexes[0]);
213: assertEquals(Complex.B, complexes[1]);
214: assertEquals(Complex.C, complexes[2]);
215:
216: Subclassing[] subs = Subclassing.values();
217: assertEquals(3, subs.length);
218: assertEquals(Subclassing.A, subs[0]);
219: assertEquals(Subclassing.B, subs[1]);
220: assertEquals(Subclassing.C, subs[2]);
221: }
222: }
|