nullity: initial tests

This commit is contained in:
anna
2010-09-06 12:07:26 +04:00
parent 58eae8963e
commit 0f1d1622f7
13 changed files with 466 additions and 4 deletions
@@ -0,0 +1,51 @@
import org.jetbrains.annotations.*;
class Test {
@NotNull
String myFoo = "";
@Nullable
String myFoo1 = null;
@NotNull
String myFoo2 = foo2();
@NotNull String foo2() { return "";}
@Nullable
String myFoo3 = foo3();
@Nullable String foo3() { return null;}
String myFoo4;
void setFoo4() {
myFoo4 = "";
}
@NotNull
final String myFoo5;
@Nullable
final String myFoo6;
@NotNull
final String myFoo7;
@Nullable
final String myFoo8;
final String myFoo9;
@Nullable
final String myFoo10;
/**
* {@link #myFoo6}
*/
Test(@NotNull String param, @Nullable String paramNullable, String simpleParam) {
myFoo5 = "";
myFoo6 = null;
myFoo7 = param;
myFoo8 = paramNullable;
myFoo9 = simpleParam;
myFoo10 = foo10(false);
}
@Nullable
String foo10(boolean flag) {
return flag ? foo2() : foo3();
}
}
@@ -0,0 +1,84 @@
import org.jetbrains.annotations.*;
class Test {
@Nullable
String foo1() {
return null;
}
@NotNull
String foo2() {
return "";
}
String foo3(String s) {
return s;
}
String foo4(@NotNull String s) {
return s.substring(0);
}
@NotNull
Integer foo5(Integer i) {
return i++;
}
@NotNull
Integer foo6(Integer i) {
if (i == 0) return 1;
return i * foo6(i--);
}
@Nullable
Integer foo7(boolean flag) {
return flag ? null : 1;
}
@Nullable
Integer foo8(boolean flag) {
if (flag) {
return null;
}
else {
return 1;
}
}
@Nullable
String bar9() {
return foo3("");
}
@Nullable
String foo9() {
return bar9();
}
@Nullable
String bar10() {
return foo3("");
}
@NotNull
String bar101() {
return foo3("");
}
@Nullable
String foo10(boolean flag) {
return flag ? bar10() : bar101();
}
@NotNull
String foo11() {
class Foo{
@Nullable
String mess() {
return null;
}
}
return "";
}
}
@@ -0,0 +1,37 @@
import org.jetbrains.annotations.*;
class Test {
void bar(@Nullable String str) {
if (str == null) {
foo(str);
}
}
String foo(String str) {
return str;
}
@Nullable
String foo1(@Nullable String str) {
if (str == null);
return (str);
}
@NotNull
String foo2(@Nullable String str) {
if (str == null);
return ((String)str);
}
@Nullable
String fram(@Nullable String str, boolean b) {
if (str != null) {
return b ? str : "not null strimg";
}
return "str was null";
}
}
@@ -0,0 +1,15 @@
import org.jetbrains.annotations.*;
class Test {
void foo(@NotNull String s) {
s.substring(0);
}
/**
* @param str
*/
void bar(@NotNull String str) {
if (str.substring(0) == null) {
}
}
}
@@ -0,0 +1,10 @@
import org.jetbrains.annotations.*;
class Test {
void foo(@NotNull String s) {
}
void bar(@NotNull String str) {
foo(str);
}
}
@@ -0,0 +1,41 @@
import org.jetbrains.annotations.*;
class Test {
String myFoo = "";
String myFoo1 = null;
String myFoo2 = foo2();
@NotNull String foo2() { return "";}
String myFoo3 = foo3();
@Nullable String foo3() { return null;}
String myFoo4;
void setFoo4() {
myFoo4 = "";
}
final String myFoo5;
final String myFoo6;
final String myFoo7;
final String myFoo8;
final String myFoo9;
final String myFoo10;
/**
* {@link #myFoo6}
*/
Test(@NotNull String param, @Nullable String paramNullable, String simpleParam) {
myFoo5 = "";
myFoo6 = null;
myFoo7 = param;
myFoo8 = paramNullable;
myFoo9 = simpleParam;
myFoo10 = foo10(false);
}
String foo10(boolean flag) {
return flag ? foo2() : foo3();
}
}
@@ -0,0 +1,74 @@
import org.jetbrains.annotations.*;
class Test {
String foo1() {
return null;
}
String foo2() {
return "";
}
String foo3(String s) {
return s;
}
String foo4(String s) {
return s.substring(0);
}
Integer foo5(Integer i) {
return i++;
}
Integer foo6(Integer i) {
if (i == 0) return 1;
return i * foo6(i--);
}
Integer foo7(boolean flag) {
return flag ? null : 1;
}
Integer foo8(boolean flag) {
if (flag) {
return null;
}
else {
return 1;
}
}
@Nullable
String bar9() {
return foo3("");
}
String foo9() {
return bar9();
}
@Nullable
String bar10() {
return foo3("");
}
@NotNull
String bar101() {
return foo3("");
}
String foo10(boolean flag) {
return flag ? bar10() : bar101();
}
String foo11() {
class Foo{
String mess() {
return null;
}
}
return "";
}
}
@@ -0,0 +1,34 @@
import org.jetbrains.annotations.*;
class Test {
void bar(String str) {
if (str == null) {
foo(str);
}
}
String foo(String str) {
return str;
}
String foo1(String str) {
if (str == null);
return (str);
}
String foo2(String str) {
if (str == null);
return ((String)str);
}
String fram(String str, boolean b) {
if (str != null) {
return b ? str : "not null strimg";
}
return "str was null";
}
}
@@ -0,0 +1,15 @@
import org.jetbrains.annotations.*;
class Test {
void foo(String s) {
s.substring(0);
}
/**
* @param str
*/
void bar(String str) {
if (str.substring(0) == null) {
}
}
}
@@ -0,0 +1,10 @@
import org.jetbrains.annotations.*;
class Test {
void foo(@NotNull String s) {
}
void bar(String str) {
foo(str);
}
}
@@ -0,0 +1,91 @@
/*
* Copyright 2000-2010 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.intellij.codeInsight;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.inferNullity.NullityInferrer;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
/**
* User: anna
* Date: Sep 2, 2010
*/
public class NullityInferrerTest extends CodeInsightTestCase {
@Override
protected String getTestDataPath() {
return JavaTestUtil.getJavaTestDataPath();
}
//-----------------------params and return values---------------------------------
public void testParameterPassed2NotNull() throws Exception {
doTest(false);
}
public void testParameterCheckedForNull() throws Exception {
doTest(false);
}
public void testParameterDereferenced() throws Exception {
doTest(false);
}
//-----------------------fields---------------------------------------------------
public void testFieldsAssignment() throws Exception {
doTest(false);
}
//-----------------------methods---------------------------------------------------
public void testMethodReturnValue() throws Exception {
doTest(false);
}
private void doTest(boolean annotateLocalVariables) throws Exception {
final String nullityPath = "/codeInsight/nullityinferrer";
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
final VirtualFile aLib = LocalFileSystem.getInstance().findFileByPath(getTestDataPath() + nullityPath + "/lib/annotations.jar");
if (aLib != null) {
final VirtualFile file = JarFileSystem.getInstance().getJarRootForLocalFile(aLib);
if (file != null) {
final ModifiableRootModel model = ModuleRootManager.getInstance(myModule).getModifiableModel();
final LibraryTable libraryTable = model.getModuleLibraryTable();
final Library library = libraryTable.createLibrary("test");
final Library.ModifiableModel libraryModel = library.getModifiableModel();
libraryModel.addRoot(file.getUrl(), OrderRootType.CLASSES);
libraryModel.commit();
model.commit();
}
}
}
});
configureByFile(nullityPath + "/before" + getTestName(false) + ".java");
final NullityInferrer nullityInferrer = new NullityInferrer(annotateLocalVariables, getProject());
nullityInferrer.collect(getFile());
nullityInferrer.apply(getProject());
checkResultByFile(nullityPath + "/after" + getTestName(false)+ ".java");
}
}