EA-83566 PyStructureViewElement considers that underlying PSI element can be invalid
[idea/community.git] / python / testSrc / com / jetbrains / python / PyStructureViewTest.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.jetbrains.python;
17
18 import com.intellij.navigation.ItemPresentation;
19 import com.intellij.openapi.command.WriteCommandAction;
20 import com.jetbrains.python.fixtures.PyTestCase;
21 import com.jetbrains.python.psi.PyFunction;
22 import com.jetbrains.python.structureView.PyStructureViewElement;
23
24 import javax.swing.*;
25
26 import static com.intellij.testFramework.PlatformTestUtil.assertTreeEqual;
27
28 /**
29  * @author vlan
30  */
31 public class PyStructureViewTest extends PyTestCase {
32   private static final String TEST_DIRECTORY = "structureView/";
33
34   public void testBaseClassNames() {
35     myFixture.configureByFiles(TEST_DIRECTORY + "baseClassNames.py",
36                                TEST_DIRECTORY + "lib1.py");
37     doTest("-baseClassNames.py\n" +
38            " -B1\n" +
39            "  f(self, x)\n" +
40            " -B2(object)\n" +
41            "  g(x)\n" +
42            " C(B1, B2)\n" +
43            " D1(C)\n" +
44            " D2(C)\n" +
45            " D3(lib1.C)\n" +
46            " D4(foo.bar.C)\n",
47            false);
48   }
49
50   // PY-3371
51   public void testAttributes() {
52     myFixture.configureByFile(TEST_DIRECTORY + "attributes.py");
53     doTest("-attributes.py\n" +
54            " -B(object)\n" +
55            "  f(self, x)\n" +
56            "  __init__(self, x, y)\n" +
57            "  g(cls, x)\n" +
58            "  c1\n" +
59            "  c2\n" +
60            "  i1\n" +
61            "  i2\n" +
62            "  i3\n" +
63            " g1\n" +
64            " -C(B)\n" +
65            "  __init__(self, x, y)\n" +
66            "  h(self)\n" +
67            "  c2\n" +
68            "  c3\n" +
69            "  i3\n" +
70            "  i4\n" +
71            "  i5\n" +
72            " g2\n",
73            false);
74   }
75
76   // PY-3936
77   public void testInherited() {
78     myFixture.configureByFile(TEST_DIRECTORY + "inherited.py");
79     doTest("-inherited.py\n" +
80            " -C(object)\n" +
81            "  f(self, x)\n" +
82            "  __str__(self)\n" +
83            "  x\n" +
84            "  __delattr__(self, name)\n" +
85            "  __format__(self, *args, **kwargs)\n" +
86            "  __getattribute__(self, name)\n" +
87            "  __hash__(self)\n" +
88            "  __init__(self)\n" +
89            "  __new__(cls, *more)\n" +
90            "  __reduce_ex__(self, *args, **kwargs)\n" +
91            "  __reduce__(self, *args, **kwargs)\n" +
92            "  __repr__(self)\n" +
93            "  __setattr__(self, name, value)\n" +
94            "  __sizeof__(self)\n" +
95            "  __subclasshook__(cls, subclass)\n" +
96            "  __class__\n" +
97            "  __dict__\n" +
98            "  __doc__\n" +
99            "  __module__\n",
100            true);
101   }
102
103   // EA-83566
104   public void testInvalidatedElement() {
105     myFixture.configureByText("a.py",
106                               "def f():\n" +
107                               "    pass");
108     final PyFunction function = myFixture.findElementByText("f", PyFunction.class);
109     final PyStructureViewElement node = new PyStructureViewElement(function);
110     WriteCommandAction.runWriteCommandAction(myFixture.getProject(), function::delete);
111     assertNull(node.getValue());
112     final ItemPresentation presentation = node.getPresentation();
113     assertNotNull(presentation);
114     final Icon icon = presentation.getIcon(false);
115     assertNull(icon);
116   }
117
118   private void doTest(final String expected, final boolean inherited) {
119     myFixture.testStructureView(component -> {
120       component.setActionActive("SHOW_INHERITED", !inherited);
121       assertTreeEqual(component.getTree(), expected);
122     });
123   }
124 }