⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pyparsertest.java

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on 27/08/2005
 */
package org.python.pydev.parser;

import java.io.File;
import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.python.pydev.core.IPythonNature;
import org.python.pydev.core.REF;
import org.python.pydev.core.TestDependent;
import org.python.pydev.core.Tuple;
import org.python.pydev.core.Tuple3;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.ast.ClassDef;
import org.python.pydev.parser.jython.ast.FunctionDef;
import org.python.pydev.parser.jython.ast.Module;
import org.python.pydev.parser.jython.ast.Name;
import org.python.pydev.parser.jython.ast.Str;
import org.python.pydev.parser.jython.ast.commentType;
import org.python.pydev.parser.visitors.scope.ASTEntry;
import org.python.pydev.parser.visitors.scope.SequencialASTIteratorVisitor;

public class PyParserTest extends PyParserTestBase{

    public static void main(String[] args) {
        try {
            PyParserTest test = new PyParserTest();
            test.setUp();
            test.testTryReparse();
            test.tearDown();
            System.out.println("Finished");
            junit.textui.TestRunner.run(PyParserTest.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void setUp() throws Exception {
    	super.setUp();
    	PyParser.USE_FAST_STREAM = true;
    }
    
    

    public void testRemoveEndingComments() throws Exception {
        String s = 
                "class Foo:pass\n" +
                "#comm1\n" +
                "#comm2\n" +
                "print 'no comm'\n" +
                "#comm3\n" +
                "#comm4";
        Document doc = new Document(s);
        
        List<commentType> comments = PyParser.removeEndingComments(doc);
        assertEquals("#comm3", comments.get(0).id);
        assertEquals(1, comments.get(0).beginColumn);
        assertEquals(5, comments.get(0).beginLine);
        
        assertEquals("#comm4", comments.get(1).id);
        assertEquals(1, comments.get(1).beginColumn);
        assertEquals(6, comments.get(1).beginLine);
        
        assertEquals("class Foo:pass\n" +
                "#comm1\n" +
                "#comm2\n" +
                "print 'no comm'\n", doc.get());
    }
    
    public void testRemoveEndingComments2() throws Exception {
        String s = 
            "class C: \n" +
            "    pass\n" +
            "#end\n" +
            "";
        Document doc = new Document(s);
        List<commentType> comments = PyParser.removeEndingComments(doc);
        assertEquals(1, comments.get(0).beginColumn);
        assertEquals(3, comments.get(0).beginLine);
        assertEquals("#end" , comments.get(0).id);
        assertEquals("class C: \n" +
                "    pass\n" 
                , doc.get());
    }
    
    public void testRemoveEndingComments3() throws Exception {
        String s = 
            "##end\n" +
            "##end\n" +
            "";
        Document doc = new Document(s);
        List<commentType> comments = PyParser.removeEndingComments(doc);
        
        assertEquals(1, comments.get(0).beginColumn);
        assertEquals(1, comments.get(0).beginLine);
        assertEquals("##end" , comments.get(0).id);
        
        assertEquals(2, comments.get(1).beginLine);
        assertEquals(1, comments.get(1).beginColumn);
        assertEquals("##end" , comments.get(1).id);
        assertEquals("", doc.get());
    }
    
    public void testTryReparse() throws BadLocationException{
        Document doc = new Document("");
        for(int i=0; i< 5; i++){
            doc.replace(0, 0, "this is a totally and completely not parseable doc\n");
        }
        
        PyParser.ParserInfo parserInfo = new PyParser.ParserInfo(doc, true, IPythonNature.LATEST_GRAMMAR_VERSION);
        parserInfo.tryReparse = true;
        Tuple<SimpleNode,Throwable> reparseDocument = PyParser.reparseDocument(parserInfo);
        assertTrue(reparseDocument.o1 == null);
        assertTrue(reparseDocument.o2 != null);
    }
    
    public void testCorrectArgs() {
        String s = "" +
        "class Class1:         \n" +
        "    def met1(self, a):\n" +
        "        pass";
        SimpleNode node = parseLegalDocStr(s);
        Module m = (Module) node;
        ClassDef d = (ClassDef) m.body[0];
        FunctionDef f = (FunctionDef) d.body[0];
        assertEquals("self",((Name)f.args.args[0]).id);
        assertEquals("a",((Name)f.args.args[1]).id);
    }
    
    public void testMultilineStr() {
    	String s = "" +
    	"a = '''\n" +
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n"+
    	"really really big string\n" +
    	"really really big string\n" +
    	"really really big string\n" +
    	"'''";
    	parseLegalDocStr(s);
    }
    
    public void testErr() {
    	String s = "" +
    	"def m():\n" +
    	"    call(a,";
    	parseILegalDoc(new Document(s));
    }
    
    
    public void testEmptyBaseForClass() {
        String s = "" +
        "class B2(): pass\n" +
        "\n" +
        "";
        parseLegalDocStr(s);
    }
    public void testFor2() {
        String s = "" +
        "[x for x in 1,2,3,4]\n" +
        "";
        parseLegalDocStr(s);
    }
    public void testFor2a() {
        String s = "" +
        "[x for x in 2,3,4 if x > 2]\n" +
        "";
        parseLegalDocStr(s);
    }
    
    public void testFor3() {
        String s = "" +
        "[x() for x in lambda: True, lambda: False if x() ] \n" +
        "";
        parseLegalDocStr(s);
    }
    
    
    public void testYield() {
        String s = "" +
                "def m():\n" +
                "    yield 1";
        parseLegalDocStr(s);
    }
    
    public void testYield2() {
        setDefaultVersion(IPythonNature.GRAMMAR_PYTHON_VERSION_2_4);
    	String s = "" +
    	"class Generator:\n" +
    	"    def __iter__(self): \n" +
    	"        for a in range(10):\n" +
    	"            yield foo(a)\n" +
    	"";
    	parseLegalDocStr(s);
    }

    
    public void testDecorator() {
        String s = "" +
            "class C:\n" +
            "    \n" +
            "    @staticmethod\n" +
            "    def m():\n" +
            "        pass\n" +
            "";
        parseLegalDocStr(s);
    }
    
    public void testDecorator2() {
        String s = "" +
            "@funcattrs(status=\"experimental\", author=\"BDFL\")\n" +
            "@staticmethod\n" +
            "def longMethodNameForEffect(*args):\n" +
            "    pass\n" +
            "\n" +
            "";
        parseLegalDocStr(s);
    }
    
    public void testDecorator4() {
        String s = "" +
        "@funcattrs(1)\n" +
        "def longMethodNameForEffect(*args):\n" +
        "    pass\n" +
        "\n" +
        "";
        parseLegalDocStr(s);
    }
    
    public void testDecorator5() {
        String s = "" +
        "@funcattrs(a)\n" +
        "def longMethodNameForEffect(*args):\n" +
        "    funcattrs(1)\n" +
        "\n" +
        "";
        parseLegalDocStr(s);
    }
    
    public void testDecorator3() {
        String s = "" +
        "@funcattrs(a, 1, status=\"experimental\", author=\"BDFL\", *args, **kwargs)\n" +
        "@staticmethod1\n" +
        "@staticmethod2(b)\n" +
        "def longMethodNameForEffect(*args):\n" +
        "    pass\n" +
        "\n" +
        "";
        parseLegalDocStr(s);
    }
    
    public void testDecorator6() {
        String s = "" +
        "@funcattrs(b for b in x)\n" +
        "def longMethodNameForEffect(*args):\n" +
        "    pass\n" +
        "\n" +
        "";
        parseLegalDocStr(s);
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -