sourceprintertest.java

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

JAVA
870
字号
    public void testMethodDef() throws Exception{
        assertEquals("def foo(int bar, boolean boo) {}", pretty("def foo(int bar,boolean boo) {}"));
    }

    public void testMinus() throws Exception {
        assertEquals("def bar = 4 - foo", pretty("def bar=4-foo"));
    }

    public void testMinusAssign() throws Exception {
        assertEquals("x -= 23", pretty("x -= 23"));
    }

    public void testMod() throws Exception {
        assertEquals("x = 4 % 3", pretty("x = 4 % 3"));
    }

    public void testModifiers() throws Exception {
    	assertEquals("public static transient final native threadsafe synchronized volatile strictfp foo() {}", pretty("public static transient final native threadsafe synchronized volatile strictfp foo() {}"));
    }
    
    public void testModAssign() throws Exception {
        assertEquals("x %= 23", pretty("x %= 23"));
    }

    public void testNotEqual() throws Exception {
        assertEquals("a != b", pretty("a!=b"));
    }

    public void testNumBigDecimal() throws Exception {
    	assertEquals("a = 9.8g", pretty("a  =9.8g"));
    }
    public void testNumBigInt() throws Exception {
    	assertEquals("a = 12g", pretty("a=   12g"));
    }
    
    public void testNumDouble() throws Exception {
        assertEquals("b = 34.4d", pretty("b=34.4d"));
        assertEquals("b = 34.4D", pretty("b=34.4D"));
    }
    public void testNumFloat() throws Exception {
        assertEquals("b = 34.4f", pretty("b=34.4f"));
        assertEquals("b = 34.4F", pretty("b=34.4F"));
    }
    public void testNumInt() throws Exception {
        assertEquals("a = 12", pretty("a=12"));
    }

    public void testNumLong() throws Exception {
    	assertEquals("a = 12l", pretty("a=12l"));    	
    }
    public void testObjblock() throws Exception {
        assertEquals("class Foo {def bar}", pretty("class Foo {def bar}"));
    }
    public void testOptionalDot() throws Exception {
        assertEquals("foo = england.london.kings?.head", pretty("foo = england.london.kings?.head"));
    }

    public void testPackageDef() throws Exception {
        assertEquals("package foo.bar", pretty("package foo.bar"));
    }

    public void testParameterDef() throws Exception {
    	//todo spacing slightly suspect with "foo( bar)"
    	assertEquals("def foo( bar) {}", pretty("def foo(bar){}"));
        assertEquals("def foo(String bar, String mooky) {}", pretty("def foo(String bar, String mooky){}"));
        assertEquals("def c = { x -> println x}", pretty("def c = {x->println x}"));
        assertEquals("def c = { x, y -> println(x + y)}", pretty("def c={x,y->println(x+y)}"));
        assertEquals("def c = {int x,int y -> println(x + y)}", pretty("def c={int x, int y->println(x+y)}"));
    }

    public void testParameters() throws Exception {
        assertEquals("def foo(String bar, String mooky) {}", pretty("def foo(String bar, String mooky){}"));
    }
    public void testPlus() throws Exception {
        assertEquals("a + b", pretty("a+b"));
    }
    
    public void testPlusAssign() throws Exception {
        assertEquals("x += 23", pretty("x += 23"));
    }

    public void testPostDec() throws Exception {
    	assertEquals("a--", pretty("a--"));
    }
    public void testPostInc() throws Exception {
    	assertEquals("a++", pretty("a++"));
    }
    public void testQuestion() throws Exception {
        assertEquals("foo == bar?10:20", pretty("foo==bar?10:20"));
    	assertEquals("public boolean process(Set<? extends B> a) {println a}", pretty("public boolean process(Set<? extends B> a) {println a}"));
    	assertEquals("public boolean process(Set<? extends B, ? super C> a) {println a}", pretty("public boolean process(Set<? extends B, ? super C> a) {println a}"));
    }
    public void testRangeExclusive() throws Exception {
        assertEquals("foo[45..<89]", pretty("foo[45 ..< 89]"));
    }
    public void testRangeInclusive() throws Exception {
        assertEquals("foo[bar..12]", pretty("foo[bar .. 12]"));
    }
    public void testRegexpLiteral() throws Exception {
    	assertEquals("println", pretty("println //")); // empty regexp_literal should be treated as single line comment
    }

    public void testRegexpLiteral_FAILS() throws Exception { if (notYetImplemented()) return;
		//todo: these fail because regexp_literals are converted into string_literals on the antlr AST
		assertEquals("def x = /./", pretty("def x = /./"));
		assertEquals("def z = /blah\\s/", pretty("def z = /blah\\s/")); // actually: def z = /blah\s/
    }
    
    public void testRegexFind() throws Exception {
    	assertEquals("def m = foo =~ \"bar\"", pretty("def m = foo =~ \"bar\""));
    	assertEquals("if (foo =~ \"bar\") {}", pretty("if (foo=~\"bar\"){}"));
    }
    
    public void testRegexMatch() throws Exception {
    	assertEquals("if (foo ==~ \"bar\") {}", pretty("if (foo==~\"bar\"){}"));
    }
    
    public void testScopeEscape() throws Exception {
    	// todo - 31 July + 14 Dec 2006 - test fine, however this parses but causes error in AntlrParserPlugin
    	assertEquals("println([$x, x, y])", pretty("println([$x, x, y])"));
    }
    
    public void testSelectSlot() throws Exception {
    	assertEquals("def x = foo.@bar", pretty("def x = foo . @ bar"));
    }
    
    public void testSl() throws Exception {
    	assertEquals("foo << 123", pretty("foo << 123"));
    }
    
    public void testSlAssign() throws Exception {
    	assertEquals("foo <<= 123", pretty("foo <<= 123")); // does this operator make any sense?
    }

    public void testSlist() throws Exception {
    	assertEquals("class Foo {private Foo() {println bar}}",pretty("class Foo {private Foo() {println bar}}"));
    	assertEquals("if (true) {foo}", pretty("if (true) {foo}"));
    	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)
    	assertEquals("def foo() {l:{x = 2}}", pretty("def foo(){l:{x=2}}")); // slist inside a method body (needed label to distinguish from a closure)
    	assertEquals("switch (f) {case 1:break }", pretty("switch(f){case 1:break}")); // slist inside each case body...
    } 
    
    public void testSpreadArg() throws Exception {
    	assertEquals("myList {*name}", pretty("myList{*name}"));
        assertEquals("\"foo$*bar\"", pretty("\"foo$*bar\"")); // this doesn't work beyond parser (AntlrParserPlugin)
        assertEquals("\"foo${*bar}\"", pretty("\"foo${*bar}\"")); // this doesn't work beyond parser (AntlrParserPlugin)
        assertEquals("f(*[a, b, c])", pretty("f(*[a,b,c])"));
        assertEquals("f(*null)", pretty("f(*null)")); // equiv to f()
    }
    public void testSpreadMapArg() throws Exception {
    	assertEquals("f(*:myMap)", pretty("f(*:myMap)"));
    }
    public void testSr() throws Exception {
    	assertEquals("foo >> 123", pretty("foo >> 123"));
    }
    
    public void testSrAssign() throws Exception {
    	assertEquals("foo >>= 123", pretty("foo >>= 123")); // does this operator make any sense?
    }

    public void testStar() throws Exception {
        assertEquals("import foo.*", pretty("import foo.*"));
        assertEquals("a*b", pretty("a*b"));
    }
    
    public void testStarAssign() throws Exception {
    	assertEquals("foo *= 123", pretty("foo *= 123"));
    }

    public void testStarStar() throws Exception {
        assertEquals("def square = +5**2", pretty("def square=+5**2"));
        assertEquals("def cube = 5**3", pretty("def cube = 5**3"));
    } 
    
    public void testStarStarAssign() throws Exception {
    	assertEquals("cubeMe **= 3", pretty("cubeMe **= 3"));
    }

    public void testStaticImport() throws Exception {
    	assertEquals("import static foo.Bar.mooky", pretty("import static foo.Bar.mooky"));
    	assertEquals("import static foo.Bar.*", pretty("import static foo.Bar.*"));
    }

    public void testStaticInit() throws Exception {
    	assertEquals("class Foo {static {println(1 + 1)}}", pretty("class Foo{static{println(1+1)}}"));
    }
    
    public void testStrictFp() throws Exception {
    	assertEquals("private strictfp flibble = 1.2", pretty("private strictfp flibble = 1.2"));
    }
    
    public void testStringConstructor() throws Exception{
        assertEquals("def x = \"foo$bar\"", pretty("def x=\"foo$bar\""));
        assertEquals("def y = \"foo${mooky}\"", pretty("def y = \"foo${mooky}\""));
    }

    public void testStringLiteral_FAILS() throws Exception{ if (notYetImplemented()) return;
        assertEquals("\"mooky\"", pretty("\"mooky\""));
        assertEquals("'mooky'", pretty("'mooky'"));
    	assertEquals("def x = '''can go over newline'''", pretty("def x = '''can go over newline'''"));
    	assertEquals("def x = \"\"\"can go over newline\"\"\"", pretty("def x = \"\"\"can go over newline\"\"\""));
    	//todo test newlines inside strings somehow...
    }

    public void testSuperCtorCall() throws Exception{
    	assertEquals("class Foo {Foo(int x) {super(12, 3)}}",pretty("class Foo{Foo(int x) {super(12, 3)}}"));
    	assertEquals("class Foo {Foo( x) {super()}}",pretty("class Foo{Foo(x) {super()}}"));
        // todo: above is not quite the spacing I would expect, but good enough for now...
    	// todo not yet implemented in parser: assertEquals("(new Outer()).super()", pretty("(new Outer()).super()"));
    }
    
    public void testType() throws Exception {
    	assertEquals("def bar", pretty("def bar"));
        assertEquals("public bar", pretty("public bar"));
        assertEquals("public String bar", pretty("public String bar"));
        assertEquals("String bar", pretty("String bar"));
    }

    public void testTypecast() throws Exception {
        assertEquals("foo = (int)bar", pretty("foo = (int)bar"));
        assertEquals("foo = (int[])bar", pretty("foo = (int[])bar"));
        assertEquals("foo = (String)bar", pretty("foo = (String)bar"));
        assertEquals("foo = (String[])bar", pretty("foo = (String[])bar"));
    }

    public void testTypeArguments() throws Exception {
    	assertEquals("void printCollection(Collection<?> c) {}", pretty("void printCollection(Collection<?> c) {}"));
    }
    public void testTypeLowerBounds() throws Exception {
    	assertEquals("void printCollection(Collection<? super X> c) {}", pretty("void printCollection(Collection<? super X> c) {}"));
    }
    public void testTypeUpperBounds() throws Exception {
    	assertEquals("void printCollection(Collection<? extends X> c) {}", pretty("void printCollection(Collection<? extends X> c) {}"));
    }

    public void testTypeParameters() throws Exception {
    	assertEquals("class Foo<T extends C & I> {T t}",pretty("class Foo<T extends C & I> {T t}"));
    }
    public void testUnaryMinus() throws Exception {
        assertEquals("def x = -3", pretty("def x= -3"));
    }
    public void testUnaryPlus() throws Exception {
        assertEquals("def x = +2", pretty("def x= +2"));
    }

    public void testVariableDef() throws Exception {
        assertEquals("def x = 1", pretty("def x = 1"));
        assertEquals("int y = 2", pretty("int y = 2"));
        assertEquals("String y", pretty("String y"));
        assertEquals("def foo() {int b = 9}", pretty("def foo(){int b = 9}"));
    }
    public void testVariableDef_FAILS() throws Exception { if (notYetImplemented()) return;
    	assertEquals("boolean x, y, z = false", pretty("boolean x,y,z = false"));       
    }
    
    public void testVaribleParameterDef() throws Exception {
    	assertEquals("void myMethod(String param1, String ... others) {}", pretty("void myMethod(String param1, String ... others) {}"));
    	assertEquals("void myMethod(final int ... others) {}", pretty("void myMethod(final int ... others) {}"));
    	assertEquals("void myMethod(def ... others) {}", pretty("void myMethod(def ... others) {}"));
    }
    
    public void testWildcardType() throws Exception {
    	assertEquals("public boolean process(Set<? extends TypeElement> annotations) {println annotations}", pretty("public boolean process(Set<? extends TypeElement> annotations) {println annotations}"));
    }

    public String pretty(String input) throws Exception{
        GroovyRecognizer parser = null;
        SourceBuffer sourceBuffer = new SourceBuffer();
        UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new StringReader(input),sourceBuffer);
        GroovyLexer lexer = new GroovyLexer(unicodeReader);
        unicodeReader.setLexer(lexer);
        parser = GroovyRecognizer.make(lexer);
        parser.setSourceBuffer(sourceBuffer);

        String[] tokenNames;
        tokenNames = parser.getTokenNames();
        parser.compilationUnit();
        AST ast = parser.getAST();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Visitor visitor = new SourcePrinter(new PrintStream(baos),tokenNames,false);
        AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);

        traverser.process(ast);

        return new String(baos.toByteArray());
    }

}

⌨️ 快捷键说明

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