JetOverloadTest.java 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2010-2012 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.
 */

17 18 19 20 21
package org.jetbrains.jet.types;

import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.JetTestCaseBuilder;
import org.jetbrains.jet.JetTestUtils;
22
import org.jetbrains.jet.di.InjectorForTests;
23 24 25 26
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
27
import org.jetbrains.jet.lang.resolve.DescriptorResolver;
28
import org.jetbrains.jet.lang.resolve.OverloadUtil;
S
Stepan Koltsov 已提交
29
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
30 31 32 33 34 35 36 37

/**
 * @author Stepan Koltsov
 */
public class JetOverloadTest extends JetLiteFixture {

    private ModuleDescriptor root = new ModuleDescriptor("test_root");
    private JetStandardLibrary library;
38
    private DescriptorResolver descriptorResolver;
39 40 41 42

    @Override
    public void setUp() throws Exception {
        super.setUp();
S
Stepan Koltsov 已提交
43
        createEnvironmentWithMockJdkAndIdeaAnnotations();
44 45 46
        InjectorForTests injector = new InjectorForTests(getProject());
        library = injector.getJetStandardLibrary();
        descriptorResolver = injector.getDescriptorResolver();
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
    }

    @Override
    protected String getTestDataPath() {
        return JetTestCaseBuilder.getTestDataPathBase();
    }

    public void testBasic() throws Exception {

        assertNotOverloadable(
                "fun a() : Int",
                "fun a() : Int");

        assertNotOverloadable(
                "fun a() : Int",
                "fun a() : Any");

        assertNotOverloadable(
                "fun a<T1>() : T1",
                "fun a<T>() : T");

        assertNotOverloadable(
                "fun a<T1>(a : T1) : T1",
                "fun a<T>(a : T) : T");

        assertNotOverloadable(
                "fun a<T1, X : T1>(a : T1) : T1",
                "fun a<T, Y : T>(a : T) : T");

        assertNotOverloadable(
                "fun a<T1, X : T1>(a : T1) : T1",
                "fun a<T, Y : T>(a : T) : Y");

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////


        assertNotOverloadable(
                "fun a() : Int",
                "fun a() : Any");

        assertOverloadable(
                "fun a(a : Int) : Int",
                "fun a() : Int");

        assertOverloadable(
                "fun a() : Int",
                "fun a(a : Int) : Int");

        assertOverloadable(
                "fun a(a : Int?) : Int",
                "fun a(a : Int) : Int");

        assertNotOverloadable(
                "fun a<T>(a : Int) : Int",
                "fun a(a : Int) : Int");

103 104
        // TODO
        /*
105 106 107
        assertOverloadable(
                "fun a<T1, X : T1>(a : T1) : T1",
                "fun a<T, Y>(a : T) : T");
108
        */
109 110 111 112 113 114 115 116 117

        assertOverloadable(
                "fun a<T1, X : T1>(a : T1) : T1",
                "fun a<T, Y : T>(a : Y) : T");

        assertNotOverloadable(
                "fun a<T1, X : T1>(a : T1) : X",
                "fun a<T, Y : T>(a : T) : T");

118 119
        // TODO
        /*
120 121 122
        assertNotOverloadable(
                "fun a<T1, X : Array<out T1>>(a : Array<in T1>) : T1",
                "fun a<T, Y : Array<out T>>(a : Array<in T>) : T");
123
        */
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

        assertOverloadable(
                "fun a<T1, X : Array<T1>>(a : Array<in T1>) : T1",
                "fun a<T, Y : Array<out T>>(a : Array<in T>) : T");

        assertOverloadable(
                "fun a<T1, X : Array<out T1>>(a : Array<in T1>) : T1",
                "fun a<T, Y : Array<in T>>(a : Array<in T>) : T");

        assertOverloadable(
                "fun a<T1, X : Array<out T1>>(a : Array<in T1>) : T1",
                "fun a<T, Y : Array<*>>(a : Array<in T>) : T");

        assertOverloadable(
                "fun a<T1, X : Array<out T1>>(a : Array<in T1>) : T1",
                "fun a<T, Y : Array<out T>>(a : Array<out T>) : T");

        assertOverloadable(
                "fun a<T1, X : Array<out T1>>(a : Array<*>) : T1",
                "fun a<T, Y : Array<out T>>(a : Array<in T>) : T");

145 146 147 148
        assertOverloadable(
                "fun ff() : Int",
                "fun Int.ff() : Int"
        );
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    }

    private void assertNotOverloadable(String funA, String funB) {
        assertOverloadabilityRelation(funA, funB, true);
    }

    private void assertOverloadable(String funA, String funB) {
        assertOverloadabilityRelation(funA, funB, false);
    }

    private void assertOverloadabilityRelation(String funA, String funB, boolean expectedIsError) {
        FunctionDescriptor a = makeFunction(funA);
        FunctionDescriptor b = makeFunction(funB);
        {
163
            OverloadUtil.OverloadCompatibilityInfo overloadableWith = OverloadUtil.isOverloadable(a, b);
164 165 166
            assertEquals(overloadableWith.getMessage(), expectedIsError, !overloadableWith.isSuccess());
        }
        {
167
            OverloadUtil.OverloadCompatibilityInfo overloadableWith = OverloadUtil.isOverloadable(b, a);
168 169 170 171 172 173
            assertEquals(overloadableWith.getMessage(), expectedIsError, !overloadableWith.isSuccess());
        }
    }

    private FunctionDescriptor makeFunction(String funDecl) {
        JetNamedFunction function = JetPsiFactory.createFunction(getProject(), funDecl);
174
        return descriptorResolver.resolveFunctionDescriptor(root, library.getLibraryScope(), function, JetTestUtils.DUMMY_TRACE);
175 176 177
    }

}