01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.js;
17:
18: import com.google.gwt.dev.js.ast.JsProgram;
19: import com.google.gwt.dev.js.ast.JsStatement;
20: import com.google.gwt.dev.js.ast.JsVisitor;
21: import com.google.gwt.dev.util.DefaultTextOutput;
22: import com.google.gwt.dev.util.TextOutput;
23:
24: import junit.framework.TestCase;
25:
26: import java.io.StringReader;
27: import java.util.List;
28:
29: public class JsToStringGenerationVisitorConcisenessTest extends
30: TestCase {
31:
32: private JsParser parser = new JsParser();
33:
34: public void testComplexDecrement() throws Exception {
35: String output = parse("var x = -(-(-(--y)))");
36: assertEquals("var x=- - - --y", output);
37: }
38:
39: public void testComplexIncrement() throws Exception {
40: String output = parse("var x = (y++) + (-(--z))");
41: assertEquals("var x=y+++- --z", output);
42: }
43:
44: public void testConstruction() throws Exception {
45: String output = parse("new a.b(c,d)");
46: assertEquals("new a.b(c,d)", output);
47: }
48:
49: public void testIncrement() throws Exception {
50: // y++-x is valid
51: assertEquals("var x=y++-z", parse("var x = (y++) - z"));
52: }
53:
54: public void testObjectLiteralAssignment() throws Exception {
55: assertEquals("var x={a:b=2,c:d}",
56: parse("var x = {a : (b = 2), c : d}"));
57: }
58:
59: public void testObjectLiteralConditional() throws Exception {
60: // the parentheses are not required around the conditional
61: assertEquals("var x={a:b?c:d,e:f}",
62: parse("var x = {a : (b ? c : d), e : f}"));
63: }
64:
65: public void testObjectLiteralDeclarationConcise() throws Exception {
66: // quotes are not necessary around many property variables in object
67: // literals
68: assertEquals("var x={1:'b'}", parse("var x = {1 : 'b'}"));
69: assertEquals("var x={$a_:'b'}", parse("var x = {'$a_' : 'b'}"));
70: assertEquals("var x={1.2:'b'}", parse("var x = {1.2 : 'b'}"));
71: }
72:
73: private String parse(String js) throws Exception {
74: List<JsStatement> statements = parser.parse(new JsProgram()
75: .getScope(), new StringReader(js), 0);
76: TextOutput text = new DefaultTextOutput(true);
77: JsVisitor generator = new JsToStringGenerationVisitor(text);
78: generator.acceptList(statements);
79: return text.toString();
80: }
81: }
|