Inline function: add extra tests taken from intellij-community

This commit is contained in:
Mikhail Glukhikh
2017-03-30 17:21:46 +03:00
parent b8b7d1f644
commit 8cfcd7e5a6
37 changed files with 661 additions and 0 deletions
@@ -0,0 +1,10 @@
fun usage(arr: IntArray) {
val array: IntArray
for (i in array.indices) {
method(array[i])
}
}
fun <caret>method(i: Int) {
println(i)
}
@@ -0,0 +1,7 @@
fun usage(arr: IntArray) {
val array: IntArray
for (i in array.indices) {
println(array[i])
}
}
@@ -0,0 +1,14 @@
import java.util.LinkedHashSet
class A {
fun <T> bar(root: List<T>, list: LinkedHashSet<List<T>>) {
addIfNotNull(root, list)
}
private fun <T> <caret>addIfNotNull(element: T, result: Collection<T>) {
nested(result, element)
}
private fun <S> nested(result: Collection<S>, element: S) {}
}
@@ -0,0 +1,10 @@
import java.util.LinkedHashSet
class A {
fun <T> bar(root: List<T>, list: LinkedHashSet<List<T>>) {
nested(list, root)
}
private fun <S> nested(result: Collection<S>, element: S) {}
}
@@ -0,0 +1,19 @@
class C {
fun doSomething() {
val it = getSomeObjects().iterator()
while (it.hasNext()) {
println("text = " + it.next())
}
}
private fun <caret>getSomeObjects(): Collection<Any> {
val text = "hello"
return getSomeObjects(text)
}
private fun getSomeObjects(text: String): Collection<Any> {
val list = arrayListOf<Any>()
list.add(text)
return list
}
}
@@ -0,0 +1,15 @@
class C {
fun doSomething() {
val text = "hello"
val it = getSomeObjects(text).iterator()
while (it.hasNext()) {
println("text = " + it.next())
}
}
private fun getSomeObjects(text: String): Collection<Any> {
val list = arrayListOf<Any>()
list.add(text)
return list
}
}
@@ -0,0 +1,17 @@
class Foo {
fun getComponent(i: Int?): String? {
return null
}
var myI: Int? = null
fun usage() {
if (myI != null)
method(myI)
}
fun <caret>method(i: Int?) {
println(getComponent(myI)!! + getComponent(i)!!)
}
}
@@ -0,0 +1,14 @@
class Foo {
fun getComponent(i: Int?): String? {
return null
}
var myI: Int? = null
fun usage() {
if (myI != null) {
println(getComponent(myI)!! + getComponent(myI)!!)
}
}
}
@@ -0,0 +1,7 @@
// ERROR: Cannot perform refactoring.\nInline Function is not supported for functions with multiple return statements.
enum class EnumWithConstructor(s: String, s2: String) {
Test("1"), Rest("2", "b");
<caret>constructor(s: String): this(s, "")
}
@@ -0,0 +1,7 @@
class A {
val field = foo()
fun <caret>foo(): Int {
return 1
}
}
@@ -0,0 +1,4 @@
class A {
val field = 1
}
@@ -0,0 +1,13 @@
fun xxx() {
foo(*arrayOf("aa", "hh"))
}
fun <caret>foo(vararg ss: String) {
bar(*ss)
}
fun bar(s: String, ss: String) {
}
fun bar(vararg ss: String) {}
@@ -0,0 +1,9 @@
fun xxx() {
bar("aa", "hh")
}
fun bar(s: String, ss: String) {
}
fun bar(vararg ss: String) {}
@@ -0,0 +1,13 @@
class A {
private val i = 0
fun foo() {
i.toString()
}
}
class B : A() {
fun bar() {
<caret>foo()
}
}
@@ -0,0 +1,10 @@
class A {
private val i = 0
}
class B : A() {
fun bar() {
i.toString()
}
}
@@ -0,0 +1,15 @@
import java.util.*
object Test {
private fun <T> <caret>foo(): List<T> {
return emptyList<T>()
}
private fun <T> bar(xs: List<T>): List<T> {
return xs
}
private fun gazonk() {
val ss = bar(Test.foo<String>())
}
}
@@ -0,0 +1,10 @@
object Test {
private fun <T> bar(xs: List<T>): List<T> {
return xs
}
private fun gazonk() {
val ss = bar(emptyList<String>())
}
}
@@ -0,0 +1,23 @@
object Foo {
fun <caret>foo() {
bar(object : Runnable {
override fun run() {
doRun()
}
private fun doRun() {
// Woo-hoo
}
})
}
fun bar(runnable: Runnable) {
runnable.run()
}
}
internal object Bar {
@JvmStatic fun main(args: Array<String>) {
Foo.foo()
}
}
@@ -0,0 +1,22 @@
import Foo.bar
object Foo {
fun bar(runnable: Runnable) {
runnable.run()
}
}
internal object Bar {
@JvmStatic fun main(args: Array<String>) {
bar(object : Runnable {
override fun run() {
Foo.doRun()
}
private fun doRun() {
// Woo-hoo
}
})
}
}
@@ -0,0 +1,14 @@
object Foo {
val bars = arrayOf(Bar.callMe("a", 0, "A", "B", "C"), Bar.callMe("b", 1, "A", "B"))
}
class Bar(a: String, nr: Int, vararg args: String) {
companion object {
fun call<caret>Me(a: String, nr: Int, vararg args: String): Bar {
return Bar(a, nr, *args)
}
}
}
@@ -0,0 +1,8 @@
object Foo {
val bars = arrayOf(Bar("a", 0, "A", "B", "C"), Bar("b", 1, "A", "B"))
}
class Bar(a: String, nr: Int, vararg args: String) {
}
@@ -0,0 +1,31 @@
class Element {
var id: String? = null
fun <caret>getName(): String = id
fun method(element: Element): String {
return getName() + element.getName()
}
fun staticMethod(element: Element): String {
val buffer = StringBuffer()
buffer.append(element.getName())
return buffer.toString()
}
companion object {
fun toXML(element: Element): Element {
val el = X("El")
el.setAttribute("attr", element.getName())
return el
}
}
}
class Usage {
fun staticMethod(element: Element): String {
val buffer = StringBuffer()
buffer.append(element.getName())
return buffer.toString()
}
}
@@ -0,0 +1,29 @@
class Element {
var id: String? = null
fun method(element: Element): String {
return id + element.id
}
fun staticMethod(element: Element): String {
val buffer = StringBuffer()
buffer.append(element.id)
return buffer.toString()
}
companion object {
fun toXML(element: Element): Element {
val el = X("El")
el.setAttribute("attr", element.id)
return el
}
}
}
class Usage {
fun staticMethod(element: Element): String {
val buffer = StringBuffer()
buffer.append(element.id)
return buffer.toString()
}
}
@@ -0,0 +1,17 @@
// ERROR: Cannot perform refactoring.\nInline Function is not supported for functions with return statements not at the end of the body.
class A {
init {
g()
}
fun <caret>g(): Int {
try {
return 0
}
catch (e: Error) {
throw e
}
}
}
@@ -0,0 +1,22 @@
class AAA {
fun fff(myProject: Project) {
ensureFilesWritable(myProject, *Array(1) { "2" })
}
private fun ensure<caret>FilesWritable(project: Project, vararg strings: String): Boolean {
return !ensureFilesWritable(strings).hasReadonlyFiles()
}
private fun ensureFilesWritable(strings: Array<out String>): Status {
return Status(strings)
}
inner class Status(strings: Array<out String>) {
fun hasReadonlyFiles(): Boolean {
return true
}
}
inner class Project
}
@@ -0,0 +1,18 @@
class AAA {
fun fff(myProject: Project) {
!ensureFilesWritable(Array(1) { "2" }).hasReadonlyFiles()
}
private fun ensureFilesWritable(strings: Array<out String>): Status {
return Status(strings)
}
inner class Status(strings: Array<out String>) {
fun hasReadonlyFiles(): Boolean {
return true
}
}
inner class Project
}
@@ -0,0 +1,13 @@
fun foo(fe: Int) {
println(fe)
}
fun bar(br: Boolean) {
val fe = 0
if (br) {
foo(fe)
}
else {
<caret>foo(11)
}
}
@@ -0,0 +1,9 @@
fun bar(br: Boolean) {
val fe = 0
if (br) {
println(fe)
}
else {
println(11)
}
}
@@ -0,0 +1,13 @@
class Test {
private var s: String? = null
fun <caret>method(): String {
s = "Hello"
return s
}
fun test() {
println(method())
println(s)
}
}
@@ -0,0 +1,9 @@
class Test {
private var s: String? = null
fun test() {
s = "Hello"
println(s)
println(s)
}
}
@@ -0,0 +1,39 @@
class Foo {
fun method(b: Boolean,
elementComputable: Computable<String>,
processingContext: Any): WeighingComparable<String, ProximityLocation> {
return weigh(WEIGHER_KEY, elementComputable, ProximityLocation())
}
companion object {
fun <T, Loc> <caret>weigh(
key: Key<out Weigher<T, Loc>>?, element: Computable<T>, location: Loc
): WeighingComparable<T, Loc> {
return WeighingComparable(element, location, Array<Weigher<*, *>>(0) { null!! })
}
val WEIGHER_KEY: Key<ProximityWeigher>? = null
}
}
abstract class ProximityWeigher : Weigher<String, ProximityLocation>()
class ProximityLocation
class Key<P>
open class Weigher<A, B>
class Computable<O>
class WeighingComparable<T, Loc>(element: Computable<T>, location: Loc, weighers: Array<Weigher<*, *>>) : Comparable<WeighingComparable<T, Loc>> {
override fun compareTo(other: WeighingComparable<T, Loc>): Int {
return 0
}
private fun getWeight(index: Int): Comparable<*>? {
return null
}
}
@@ -0,0 +1,34 @@
class Foo {
fun method(b: Boolean,
elementComputable: Computable<String>,
processingContext: Any): WeighingComparable<String, ProximityLocation> {
return WeighingComparable(elementComputable, ProximityLocation(), Array<Weigher<*, *>>(0) { null!! })
}
companion object {
val WEIGHER_KEY: Key<ProximityWeigher>? = null
}
}
abstract class ProximityWeigher : Weigher<String, ProximityLocation>()
class ProximityLocation
class Key<P>
open class Weigher<A, B>
class Computable<O>
class WeighingComparable<T, Loc>(element: Computable<T>, location: Loc, weighers: Array<Weigher<*, *>>) : Comparable<WeighingComparable<T, Loc>> {
override fun compareTo(other: WeighingComparable<T, Loc>): Int {
return 0
}
private fun getWeight(index: Int): Comparable<*>? {
return null
}
}
@@ -0,0 +1,8 @@
fun method() {
otherMethod()
println("Here")
}
fun otherMethod<caret>() {
return
}
@@ -0,0 +1,4 @@
fun method() {
println("Here")
}
@@ -0,0 +1,17 @@
class Usage {
fun foo() {
<caret>bar()
}
fun bar() {
object : W() {
override fun www() {
super.www()
}
}
}
}
class W {
protected fun www() {}
}
@@ -0,0 +1,14 @@
class Usage {
fun foo() {
object : W() {
override fun www() {
super.www()
}
}
}
}
class W {
protected fun www() {}
}
@@ -173,6 +173,129 @@ public class InlineTestGenerated extends AbstractInlineTest {
}
}
@TestMetadata("idea/testData/refactoring/inline/function/fromIntellij")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class FromIntellij extends AbstractInlineTest {
public void testAllFilesPresentInFromIntellij() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/refactoring/inline/function/fromIntellij"), Pattern.compile("^(\\w+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("ArrayAccess.kt")
public void testArrayAccess() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/ArrayAccess.kt");
doTest(fileName);
}
@TestMetadata("AvoidMultipleSubstitutionsInParameterTypes.kt")
public void testAvoidMultipleSubstitutionsInParameterTypes() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/AvoidMultipleSubstitutionsInParameterTypes.kt");
doTest(fileName);
}
@TestMetadata("CallInFor.kt")
public void testCallInFor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/CallInFor.kt");
doTest(fileName);
}
@TestMetadata("CallUnderIf.kt")
public void testCallUnderIf() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/CallUnderIf.kt");
doTest(fileName);
}
@TestMetadata("EnumConstructor.kt")
public void testEnumConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/EnumConstructor.kt");
doTest(fileName);
}
@TestMetadata("FieldInitializer.kt")
public void testFieldInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/FieldInitializer.kt");
doTest(fileName);
}
@TestMetadata("FlatVarargs.kt")
public void testFlatVarargs() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/FlatVarargs.kt");
doTest(fileName);
}
@TestMetadata("InaccessibleFieldInSuperclass.kt")
public void testInaccessibleFieldInSuperclass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/InaccessibleFieldInSuperclass.kt");
doTest(fileName);
}
@TestMetadata("InferredType.kt")
public void testInferredType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/InferredType.kt");
doTest(fileName);
}
@TestMetadata("InlineAnonymousClassWithPrivateMethod.kt")
public void testInlineAnonymousClassWithPrivateMethod() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/InlineAnonymousClassWithPrivateMethod.kt");
doTest(fileName);
}
@TestMetadata("InlineMultipleOccurrencesInFieldInitializer.kt")
public void testInlineMultipleOccurrencesInFieldInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/InlineMultipleOccurrencesInFieldInitializer.kt");
doTest(fileName);
}
@TestMetadata("InlineWithQualifier.kt")
public void testInlineWithQualifier() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/InlineWithQualifier.kt");
doTest(fileName);
}
@TestMetadata("InlineWithTry.kt")
public void testInlineWithTry() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/InlineWithTry.kt");
doTest(fileName);
}
@TestMetadata("NotAStatement.kt")
public void testNotAStatement() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/NotAStatement.kt");
doTest(fileName);
}
@TestMetadata("ParamNameConflictsWithLocalVar.kt")
public void testParamNameConflictsWithLocalVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/ParamNameConflictsWithLocalVar.kt");
doTest(fileName);
}
@TestMetadata("SideEffect.kt")
public void testSideEffect() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/SideEffect.kt");
doTest(fileName);
}
@TestMetadata("Substitution.kt")
public void testSubstitution() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/Substitution.kt");
doTest(fileName);
}
@TestMetadata("VoidWithReturn.kt")
public void testVoidWithReturn() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/VoidWithReturn.kt");
doTest(fileName);
}
@TestMetadata("WithSuperInside.kt")
public void testWithSuperInside() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/fromIntellij/WithSuperInside.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/refactoring/inline/function/returnAtEnd")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)