Search in sources :

Example 26 with FunctionDef

use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.

the class FastDefinitionsParserTest method testDefinitionsParser20.

public void testDefinitionsParser20() {
    Module m = (Module) FastDefinitionsParser.parse("def methodempty():\n" + "    pass\n" + "\n" + "if a:\n" + "    def method():\n" + "        a = 10\n" + "else:\n" + "    def method2():\n" + "        bar = 10\n" + "");
    assertEquals(3, m.body.length);
    FunctionDef d = (FunctionDef) m.body[1];
    assertEquals("method", NodeUtils.getRepresentationString(d.name));
    assertNull(d.body);
    d = (FunctionDef) m.body[2];
    assertEquals("method2", NodeUtils.getRepresentationString(d.name));
    assertNull(d.body);
}
Also used : FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Module(org.python.pydev.parser.jython.ast.Module)

Example 27 with FunctionDef

use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.

the class FastDefinitionsParserTest method testMixSpacesAndTabs.

/**
 * A tab must be counted as 8 spaces!
 */
public void testMixSpacesAndTabs() {
    Module m = (Module) FastDefinitionsParser.parse("" + "def method():\n" + "\n" + "\tfor remainder in match_1.remainders:\n" + "\t    a_2 = remainder.a\n" + "            c_2 = remainder.c\n" + "            match = a\n" + "            if not match.isError():\n" + "                return Match()\n" + "        return Error(\"\")");
    assertEquals(1, m.body.length);
    assertEquals("method", NodeUtils.getRepresentationString(((FunctionDef) m.body[0]).name));
}
Also used : FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Module(org.python.pydev.parser.jython.ast.Module)

Example 28 with FunctionDef

use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.

the class FastDefinitionsParserTest method testAttributes5.

public void testAttributes5() {
    Module m = (Module) FastDefinitionsParser.parse("class Bar:\n" + "    def m1(self):\n" + // local scope: get it because of self.
    "        self.ATTRIBUTE0 = 10\n" + // local scope: get it because of self.
    "        self.ATTRIBUTE1 = 10\n" + // local scope: get it because of self.
    "        self.ATTRIBUTE2 = = 10\n" + "\n" + "");
    assertEquals(1, m.body.length);
    ClassDef classDef = ((ClassDef) m.body[0]);
    assertEquals("Bar", ((NameTok) classDef.name).id);
    // method
    assertEquals(1, classDef.body.length);
    FunctionDef funcDef = (FunctionDef) classDef.body[0];
    assertEquals("m1", ((NameTok) funcDef.name).id);
    for (int i = 0; i < 3; i++) {
        Assign assign = (Assign) funcDef.body[i];
        assertEquals(1, assign.targets.length);
        Attribute attribute = (Attribute) assign.targets[0];
        NameTok attr = (NameTok) attribute.attr;
        assertEquals("ATTRIBUTE" + i, attr.id.toString());
    }
    assertEquals(3, funcDef.body.length);
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) Attribute(org.python.pydev.parser.jython.ast.Attribute) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Assign(org.python.pydev.parser.jython.ast.Assign) Module(org.python.pydev.parser.jython.ast.Module) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 29 with FunctionDef

use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.

the class FastDefinitionsParserTest method testDefinitionsParser14.

public void testDefinitionsParser14() {
    Module m = (Module) FastDefinitionsParser.parse("\n" + "def method():\n" + "    a = 10\n" + "class F:\n" + "    def method2(self):\n" + "        self.bar = 10\n" + "def another():\n" + "    b = 20\n" + "");
    assertEquals(3, m.body.length);
    FunctionDef d = (FunctionDef) m.body[0];
    assertEquals("method", NodeUtils.getRepresentationString(d.name));
    assertNull(d.body);
    ClassDef cd = (ClassDef) m.body[1];
    assertEquals("F", NodeUtils.getRepresentationString(cd.name));
    assertEquals(1, cd.body.length);
    d = (FunctionDef) m.body[2];
    assertEquals("another", NodeUtils.getRepresentationString(d.name));
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Module(org.python.pydev.parser.jython.ast.Module)

Example 30 with FunctionDef

use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.

the class FastDefinitionsParserTest method testDefinitionsParser6.

public void testDefinitionsParser6() {
    Module m = (Module) FastDefinitionsParser.parse("class Bar(object):\n" + "    class Zoo(object):\n" + "        def m1(self):pass\n" + "def m2(self):pass\n");
    assertEquals(2, m.body.length);
    ClassDef classDefBar = (ClassDef) m.body[0];
    assertEquals(1, classDefBar.beginColumn);
    assertEquals(1, classDefBar.beginLine);
    assertEquals("Bar", ((NameTok) classDefBar.name).id);
    ClassDef classDefZoo = (ClassDef) classDefBar.body[0];
    assertEquals("Zoo", ((NameTok) classDefZoo.name).id);
    assertEquals("m1", ((NameTok) ((FunctionDef) classDefZoo.body[0]).name).id);
    assertEquals("m2", ((NameTok) ((FunctionDef) m.body[1]).name).id);
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Module(org.python.pydev.parser.jython.ast.Module)

Aggregations

FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)137 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)63 Module (org.python.pydev.parser.jython.ast.Module)44 SimpleNode (org.python.pydev.parser.jython.SimpleNode)36 NameTok (org.python.pydev.parser.jython.ast.NameTok)30 ArrayList (java.util.ArrayList)27 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)27 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)26 Name (org.python.pydev.parser.jython.ast.Name)25 Assign (org.python.pydev.parser.jython.ast.Assign)17 org.python.pydev.parser.jython.ast.argumentsType (org.python.pydev.parser.jython.ast.argumentsType)17 Attribute (org.python.pydev.parser.jython.ast.Attribute)15 Call (org.python.pydev.parser.jython.ast.Call)14 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)13 Expr (org.python.pydev.parser.jython.ast.Expr)13 Return (org.python.pydev.parser.jython.ast.Return)12 AdapterPrefs (org.python.pydev.parser.jython.ast.factory.AdapterPrefs)12 PyAstFactory (org.python.pydev.parser.jython.ast.factory.PyAstFactory)12 TokensList (org.python.pydev.core.TokensList)10 Yield (org.python.pydev.parser.jython.ast.Yield)10