📄 pyautoindentstrategytest.java
字号:
/*
* Created on May 5, 2005
*
* @author Fabio Zadrozny
*/
package org.python.pydev.editor;
import junit.framework.TestCase;
import org.eclipse.jface.text.Document;
import org.python.pydev.editor.autoedit.DocCmd;
import org.python.pydev.editor.autoedit.PyAutoIndentStrategy;
/**
* @author Fabio Zadrozny
*/
public class PyAutoIndentStrategyTest extends TestCase {
private PyAutoIndentStrategy strategy;
private String doc;
private DocCmd docCmd;
private String expected;
public static void main(String[] args) {
try {
PyAutoIndentStrategyTest s = new PyAutoIndentStrategyTest("testt");
s.setUp();
s.testAutoImportStr2();
s.tearDown();
junit.textui.TestRunner.run(PyAutoIndentStrategyTest.class);
} catch (Throwable e) {
e.printStackTrace();
}
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Constructor for PyAutoIndentStrategyTest.
* @param arg0
*/
public PyAutoIndentStrategyTest(String arg0) {
super(arg0);
strategy = new PyAutoIndentStrategy();
}
public void testNewLineWithNewWithConstruct() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"class C:\n" +
" with a:" +
"";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
}
public void testTab() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str =
" args = [ '-1', '-2',\n"+
" ";
DocCmd docCmd = new DocCmd(str.length(), 0, "\t");
strategy.customizeDocumentCommand(new Document(str), docCmd);
assertEquals(" ", docCmd.text);
}
public void testTab2() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str =
"class Foo:\n"+
" def m"+
"";
DocCmd docCmd = new DocCmd(str.length()-" def m".length(), 4, "\t");
Document document = new Document(str);
strategy.customizeDocumentCommand(document, docCmd);
assertEquals(" ", docCmd.text);
assertEquals(str, document.get()); //as we already have a selection, nothing should be deleted
}
public void testSpaces() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
DocCmd docCmd = new DocCmd(0, 0, "\t");
strategy.customizeDocumentCommand(new Document(""), docCmd);
assertEquals(" ", docCmd.text);
docCmd = new DocCmd(0, 0, "\t\t");
strategy.customizeDocumentCommand(new Document(""), docCmd);
assertEquals(" ", docCmd.text);
docCmd = new DocCmd(0, 0, "\tabc");
strategy.customizeDocumentCommand(new Document(""), docCmd);
assertEquals(" abc", docCmd.text);
docCmd = new DocCmd(0, 0, "\tabc\t");
strategy.customizeDocumentCommand(new Document(""), docCmd);
assertEquals(" abc ", docCmd.text);
docCmd = new DocCmd(0, 0, " ");
strategy.customizeDocumentCommand(new Document(""), docCmd);
assertEquals(" ", docCmd.text);
}
public void testNewLineAfterReturn() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"def m1(self):\n" +
" return 'foo'\n" +
"#ffo" +
"" +
"";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength()-"#ffo".length(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n", docCmd.text);
}
public void testIgnoreComment() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"titleEnd = ('[#')" +
"";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n", docCmd.text);
}
public void testIgnoreComment2() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"titleEnd = ('''\n" +
" [#''')" + //should wrap to the start
"";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n", docCmd.text);
}
public void testNewLineAfterOpeningParWithOtherContents() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"def m1( self,";
// |<-- should indent here in this case, and not on the parenthesis
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
}
public void testNewLineAfterReturn2() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"def m1(self):\n" +
" return ('foo',";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
}
public void testMaintainIndent() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"def moo():\n" +
" if not 1:\n" +
" print 'foo'\n" +
" print 'bla'"+
"";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength()-"print 'bla'".length(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
}
public void testMaintainIndent2() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"def moo():\n" +
" if not 1:\n" +
" print 'foo'\n" +
" print 'bla'"+
"";
final Document doc = new Document(str);
int offset = doc.getLength()-" print 'bla'".length();
DocCmd docCmd = new DocCmd(offset, 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
assertEquals(offset+2, docCmd.caretOffset);
}
public void testDontChangeCursorOffset() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"def moo():\n" +
" if not 1:\n" +
" print 'foo'" +
"";
final Document doc = new Document(str);
int offset = doc.getLength()-" 'foo'".length();
DocCmd docCmd = new DocCmd(offset, 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
assertEquals(0, docCmd.caretOffset); //don't change it
}
public void testTabIndentToLevel() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"properties.create( \n" +
" a,\n" +
" \n" +
"\n" + //cursor is here
" b,\n" +
")" +
"";
final Document doc = new Document(str);
int offset = doc.getLength()-"\n b,\n)".length();
DocCmd docCmd = new DocCmd(offset, 0, "\t");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals(" ", docCmd.text);
}
public void testTabIndentToLevel2() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"class ContaminantFont( Barrier, ModelBase ):\n" +
" '''\n" +
" This class contains information to edit a contaminant.\n" +
" '''\n" +
" properties.create( \n" +
" \n" +
" #defines where is the source (in the water or in the soil)\n" +
" sourceLocation = SOURCE_LOCATION_WATER,\n" +
" \n" +
"" + //we're here (indent to the first level)
"";
final Document doc = new Document(str);
int offset = doc.getLength();
DocCmd docCmd = new DocCmd(offset, 0, "\t");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals(" ", docCmd.text);
}
public void testTabIndentToLevel3() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"class ContaminantFont( Barrier, ModelBase ):\n" +
" '''\n" +
" This class contains information to edit a contaminant.\n" +
" '''\n" +
" properties.create( \n" +
" \n" +
" #defines where is the source (in the water or in the soil)\n" +
" sourceLocation = SOURCE_LOCATION_WATER,\n" +
" \n" +
" " + //now that we're already in the first level, indent to the current level
"";
final Document doc = new Document(str);
int offset = doc.getLength();
DocCmd docCmd = new DocCmd(offset, 0, "\t");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals(" ", docCmd.text);
}
public void testNoAutoIndentClosingPar() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"newTuple = (\n" +
" what(),\n" + //the next line should be indented to this one, and not to the start of the indent
" )\n" +
"";
final Document doc = new Document(str);
String s =
"\n"+
" )\n";
DocCmd docCmd = new DocCmd(doc.getLength()-s.length(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
}
public void testNoAutoIndentClosingPar2() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"newTuple = (\n" +
" what(),\n" +
"\n" + //pressing tab in the start of this line will bring us to the 'what()' level.
" )\n" +
"";
final Document doc = new Document(str);
String s =
"\n"+
" )\n";
DocCmd docCmd = new DocCmd(doc.getLength()-s.length(), 0, "\t");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals(" ", docCmd.text);
}
public void testNewLineAfterLineWithComment() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"string1 = '01234546789[#]'" +
"";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n", docCmd.text);
}
public void testNewLine10() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"def M1(a):\n" +
" doFoo(a,b(),\n" +
" '',b)" +
"";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
}
public void testNewLine11() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"def fun():\n" +
" if True:\n" +
" passif False: 'foo'" +
"";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength()-"if False: 'foo'".length(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
}
public void testNewLine12() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "" +
"if False:print 'done'" +
"";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength()-"print 'done'".length(), 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
}
public void testNewLine3() {
strategy.setIndentPrefs(new TestIndentPrefs(true, 4));
String str = "for a in b: " +
"";
final Document doc = new Document(str);
DocCmd docCmd = new DocCmd(doc.getLength()-4, 0, "\n");
strategy.customizeDocumentCommand(doc, docCmd);
assertEquals("\n ", docCmd.text);
String expected = "for a in b: ";
assertEquals(expected, doc.get());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -