~vil/pydev/upstream

« back to all changes in this revision

Viewing changes to org.python.pydev/tests/org/python/pydev/editor/actions/PySelectionTest.java

  • Committer: Vladimír Lapáček
  • Date: 2006-08-30 18:38:44 UTC
  • Revision ID: vladimir.lapacek@gmail.com-20060830183844-f4d82c1239a7770a
Initial import of upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on Apr 12, 2005
 
3
 *
 
4
 * @author Fabio Zadrozny
 
5
 */
 
6
package org.python.pydev.editor.actions;
 
7
 
 
8
import java.util.List;
 
9
 
 
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;
 
16
 
 
17
import junit.framework.TestCase;
 
18
 
 
19
/**
 
20
 * @author Fabio Zadrozny
 
21
 */
 
22
public class PySelectionTest extends TestCase {
 
23
 
 
24
    private PySelection ps;
 
25
    private Document doc;
 
26
    private String docContents;
 
27
 
 
28
    public static void main(String[] args) {
 
29
        try {
 
30
            PySelectionTest test = new PySelectionTest();
 
31
            test.setUp();
 
32
            test.testGetCurrLineWithoutCommsOrLiterals();
 
33
            test.tearDown();
 
34
            
 
35
            junit.textui.TestRunner.run(PySelectionTest.class);
 
36
        } catch (Throwable e) {
 
37
            e.printStackTrace();
 
38
        }
 
39
    }
 
40
 
 
41
    /*
 
42
     * @see TestCase#setUp()
 
43
     */
 
44
    protected void setUp() throws Exception {
 
45
        super.setUp();
 
46
        docContents = "" +
 
47
                "TestLine1\n"+
 
48
                "TestLine2#comm2\n"+
 
49
                "TestLine3#comm3\n"+
 
50
                "TestLine4#comm4\n";
 
51
        doc = new Document(docContents);
 
52
    }
 
53
 
 
54
    /*
 
55
     * @see TestCase#tearDown()
 
56
     */
 
57
    protected void tearDown() throws Exception {
 
58
        super.tearDown();
 
59
    }
 
60
 
 
61
    public void testAddLine() {
 
62
        ps = new PySelection(new Document("line1\nline2\n"), new TextSelection(doc, 0,0));
 
63
        ps.addLine("foo", 0);
 
64
        assertEquals("line1\nfoo\nline2\n", ps.getDoc().get());
 
65
        
 
66
        ps = new PySelection(new Document("line1\n"), new TextSelection(doc, 0,0));
 
67
        ps.addLine("foo", 0);
 
68
        assertEquals("line1\nfoo\n", ps.getDoc().get());
 
69
        
 
70
        ps = new PySelection(new Document("line1"), new TextSelection(doc, 0,0));
 
71
        ps.addLine("foo", 0);
 
72
        assertEquals("line1\r\nfoo\r\n", ps.getDoc().get());
 
73
        }
 
74
    /**
 
75
     * @throws BadLocationException
 
76
     * 
 
77
     */
 
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();
 
83
        
 
84
        assertEquals("TestLine1",ps.getCursorLineContents());
 
85
        assertEquals("TestLine1",ps.getLine(0));
 
86
        assertEquals("TestLine2#comm2",ps.getLine(1));
 
87
        
 
88
        ps.deleteLine(0);
 
89
        assertEquals("TestLine2#comm2",ps.getLine(0));
 
90
        ps.addLine("TestLine1", 0);
 
91
        
 
92
    }
 
93
    
 
94
    public void testImportLine() {
 
95
        String strDoc = "" +
 
96
        "#coding                   \n"+
 
97
        "''' this should be ignored\n"+
 
98
        "from xxx import yyy       \n"+
 
99
        "import www'''             \n"+
 
100
        "#we want the import to appear in this line\n"+
 
101
        "Class C:                  \n"+
 
102
        "    pass                  \n"+
 
103
        "import kkk                \n"+
 
104
        "\n"+
 
105
        "\n";
 
106
        Document document = new Document(strDoc);
 
107
        PySelection selection = new PySelection(document);
 
108
        assertEquals(4, selection.getLineAvailableForImport());
 
109
    }
 
110
 
 
111
    public void testImportLine2() {
 
112
        String strDoc = "" +
 
113
        "#coding                   \n"+
 
114
        "#we want the import to appear after this line\n"+
 
115
        "Class C:                  \n"+
 
116
        "    pass                  \n"+
 
117
        "import kkk                \n"+
 
118
        "\n"+
 
119
        "\n";
 
120
        Document document = new Document(strDoc);
 
121
        PySelection selection = new PySelection(document);
 
122
        assertEquals(2, selection.getLineAvailableForImport());
 
123
    }
 
124
    
 
125
    public void testImportLine3() {
 
126
        String strDoc = "" +
 
127
        "#coding                   \n"+
 
128
        "#we want the import to appear after this line\n"+
 
129
        "Class C:                  \n"+
 
130
        "    pass                  \n"+
 
131
        "import kkk                \n"+
 
132
        "                          \n"+
 
133
        "''' this should be ignored\n"+
 
134
        "from xxx import yyy       \n"+
 
135
        "import www'''             \n"+
 
136
        "\n"+
 
137
        "\n";
 
138
        Document document = new Document(strDoc);
 
139
        PySelection selection = new PySelection(document);
 
140
        assertEquals(2, selection.getLineAvailableForImport());
 
141
    }
 
142
 
 
143
    
 
144
    
 
145
    public void testImportLine4() {
 
146
        String strDoc = "" +
 
147
                "class SomeClass( object ):\n"+
 
148
                "    '''This is the data that should be set...\n"+
 
149
                "    '''\n"+
 
150
                "\n"+
 
151
                "\n";
 
152
        Document document = new Document(strDoc);
 
153
        PySelection selection = new PySelection(document);
 
154
        assertEquals(0, selection.getLineAvailableForImport());
 
155
    }
 
156
    
 
157
    public void testImportLine5() {
 
158
        String strDoc = "" +
 
159
        "'''This is the data that should be set...\n"+
 
160
        "'''\n"+
 
161
        "\n"+
 
162
        "\n";
 
163
        Document document = new Document(strDoc);
 
164
        PySelection selection = new PySelection(document);
 
165
        assertEquals(2, selection.getLineAvailableForImport());
 
166
    }
 
167
    
 
168
    
 
169
    public void testSelectAll() {
 
170
        ps = new PySelection(doc, new TextSelection(doc, 0,0));
 
171
        ps.selectAll(true);
 
172
        assertEquals(docContents, ps.getCursorLineContents()+"\n");
 
173
        assertEquals(docContents, ps.getSelectedText());
 
174
        
 
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());
 
179
        
 
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());
 
184
    }
 
185
    
 
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());
 
191
 
 
192
        s = "v=aa.bb.cc";
 
193
        doc = new Document(s);
 
194
        ps = new PySelection(doc, new TextSelection(doc, 2,2));
 
195
        assertEquals("aa.bb.cc", ps.getFullRepAfterSelection());
 
196
        
 
197
        
 
198
    }
 
199
    
 
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());
 
206
    }
 
207
    
 
208
    
 
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));
 
217
        
 
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));
 
225
        
 
226
        
 
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));
 
235
        
 
236
 
 
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));
 
244
        
 
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));
 
253
        
 
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));
 
263
        
 
264
        
 
265
    }
 
266
    
 
267
    public void testRemoveEndingComments() throws Exception {
 
268
        String s = "class Foo:pass\n" +
 
269
                "#comm1\n" +
 
270
                "#comm2\n" +
 
271
                "print 'no comm'\n" +
 
272
                "#comm3\n" +
 
273
                "#comm4";
 
274
        doc = new Document(s);
 
275
        StringBuffer buffer = PySelection.removeEndingComments(doc);
 
276
        
 
277
        assertEquals("\n#comm3\n" +
 
278
                "#comm4", buffer.toString());
 
279
        assertEquals("class Foo:pass\n" +
 
280
                "#comm1\n" +
 
281
                "#comm2\n" +
 
282
                "print 'no comm'\n", doc.get());
 
283
    }
 
284
    public void testRemoveEndingComments2() throws Exception {
 
285
        String s = "class C: \n" +
 
286
        "    pass\n" +
 
287
        "#end\n" +
 
288
        "";
 
289
        doc = new Document(s);
 
290
        StringBuffer buffer = PySelection.removeEndingComments(doc);
 
291
        
 
292
        assertEquals("\n#end\n" , buffer.toString());
 
293
        assertEquals("class C: \n" +
 
294
                "    pass\n" 
 
295
                , doc.get());
 
296
    }
 
297
    public void testGetLastIf() throws Exception {
 
298
        String s = 
 
299
            "if False:\n" +
 
300
            "    print foo";
 
301
        doc = new Document(s);
 
302
        ps = new PySelection(doc, doc.getLength());
 
303
        assertEquals("if False:", ps.getPreviousLineThatAcceptsElse());
 
304
 
 
305
        s = 
 
306
        "while False:\n" +
 
307
        "    print foo";
 
308
        doc = new Document(s);
 
309
        ps = new PySelection(doc, doc.getLength());
 
310
        assertEquals(null, ps.getPreviousLineThatAcceptsElse());
 
311
        
 
312
    }
 
313
    
 
314
    public void testGetLineWithoutComments() {
 
315
        String s = 
 
316
            "a = 'ethuenoteuho#ueoth'";
 
317
        doc = new Document(s);
 
318
        ps = new PySelection(doc, doc.getLength());
 
319
        assertEquals("a =                     ", ps.getLineWithoutCommentsOrLiterals());
 
320
    }
 
321
    
 
322
    public void testGetCurrToken() throws BadLocationException {
 
323
        String s = 
 
324
            " aa = bb";
 
325
        doc = new Document(s);
 
326
        
 
327
        ps = new PySelection(doc, 0);
 
328
        assertEquals(new Tuple<String, Integer>("",0), ps.getCurrToken());
 
329
        
 
330
        ps = new PySelection(doc, 1);
 
331
        assertEquals(new Tuple<String, Integer>("aa",1), ps.getCurrToken());
 
332
        
 
333
        ps = new PySelection(doc, 2);
 
334
        assertEquals(new Tuple<String, Integer>("aa",1), ps.getCurrToken());
 
335
        
 
336
        ps = new PySelection(doc, doc.getLength()-1);
 
337
        assertEquals(new Tuple<String, Integer>("bb",6), ps.getCurrToken());
 
338
        
 
339
        ps = new PySelection(doc, doc.getLength());
 
340
        assertEquals(new Tuple<String, Integer>( "bb",6), ps.getCurrToken());
 
341
        
 
342
        s =" aa = bb ";
 
343
        doc = new Document(s);
 
344
        
 
345
        ps = new PySelection(doc, doc.getLength());
 
346
        assertEquals(new Tuple<String, Integer>("",9), ps.getCurrToken());
 
347
        
 
348
        ps = new PySelection(doc, doc.getLength()-1);
 
349
        assertEquals(new Tuple<String, Integer>("bb",6), ps.getCurrToken());
 
350
    }
 
351
    
 
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));
 
356
        }
 
357
    
 
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)));
 
362
        
 
363
        assertEquals(true, PySelection.isInside(4, doc.getLineInformation(1)));
 
364
    }
 
365
    
 
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));
 
370
        
 
371
        String str = "" +
 
372
        "titleEnd = ('''\n" +
 
373
        "            [#''')" + //get with spaces in the place of lines or comments
 
374
        "";
 
375
        doc = new Document(str);
 
376
        selection = new PySelection(doc, str.length());
 
377
        assertEquals("                 )", selection.getLineContentsToCursor(true, true));
 
378
        
 
379
        str = "" +
 
380
        "foopp" + 
 
381
        "";
 
382
        doc = new Document(str);
 
383
        selection = new PySelection(doc, 3); //only 'foo'
 
384
        assertEquals("foo", selection.getLineContentsToCursor(true, true));
 
385
        
 
386
 
 
387
    }
 
388
    
 
389
    public void testDocIterator() throws Exception {
 
390
        String str = "" +
 
391
        "''\n" +
 
392
        "bla" + 
 
393
        "";
 
394
        doc = new Document(str);
 
395
        PyDocIterator iterator = new PyDocIterator(doc,  false, true, true);
 
396
        assertEquals("  ",iterator.next());
 
397
        
 
398
    }
 
399
}