Inline function: add extra tests taken from intellij-community
This commit is contained in:
@@ -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])
|
||||
}
|
||||
}
|
||||
|
||||
idea/testData/refactoring/inline/function/fromIntellij/AvoidMultipleSubstitutionsInParameterTypes.kt
Vendored
+14
@@ -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) {}
|
||||
}
|
||||
+10
@@ -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
|
||||
}
|
||||
}
|
||||
+4
@@ -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) {}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
class A {
|
||||
private val i = 0
|
||||
|
||||
fun foo() {
|
||||
i.toString()
|
||||
}
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
fun bar() {
|
||||
<caret>foo()
|
||||
}
|
||||
}
|
||||
Vendored
+10
@@ -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>())
|
||||
}
|
||||
}
|
||||
Vendored
+23
@@ -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()
|
||||
}
|
||||
}
|
||||
+22
@@ -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
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+14
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -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) {
|
||||
}
|
||||
+31
@@ -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()
|
||||
}
|
||||
}
|
||||
+29
@@ -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
|
||||
}
|
||||
+18
@@ -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
|
||||
}
|
||||
+13
@@ -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)
|
||||
}
|
||||
}
|
||||
Vendored
+9
@@ -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
|
||||
}
|
||||
+4
@@ -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() {}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
class Usage {
|
||||
fun foo() {
|
||||
object : W() {
|
||||
override fun www() {
|
||||
super.www()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class W {
|
||||
protected fun www() {}
|
||||
}
|
||||
Reference in New Issue
Block a user