📄 prettyprintertest.java
字号:
/*
* Created on Feb 11, 2006
*/
package org.python.pydev.parser.prettyprinter;
import java.io.IOException;
import org.python.pydev.core.IPythonNature;
import org.python.pydev.parser.PyParserTestBase;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.ast.Module;
import org.python.pydev.parser.prettyprinter.PrettyPrinter;
import org.python.pydev.parser.prettyprinter.PrettyPrinterPrefs;
import org.python.pydev.parser.prettyprinter.WriterEraser;
public class PrettyPrinterTest extends PyParserTestBase{
private static final boolean DEBUG = false;
public static void main(String[] args) {
try {
PrettyPrinterTest test = new PrettyPrinterTest();
test.setUp();
test.testFuncComment();
test.tearDown();
System.out.println("Finished");
junit.textui.TestRunner.run(PrettyPrinterTest.class);
} catch (Exception e) {
e.printStackTrace();
}
}
public void testListComp5() throws Exception {
String s =
"data = [[1,2,3],[4,5,6]]\n" +
"newdata = [[val * 2 for val in lst] for lst in data]\n";
checkPrettyPrintEqual(s);
}
public void testIfs() throws Exception {
String s = "" +
"def method1():\n" +
" if idx > 2:\n" +
" print ''\n" +
" else:\n" +
" print ''\n" +
" if idx == 5:\n" +
" print 'nothing!'\n";
checkPrettyPrintEqual(s);
}
public void testTryFinallyBeginNode() throws Exception {
doTryFinallyBeginNode(IPythonNature.GRAMMAR_PYTHON_VERSION_2_4);
doTryFinallyBeginNode(IPythonNature.GRAMMAR_PYTHON_VERSION_2_5);
}
public void doTryFinallyBeginNode(int version) throws Exception {
String str = "" +
"try:\n" +
" a = 1\n" +
"finally:\n" +
" pass\n" +
"";
SimpleNode node = checkPrettyPrintEqual(str);
Module m = (Module) node;
SimpleNode f = (SimpleNode) m.body[0];
assertEquals(1, f.beginLine);
}
public void testComments() throws Exception {
String str = "" +
"class MyMeta(type):\n" +
" def __str__(cls):\n" +
" return \"Beautiful class '%s'\" % cls.__name__\n" +
"class MyClass:\n" +
" __metaclass__ = MyMeta\n" +
"print type(foox)\n" +
"# after print type\n" +
"class A(object):# on-line\n" +
" # foo test\n" +
" def met(self):\n" +
" print 'A'\n" +
"";
checkPrettyPrintEqual(str);
}
public void testComment5() throws Exception {
String str = "" +
"class CoolApproach(object):\n" +
" # this tests also a tuple \"special case\"\n" +
" def foodeco(**arg5):\n" +
" pass\n" +
"";
checkPrettyPrintEqual(str);
}
public void testDecoration() throws Exception {
String str = "" +
"class Foo:\n" +
" @foodeco(('arg_3',),2,a=2,b=3)\n" +
" def __init__(self,arg_1,(arg_2,arg_3),arg_4,arg_5):\n" +
" pass\n" +
"";
checkPrettyPrintEqual(str);
}
public void testComments6() throws Exception {
String str = "" +
"class FooApproach(CoolApproach):\n" +
" def __init__(self,arg_1,(arg_2,arg_3),*arg_4,**arg_5):\n" +
" # .. at this point all parameters except for 'arg_3' have been\n" +
" # copied to object attributes\n" +
" pass\n" +
"";
checkPrettyPrintEqual(str);
}
public void testComprehension() throws Exception {
String str = "compre4list = [zahl ** 2 for zahl in (1,4,6) if zahl % 2 == 1 if zahl % 3 == 2]# on-line\n";
checkPrettyPrintEqual(str);
}
public void test25If() throws Exception {
String str = "a = 1 if True else 2\n";
checkPrettyPrintEqual(str);
}
public void test25Import() throws Exception {
String str = "from . import foo\n";
checkPrettyPrintEqual(str);
}
public void test25Import2() throws Exception {
String str = "from ..bar import foo\n";
checkPrettyPrintEqual(str);
}
public void test25Import3() throws Exception {
String str = "from ... import foo\n";
checkPrettyPrintEqual(str);
}
public void test25With() throws Exception {
String str = "" +
"from __future__ import with_statement\n" +
"with a:\n" +
" print a\n" +
"";
checkPrettyPrintEqual(str);
}
public void test25With2() throws Exception {
String str = "" +
"from __future__ import with_statement\n" +
"with a as b:\n" +
" print b\n" +
"";
checkPrettyPrintEqual(str);
}
public void test25With3() throws Exception {
String str = "" +
"from __future__ import with_statement\n" +
"def m1():\n" +
" with a as b:\n" +
" print b\n" +
"";
checkPrettyPrintEqual(str);
}
public void test25With4() throws Exception {
String str = "" +
"from __future__ import with_statement\n" +
"with a:\n" +
" callIt1()\n" +
" callIt2()\n" +
"";
checkPrettyPrintEqual(str);
}
public void testGlobal() throws Exception {
String s = ""+
"global foo\n" +
"";
checkPrettyPrintEqual(s);
}
public void testImport() throws Exception {
String s = ""+
"import foo\n" +
"";
checkPrettyPrintEqual(s);
}
public void testImport6() throws Exception {
String s = ""+
"#foo\n" +
"from hashlib import md5\n" +
"new = md5\n" +
"";
checkPrettyPrintEqual(s);
}
public void testKwArgs2() throws Exception {
String s = ""+
"HTTPS(host,None,**(x509 or {}))\n" +
"";
checkPrettyPrintEqual(s);
}
public void testImport5() throws Exception {
String s = ""+
"import foo,bar\n" +
"";
checkPrettyPrintEqual(s);
}
public void testLambda3() throws Exception {
String s = ""+
"lambda a:(1 + 2)\n" +
"";
checkPrettyPrintEqual(s);
}
public void testLambda4() throws Exception {
String s = ""+
"lambda d='':digestmod.new(d)\n" +
"";
checkPrettyPrintEqual(s);
}
public void testComment4() throws Exception {
String s = ""+
"class AAA:\n" +
" def m1(self):\n" +
" pass\n" +
"#--- barrr\n" +
"a = 10\n" +
"#--- fooo" +
"";
checkPrettyPrintEqual(s);
}
public void testComment3() throws Exception {
String s = ""+
"class Foo:\n" +
" pass\n" +
"#--- barrr\n" +
"a = 10\n" +
"#--- fooo" +
"";
checkPrettyPrintEqual(s);
}
public void testLambda2() throws Exception {
String s = ""+
"a = lambda:None\n" +
"";
checkPrettyPrintEqual(s);
}
public void testDict3() throws Exception {
String s = ""+
"d = {#comm1\n" +
" 1:2}\n" +
"";
checkPrettyPrintEqual(s);
}
public void testFuncAndComment2() throws Exception {
String s = ""+
"class Foo:\n" +
" def func1(self):\n" +
" pass\n" +
" # ------ Head elements\n" +
" def func2(self):\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testFuncAndComment() throws Exception {
String s = ""+
"class Foo:\n" +
" def func1(self):pass\n" +
" # ------ Head elements\n" +
" def func2(self):pass\n" +
"";
String expected = ""+
"class Foo:\n" +
" def func1(self):\n" +
" pass\n" +
" # ------ Head elements\n" +
" def func2(self):\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s, expected);
}
public void testSubscript4() throws Exception {
String s = ""+
"print a[b:c()]\n" +
"";
checkPrettyPrintEqual(s);
}
public void testAssign3() throws Exception {
String s = ""+
"a = b = 0\n" +
"";
checkPrettyPrintEqual(s);
}
public void testComment1() throws Exception {
String s = ""+
"del a[-1]#comment\n" +
"";
checkPrettyPrintEqual(s);
}
public void testArgs() throws Exception {
String s = ""+
"def func():\n" +
" return a(*b)\n" +
"";
checkPrettyPrintEqual(s);
}
public void testArgs2() throws Exception {
String s = ""+
"def func():\n" +
" return a(*(b))\n" +
"";
checkPrettyPrintEqual(s);
}
public void testListComp4() throws Exception {
String s = ""+
"print [e for e in group if e[0] in a]\n" +
"";
checkPrettyPrintEqual(s);
}
public void testReturn3() throws Exception {
String s = ""+
"if a:\n" +
" return foo(other)#comment\n" +
"shouldround = 1\n" +
"";
checkPrettyPrintEqual(s);
}
public void testAssert() throws Exception {
String s = ""+
"assert a not in b\n" +
"";
checkPrettyPrintEqual(s);
}
public void testStr2() throws Exception {
String s = ""+
"r\"foo\"\n" +
"";
checkPrettyPrintEqual(s);
}
public void testStr() throws Exception {
String s = ""+
"a = (r\"a\"#comm1\n" +
" r'\"b\"'#comm2\n" +
" )\n" +
"";
checkPrettyPrintEqual(s);
}
public void testAdd() throws Exception {
String s = ""+
"m += 'a'\n" +
"";
checkPrettyPrintEqual(s);
}
public void testWildImport() throws Exception {
String s = ""+
"from a import *\n" +
"";
checkPrettyPrintEqual(s);
}
public void testRaise() throws Exception {
String s = ""+
"try:\n" +
" pass\n" +
"except:\n" +
" raise SystemError,'err'\n" +
"";
checkPrettyPrintEqual(s);
}
public void testLambda() throws Exception {
String s = ""+
"print lambda n:n\n" +
"";
checkPrettyPrintEqual(s);
}
public void testListComp3() throws Exception {
String s = ""+
"print [s2 for s1 in b for s2 in a]\n" +
"";
checkPrettyPrintEqual(s);
}
public void testList2() throws Exception {
String s = ""+
"print [(a,b)]\n" +
"";
checkPrettyPrintEqual(s);
}
public void testList3() throws Exception {
String s = ""+
"all = [#comm1\n" +
" 'encode','decode',]\n" +
"";
checkPrettyPrintEqual(s);
}
public void testListComp2() throws Exception {
String s = ""+
"for (raw,cooked) in foo:\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testKwArgs() throws Exception {
String s = ""+
"def a(**kwargs):\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testTryExcept9() throws Exception {
String s = ""+
"def run():\n" +
" try:\n" +
" exec cmd\n" +
" except BdbQuit:\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testSubscript3() throws Exception {
String s = ""+
"for a in b[:]:\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testEndComments() throws Exception {
String s = ""+
"import foo\n" +
"#end" +
"";
checkPrettyPrintEqual(s);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -