2
* Created on Apr 12, 2005
4
* @author Fabio Zadrozny
6
package org.python.pydev.editor.actions;
10
import org.eclipse.jface.text.BadLocationException;
11
import org.eclipse.jface.text.Document;
12
import org.eclipse.jface.text.TextSelection;
13
import org.python.pydev.core.Tuple;
14
import org.python.pydev.core.docutils.PyDocIterator;
15
import org.python.pydev.core.docutils.PySelection;
17
import junit.framework.TestCase;
20
* @author Fabio Zadrozny
22
public class PySelectionTest extends TestCase {
24
private PySelection ps;
26
private String docContents;
28
public static void main(String[] args) {
30
PySelectionTest test = new PySelectionTest();
32
test.testGetCurrLineWithoutCommsOrLiterals();
35
junit.textui.TestRunner.run(PySelectionTest.class);
36
} catch (Throwable e) {
42
* @see TestCase#setUp()
44
protected void setUp() throws Exception {
51
doc = new Document(docContents);
55
* @see TestCase#tearDown()
57
protected void tearDown() throws Exception {
61
public void testAddLine() {
62
ps = new PySelection(new Document("line1\nline2\n"), new TextSelection(doc, 0,0));
64
assertEquals("line1\nfoo\nline2\n", ps.getDoc().get());
66
ps = new PySelection(new Document("line1\n"), new TextSelection(doc, 0,0));
68
assertEquals("line1\nfoo\n", ps.getDoc().get());
70
ps = new PySelection(new Document("line1"), new TextSelection(doc, 0,0));
72
assertEquals("line1\r\nfoo\r\n", ps.getDoc().get());
75
* @throws BadLocationException
78
public void testGeneral() throws BadLocationException {
79
ps = new PySelection(doc, new TextSelection(doc, 0,0));
80
assertEquals("TestLine1",ps.getCursorLineContents());
81
assertEquals("",ps.getLineContentsToCursor());
82
ps.selectCompleteLine();
84
assertEquals("TestLine1",ps.getCursorLineContents());
85
assertEquals("TestLine1",ps.getLine(0));
86
assertEquals("TestLine2#comm2",ps.getLine(1));
89
assertEquals("TestLine2#comm2",ps.getLine(0));
90
ps.addLine("TestLine1", 0);
94
public void testImportLine() {
97
"''' this should be ignored\n"+
98
"from xxx import yyy \n"+
100
"#we want the import to appear in this line\n"+
106
Document document = new Document(strDoc);
107
PySelection selection = new PySelection(document);
108
assertEquals(4, selection.getLineAvailableForImport());
111
public void testImportLine2() {
114
"#we want the import to appear after this line\n"+
120
Document document = new Document(strDoc);
121
PySelection selection = new PySelection(document);
122
assertEquals(2, selection.getLineAvailableForImport());
125
public void testImportLine3() {
128
"#we want the import to appear after this line\n"+
133
"''' this should be ignored\n"+
134
"from xxx import yyy \n"+
138
Document document = new Document(strDoc);
139
PySelection selection = new PySelection(document);
140
assertEquals(2, selection.getLineAvailableForImport());
145
public void testImportLine4() {
147
"class SomeClass( object ):\n"+
148
" '''This is the data that should be set...\n"+
152
Document document = new Document(strDoc);
153
PySelection selection = new PySelection(document);
154
assertEquals(0, selection.getLineAvailableForImport());
157
public void testImportLine5() {
159
"'''This is the data that should be set...\n"+
163
Document document = new Document(strDoc);
164
PySelection selection = new PySelection(document);
165
assertEquals(2, selection.getLineAvailableForImport());
169
public void testSelectAll() {
170
ps = new PySelection(doc, new TextSelection(doc, 0,0));
172
assertEquals(docContents, ps.getCursorLineContents()+"\n");
173
assertEquals(docContents, ps.getSelectedText());
175
ps = new PySelection(doc, new TextSelection(doc, 0,9)); //first line selected
176
ps.selectAll(true); //changes
177
assertEquals(docContents, ps.getCursorLineContents()+"\n");
178
assertEquals(docContents, ps.getSelectedText());
180
ps = new PySelection(doc, new TextSelection(doc, 0,9)); //first line selected
181
ps.selectAll(false); //nothing changes
182
assertEquals(ps.getLine(0), ps.getCursorLineContents());
183
assertEquals(ps.getLine(0), ps.getSelectedText());
186
public void testFullRep() throws Exception {
187
String s = "v=aa.bb.cc()";
188
doc = new Document(s);
189
ps = new PySelection(doc, new TextSelection(doc, 2,2));
190
assertEquals("aa.bb.cc", ps.getFullRepAfterSelection());
193
doc = new Document(s);
194
ps = new PySelection(doc, new TextSelection(doc, 2,2));
195
assertEquals("aa.bb.cc", ps.getFullRepAfterSelection());
200
public void testReplaceToSelection() throws Exception {
201
String s = "vvvvppppaaaa";
202
doc = new Document(s);
203
ps = new PySelection(doc, 4);
204
ps.replaceLineContentsToSelection("xxxx");
205
assertEquals("xxxxppppaaaa", ps.getDoc().get());
209
public void testGetInsideParentesis() throws Exception {
210
String s = "def m1(self, a, b)";
211
doc = new Document(s);
212
ps = new PySelection(doc, new TextSelection(doc, 0,0));
213
List<String> insideParentesisToks = ps.getInsideParentesisToks(false).o1;
214
assertEquals(2, insideParentesisToks.size());
215
assertEquals("a", insideParentesisToks.get(0));
216
assertEquals("b", insideParentesisToks.get(1));
218
s = "def m1(self, a, b, )";
219
doc = new Document(s);
220
ps = new PySelection(doc, new TextSelection(doc, 0,0));
221
insideParentesisToks = ps.getInsideParentesisToks(false).o1;
222
assertEquals(2, insideParentesisToks.size());
223
assertEquals("a", insideParentesisToks.get(0));
224
assertEquals("b", insideParentesisToks.get(1));
227
s = "def m1(self, a, b=None)";
228
doc = new Document(s);
229
ps = new PySelection(doc, new TextSelection(doc, 0,0));
230
insideParentesisToks = ps.getInsideParentesisToks(true).o1;
231
assertEquals(3, insideParentesisToks.size());
232
assertEquals("self", insideParentesisToks.get(0));
233
assertEquals("a", insideParentesisToks.get(1));
234
assertEquals("b", insideParentesisToks.get(2));
237
s = "def m1(self, a, b=None)";
238
doc = new Document(s);
239
ps = new PySelection(doc, new TextSelection(doc, 0,0));
240
insideParentesisToks = ps.getInsideParentesisToks(false).o1;
241
assertEquals(2, insideParentesisToks.size());
242
assertEquals("a", insideParentesisToks.get(0));
243
assertEquals("b", insideParentesisToks.get(1));
245
s = "def m1(self, a, (b,c) )";
246
doc = new Document(s);
247
ps = new PySelection(doc, new TextSelection(doc, 0,0));
248
insideParentesisToks = ps.getInsideParentesisToks(false).o1;
249
assertEquals(3, insideParentesisToks.size());
250
assertEquals("a", insideParentesisToks.get(0));
251
assertEquals("b", insideParentesisToks.get(1));
252
assertEquals("c", insideParentesisToks.get(2));
254
s = "def m1(self, a, b, \nc,\nd )";
255
doc = new Document(s);
256
ps = new PySelection(doc, new TextSelection(doc, 0,0));
257
insideParentesisToks = ps.getInsideParentesisToks(false).o1;
258
assertEquals(4, insideParentesisToks.size());
259
assertEquals("a", insideParentesisToks.get(0));
260
assertEquals("b", insideParentesisToks.get(1));
261
assertEquals("c", insideParentesisToks.get(2));
262
assertEquals("d", insideParentesisToks.get(3));
267
public void testRemoveEndingComments() throws Exception {
268
String s = "class Foo:pass\n" +
271
"print 'no comm'\n" +
274
doc = new Document(s);
275
StringBuffer buffer = PySelection.removeEndingComments(doc);
277
assertEquals("\n#comm3\n" +
278
"#comm4", buffer.toString());
279
assertEquals("class Foo:pass\n" +
282
"print 'no comm'\n", doc.get());
284
public void testRemoveEndingComments2() throws Exception {
285
String s = "class C: \n" +
289
doc = new Document(s);
290
StringBuffer buffer = PySelection.removeEndingComments(doc);
292
assertEquals("\n#end\n" , buffer.toString());
293
assertEquals("class C: \n" +
297
public void testGetLastIf() throws Exception {
301
doc = new Document(s);
302
ps = new PySelection(doc, doc.getLength());
303
assertEquals("if False:", ps.getPreviousLineThatAcceptsElse());
308
doc = new Document(s);
309
ps = new PySelection(doc, doc.getLength());
310
assertEquals(null, ps.getPreviousLineThatAcceptsElse());
314
public void testGetLineWithoutComments() {
316
"a = 'ethuenoteuho#ueoth'";
317
doc = new Document(s);
318
ps = new PySelection(doc, doc.getLength());
319
assertEquals("a = ", ps.getLineWithoutCommentsOrLiterals());
322
public void testGetCurrToken() throws BadLocationException {
325
doc = new Document(s);
327
ps = new PySelection(doc, 0);
328
assertEquals(new Tuple<String, Integer>("",0), ps.getCurrToken());
330
ps = new PySelection(doc, 1);
331
assertEquals(new Tuple<String, Integer>("aa",1), ps.getCurrToken());
333
ps = new PySelection(doc, 2);
334
assertEquals(new Tuple<String, Integer>("aa",1), ps.getCurrToken());
336
ps = new PySelection(doc, doc.getLength()-1);
337
assertEquals(new Tuple<String, Integer>("bb",6), ps.getCurrToken());
339
ps = new PySelection(doc, doc.getLength());
340
assertEquals(new Tuple<String, Integer>( "bb",6), ps.getCurrToken());
343
doc = new Document(s);
345
ps = new PySelection(doc, doc.getLength());
346
assertEquals(new Tuple<String, Integer>("",9), ps.getCurrToken());
348
ps = new PySelection(doc, doc.getLength()-1);
349
assertEquals(new Tuple<String, Integer>("bb",6), ps.getCurrToken());
352
public void testGetLine() throws Exception {
353
PySelection sel = new PySelection(new Document("foo\nbla"));
354
assertEquals("foo", sel.getLine());
355
assertEquals(0, sel.getLineOfOffset(1));
358
public void testSameLine() throws Exception {
359
final Document doc = new Document("foo\nbla\nxxx");
360
assertEquals(true, PySelection.isInside(0, doc.getLineInformation(0)));
361
assertEquals(false, PySelection.isInside(0, doc.getLineInformation(1)));
363
assertEquals(true, PySelection.isInside(4, doc.getLineInformation(1)));
366
public void testGetCurrLineWithoutCommsOrLiterals() throws Exception {
367
Document doc = new Document("a#foo\nxxx");
368
PySelection selection = new PySelection(doc, 1);
369
assertEquals("a", selection.getLineContentsToCursor(true, true));
372
"titleEnd = ('''\n" +
373
" [#''')" + //get with spaces in the place of lines or comments
375
doc = new Document(str);
376
selection = new PySelection(doc, str.length());
377
assertEquals(" )", selection.getLineContentsToCursor(true, true));
382
doc = new Document(str);
383
selection = new PySelection(doc, 3); //only 'foo'
384
assertEquals("foo", selection.getLineContentsToCursor(true, true));
389
public void testDocIterator() throws Exception {
394
doc = new Document(str);
395
PyDocIterator iterator = new PyDocIterator(doc, false, true, true);
396
assertEquals(" ",iterator.next());