ask each intermediate context file for a forced resolve scope (IDEA-136304)
[idea/community.git] / java / java-tests / testSrc / com / intellij / psi / resolve / ResolveInCodeFragmentTest.java
1 package com.intellij.psi.resolve;
2
3 import com.intellij.psi.*;
4 import com.intellij.psi.impl.search.JavaSourceFilterScope;
5 import com.intellij.psi.util.PsiTreeUtil;
6 import com.intellij.testFramework.ResolveTestCase;
7
8 /**
9  * @author max
10  */
11 public class ResolveInCodeFragmentTest extends ResolveTestCase {
12   public void testLocalVariable() throws Exception {
13     final PsiReference iRef = configure();
14
15     PsiElement context = PsiTreeUtil.getParentOfType(iRef.getElement(), PsiCodeBlock.class);
16     JavaCodeFragment codeFragment = JavaCodeFragmentFactory.getInstance(myProject)
17       .createExpressionCodeFragment(iRef.getElement().getText(), context, null, true);
18     codeFragment.setVisibilityChecker(JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE);
19
20     PsiElement[] fileContent = codeFragment.getChildren();
21     assertEquals(1, fileContent.length);
22     assertTrue(fileContent[0] instanceof PsiExpression);
23
24     PsiExpression expr = (PsiExpression) fileContent[0];
25     expr.accept(new JavaRecursiveElementWalkingVisitor() {
26       @Override public void visitReferenceExpression(PsiReferenceExpression expression) {
27         assertEquals(iRef.resolve(),
28                      expression.resolve());
29       }
30     });
31   }
32
33   public void testjavaLangClass() throws Exception {
34     PsiCodeFragment codeFragment = JavaCodeFragmentFactory.getInstance(myProject).createExpressionCodeFragment(
35           "Boolean.getBoolean(\"true\")", null, null, true);
36
37     PsiElement[] fileContent = codeFragment.getChildren();
38     assertEquals(1, fileContent.length);
39     assertTrue(fileContent[0] instanceof PsiExpression);
40
41     PsiExpression expr = (PsiExpression) fileContent[0];
42     assertNotNull(expr.getType());
43     assertEquals("boolean", expr.getType().getCanonicalText());
44   }
45
46   private PsiReference configure() throws Exception {
47     return configureByFile("codeFragment/" + getTestName(false) + ".java");
48   }
49
50   public void testResolveScopeWithFragmentContext() throws Exception {
51     PsiElement physical = configureByFile("codeFragment/LocalVariable.java").getElement();
52     JavaCodeFragment fragment = JavaCodeFragmentFactory.getInstance(myProject)
53       .createExpressionCodeFragment("ref", physical, null, true);
54     fragment.forceResolveScope(new JavaSourceFilterScope(physical.getResolveScope()));
55     assertFalse(fragment.getResolveScope().equals(physical.getResolveScope()));
56
57     PsiExpression lightExpr = JavaPsiFacade.getElementFactory(myProject).createExpressionFromText("xxx.xxx", fragment);
58     assertEquals(lightExpr.getResolveScope(), fragment.getResolveScope());
59   }
60 }