new inference: collect additional constraints from lambda return expressions when...
[idea/community.git] / java / java-tests / testSrc / com / intellij / codeInsight / daemon / lambda / Java8ExpressionsCheckTest.java
1 /*
2  * Copyright 2000-2015 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.intellij.codeInsight.daemon.lambda;
17
18 import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
19 import com.intellij.openapi.application.ApplicationManager;
20 import com.intellij.openapi.projectRoots.Sdk;
21 import com.intellij.psi.*;
22 import com.intellij.psi.util.PsiTreeUtil;
23 import com.intellij.testFramework.IdeaTestUtil;
24 import org.jetbrains.annotations.NonNls;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.List;
30
31 public class Java8ExpressionsCheckTest extends LightDaemonAnalyzerTestCase {
32   @NonNls static final String BASE_PATH = "/codeInsight/daemonCodeAnalyzer/lambda/expressions";
33
34   public void testSecondConflictResolutionOnSameMethodCall() throws Exception {
35     doTestAllMethodCallExpressions();
36   }
37
38   public void testNestedLambdaAdditionalConstraints() throws Exception {
39     doTestAllMethodCallExpressions();
40   }
41
42   public void testAvoidClassRefCachingDuringInference() throws Exception {
43     doTestAllMethodCallExpressions();
44   }
45
46   public void testInfinitiveParameterBoundsCheck() throws Exception {
47     doTestAllMethodCallExpressions();
48   }
49
50   public void testCachedUnresolvedMethods() throws Exception {
51     doTestCachedUnresolved();
52   }
53
54   public void testCacheUnresolvedMethods2() throws Exception {
55     doTestCachedUnresolved();
56   }
57   
58   public void testCacheUnresolvedMethods3() throws Exception {
59     doTestCachedUnresolved();
60   }
61
62   private void doTestCachedUnresolved() {
63     configureByFile(BASE_PATH + "/" + getTestName(false) + ".java");
64     PsiMethodCallExpression callExpression =
65       PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiMethodCallExpression.class);
66
67     assertNotNull(callExpression);
68     assertNotNull(callExpression.getType());
69
70     final Collection<PsiCallExpression> methodCallExpressions = PsiTreeUtil.findChildrenOfType(getFile(), PsiCallExpression.class);
71     for (PsiCallExpression expression : methodCallExpressions) {
72       assertNotNull("Failed inference for: " + expression.getText(), expression.getType());
73     }
74   }
75
76   public void testIDEA140035() throws Exception {
77     doTestAllMethodCallExpressions();
78     final Collection<PsiParameter> parameterLists = PsiTreeUtil.findChildrenOfType(getFile(), PsiParameter.class);
79     for (PsiParameter parameter : parameterLists) {
80       if (parameter.getTypeElement() != null) continue;
81       getPsiManager().dropResolveCaches();
82       final PsiType type = parameter.getType();
83       assertFalse("Failed inference for: " + parameter.getParent().getText(), type instanceof PsiLambdaParameterType);
84     }
85   }
86
87   public void testAdditionalConstraintsBasedOnLambdaResolution() throws Exception {
88     doTestAllMethodCallExpressions();
89   }
90
91   private void doTestAllMethodCallExpressions() {
92     configureByFile(BASE_PATH + "/" + getTestName(false) + ".java");
93     final Collection<PsiCallExpression> methodCallExpressions = PsiTreeUtil.findChildrenOfType(getFile(), PsiCallExpression.class);
94     for (PsiCallExpression expression : methodCallExpressions) {
95       getPsiManager().dropResolveCaches();
96       assertNotNull("Failed inference for: " + expression.getText(), expression.getType());
97     }
98
99     final Collection<PsiReferenceParameterList> parameterLists = PsiTreeUtil.findChildrenOfType(getFile(), PsiReferenceParameterList.class);
100     for (PsiReferenceParameterList list : parameterLists) {
101       getPsiManager().dropResolveCaches();
102       final PsiType[] arguments = list.getTypeArguments();
103       assertNotNull("Failed inference for: " + list.getParent().getText(), arguments);
104     }
105   }
106
107   @Override
108   protected Sdk getProjectJDK() {
109     return IdeaTestUtil.getMockJdk18();
110   }
111 }