0001: /**
0002: *
0003: * Copyright 2005 Jeremy Rayner
0004: *
0005: * Licensed under the Apache License, Version 2.0 (the "License");
0006: * you may not use this file except in compliance with the License.
0007: * You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: *
0017: **/package org.codehaus.groovy.antlr.treewalker;
0018:
0019: import groovy.util.GroovyTestCase;
0020:
0021: import java.io.ByteArrayOutputStream;
0022: import java.io.PrintStream;
0023: import java.io.StringReader;
0024:
0025: import org.codehaus.groovy.antlr.AntlrASTProcessor;
0026: import org.codehaus.groovy.antlr.SourceBuffer;
0027: import org.codehaus.groovy.antlr.UnicodeEscapingReader;
0028: import org.codehaus.groovy.antlr.parser.GroovyLexer;
0029: import org.codehaus.groovy.antlr.parser.GroovyRecognizer;
0030:
0031: import antlr.collections.AST;
0032:
0033: /**
0034: * Testcases for the antlr AST visitor that prints groovy source code.
0035: *
0036: * @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
0037: * @version $Revision: 4653 $
0038: */
0039: public class SourcePrinterTest extends GroovyTestCase {
0040:
0041: public void testAbstract() throws Exception {
0042: assertEquals("public abstract class Foo {}",
0043: pretty("public abstract class Foo{}"));
0044: }
0045:
0046: public void testAnnotation() throws Exception {
0047: assertEquals("@Crimson foo", pretty("@Crimson foo"));
0048: assertEquals("@Override int hashCode() {return 0}",
0049: pretty("@Override int hashCode() {return 0}"));
0050: }
0051:
0052: public void testAnnotations() throws Exception {
0053: assertEquals("@Important @Blue package foo.bar",
0054: pretty("@Important @Blue package foo.bar"));
0055: }
0056:
0057: public void testAnnotationArrayInit() throws Exception {
0058: // obsolete java syntax
0059: }
0060:
0061: public void testAnnotationDef() throws Exception {
0062: // todo - 17 July 2006 - test fine, however this parses but causes error in AntlrParserPlugin
0063: assertEquals("public @interface Foo{}",
0064: pretty("public @interface Foo{}"));
0065: }
0066:
0067: public void testAnnotationFieldDef() throws Exception {
0068: assertEquals("public @interface Foo{int bar() default 123}",
0069: pretty("public @interface Foo{int bar() default 123}"));
0070: }
0071:
0072: public void testAnnotationMemberValuePair() throws Exception {
0073: assertEquals("@Prime(value = 17) int foo",
0074: pretty("@Prime(value=17) int foo"));
0075: assertEquals("@Blue(v = 3, v = 5) int bar",
0076: pretty("@Blue(v = 3, v = 5) int bar"));
0077: }
0078:
0079: public void testArrayDeclarator() throws Exception {
0080: assertEquals("int[] primes = new int[5]",
0081: pretty("int[] primes = new int[5]"));
0082: }
0083:
0084: public void testAssign() throws Exception {
0085: assertEquals("a = 12", pretty("a=12"));
0086: }
0087:
0088: public void testBand() throws Exception {
0089: assertEquals("def x = 1 & 2", pretty("def x=1&2"));
0090: }
0091:
0092: public void testBandAssign() throws Exception {
0093: assertEquals("x &= 2", pretty("x&=2"));
0094: }
0095:
0096: public void testBnot() throws Exception {
0097: assertEquals("def z = ~123", pretty("def z = ~123"));
0098: }
0099:
0100: public void testBor() throws Exception {
0101: assertEquals("def y = 1 | 2", pretty("def y = 1 | 2"));
0102: }
0103:
0104: public void testBorAssign() throws Exception {
0105: assertEquals("y |= 2", pretty("y|=2"));
0106: }
0107:
0108: public void testBsr() throws Exception {
0109: // unsigned right shift
0110: assertEquals("def q = 1 >>> 2", pretty("def q = 1 >>> 2"));
0111: }
0112:
0113: public void testBsrAssign() throws Exception {
0114: assertEquals("y >>>= 2", pretty("y>>>=2"));
0115: }
0116:
0117: public void testBxor() throws Exception {
0118: assertEquals("def y = true ^ false",
0119: pretty("def y = true ^ false"));
0120: }
0121:
0122: public void testBxorAssign() throws Exception {
0123: assertEquals("y ^= false", pretty("y^=false"));
0124: }
0125:
0126: public void testCaseGroup() throws Exception {
0127: assertEquals("switch (foo) {case bar:x = 1}",
0128: pretty("switch(foo){case bar:x=1}"));
0129: }
0130:
0131: public void testClassDef() throws Exception {
0132: assertEquals("class Foo {def bar}",
0133: pretty("class Foo{def bar}"));
0134: }
0135:
0136: public void testClosedBlock() throws Exception {
0137: assertEquals("[1, 2, 3].each {println it}",
0138: pretty("[1,2,3].each{println it}"));
0139: assertEquals("def x = foo.bar(mooky){ x, y -> wibble(y, x)}",
0140: pretty("def x = foo.bar(mooky) {x,y-> wibble(y,x)}"));
0141: // todo: above is not quite the spacing I would expect, but good enough for now...
0142: }
0143:
0144: public void testCompareTo() throws Exception {
0145: assertEquals("1 <=> 2", pretty("1<=>2"));
0146: }
0147:
0148: public void testCtorCall() throws Exception {
0149: assertEquals("class Foo {Foo(int x) {this()}}",
0150: pretty("class Foo{Foo(int x) {this()}}"));
0151: assertEquals("class Foo {Foo( x) {this()}}",
0152: pretty("class Foo{Foo(x) {this()}}"));
0153: // todo: above is not quite the spacing I would expect, but good enough for now...
0154: }
0155:
0156: public void testCtorIdent() throws Exception {
0157: assertEquals("class Foo {private Foo() {}}",
0158: pretty("class Foo {private Foo() {}}"));
0159: }
0160:
0161: public void testDec() throws Exception {
0162: assertEquals("--b", pretty("--b"));
0163: }
0164:
0165: public void testDiv() throws Exception {
0166: assertEquals("1 / 2", pretty("1/2"));
0167: }
0168:
0169: public void testDivAssign() throws Exception {
0170: assertEquals("x /= 2", pretty("x/=2"));
0171: }
0172:
0173: public void testDot() throws Exception {
0174: assertEquals("java.util.Date d = new java.util.Date()",
0175: pretty("java.util.Date d = new java.util.Date()"));
0176: assertEquals("class Foo extends java.util.Date {}",
0177: pretty("class Foo extends java.util.Date {}"));
0178: assertEquals("foo.bar.mooky()", pretty("foo.bar.mooky()"));
0179: assertEquals("package foo.bar", pretty("package foo.bar"));
0180: assertEquals("import java.util.Date",
0181: pretty("import java.util.Date"));
0182: assertEquals("import java.io.*", pretty("import java.io.*"));
0183: assertEquals("@foo.Bar mooky", pretty("@foo.Bar mooky"));
0184: assertEquals("def foo() throws bar.MookyException{}",
0185: pretty("def foo() throws bar.MookyException{}"));
0186: assertEquals("def x = \"${foo.bar}\"",
0187: pretty("def x = \"${foo.bar}\""));
0188: }
0189:
0190: public void testDynamicMember() throws Exception {
0191: assertEquals("foo.(bar)", pretty("foo.(bar)"));
0192: assertEquals("foo.\"${bar}\"", pretty("foo.\"${bar}\""));
0193: }
0194:
0195: public void testElist() throws Exception {
0196: assertEquals("println 2 + 2", pretty("println 2 + 2"));
0197: assertEquals("for (i = 0, j = 2 ; i < 10 ; i++, j--){print i}",
0198: pretty("for (i = 0,j = 2;i < 10; i++, j--) {print i}"));
0199: assertEquals("foo()", pretty("foo()")); // empty ELIST
0200: assertEquals("foo(bar, mooky)", pretty("foo( bar , mooky )"));
0201: }
0202:
0203: public void testEnumConstantDef() throws Exception {
0204: assertEquals("enum Coin {PENNY(1), DIME(10), QUARTER(25)}",
0205: pretty("enum Coin {PENNY(1), DIME(10), QUARTER(25)}"));
0206: }
0207:
0208: public void testEnumDef() throws Exception {
0209: assertEquals("enum Season {WINTER, SPRING, SUMMER, AUTUMN}",
0210: pretty("enum Season{WINTER,SPRING,SUMMER,AUTUMN}"));
0211: //todo: more strange spacing in following line
0212: assertEquals(
0213: "enum Operation {ADDITION{double eval( x, y) {return x + y}}}",
0214: pretty("enum Operation {ADDITION {double eval(x,y) {return x + y}}}"));
0215: }
0216:
0217: public void testEqual() throws Exception {
0218: assertEquals("a == b", pretty("a==b"));
0219: }
0220:
0221: public void testEsc_FAILS() throws Exception {
0222: if (notYetImplemented())
0223: return;
0224: // dquote-tab-dquote
0225: assertEquals("println \"\\\"\t\\\"\"",
0226: pretty("println \"\\\"\t\\\"\""));
0227: }
0228:
0229: public void testExponent() throws Exception {
0230: assertEquals("println 1.2e-10", pretty("println 1.2e-10"));
0231: }
0232:
0233: public void testExpr_FAILS() throws Exception {
0234: if (notYetImplemented())
0235: return;
0236: assertEquals("System.out.println(x)",
0237: pretty("System.out.println(x)"));
0238: assertEquals("return f", pretty("return f"));
0239: assertEquals("foo(bar);mooky(bar)",
0240: pretty("foo(bar);mooky(bar)"));
0241: }
0242:
0243: public void testExtendsClause() throws Exception {
0244: assertEquals("class Foo extends Bar {}",
0245: pretty("class Foo extends Bar {}"));
0246: assertEquals("interface Wibble extends Mooky{}",
0247: pretty("interface Wibble extends Mooky {}"));
0248: //todo spacing is odd, c.f. last space in class vs interface above
0249: }
0250:
0251: public void testFinal() throws Exception {
0252: assertEquals("public final int getX() {return 0}",
0253: pretty("public final int getX() {return 0}"));
0254: }
0255:
0256: public void testForCondition() throws Exception {
0257: assertEquals("for (i = 0 ; i < 10 ; i++){println i}",
0258: pretty("for (i=0;i<10;i++) {println i}"));
0259: }
0260:
0261: // testForInit() covered by testForCondition()
0262:
0263: public void testForInIterable() throws Exception {
0264: assertEquals("for (i in [1, 2]) {}",
0265: pretty("for (i in [1,2]) {}"));
0266: }
0267:
0268: // testForIterator() covered by testForCondition()
0269:
0270: public void testGe() throws Exception {
0271: assertEquals("if (60 >= 70) {}", pretty("if (60>=70) {}"));
0272: }
0273:
0274: public void testGt() throws Exception {
0275: assertEquals("if (2070 > 354) {}", pretty("if (2070 > 354) {}"));
0276: }
0277:
0278: public void testHexDigit() throws Exception {
0279: assertEquals("def bar = 0xCaFe", pretty("def bar = 0xCaFe"));
0280: }
0281:
0282: public void testHexDigitInUnicodeEscape_FAILS() throws Exception {
0283: if (notYetImplemented())
0284: return;
0285: assertEquals("def foo = '\\ubabe'",
0286: pretty("def foo = '\\ubabe'"));
0287: }
0288:
0289: public void testIdent() throws Exception {
0290: // used _everywhere_ , lets assume that the other specific
0291: // testcases include enough ident usage for now.
0292: assertEquals("foo.bar", pretty("foo.bar"));
0293: }
0294:
0295: public void testImplementsClause() throws Exception {
0296: assertEquals("class Foo implements Bar {}",
0297: pretty("class Foo implements Bar {}"));
0298: }
0299:
0300: public void testImplicitParameters() throws Exception {
0301: assertEquals("[1, 2, 3].each {println it}",
0302: pretty("[1,2,3].each{println it}"));
0303: }
0304:
0305: public void testImport() throws Exception {
0306: assertEquals("import foo.bar.Wibble",
0307: pretty("import foo.bar.Wibble"));
0308: }
0309:
0310: public void testInc() throws Exception {
0311: assertEquals("++x", pretty("++x"));
0312: }
0313:
0314: public void testIndexOp() throws Exception {
0315: assertEquals("foo.bar()[fred.wilma()]",
0316: pretty("foo.bar()[fred.wilma()]"));
0317: }
0318:
0319: public void testInterfaceDef() throws Exception {
0320: //todo, the spacing here is... unusual
0321: assertEquals("interface Foo{void blah() }",
0322: pretty("interface Foo{void blah()}"));
0323: }
0324:
0325: public void testInstanceInit() throws Exception {
0326: assertEquals("class Foo {{x = 1}}", pretty("class Foo {{x=1}}"));
0327: }
0328:
0329: public void testLabeledArg() throws Exception {
0330: assertEquals("myMethod(argOne:123, argTwo:123)",
0331: pretty("myMethod(argOne:123,argTwo:123)"));
0332: assertEquals("myMap = [keyOne:123, keyTwo:234]",
0333: pretty("myMap = [keyOne:123,keyTwo:234]"));
0334: }
0335:
0336: public void testLabeledStat() throws Exception {
0337: assertEquals("foo:x = 1", pretty("foo:x = 1"));
0338: }
0339:
0340: public void testLand() throws Exception {
0341: assertEquals("true && false", pretty("true && false"));
0342: }
0343:
0344: public void testLe() throws Exception {
0345: assertEquals("if (60 <= 70) {}", pretty("if (60<=70) {}"));
0346: }
0347:
0348: public void testListConstructor() throws Exception {
0349: assertEquals("[a, b]", pretty("[a,b]"));
0350: }
0351:
0352: public void testLiteralAny() throws Exception {
0353: assertEquals("any x = 2", pretty("any x = 2"));
0354: }
0355:
0356: public void testLiteralAs() throws Exception {
0357: assertEquals("import java.util.Date as MyDate",
0358: pretty("import java.util.Date as MyDate"));
0359: // todo suspicious spacing in the following assertion
0360: assertEquals("x = 12 as Long ", pretty("x = 12 as Long"));
0361: }
0362:
0363: public void testLiteralAssert() throws Exception {
0364: assertEquals("assert a == true", pretty("assert a== true"));
0365: assertEquals("assert b == true : 99",
0366: pretty("assert b==true:99"));
0367: // todo is ',' deprecated now?
0368: //assertEquals("assert b == true , 99", pretty("assert b==true,99"));
0369: }
0370:
0371: public void testLiteralBoolean() throws Exception {
0372: assertEquals("boolean b = true", pretty("boolean b =true"));
0373: }
0374:
0375: public void testLiteralBreak() throws Exception {
0376: assertEquals("for (i in 1..100) {break }",
0377: pretty("for (i in 1..100) {break}"));
0378: assertEquals("switch (foo) {default:break }",
0379: pretty("switch(foo){default:break}"));
0380: assertEquals("def myMethod() {break }",
0381: pretty("def myMethod(){break}"));
0382: assertEquals("for (i in 1..100) {break 2}",
0383: pretty("for (i in 1..100) {break 2}"));
0384: //todo should the colon be postfixed to the label?
0385: assertEquals("for (i in 1..100) {break label1:}",
0386: pretty("for (i in 1..100) {break label1:}"));
0387: }
0388:
0389: public void testLiteralByte() throws Exception {
0390: assertEquals("byte b = 1", pretty("byte b=1"));
0391: }
0392:
0393: public void testLiteralCase() throws Exception {
0394: assertEquals("switch (foo) {case 1:x = 3}",
0395: pretty("switch(foo){case 1:x=3}"));
0396: }
0397:
0398: public void testLiteralCatch() throws Exception {
0399: assertEquals("try {} catch (Exception e) {}",
0400: pretty("try {} catch (Exception e) {}"));
0401: assertEquals(
0402: "try {} catch (Exception e1) {} catch (Exception2 e2) {}",
0403: pretty("try {} catch (Exception e1) {} catch (Exception2 e2) {}"));
0404: }
0405:
0406: public void testLiteralChar() throws Exception {
0407: assertEquals("char c = \"a\"", pretty("char c = \"a\""));
0408: }
0409:
0410: public void testLiteralClass() throws Exception {
0411: assertEquals("public class Foo {int bar}",
0412: pretty("public class Foo{int bar}"));
0413: }
0414:
0415: public void testLiteralContinue() throws Exception {
0416: assertEquals("for (i in 1..100) {continue }",
0417: pretty("for (i in 1..100) {continue}"));
0418: assertEquals("for (i in 1..100) {continue 2}",
0419: pretty("for (i in 1..100) {continue 2}"));
0420: //todo should the colon be postfixed to the label?
0421: assertEquals("for (i in 1..100) {continue label1:}",
0422: pretty("for (i in 1..100) {continue label1:}"));
0423: assertEquals("[1, 2, 3].each {continue }",
0424: pretty("[1,2,3].each{continue}"));
0425: }
0426:
0427: public void testLiteralDef() throws Exception {
0428: assertEquals("def x = 123", pretty("def x=123"));
0429: assertEquals("def myMethod() {return 0}",
0430: pretty("def myMethod(){return 0}"));
0431: // note: def not needed in parameter declarations, but it is valid
0432: //todo: is it ok to strip out 'def' from parameter declarations?
0433: assertEquals("def foo( bar) {}", pretty("def foo(def bar){}"));
0434: }
0435:
0436: public void testLiteralDefault() throws Exception {
0437: assertEquals("switch (foo) {default:x = 2}",
0438: pretty("switch(foo){default:x=2}"));
0439: assertEquals("public @interface Foo{int bar() default 123}",
0440: pretty("public @interface Foo{int bar() default 123}"));
0441: }
0442:
0443: public void testLiteralDouble() throws Exception {
0444: assertEquals("double d = 1.0", pretty("double d = 1.0"));
0445: }
0446:
0447: public void testLiteralElse() throws Exception {
0448: assertEquals("if (false) {a = 1} else {a = 2}",
0449: pretty("if (false) {a=1} else {a=2}"));
0450: }
0451:
0452: public void testLiteralEnum() throws Exception {
0453: assertEquals("enum Season {WINTER, SPRING, SUMMER, AUTUMN}",
0454: pretty("enum Season{WINTER,SPRING,SUMMER,AUTUMN}"));
0455: }
0456:
0457: public void testLiteralExtends() throws Exception {
0458: assertEquals("class Foo extends java.util.Date {}",
0459: pretty("class Foo extends java.util.Date {}"));
0460: assertEquals("class Foo extends Bar {}",
0461: pretty("class Foo extends Bar {}"));
0462: assertEquals("interface Wibble extends Mooky{}",
0463: pretty("interface Wibble extends Mooky {}"));
0464: //todo spacing is odd, c.f. last space in class vs interface above
0465: assertEquals(
0466: "public boolean process(Set<? extends TypeElement> annotations) {println annotations}",
0467: pretty("public boolean process(Set<? extends TypeElement> annotations) {println annotations}"));
0468: }
0469:
0470: public void testLiteralFalse() throws Exception {
0471: assertEquals("if (false) {}", pretty("if (false) {}"));
0472: }
0473:
0474: public void testLiteralFinally() throws Exception {
0475: assertEquals("try {}finally {}", pretty("try {}finally {}"));
0476: }
0477:
0478: public void testLiteralFloat() throws Exception {
0479: assertEquals("float x", pretty("float x"));
0480: }
0481:
0482: public void testLiteralFor() throws Exception {
0483: assertEquals("for (i in [1, 2, 3]) {}",
0484: pretty("for (i in [1,2,3]) {}"));
0485: // check non-braced single statement
0486: assertEquals("for (i in 1..100) rotateAntiClockwise()",
0487: pretty("for (i in 1..100) rotateAntiClockwise()"));
0488: }
0489:
0490: public void testLiteralIf() throws Exception {
0491: assertEquals("if (a == b) return false",
0492: pretty("if (a==b) return false"));
0493: assertEquals("if (a == b) {}", pretty("if (a==b) {}"));
0494: }
0495:
0496: public void testLiteralImplements() throws Exception {
0497: assertEquals("class Foo implements Bar {}",
0498: pretty("class Foo implements Bar {}"));
0499: //todo the following is legal Java, but pretty strange...?
0500: assertEquals("enum EarthSeason implements Season {SPRING}",
0501: pretty("enum EarthSeason implements Season{SPRING}"));
0502: }
0503:
0504: public void testLiteralImport() throws Exception {
0505: assertEquals("import foo.Bar", pretty("import foo.Bar"));
0506: assertEquals("import mooky.*", pretty("import mooky.*"));
0507: }
0508:
0509: public void testLiteralIn() throws Exception {
0510: assertEquals("for (i in 1..10) {}",
0511: pretty("for (i in 1..10) {}"));
0512: assertEquals("if (i in myList) {}",
0513: pretty("if (i in myList) {}"));
0514: }
0515:
0516: public void testLiteralInstanceOf() throws Exception {
0517: assertEquals("if (a instanceof String) {}",
0518: pretty("if (a instanceof String) {}"));
0519: }
0520:
0521: public void testLiteralInt() throws Exception {
0522: assertEquals("int a", pretty("int a"));
0523: }
0524:
0525: public void testLiteralInterface() throws Exception {
0526: assertEquals("interface Foo{}", pretty("interface Foo{}"));
0527: }
0528:
0529: public void testLiteralLong() throws Exception {
0530: assertEquals("long a = 1", pretty("long a = 1"));
0531: }
0532:
0533: public void testLiteralNative() throws Exception {
0534: assertEquals(
0535: "public class R {public native void seek(long pos) }",
0536: pretty("public class R{public native void seek(long pos)}"));
0537: assertEquals("native foo() ", pretty("native foo()"));
0538: }
0539:
0540: public void testLiteralNew() throws Exception {
0541: assertEquals("new Foo()", pretty("new Foo()"));
0542: assertEquals("def x = new int[5]", pretty("def x = new int[5]"));
0543: }
0544:
0545: public void testLiteralNull() throws Exception {
0546: assertEquals("def foo = null", pretty("def foo=null"));
0547: }
0548:
0549: public void testLiteralPackage() throws Exception {
0550: assertEquals("package foo.bar", pretty("package foo.bar"));
0551: }
0552:
0553: public void testLiteralPrivate() throws Exception {
0554: assertEquals("private bar", pretty("private bar"));
0555: }
0556:
0557: public void testLiteralProtected() throws Exception {
0558: assertEquals("protected mooky", pretty("protected mooky"));
0559: }
0560:
0561: public void testLiteralPublic() throws Exception {
0562: assertEquals("public foo", pretty("public foo"));
0563: }
0564:
0565: public void testLiteralReturn() throws Exception {
0566: assertEquals("def foo() {return false}",
0567: pretty("def foo() { return false }"));
0568: assertEquals("void bar() {return }",
0569: pretty("void bar() {return}"));
0570: }
0571:
0572: public void testLiteralShort() throws Exception {
0573: assertEquals("short a = 1", pretty("short a = 1"));
0574: }
0575:
0576: public void testLiteralStatic() throws Exception {
0577: assertEquals("static void foo() {}",
0578: pretty("static void foo() {}"));
0579: //classes, interfaces, class/instance vars and methods
0580: assertEquals("static int bar = 1", pretty("static int bar = 1"));
0581: //todo: this should parse... assertEquals("private static <T> void foo(List<T> list){}", pretty("private static <T> void foo(List<T> list){}"));
0582: assertEquals("class Foo {static {bar = 1}}",
0583: pretty("class Foo{static {bar=1}}"));
0584: }
0585:
0586: public void testLiteralSuper() throws Exception {
0587: assertEquals("class Foo {public Foo() {super()}}",
0588: pretty("class Foo{public Foo(){super()}}"));
0589: // todo will 'super' be allowed in non-parentheses method call styles?
0590: assertEquals("class Bar {public Bar() {super 99}}",
0591: pretty("class Bar{public Bar(){super 99}}"));
0592: assertEquals("class Bar {public Bar() {super(1, 2, 3)}}",
0593: pretty("class Bar{public Bar(){super(1,2,3)}}"));
0594: assertEquals("println(super.toString())",
0595: pretty("println(super.toString())"));
0596: //todo: doesn't parse correctly... assertEquals("class Foo<T super C> {T t}",pretty("class Foo<T super C> {T t}"));
0597: }
0598:
0599: public void testLiteralSwitch() throws Exception {
0600: assertEquals("switch (foo) {case bar:x = 2}",
0601: pretty("switch(foo){case bar:x=2}"));
0602: }
0603:
0604: public void testLiteralSynchronized() throws Exception {
0605: assertEquals("synchronized foo() {}",
0606: pretty("synchronized foo(){}"));
0607: assertEquals("synchronized (t) {doStuff(t)}",
0608: pretty("synchronized (t) {doStuff(t)}"));
0609: }
0610:
0611: public void testLiteralThis() throws Exception {
0612: assertEquals("this", pretty("this"));
0613: assertEquals("this 2", pretty("this 2"));
0614: assertEquals("this()", pretty("this()"));
0615: assertEquals("this(1, 2, 3)", pretty("this(1,2,3)"));
0616: assertEquals("this.x = this.y", pretty("this.x=this.y"));
0617: }
0618:
0619: public void testLiteralThreadsafe() throws Exception {
0620: assertEquals("threadsafe foo() {}",
0621: pretty("threadsafe foo() {}"));
0622: }
0623:
0624: public void testLiteralThrow() throws Exception {
0625: assertEquals(
0626: "def foo() {if (false) throw new RuntimeException()}",
0627: pretty("def foo() {if (false) throw new RuntimeException()}"));
0628: }
0629:
0630: public void testLiteralThrows() throws Exception {
0631: //todo AntlrParserPlugin: Unexpected node type: '.' found when expecting type: an identifier
0632: assertEquals("def foo() throws java.io.IOException{}",
0633: pretty("def foo() throws java.io.IOException{}"));
0634: }
0635:
0636: public void testLiteralTransient() throws Exception {
0637: assertEquals("transient bar", pretty("transient bar"));
0638: }
0639:
0640: public void testLiteralTrue() throws Exception {
0641: assertEquals("foo = true", pretty("foo = true"));
0642: }
0643:
0644: public void testLiteralTry() throws Exception {
0645: assertEquals("try {} catch (Exception e) {}",
0646: pretty("try {} catch (Exception e) {}"));
0647: }
0648:
0649: public void testLiteralVoid() throws Exception {
0650: assertEquals("void foo() {}", pretty("void foo(){}"));
0651: }
0652:
0653: public void testLiteralVolatile() throws Exception {
0654: assertEquals("volatile mooky", pretty("volatile mooky"));
0655: }
0656:
0657: public void testLiteralWhile() throws Exception {
0658: assertEquals("while (true) {}", pretty("while(true){}"));
0659: }
0660:
0661: public void testLiteralWith() throws Exception {
0662: assertEquals("with (myObject) {x = 1}",
0663: pretty("with(myObject) {x = 1}"));
0664: }
0665:
0666: public void testLnot() throws Exception {
0667: assertEquals("if (!isRaining) {}", pretty("if (!isRaining) {}"));
0668: }
0669:
0670: public void testLor() throws Exception {
0671: assertEquals("true || false", pretty("true || false"));
0672: }
0673:
0674: public void testLparen_FAILS() throws Exception {
0675: if (notYetImplemented())
0676: return;
0677: assertEquals("for (i in (history.size() - 1)..0) {}",
0678: pretty("for (i in (history.size() - 1)..0) {}"));
0679: }
0680:
0681: public void testLt() throws Exception {
0682: assertEquals("if (3.4f < 12f) {}", pretty("if (3.4f < 12f) {}"));
0683: }
0684:
0685: public void testMapConstructor() throws Exception {
0686: assertEquals("Map foo = [:]", pretty("Map foo = [:]"));
0687: assertEquals("[a:1, b:2]", pretty("[a:1,b:2]"));
0688: }
0689:
0690: public void testMemberPointer() throws Exception {
0691: assertEquals("def x = foo.&bar()", pretty("def x=foo.&bar()"));
0692: }
0693:
0694: public void testMethodCall() throws Exception {
0695: assertEquals("foo(bar)", pretty("foo(bar)"));
0696: assertEquals("[1, 2, 3].each {println it}",
0697: pretty("[1,2,3].each{println it}"));
0698: assertEquals("foo(bar){mooky()}", pretty("foo(bar){mooky()}"));
0699: }
0700:
0701: public void testMethodDef() throws Exception {
0702: assertEquals("def foo(int bar, boolean boo) {}",
0703: pretty("def foo(int bar,boolean boo) {}"));
0704: }
0705:
0706: public void testMinus() throws Exception {
0707: assertEquals("def bar = 4 - foo", pretty("def bar=4-foo"));
0708: }
0709:
0710: public void testMinusAssign() throws Exception {
0711: assertEquals("x -= 23", pretty("x -= 23"));
0712: }
0713:
0714: public void testMod() throws Exception {
0715: assertEquals("x = 4 % 3", pretty("x = 4 % 3"));
0716: }
0717:
0718: public void testModifiers() throws Exception {
0719: assertEquals(
0720: "public static transient final native threadsafe synchronized volatile strictfp foo() {}",
0721: pretty("public static transient final native threadsafe synchronized volatile strictfp foo() {}"));
0722: }
0723:
0724: public void testModAssign() throws Exception {
0725: assertEquals("x %= 23", pretty("x %= 23"));
0726: }
0727:
0728: public void testNotEqual() throws Exception {
0729: assertEquals("a != b", pretty("a!=b"));
0730: }
0731:
0732: public void testNumBigDecimal() throws Exception {
0733: assertEquals("a = 9.8g", pretty("a =9.8g"));
0734: }
0735:
0736: public void testNumBigInt() throws Exception {
0737: assertEquals("a = 12g", pretty("a= 12g"));
0738: }
0739:
0740: public void testNumDouble() throws Exception {
0741: assertEquals("b = 34.4d", pretty("b=34.4d"));
0742: assertEquals("b = 34.4D", pretty("b=34.4D"));
0743: }
0744:
0745: public void testNumFloat() throws Exception {
0746: assertEquals("b = 34.4f", pretty("b=34.4f"));
0747: assertEquals("b = 34.4F", pretty("b=34.4F"));
0748: }
0749:
0750: public void testNumInt() throws Exception {
0751: assertEquals("a = 12", pretty("a=12"));
0752: }
0753:
0754: public void testNumLong() throws Exception {
0755: assertEquals("a = 12l", pretty("a=12l"));
0756: }
0757:
0758: public void testObjblock() throws Exception {
0759: assertEquals("class Foo {def bar}",
0760: pretty("class Foo {def bar}"));
0761: }
0762:
0763: public void testOptionalDot() throws Exception {
0764: assertEquals("foo = england.london.kings?.head",
0765: pretty("foo = england.london.kings?.head"));
0766: }
0767:
0768: public void testPackageDef() throws Exception {
0769: assertEquals("package foo.bar", pretty("package foo.bar"));
0770: }
0771:
0772: public void testParameterDef() throws Exception {
0773: //todo spacing slightly suspect with "foo( bar)"
0774: assertEquals("def foo( bar) {}", pretty("def foo(bar){}"));
0775: assertEquals("def foo(String bar, String mooky) {}",
0776: pretty("def foo(String bar, String mooky){}"));
0777: assertEquals("def c = { x -> println x}",
0778: pretty("def c = {x->println x}"));
0779: assertEquals("def c = { x, y -> println(x + y)}",
0780: pretty("def c={x,y->println(x+y)}"));
0781: assertEquals("def c = {int x,int y -> println(x + y)}",
0782: pretty("def c={int x, int y->println(x+y)}"));
0783: }
0784:
0785: public void testParameters() throws Exception {
0786: assertEquals("def foo(String bar, String mooky) {}",
0787: pretty("def foo(String bar, String mooky){}"));
0788: }
0789:
0790: public void testPlus() throws Exception {
0791: assertEquals("a + b", pretty("a+b"));
0792: }
0793:
0794: public void testPlusAssign() throws Exception {
0795: assertEquals("x += 23", pretty("x += 23"));
0796: }
0797:
0798: public void testPostDec() throws Exception {
0799: assertEquals("a--", pretty("a--"));
0800: }
0801:
0802: public void testPostInc() throws Exception {
0803: assertEquals("a++", pretty("a++"));
0804: }
0805:
0806: public void testQuestion() throws Exception {
0807: assertEquals("foo == bar?10:20", pretty("foo==bar?10:20"));
0808: assertEquals(
0809: "public boolean process(Set<? extends B> a) {println a}",
0810: pretty("public boolean process(Set<? extends B> a) {println a}"));
0811: assertEquals(
0812: "public boolean process(Set<? extends B, ? super C> a) {println a}",
0813: pretty("public boolean process(Set<? extends B, ? super C> a) {println a}"));
0814: }
0815:
0816: public void testRangeExclusive() throws Exception {
0817: assertEquals("foo[45..<89]", pretty("foo[45 ..< 89]"));
0818: }
0819:
0820: public void testRangeInclusive() throws Exception {
0821: assertEquals("foo[bar..12]", pretty("foo[bar .. 12]"));
0822: }
0823:
0824: public void testRegexpLiteral() throws Exception {
0825: assertEquals("println", pretty("println //")); // empty regexp_literal should be treated as single line comment
0826: }
0827:
0828: public void testRegexpLiteral_FAILS() throws Exception {
0829: if (notYetImplemented())
0830: return;
0831: //todo: these fail because regexp_literals are converted into string_literals on the antlr AST
0832: assertEquals("def x = /./", pretty("def x = /./"));
0833: assertEquals("def z = /blah\\s/", pretty("def z = /blah\\s/")); // actually: def z = /blah\s/
0834: }
0835:
0836: public void testRegexFind() throws Exception {
0837: assertEquals("def m = foo =~ \"bar\"",
0838: pretty("def m = foo =~ \"bar\""));
0839: assertEquals("if (foo =~ \"bar\") {}",
0840: pretty("if (foo=~\"bar\"){}"));
0841: }
0842:
0843: public void testRegexMatch() throws Exception {
0844: assertEquals("if (foo ==~ \"bar\") {}",
0845: pretty("if (foo==~\"bar\"){}"));
0846: }
0847:
0848: public void testScopeEscape() throws Exception {
0849: // todo - 31 July + 14 Dec 2006 - test fine, however this parses but causes error in AntlrParserPlugin
0850: assertEquals("println([$x, x, y])",
0851: pretty("println([$x, x, y])"));
0852: }
0853:
0854: public void testSelectSlot() throws Exception {
0855: assertEquals("def x = foo.@bar", pretty("def x = foo . @ bar"));
0856: }
0857:
0858: public void testSl() throws Exception {
0859: assertEquals("foo << 123", pretty("foo << 123"));
0860: }
0861:
0862: public void testSlAssign() throws Exception {
0863: assertEquals("foo <<= 123", pretty("foo <<= 123")); // does this operator make any sense?
0864: }
0865:
0866: public void testSlist() throws Exception {
0867: assertEquals("class Foo {private Foo() {println bar}}",
0868: pretty("class Foo {private Foo() {println bar}}"));
0869: assertEquals("if (true) {foo}", pretty("if (true) {foo}"));
0870: assertEquals("def x = foo.{bar}", pretty("def x = foo.{bar}")); // todo - inline open block is great, but it doesn't work as one would expect (yet). (c.f. with)
0871: assertEquals("def foo() {l:{x = 2}}",
0872: pretty("def foo(){l:{x=2}}")); // slist inside a method body (needed label to distinguish from a closure)
0873: assertEquals("switch (f) {case 1:break }",
0874: pretty("switch(f){case 1:break}")); // slist inside each case body...
0875: }
0876:
0877: public void testSpreadArg() throws Exception {
0878: assertEquals("myList {*name}", pretty("myList{*name}"));
0879: assertEquals("\"foo$*bar\"", pretty("\"foo$*bar\"")); // this doesn't work beyond parser (AntlrParserPlugin)
0880: assertEquals("\"foo${*bar}\"", pretty("\"foo${*bar}\"")); // this doesn't work beyond parser (AntlrParserPlugin)
0881: assertEquals("f(*[a, b, c])", pretty("f(*[a,b,c])"));
0882: assertEquals("f(*null)", pretty("f(*null)")); // equiv to f()
0883: }
0884:
0885: public void testSpreadMapArg() throws Exception {
0886: assertEquals("f(*:myMap)", pretty("f(*:myMap)"));
0887: }
0888:
0889: public void testSr() throws Exception {
0890: assertEquals("foo >> 123", pretty("foo >> 123"));
0891: }
0892:
0893: public void testSrAssign() throws Exception {
0894: assertEquals("foo >>= 123", pretty("foo >>= 123")); // does this operator make any sense?
0895: }
0896:
0897: public void testStar() throws Exception {
0898: assertEquals("import foo.*", pretty("import foo.*"));
0899: assertEquals("a*b", pretty("a*b"));
0900: }
0901:
0902: public void testStarAssign() throws Exception {
0903: assertEquals("foo *= 123", pretty("foo *= 123"));
0904: }
0905:
0906: public void testStarStar() throws Exception {
0907: assertEquals("def square = +5**2", pretty("def square=+5**2"));
0908: assertEquals("def cube = 5**3", pretty("def cube = 5**3"));
0909: }
0910:
0911: public void testStarStarAssign() throws Exception {
0912: assertEquals("cubeMe **= 3", pretty("cubeMe **= 3"));
0913: }
0914:
0915: public void testStaticImport() throws Exception {
0916: assertEquals("import static foo.Bar.mooky",
0917: pretty("import static foo.Bar.mooky"));
0918: assertEquals("import static foo.Bar.*",
0919: pretty("import static foo.Bar.*"));
0920: }
0921:
0922: public void testStaticInit() throws Exception {
0923: assertEquals("class Foo {static {println(1 + 1)}}",
0924: pretty("class Foo{static{println(1+1)}}"));
0925: }
0926:
0927: public void testStrictFp() throws Exception {
0928: assertEquals("private strictfp flibble = 1.2",
0929: pretty("private strictfp flibble = 1.2"));
0930: }
0931:
0932: public void testStringConstructor() throws Exception {
0933: assertEquals("def x = \"foo$bar\"", pretty("def x=\"foo$bar\""));
0934: assertEquals("def y = \"foo${mooky}\"",
0935: pretty("def y = \"foo${mooky}\""));
0936: }
0937:
0938: public void testStringLiteral_FAILS() throws Exception {
0939: if (notYetImplemented())
0940: return;
0941: assertEquals("\"mooky\"", pretty("\"mooky\""));
0942: assertEquals("'mooky'", pretty("'mooky'"));
0943: assertEquals("def x = '''can go over newline'''",
0944: pretty("def x = '''can go over newline'''"));
0945: assertEquals("def x = \"\"\"can go over newline\"\"\"",
0946: pretty("def x = \"\"\"can go over newline\"\"\""));
0947: //todo test newlines inside strings somehow...
0948: }
0949:
0950: public void testSuperCtorCall() throws Exception {
0951: assertEquals("class Foo {Foo(int x) {super(12, 3)}}",
0952: pretty("class Foo{Foo(int x) {super(12, 3)}}"));
0953: assertEquals("class Foo {Foo( x) {super()}}",
0954: pretty("class Foo{Foo(x) {super()}}"));
0955: // todo: above is not quite the spacing I would expect, but good enough for now...
0956: // todo not yet implemented in parser: assertEquals("(new Outer()).super()", pretty("(new Outer()).super()"));
0957: }
0958:
0959: public void testType() throws Exception {
0960: assertEquals("def bar", pretty("def bar"));
0961: assertEquals("public bar", pretty("public bar"));
0962: assertEquals("public String bar", pretty("public String bar"));
0963: assertEquals("String bar", pretty("String bar"));
0964: }
0965:
0966: public void testTypecast() throws Exception {
0967: assertEquals("foo = (int)bar", pretty("foo = (int)bar"));
0968: assertEquals("foo = (int[])bar", pretty("foo = (int[])bar"));
0969: assertEquals("foo = (String)bar", pretty("foo = (String)bar"));
0970: assertEquals("foo = (String[])bar",
0971: pretty("foo = (String[])bar"));
0972: }
0973:
0974: public void testTypeArguments() throws Exception {
0975: assertEquals("void printCollection(Collection<?> c) {}",
0976: pretty("void printCollection(Collection<?> c) {}"));
0977: }
0978:
0979: public void testTypeLowerBounds() throws Exception {
0980: assertEquals(
0981: "void printCollection(Collection<? super X> c) {}",
0982: pretty("void printCollection(Collection<? super X> c) {}"));
0983: }
0984:
0985: public void testTypeUpperBounds() throws Exception {
0986: assertEquals(
0987: "void printCollection(Collection<? extends X> c) {}",
0988: pretty("void printCollection(Collection<? extends X> c) {}"));
0989: }
0990:
0991: public void testTypeParameters() throws Exception {
0992: assertEquals("class Foo<T extends C & I> {T t}",
0993: pretty("class Foo<T extends C & I> {T t}"));
0994: }
0995:
0996: public void testUnaryMinus() throws Exception {
0997: assertEquals("def x = -3", pretty("def x= -3"));
0998: }
0999:
1000: public void testUnaryPlus() throws Exception {
1001: assertEquals("def x = +2", pretty("def x= +2"));
1002: }
1003:
1004: public void testVariableDef() throws Exception {
1005: assertEquals("def x = 1", pretty("def x = 1"));
1006: assertEquals("int y = 2", pretty("int y = 2"));
1007: assertEquals("String y", pretty("String y"));
1008: assertEquals("def foo() {int b = 9}",
1009: pretty("def foo(){int b = 9}"));
1010: }
1011:
1012: public void testVariableDef_FAILS() throws Exception {
1013: if (notYetImplemented())
1014: return;
1015: assertEquals("boolean x, y, z = false",
1016: pretty("boolean x,y,z = false"));
1017: }
1018:
1019: public void testVaribleParameterDef() throws Exception {
1020: assertEquals(
1021: "void myMethod(String param1, String ... others) {}",
1022: pretty("void myMethod(String param1, String ... others) {}"));
1023: assertEquals("void myMethod(final int ... others) {}",
1024: pretty("void myMethod(final int ... others) {}"));
1025: assertEquals("void myMethod(def ... others) {}",
1026: pretty("void myMethod(def ... others) {}"));
1027: }
1028:
1029: public void testWildcardType() throws Exception {
1030: assertEquals(
1031: "public boolean process(Set<? extends TypeElement> annotations) {println annotations}",
1032: pretty("public boolean process(Set<? extends TypeElement> annotations) {println annotations}"));
1033: }
1034:
1035: public String pretty(String input) throws Exception {
1036: GroovyRecognizer parser = null;
1037: SourceBuffer sourceBuffer = new SourceBuffer();
1038: UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(
1039: new StringReader(input), sourceBuffer);
1040: GroovyLexer lexer = new GroovyLexer(unicodeReader);
1041: unicodeReader.setLexer(lexer);
1042: parser = GroovyRecognizer.make(lexer);
1043: parser.setSourceBuffer(sourceBuffer);
1044:
1045: String[] tokenNames;
1046: tokenNames = parser.getTokenNames();
1047: parser.compilationUnit();
1048: AST ast = parser.getAST();
1049:
1050: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1051: Visitor visitor = new SourcePrinter(new PrintStream(baos),
1052: tokenNames, false);
1053: AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
1054:
1055: traverser.process(ast);
1056:
1057: return new String(baos.toByteArray());
1058: }
1059:
1060: }
|