sourceprintertest.java

来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· Java 代码 · 共 870 行 · 第 1/3 页

JAVA
870
字号
/**
 *
 * Copyright 2005 Jeremy Rayner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **/
package org.codehaus.groovy.antlr.treewalker;

import groovy.util.GroovyTestCase;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringReader;

import org.codehaus.groovy.antlr.AntlrASTProcessor;
import org.codehaus.groovy.antlr.SourceBuffer;
import org.codehaus.groovy.antlr.UnicodeEscapingReader;
import org.codehaus.groovy.antlr.parser.GroovyLexer;
import org.codehaus.groovy.antlr.parser.GroovyRecognizer;

import antlr.collections.AST;

/**
 * Testcases for the antlr AST visitor that prints groovy source code.
 *
 * @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
 * @version $Revision: 4653 $
 */
public class SourcePrinterTest extends GroovyTestCase {

	public void testAbstract() throws Exception {
		assertEquals("public abstract class Foo {}", pretty("public abstract class Foo{}"));
	}
	
    public void testAnnotation() throws Exception{
        assertEquals("@Crimson foo", pretty("@Crimson foo"));
        assertEquals("@Override int hashCode() {return 0}", pretty("@Override int hashCode() {return 0}"));
    }
    
    public void testAnnotations() throws Exception{
    	assertEquals("@Important @Blue package foo.bar",pretty("@Important @Blue package foo.bar"));
    }

    public void testAnnotationArrayInit() throws Exception{
    	// obsolete java syntax
    }
    
    public void testAnnotationDef() throws Exception{
    	// todo - 17 July 2006 - test fine, however this parses but causes error in AntlrParserPlugin
        assertEquals("public @interface Foo{}", pretty("public @interface Foo{}"));
    }
    
    public void testAnnotationFieldDef() throws Exception{
    	assertEquals("public @interface Foo{int bar() default 123}", pretty("public @interface Foo{int bar() default 123}"));
    }
    
    public void testAnnotationMemberValuePair() throws Exception{
    	assertEquals("@Prime(value = 17) int foo",pretty("@Prime(value=17) int foo"));
    	assertEquals("@Blue(v = 3, v = 5) int bar",pretty("@Blue(v = 3, v = 5) int bar"));
    }
    
    public void testArrayDeclarator() throws Exception {
    	assertEquals("int[] primes = new int[5]", pretty("int[] primes = new int[5]"));
    }
    
    public void testAssign() throws Exception {
        assertEquals("a = 12", pretty("a=12"));
    }
    
    public void testBand() throws Exception {
    	assertEquals("def x = 1 & 2", pretty("def x=1&2"));
    }

    public void testBandAssign() throws Exception {
    	assertEquals("x &= 2", pretty("x&=2"));
    }
    
    public void testBnot() throws Exception {
    	assertEquals("def z = ~123", pretty("def z = ~123"));
    }
    
    public void testBor() throws Exception {
    	assertEquals("def y = 1 | 2", pretty("def y = 1 | 2"));
    }
    
    public void testBorAssign() throws Exception {
    	assertEquals("y |= 2", pretty("y|=2"));
    }

    public void testBsr() throws Exception {
    	// unsigned right shift
    	assertEquals("def q = 1 >>> 2", pretty("def q = 1 >>> 2"));
    }

    public void testBsrAssign() throws Exception {
    	assertEquals("y >>>= 2", pretty("y>>>=2"));
    }
    
    public void testBxor() throws Exception {
    	assertEquals("def y = true ^ false", pretty("def y = true ^ false"));
    }
    
    public void testBxorAssign() throws Exception {
    	assertEquals("y ^= false", pretty("y^=false"));
    }

    public void testCaseGroup() throws Exception {
        assertEquals("switch (foo) {case bar:x = 1}", pretty("switch(foo){case bar:x=1}"));
    }
    
    public void testClassDef() throws Exception {
        assertEquals("class Foo {def bar}", pretty("class Foo{def bar}"));
    }

