Run formatter in function context for converted expression and statements

This commit is contained in:
Nikolay Krasko
2014-04-25 15:39:48 +04:00
parent f11e35f988
commit 13dce4dd11
75 changed files with 331 additions and 343 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2013 JetBrains s.r.o. * Copyright 2010-2014 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -36,6 +36,8 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.application.Result import com.intellij.openapi.application.Result
import java.io.BufferedReader
import java.io.StringReader
public abstract class AbstractJavaToKotlinConverterPluginTest() : AbstractJavaToKotlinConverterTest("ide.kt", PluginSettings) public abstract class AbstractJavaToKotlinConverterPluginTest() : AbstractJavaToKotlinConverterTest("ide.kt", PluginSettings)
public abstract class AbstractJavaToKotlinConverterBasicTest() : AbstractJavaToKotlinConverterTest("kt", TestSettings) public abstract class AbstractJavaToKotlinConverterBasicTest() : AbstractJavaToKotlinConverterTest("kt", TestSettings)
@@ -66,18 +68,32 @@ abstract class AbstractJavaToKotlinConverterTest(
else -> throw IllegalStateException("Specify what is it: file, class, method, statement or expression " + else -> throw IllegalStateException("Specify what is it: file, class, method, statement or expression " +
"using the first line of test data file") "using the first line of test data file")
} }
val actual = reformat(rawConverted, project)
val reformatInFun = when (prefix) {
"element", "expression", "statement" -> true
else -> false
}
val actual = reformat(rawConverted, project, reformatInFun)
val kotlinPath = javaPath.replace(".java", ".$kotlinFileExtension") val kotlinPath = javaPath.replace(".java", ".$kotlinFileExtension")
val expectedFile = File(kotlinPath) val expectedFile = File(kotlinPath)
JetTestUtils.assertEqualsToFile(expectedFile, actual) JetTestUtils.assertEqualsToFile(expectedFile, actual)
} }
private fun reformat(text: String, project: Project): String { private fun reformat(text: String, project: Project, inFunContext: Boolean): String {
val convertedFile = JetTestUtils.createFile("converted", text, project) val textToFormat = if (inFunContext) "fun convertedTemp() {\n$text\n}" else text
val convertedFile = JetTestUtils.createFile("converted", textToFormat, project)
WriteCommandAction.runWriteCommandAction(project) { WriteCommandAction.runWriteCommandAction(project) {
CodeStyleManager.getInstance(project)!!.reformat(convertedFile) CodeStyleManager.getInstance(project)!!.reformat(convertedFile)
} }
return convertedFile.getText()!!
val reformattedText = convertedFile.getText()!!
return if (inFunContext)
reformattedText.removeFirstLine().removeLastLine().trimIndent()
else
reformattedText
} }
private fun elementToKotlin(converter: Converter, text: String): String { private fun elementToKotlin(converter: Converter, text: String): String {
@@ -121,4 +137,42 @@ abstract class AbstractJavaToKotlinConverterTest(
override fun getProjectJDK(): Sdk? { override fun getProjectJDK(): Sdk? {
return PluginTestCaseBase.jdkFromIdeaHome() return PluginTestCaseBase.jdkFromIdeaHome()
} }
private fun String.removeFirstLine(): String {
val lastNewLine = indexOf('\n')
return if (lastNewLine == -1) "" else substring(lastNewLine)
}
private fun String.removeLastLine(): String {
val lastNewLine = lastIndexOf('\n')
return if (lastNewLine == -1) "" else substring(0, lastNewLine)
}
private fun String.trimIndent(): String {
val lines = split('\n')
val firstNonEmpty = lines.firstOrNull { !it.trim().isEmpty() }
if (firstNonEmpty == null) {
return this
}
val trimmedPrefix = firstNonEmpty.takeWhile { ch -> ch.isWhitespace() }
if (trimmedPrefix.isEmpty()) {
return this
}
return lines.map { line ->
if (line.trim().isEmpty()) {
""
}
else {
if (!line.startsWith(trimmedPrefix)) {
throw IllegalArgumentException(
"""Invalid line "$line", ${trimmedPrefix.size} whitespace character are expected""")
}
line.substring(trimmedPrefix.length)
}
}.makeString(separator = "\n")
}
} }
@@ -1 +1 @@
assert(true) {"string details"} assert(true) { "string details" }
@@ -1 +1 @@
assert(true) {"string details"} assert(true) { "string details" }
@@ -1,4 +1,4 @@
(if (a.isEmpty()) (if (a.isEmpty())
0 0
else else
1) 1)
@@ -1,4 +1,4 @@
(if (a.isEmpty()) (if (a.isEmpty())
0 0
else else
1) 1)
@@ -1,6 +1,4 @@
do do {
{ val i = 1
val i = 1 i = i + 1
i = i + 1 } while (a > b)
}
while (a > b)
@@ -1,6 +1,4 @@
do do {
{ var i: Int = 1
var i: Int = 1 i = i + 1
i = i + 1 } while (a > b)
}
while (a > b)
@@ -1,3 +1,2 @@
do do {
{} } while (true)
while (true)
@@ -1,3 +1,2 @@
do do {
{} } while (true)
while (true)
@@ -1,3 +1,3 @@
do do
i = i + 1 i = i + 1
while (true) while (true)
@@ -1,3 +1,3 @@
do do
i = i + 1 i = i + 1
while (true) while (true)
@@ -1,3 +1,3 @@
do do
return 1 return 1
while (true) while (true)
@@ -1,3 +1,3 @@
do do
return 1 return 1
while (true) while (true)
@@ -1,10 +1,9 @@
{ {
init() init()
while (condition()) while (condition()) {
{ body()
body() {
{ update()
update() }
} }
}
} }
@@ -1,10 +1,9 @@
{ {
init() init()
while (condition()) while (condition()) {
{ body()
body() {
{ update()
update() }
} }
}
} }
@@ -1,4 +1,4 @@
val array = IntArray(10) val array = IntArray(10)
for (i in 0..10) { for (i in 0..10) {
array[i] = i array[i] = i
} }
+1 -1
View File
@@ -1,4 +1,4 @@
var array: IntArray? = IntArray(10) var array: IntArray? = IntArray(10)
for (i in 0..10) { for (i in 0..10) {
array[i] = i array[i] = i
} }
@@ -1,4 +1,4 @@
val array = IntArray(10) val array = IntArray(10)
for (i in 0..10 - 1) { for (i in 0..10 - 1) {
array[i] = i array[i] = i
} }
+1 -1
View File
@@ -1,4 +1,4 @@
var array: IntArray? = IntArray(10) var array: IntArray? = IntArray(10)
for (i in 0..10 - 1) { for (i in 0..10 - 1) {
array[i] = i array[i] = i
} }
@@ -1,4 +1,4 @@
val array = IntArray(10) val array = IntArray(10)
for (i in 0..10 - 1) { for (i in 0..10 - 1) {
array[i] = i array[i] = i
} }
+1 -1
View File
@@ -1,4 +1,4 @@
var array: IntArray? = IntArray(10) var array: IntArray? = IntArray(10)
for (i in 0..10 - 1) { for (i in 0..10 - 1) {
array[i] = i array[i] = i
} }
@@ -1,14 +1,13 @@
{ {
val i = 0 val i = 0
while (i <0) while (i < 0) {
{ {
{ val i = 1
val i = 1 i++
i++ }
} {
{ j++
j++ i++
i++ }
} }
}
} }
@@ -1,14 +1,13 @@
{ {
var i: Int = 0 var i: Int = 0
while (i <0) while (i < 0) {
{ {
{ var i: Int = 1
var i: Int = 1 i++
i++ }
} {
{ j++
j++ i++
i++ }
} }
}
} }
@@ -1,11 +1,10 @@
{ {
val i = 0 val i = 0
while (i <0) while (i < 0) {
{ { }
{} {
{ j++
j++ i++
i++ }
} }
}
} }
@@ -1,11 +1,10 @@
{ {
var i: Int = 0 var i: Int = 0
while (i <0) while (i < 0) {
{ { }
{} {
{ j++
j++ i++
i++ }
} }
}
} }
@@ -1,10 +1,9 @@
{ {
init() init()
while (true) while (true) {
{ body()
body() {
{ update()
update() }
} }
}
} }
@@ -1,10 +1,9 @@
{ {
init() init()
while (true) while (true) {
{ body()
body() {
{ update()
update() }
} }
}
} }
@@ -1,9 +1,8 @@
{ {
while (condition()) while (condition()) {
{ body()
body() {
{ update()
update() }
} }
}
} }
@@ -1,9 +1,8 @@
{ {
while (condition()) while (condition()) {
{ body()
body() {
{ update()
update() }
} }
}
} }
@@ -1,7 +1,6 @@
{ {
init() init()
while (condition()) while (condition()) {
{ body()
body() }
}
} }
@@ -1,7 +1,6 @@
{ {
init() init()
while (condition()) while (condition()) {
{ body()
body() }
}
} }
@@ -1,11 +1,10 @@
{ {
val i = 0 val i = 0
while (i <0) while (i < 0) {
{ return i
return i {
{ j++
j++ i++
i++ }
} }
}
} }
+8 -9
View File
@@ -1,11 +1,10 @@
{ {
var i: Int = 0 var i: Int = 0
while (i <0) while (i < 0) {
{ return i
return i {
{ j++
j++ i++
i++ }
} }
}
} }
@@ -1,5 +1,4 @@
for (n in list) for (n in list) {
{ val i = 1
val i = 1 i++
i++
} }
@@ -1,5 +1,4 @@
for (n in list) for (n in list) {
{ var i: Int = 1
var i: Int = 1 i++
i++
} }
@@ -1,2 +1,2 @@
for (n in list) for (n in list) {
{} }
@@ -1,2 +1,2 @@
for (n in list) for (n in list) {
{} }
@@ -1,2 +1,2 @@
for (n in list) for (n in list)
i++ i++
@@ -1,2 +1,2 @@
for (n in list) for (n in list)
i++ i++
@@ -1,2 +1,2 @@
for (n in list) for (n in list)
return n return n
@@ -1,2 +1,2 @@
for (n in list) for (n in list)
return n return n
@@ -1,4 +1,3 @@
if (1 > 0) if (1 > 0) {
{} } else {
else }
{}
@@ -1,4 +1,3 @@
if (1 > 0) if (1 > 0) {
{} } else {
else }
{}
@@ -1,9 +1,6 @@
if (1 > 0) if (1 > 0) {
{ val n = 1
val n = 1 return n
return n } else {
} return 0
else
{
return 0
} }
@@ -1,9 +1,6 @@
if (1 > 0) if (1 > 0) {
{ var n: Int = 1
var n: Int = 1 return n
return n } else {
} return 0
else
{
return 0
} }
@@ -1,4 +1,4 @@
if (true) if (true)
return 1 return 1
else else
return 0 return 0
@@ -1,4 +1,4 @@
if (true) if (true)
return 1 return 1
else else
return 0 return 0
@@ -1,5 +1,4 @@
if (1 > 0) if (1 > 0) {
{ val n = 1
val n = 1 return n
return n
} }
@@ -1,5 +1,4 @@
if (1 > 0) if (1 > 0) {
{ var n: Int = 1
var n: Int = 1 return n
return n
} }
@@ -1,2 +1,2 @@
//element //file
import java.util.Arrays; import java.util.Arrays;
@@ -1,3 +1,3 @@
//element //file
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
@@ -1,16 +1,14 @@
@test for (i in 0..max) { @test for (i in 0..max) {
val n = substring.length() val n = substring.length()
val j = i val j = i
val k = 0 val k = 0
while (n-- != 0) while (n-- != 0) {
{ if (searchMe.charAt(j++) != substring.charAt(k++)) {
if (searchMe.charAt(j++) != substring.charAt(k++)) continue@test
{ }
continue@test }
} foundIt = true
} break@test
foundIt = true
break@test
} }
System.out.println((if (foundIt) System.out.println((if (foundIt)
"Found it" "Found it"
@@ -1,16 +1,14 @@
@test for (i in 0..max) { @test for (i in 0..max) {
var n: Int = substring.length() var n: Int = substring.length()
var j: Int = i var j: Int = i
var k: Int = 0 var k: Int = 0
while (n-- != 0) while (n-- != 0) {
{ if (searchMe.charAt(j++) != substring.charAt(k++)) {
if (searchMe.charAt(j++) != substring.charAt(k++)) continue@test
{ }
continue@test }
} foundIt = true
} break@test
foundIt = true
break@test
} }
System.out?.println((if (foundIt) System.out?.println((if (foundIt)
"Found it" "Found it"
@@ -1,5 +1,5 @@
object : Runnable() { object : Runnable() {
override fun run() { override fun run() {
System.out.println("Run") System.out.println("Run")
} }
} }
@@ -1,5 +1,5 @@
object : Runnable() { object : Runnable() {
override fun run() { override fun run() {
System.out?.println("Run") System.out?.println("Run")
} }
} }
@@ -1,3 +1,3 @@
val s = null val s = null
if (!s.isEmpty()) if (!s.isEmpty()) {
{} }
@@ -1,3 +1,3 @@
var s: String? = null var s: String? = null
if (!s?.isEmpty()!!) if (!s?.isEmpty()!!) {
{} }
@@ -1,3 +1,3 @@
synchronized (s) { synchronized (s) {
doSomething(s) doSomething(s)
} }
@@ -1,3 +1,3 @@
synchronized (s) { synchronized (s) {
doSomething(s) doSomething(s)
} }
@@ -1,14 +1,9 @@
try try {
{ callMethod(params)
callMethod(params) } catch (e: Exception) {
} println(1)
catch (e : Exception) { } catch (e: IOException) {
println(1) println(0)
} } finally {
catch (e : IOException) { println(3)
println(0)
}
finally
{
println(3)
} }
@@ -1,14 +1,9 @@
try try {
{ callMethod(params)
callMethod(params) } catch (e: Exception) {
} println(1)
catch (e : Exception) { } catch (e: IOException) {
println(1) println(0)
} } finally {
catch (e : IOException) { println(3)
println(0)
}
finally
{
println(3)
} }
@@ -1,10 +1,7 @@
try try {
{} } catch (e: Exception) {
catch (e : Exception) { println(1)
println(1) } catch (e: IOException) {
} println(0)
catch (e : IOException) { } finally {
println(0) }
}
finally
{}
@@ -1,10 +1,7 @@
try try {
{} } catch (e: Exception) {
catch (e : Exception) { println(1)
println(1) } catch (e: IOException) {
} println(0)
catch (e : IOException) { } finally {
println(0) }
}
finally
{}
@@ -1,12 +1,8 @@
try try {
{} } catch (e: Exception) {
catch (e : Exception) { println(1)
println(1) } catch (e: IOException) {
} println(0)
catch (e : IOException) { } finally {
println(0) println(3)
}
finally
{
println(3)
} }
@@ -1,12 +1,8 @@
try try {
{} } catch (e: Exception) {
catch (e : Exception) { println(1)
println(1) } catch (e: IOException) {
} println(0)
catch (e : IOException) { } finally {
println(0) println(3)
}
finally
{
println(3)
} }
@@ -1,8 +1,6 @@
try try {
{} } catch (e: Exception) {
catch (e : Exception) { println(1)
println(1) } catch (e: IOException) {
} println(0)
catch (e : IOException) {
println(0)
} }
@@ -1,8 +1,6 @@
try try {
{} } catch (e: Exception) {
catch (e : Exception) { println(1)
println(1) } catch (e: IOException) {
} println(0)
catch (e : IOException) {
println(0)
} }
@@ -1,5 +1,4 @@
while (a > b) while (a > b) {
{ val i = 1
val i = 1 i = i + 1
i = i + 1
} }
@@ -1,5 +1,4 @@
while (a > b) while (a > b) {
{ var i: Int = 1
var i: Int = 1 i = i + 1
i = i + 1
} }
@@ -1,2 +1,2 @@
while (true) while (true) {
{} }
@@ -1,2 +1,2 @@
while (true) while (true) {
{} }
@@ -1,2 +1,2 @@
while (true) while (true)
i = i + 1 i = i + 1
@@ -1,2 +1,2 @@
while (true) while (true)
i = i + 1 i = i + 1
@@ -1,2 +1,2 @@
while (true) while (true)
return 1 return 1
@@ -1,2 +1,2 @@
while (true) while (true)
return 1 return 1