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.js;
017:
018: import com.google.gwt.dev.js.ast.JsProgram;
019: import com.google.gwt.dev.js.ast.JsStatement;
020: import com.google.gwt.dev.js.ast.JsVisitor;
021: import com.google.gwt.dev.util.DefaultTextOutput;
022: import com.google.gwt.dev.util.TextOutput;
023:
024: import junit.framework.TestCase;
025:
026: import java.io.StringReader;
027: import java.util.List;
028:
029: public class JsToStringGenerationVisitorAccuracyTest extends TestCase {
030:
031: private JsParser parser = new JsParser();
032:
033: public void testAdditionPositive() throws Exception {
034: // x plus positive 3
035: doTest("x + +3");
036: }
037:
038: public void testArithmetic() throws Exception {
039: doTest("a + (b * (c - d)) / (e / f) % x");
040: }
041:
042: public void testArrayDeclarationArrayAccess() throws Exception {
043: doTest("[1,2,3,4][2]");
044: }
045:
046: public void testArrayLiteralParentheses() throws Exception {
047: doTest("var x = [a, (b, c), d]");
048: }
049:
050: public void testComplexConstruction() throws Exception {
051: doTest("(new (new (a(({a : 'b', c : 'd'}),[1,2,3,x,y,z]))())())()");
052: }
053:
054: public void testConstructionInvocation() throws Exception {
055: doTest("(new a())()");
056: }
057:
058: public void testDecrement() throws Exception {
059: doTest("(x--)-(-(--y))");
060: }
061:
062: public void testFunctionDeclarationInvocation() throws Exception {
063: doTest("(function () {})()");
064: }
065:
066: public void testInvocationConstruction() throws Exception {
067: doTest("new ((a.b.c()).d.e)(1,2,3)");
068: }
069:
070: public void testNestedConstruction() throws Exception {
071: doTest("new (new (new MyClass()))");
072: }
073:
074: public void testObjectDeclarationArrayAccess() throws Exception {
075: doTest("({ a : 'b'})['a']");
076: }
077:
078: public void testObjectDeclarationMemberAccess() throws Exception {
079: doTest("({ a : 'b'}).a");
080: }
081:
082: public void testObjectLiteral() throws Exception {
083: // declaring an object requires parentheses
084: doTest("({ 'property' : 'value'})");
085: }
086:
087: public void testObjectLiteralConditional() throws Exception {
088: doTest("var x = {a : ((b(), c) ? d : e)}");
089: }
090:
091: public void testObjectLiteralDeclaration() throws Exception {
092: // quotes are necessary around some property variables
093: doTest("var x = {'abc\\'' : 'value'}");
094: doTest("var x = {\"a.1\" : 'value'}");
095: doTest("var x = {\"\\'\\\"\" : 'value'}");
096: }
097:
098: public void testObjectLiteralParentheses() throws Exception {
099: doTest("var x = {a : (c, d), b : 3}");
100: }
101:
102: public void testUnaryOperations() throws Exception {
103: // spaces or parentheses are necessary to separate negation and decrement
104: doTest("var x = -(-(--y))");
105: }
106:
107: private void doTest(String js) throws Exception {
108: List<JsStatement> expected = parser.parse(new JsProgram()
109: .getScope(), new StringReader(js), 0);
110: List<JsStatement> actual = parse(expected, true);
111: ComparingVisitor.exec(expected, actual);
112:
113: actual = parse(expected, false);
114: ComparingVisitor.exec(expected, actual);
115: }
116:
117: private List<JsStatement> parse(List<JsStatement> expected,
118: boolean compact) throws Exception {
119: TextOutput text = new DefaultTextOutput(compact);
120: JsVisitor generator = new JsToStringGenerationVisitor(text);
121: generator.acceptList(expected);
122: return parser.parse(new JsProgram().getScope(),
123: new StringReader(text.toString()), 0);
124: }
125: }
|