    public void testClosedBlock() throws Exception{
        assertEquals("[1, 2, 3].each {println it}", pretty("[1,2,3].each{println it}"));
        assertEquals("def x = foo.bar(mooky){ x, y -> wibble(y, x)}", pretty("def x = foo.bar(mooky) {x,y-> wibble(y,x)}"));
        // todo: above is not quite the spacing I would expect, but good enough for now...
    }
    
    public void testCompareTo() throws Exception{
    	assertEquals("1 <=> 2", pretty("1<=>2"));
    }
    
    public void testCtorCall() throws Exception{
    	assertEquals("class Foo {Foo(int x) {this()}}",pretty("class Foo{Foo(int x) {this()}}"));
    	assertEquals("class Foo {Foo( x) {this()}}",pretty("class Foo{Foo(x) {this()}}"));
        // todo: above is not quite the spacing I would expect, but good enough for now...
    }
    
    public void testCtorIdent() throws Exception {
        assertEquals("class Foo {private Foo() {}}", pretty("class Foo {private Foo() {}}"));
    }
    
    public void testDec() throws Exception {
    	assertEquals("--b", pretty("--b"));
    }

    public void testDiv() throws Exception {
    	assertEquals("1 / 2", pretty("1/2"));
    }
    
    public void testDivAssign() throws Exception {
    	assertEquals("x /= 2", pretty("x/=2"));
    }
    
    public void testDot() throws Exception {
    	assertEquals("java.util.Date d = new java.util.Date()", pretty("java.util.Date d = new java.util.Date()"));
    	assertEquals("class Foo extends java.util.Date {}", pretty("class Foo extends java.util.Date {}"));
    	assertEquals("foo.bar.mooky()", pretty("foo.bar.mooky()"));
    	assertEquals("package foo.bar", pretty("package foo.bar"));
    	assertEquals("import java.util.Date", pretty("import java.util.Date"));
    	assertEquals("import java.io.*", pretty("import java.io.*"));
    	assertEquals("@foo.Bar mooky", pretty("@foo.Bar mooky"));
    	assertEquals("def foo() throws bar.MookyException{}", pretty("def foo() throws bar.MookyException{}"));
    	assertEquals("def x = \"${foo.bar}\"", pretty("def x = \"${foo.bar}\""));
    }
    
    public void testDynamicMember() throws Exception{
    	assertEquals("foo.(bar)", pretty("foo.(bar)"));
    	assertEquals("foo.\"${bar}\"", pretty("foo.\"${bar}\""));
    }
    
    public void testElist() throws Exception {
    	assertEquals("println 2 + 2", pretty("println 2 + 2"));
    	assertEquals("for (i = 0, j = 2 ; i < 10 ; i++, j--){print i}", pretty("for (i = 0,j = 2;i < 10; i++, j--) {print i}"));
    	assertEquals("foo()", pretty("foo()")); // empty ELIST
    	assertEquals("foo(bar, mooky)", pretty("foo( bar , mooky )"));
    }

    public void testEnumConstantDef() throws Exception {
    	assertEquals("enum Coin {PENNY(1), DIME(10), QUARTER(25)}", pretty("enum Coin {PENNY(1), DIME(10), QUARTER(25)}"));
    }

    public void testEnumDef() throws Exception {
    	assertEquals("enum Season {WINTER, SPRING, SUMMER, AUTUMN}", pretty("enum Season{WINTER,SPRING,SUMMER,AUTUMN}"));
    	//todo: more strange spacing in following line
    	assertEquals("enum Operation {ADDITION{double eval( x,  y) {return x + y}}}",pretty("enum Operation {ADDITION {double eval(x,y) {return x + y}}}"));    	
    }
    
    public void testEqual() throws Exception {
        assertEquals("a == b", pretty("a==b"));
    }

