Initial tests for types defined by mypy's typing module

This commit is contained in:
Andrey Vlasovskikh
2014-08-18 22:44:04 +04:00
parent 088bcbf6e6
commit 0968f995ea
2 changed files with 80 additions and 0 deletions

View File

View File

@@ -0,0 +1,80 @@
/*
* 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 com.jetbrains.python;
import com.intellij.testFramework.LightProjectDescriptor;
import com.jetbrains.python.documentation.PythonDocumentationProvider;
import com.jetbrains.python.fixtures.PyTestCase;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.types.PyType;
import com.jetbrains.python.psi.types.TypeEvalContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author vlan
*/
public class PyTypingTest extends PyTestCase {
@Nullable
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return ourPy3Descriptor;
}
@Override
public void setUp() throws Exception {
super.setUp();
setLanguageLevel(LanguageLevel.PYTHON32);
}
@Override
public void tearDown() throws Exception {
setLanguageLevel(null);
super.tearDown();
}
public void testClassType() {
doTest("Foo",
"class Foo:" +
" pass\n" +
"\n" +
"def f(expr: Foo):\n" +
" pass\n");
}
public void testClassReturnType() {
doTest("Foo",
"class Foo:" +
" pass\n" +
"\n" +
"def f() -> Foo:\n" +
" pass\n" +
"\n" +
"expr = f()\n");
}
private void doTest(@NotNull String expectedType, @NotNull String text) {
myFixture.copyDirectoryToProject("typing", "");
myFixture.configureByText(PythonFileType.INSTANCE, text);
final PyExpression expr = myFixture.findElementByText("expr", PyExpression.class);
final TypeEvalContext context = TypeEvalContext.userInitiated(expr.getContainingFile()).withTracing();
PyType actual = context.getType(expr);
final String actualType = PythonDocumentationProvider.getTypeName(actual, context);
assertEquals(expectedType, actualType);
}
}