📄 prettyprintertest.java
字号:
"a = {1:'foo'}\n" +
"";
checkPrettyPrintEqual(s);
}
public void testTupleDict() throws Exception {
String s = ""+
"a = (1,#this is 1\n" +
" 2)\n" +
"a = {1:'foo'}\n" +
"";
checkPrettyPrintEqual(s);
}
public void testDict2() throws Exception {
String s = ""+
"a = {1:2,#this is 1\n" +
" 2:2}\n" +
"a = {1:'foo'}\n" +
"";
checkPrettyPrintEqual(s);
}
public void testVarious() throws Exception {
String s = ""+
"class Foo:\n" +
" def __init__(self,a,b):\n" +
" print self,#comment0\n" +
" a,\n" +
" b\n" +
" def met1(self,a):#ok comment1\n" +
" a,\n" +
" b\n" +
" class Inner(object):\n" +
" pass\n" +
" self.met1(a)\n" +
"print 'ok'\n" +
"";
checkPrettyPrintEqual(s);
}
public void testYield() throws Exception {
String s = ""+
"def foo():\n" +
" yield 10\n" +
"print 'foo'\n" +
"a = 3\n" +
"";
checkPrettyPrintEqual(s);
}
public void testYield2() throws Exception {
String s = ""+
"def foo():\n" +
" yield (10)#comment1\n" +
" print 'foo'\n" +
"";
checkPrettyPrintEqual(s);
}
public void testYield4() throws Exception {
String s = ""+
"def foo():\n" +
" yield ((a + b) / 2)#comment1\n" +
" print 'foo'\n" +
"";
checkPrettyPrintEqual(s);
}
public void testMultipleBool() throws Exception {
String s = "X or Y and X and Y or X\n";
checkPrettyPrintEqual(s);
}
public void testFuncComment() throws Exception {
String s = ""+
"def foo():\n" +
" #comment0\n" +
" print 'foo'\n" +
"";
checkPrettyPrintEqual(s);
}
public void testPrint() throws Exception {
String s = ""+
"print >> a,'foo'\n" +
"";
checkPrettyPrintEqual(s);
}
public void testPrintComment() throws Exception {
String s = ""+
"def test():#comm1\n" +
" print >> (a,#comm2\n" +
" 'foo')#comm3\n" +
"";
checkPrettyPrintEqual(s);
}
public void testAttr() throws Exception {
String s = ""+
"print a.b\n" +
"";
checkPrettyPrintEqual(s);
}
public void testAttr2() throws Exception {
String s = ""+
"print a.b.c.d\n" +
"";
checkPrettyPrintEqual(s);
}
public void testAttr3() throws Exception {
String s = ""+
"print a.d()\n" +
"";
checkPrettyPrintEqual(s);
}
public void testAttr4() throws Exception {
String s = ""+
"hub.fun#comment\n" +
"";
checkPrettyPrintEqual(s);
}
public void testAttrCall() throws Exception {
String s = ""+
"print a.d().e(1 + 2)\n" +
"";
checkPrettyPrintEqual(s);
}
public void testSubscript() throws Exception {
String s = ""+
"print a[0]\n" +
"";
checkPrettyPrintEqual(s);
}
public void testSubscript2() throws Exception {
String s = ""+
"[0]\n" +
"";
checkPrettyPrintEqual(s);
}
public void testDefaults() throws Exception {
String s = ""+
"def defaults(hi=None):\n" +
" if False:\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testDefaults2() throws Exception {
String s = ""+
"def defaults(a,x,lo=foo,hi=None):\n" +
" if hi is None:\n" +
" hi = a\n" +
"";
checkPrettyPrintEqual(s);
}
public void testNoComments() throws Exception {
String s = ""+
"class Class1:\n" +
" def met1(self,a):\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testDocStrings() throws Exception {
String s = ""+
"class Class1:\n" +
" '''docstring1'''\n" +
" a = '''str1'''\n" +
" def met1(self,a):\n" +
" '''docstring2\n" +
" foo\n" +
" '''\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testDocStrings2() throws Exception {
String s = ""+
"class Class1:\n" +
" \"\"\"docstring1\"\"\"\n" +
" a = 'str1'\n" +
" def met1(self,a):\n" +
" \"docstring2\"\n" +
" ur'unicoderaw'\n" +
"";
checkPrettyPrintEqual(s);
}
public void testDocStrings3() throws Exception {
String s = ""+
"class Class1:\n" +
" def met1(self,a):\n" +
" ur'unicoderaw' + 'foo'\n" +
"";
checkPrettyPrintEqual(s);
}
public void testDict() throws Exception {
String s = ""+
"if a:\n"+
" a = {a:1,b:2,c:3}\n";
checkPrettyPrintEqual(s);
}
public void testList() throws Exception {
String s = ""+
"if a:\n"+
" a = [a,b,c]\n";
checkPrettyPrintEqual(s);
}
public void testTuple() throws Exception {
String s = ""+
"if a:\n"+
" a = (a,b,c)\n";
checkPrettyPrintEqual(s);
}
public void testTuple2() throws Exception {
String s = ""+
"if a:\n"+
" a = (a,b,#comment\n" +
" c)\n";
checkPrettyPrintEqual(s);
}
public void testIfElse0() throws Exception {
String s = ""+
"if a:\n"+
" a = 1\n"+
"elif b:\n"+
" b = 2\n"+
"elif c:\n"+
" c = 3#foo\n";
checkPrettyPrintEqual(s);
}
public void testIfElse1() throws Exception {
String s = ""+
"if a:\n"+
" a = 1\n"+
"elif b:\n"+
" b = 2\n"+
"elif c:\n"+
" c = 3\n"+
"else:\n"+
" d = 4\n";
checkPrettyPrintEqual(s);
}
public void testIfElse2() throws Exception {
String s = ""+
"if a:\n"+
" a = 1#comment1\n"+
"elif b:\n"+
" b = 2#comment2\n"+
"elif c:\n"+
" c = 3#comment3\n"+
"else:\n"+
" d = 4#comment4\n";
checkPrettyPrintEqual(s);
}
public void testIfElse3() throws Exception {
String s =
"#commentbefore\n"+ //1
"if a:#commentIf\n"+ //2
" a = 1\n"+ //3
"elif b:#commentElif\n"+ //4
" b = 2\n"+ //5
"elif c:\n"+ //6
" c = 3\n"+ //7
"else:#commentElse\n"+ //8
" d = 4\n" + //9
"outOfIf = True\n"; //10
checkPrettyPrintEqual(s);
}
public void testCommentAndIf() throws Exception {
String s = "" +
"def initiate_send():\n" +
" if 10:\n" +
" # try to send the buffer\n" +
" try:\n" +
" num_sent = 10\n" +
" except:\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testPlus() throws Exception {
String s = ""+
"a = 1 + 1\n";
checkPrettyPrintEqual(s);
}
public void testMinus() throws Exception {
String s = ""+
"a = 1 - 1\n";
checkPrettyPrintEqual(s);
}
public void testPow() throws Exception {
String s = ""+
"a = 1 ** 1\n";
checkPrettyPrintEqual(s);
}
public void testLShift() throws Exception {
String s = ""+
"a = 1 << 1\n";
checkPrettyPrintEqual(s);
}
public void testRShift() throws Exception {
String s = ""+
"a = 1 >> 1\n";
checkPrettyPrintEqual(s);
}
public void testBitOr() throws Exception {
String s = ""+
"a = 1 | 1\n";
checkPrettyPrintEqual(s);
}
public void testBitXOr() throws Exception {
String s = ""+
"a = 1 ^ 1\n";
checkPrettyPrintEqual(s);
}
public void testBitAnd() throws Exception {
String s = ""+
"a = 1 & 1\n";
checkPrettyPrintEqual(s);
}
public void testFloorDiv() throws Exception {
String s = ""+
"a = 1 // 1\n";
checkPrettyPrintEqual(s);
}
public void testNoComments2() throws Exception {
prefs.setSpacesAfterComma(1);
String s = ""+
"class Class1(obj1, obj2):\n" +
" def met1(self, a, b):\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testAssign() throws Exception {
String s = ""+
"a = 1\n";
checkPrettyPrintEqual(s);
}
public void testAssign2() throws Exception {
String s = ""+
"a = 1#comment\n";
checkPrettyPrintEqual(s);
}
public void testComments1() throws Exception {
String s = "#comment00\n" +
"class Class1:#comment0\n" +
" #comment1\n" +
" def met1(self,a):#comment2\n" +
" pass#comment3\n" +
"";
checkPrettyPrintEqual(s);
}
public void testComments2() throws Exception {
String s = ""+
"class Foo(object):#test comment\n" +
" def m1(self,a,#c1\n" +
" b):#c2\n" +
" pass\n" +
"";
checkPrettyPrintEqual(s);
}
public void testComments3() throws Exception {
String s = ""+
"# comment before\n" +
"i = 0\n" +
"while (i < 2):# while test comment on-line\n" +
" print 'under 5'\n" +
" i += 1# augmented assignment on-line\n" +
" # this comment disappears\n" +
"else:# else on-line\n" +
" print 'bigger'# print on-line\n" +
"# after the second body (but actually in the module node)!" +
"";
checkPrettyPrintEqual(s);
}
private PrettyPrinterPrefs prefs;
@Override
protected void setUp() throws Exception {
super.setUp();
prefs = new PrettyPrinterPrefs("\n");
}
public SimpleNode checkPrettyPrintEqual(String s, String expected) throws Exception, IOException {
return checkPrettyPrintEqual(s, prefs, expected);
}
public SimpleNode checkPrettyPrintEqual(String s) throws Exception, IOException {
return checkPrettyPrintEqual(s, s);
}
/**
* @param s
* @return
* @throws Exception
* @throws IOException
*/
public static SimpleNode checkPrettyPrintEqual(String s, PrettyPrinterPrefs prefs, String expected) throws Exception, IOException {
SimpleNode node = parseLegalDocStr(s);
final WriterEraser stringWriter = makePrint(prefs, node);
assertEquals(expected, stringWriter.getBuffer().toString());
return node;
}
/**
* @param prefs
* @param node
* @return
* @throws Exception
*/
public static WriterEraser makePrint(PrettyPrinterPrefs prefs, SimpleNode node) throws Exception {
Module m = (Module) node;
final WriterEraser stringWriter = new WriterEraser();
PrettyPrinter printer = new PrettyPrinter(prefs, stringWriter);
m.accept(printer);
if(DEBUG){
System.out.println("\n\nResult:\n");
System.out.println("'"+stringWriter.getBuffer().toString()+"'");
}
assertTrue(! printer.state.inStmt());
// assertTrue("Should not be in record:"+printer.auxComment, ! printer.auxComment.inRecord());
return stringWriter;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -