summaryrefslogtreecommitdiff
path: root/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/TypeInference2_3Test.groovy
blob: 7382469e50e46a9f13e3d1c30df0952d085eb765 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.groovy.lang.resolve

import com.intellij.testFramework.LightProjectDescriptor
import org.jetbrains.plugins.groovy.GroovyLightProjectDescriptor

/**
 * Created by Max Medvedev on 10/02/14
 */
class TypeInference2_3Test extends TypeInferenceTestBase {
  @Override
  protected LightProjectDescriptor getProjectDescriptor() {
    return GroovyLightProjectDescriptor.GROOVY_2_3
  }

  public void testContravariantType() throws Exception {
    doTest('''\
import groovy.transform.CompileStatic
import java.util.concurrent.Callable

@CompileStatic
class TestCase {

    interface Action<T> {
        void execute(T thing)
    }

    static class Wrapper<T> {

        private final T thing

        Wrapper(T thing) {
            this.thing = thing
        }

        void contravariantTake(Action<? super T> action) {
            action.execute(thing)
        }

    }

    static <T> Wrapper<T> wrap(Callable<T> callable) {
        new Wrapper(callable.call())
    }

    static Integer dub(Integer integer) {
        integer * 2
    }

    static void main(String[] args) {
        wrap {
            1
        } contravariantTake {
            dub(i<caret>t) // fails static compile, 'it' is not known to be Integer
        }
    }

}
''', 'java.lang.Integer')
  }

  void testSAMInference() {
    doTest('''\
import groovy.transform.CompileStatic

interface CustomCallable<T> {
  T call()
}

class Thing {
  static <T> T customType(CustomCallable<T> callable) {
    callable.call()
  }

  @CompileStatic
  static void run() {
    customType { [] }.ad<caret>d(1) // return type is not inferred - fails compile
  }
}
''', "java.lang.Boolean")
  }

  void testSAMInference2() {
    doTest('''\
import groovy.transform.CompileStatic

interface CustomCallable<T> {
  List<T> call()
}

class Thing {
  static <T> T first(CustomCallable<T> callable) {
    callable.call().iterator().next()
  }

  @CompileStatic
  static void run() {
    first { [[]] }.ad<caret>d(1) // return type is not inferred - fails compile
  }
}
''', "java.lang.Boolean")
  }

  void testSAMInference3() {
    doTest('''\
import groovy.transform.CompileStatic

interface CustomCallable<K, V> {
    Map<K, V> call()
}

class Thing {
    static <K, V> Map<K, V> customType(CustomCallable<K, V> callable) {
        callable.call()
    }

    @CompileStatic
    static void run() {
        customType { [(1):3] }.pu<caret>t(1, 5) // return type is not inferred - fails compile
    }
}

''', 'java.lang.Integer')
  }

  void testSamInference4() {
    doTest('''
interface Action<T> {
    void execute(T t)
}

public <T> void exec(T t, Action<T> f) {
}


def foo() {
    exec('foo') {print i<caret>t.toUpperCase() ;print 2 }
}

''', 'java.lang.String')
  }

  void testSamInference5() {
    doTest('''
interface Action<T> {
    void execute(T t)
}

public <T> void exec(T t, Action<T> f) {
}


def foo() {
    exec('foo') {i<caret>t.toUpperCase() }
}

''', 'java.lang.String')
  }

  void testSamInference6() {
    doTest('''
interface Action<T> {
    void execute(T t)
}

public <T> void exec(T t, Action<T> f) {
}


def foo() {
    exec('foo') {print i<caret>t.toUpperCase() }
}

''', 'java.lang.String')
  }

  void testSamInference7() {
    doTest('''
interface CustomCallable<T> {
    T call()
}

class Thing {
    static <T> T customType(CustomCallable<T> callable) {
    }

    static void run() {
        customType { i<caret>t }
    }
}''', null)
  }


}