    public void testEsc_FAILS() throws Exception { if (notYetImplemented()) return;
    	// dquote-tab-dquote
    	assertEquals("println \"\\\"\t\\\"\"", pretty("println \"\\\"\t\\\"\""));
    }
    
    public void testExponent() throws Exception {
    	assertEquals("println 1.2e-10", pretty("println 1.2e-10"));
    }
    
    public void testExpr_FAILS() throws Exception { if (notYetImplemented()) return;
    	assertEquals("System.out.println(x)", pretty("System.out.println(x)"));
    	assertEquals("return f", pretty("return f"));
        assertEquals("foo(bar);mooky(bar)", pretty("foo(bar);mooky(bar)"));
    }

    public void testExtendsClause() throws Exception {
        assertEquals("class Foo extends Bar {}", pretty("class Foo extends Bar {}"));
        assertEquals("interface Wibble extends Mooky{}", pretty("interface Wibble extends Mooky {}"));
        //todo spacing is odd, c.f. last space in class vs interface above
    }
    
    public void testFinal() throws Exception {
    	assertEquals("public final int getX() {return 0}", pretty("public final int getX() {return 0}"));
    }
    public void testForCondition() throws Exception {
    	assertEquals("for (i = 0 ; i < 10 ; i++){println i}", pretty("for (i=0;i<10;i++) {println i}"));
    }
    
    // testForInit() covered by testForCondition()
    
    public void testForInIterable() throws Exception {
        assertEquals("for (i in [1, 2]) {}", pretty("for (i in [1,2]) {}"));
    }

    // testForIterator() covered by testForCondition()
    
    public void testGe() throws Exception {
    	assertEquals("if (60 >= 70) {}", pretty("if (60>=70) {}"));
    }
    
    public void testGt() throws Exception {
        assertEquals("if (2070 > 354) {}", pretty("if (2070 > 354) {}"));
    }

    public void testHexDigit() throws Exception {
        assertEquals("def bar = 0xCaFe", pretty("def bar = 0xCaFe"));    	
    }
    public void testHexDigitInUnicodeEscape_FAILS() throws Exception { if (notYetImplemented()) return;
      assertEquals("def foo = '\\ubabe'", pretty("def foo = '\\ubabe'"));
    }
    
    public void testIdent() throws Exception {
    	// used _everywhere_ , lets assume that the other specific 
    	// testcases include enough ident usage for now.
        assertEquals("foo.bar", pretty("foo.bar"));
    }

    public void testImplementsClause() throws Exception {
        assertEquals("class Foo implements Bar {}", pretty("class Foo implements Bar {}"));
    }

    public void testImplicitParameters() throws Exception {
    	assertEquals("[1, 2, 3].each {println it}", pretty("[1,2,3].each{println it}"));
    }
    
    public void testImport() throws Exception {
        assertEquals("import foo.bar.Wibble", pretty("import foo.bar.Wibble"));
    }
    
    public void testInc() throws Exception {
    	assertEquals("++x",pretty("++x"));
    }

    public void testIndexOp() throws Exception {
        assertEquals("foo.bar()[fred.wilma()]", pretty("foo.bar()[fred.wilma()]"));
    }
    
    public void testInterfaceDef() throws Exception {
    	//todo, the spacing here is... unusual
    	assertEquals("interface Foo{void blah() }", pretty("interface Foo{void blah()}"));
    }

    public void testInstanceInit() throws Exception {
    	assertEquals("class Foo {{x = 1}}", pretty("class Foo {{x=1}}"));
    }

    public void testLabeledArg() throws Exception {
        assertEquals("myMethod(argOne:123, argTwo:123)", pretty("myMethod(argOne:123,argTwo:123)"));
        assertEquals("myMap = [keyOne:123, keyTwo:234]", pretty("myMap = [keyOne:123,keyTwo:234]"));
    }

    public void testLabeledStat() throws Exception {
     	assertEquals("foo:x = 1", pretty("foo:x = 1"));
    }
    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?