Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ dependencies {
api(libs.kotlin.stdlib)
api(libs.kotlin.compilerEmbeddable)
implementation(libs.ec4j)
testImplementation(libs.kotlin.test.junit4)
testImplementation(libs.googleTruth)
testImplementation(libs.junit)
testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter)
testRuntimeOnly(libs.junit.platform.launcher)
}

val generateSources by tasks.registering {
Expand All @@ -53,7 +53,10 @@ val generateSources by tasks.registering {

tasks {
// Run tests with UTF-16 encoding
test { jvmArgs("-Dfile.encoding=UTF-16") }
test {
useJUnitPlatform()
jvmArgs("-Dfile.encoding=UTF-16")
}

// Handle multiple versions of Kotlin here
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,26 @@ package com.facebook.ktfmt.cli
import com.facebook.ktfmt.format.Formatter
import com.facebook.ktfmt.format.TrailingCommaManagementStrategy
import com.facebook.ktfmt.format.TrailingCommaManagementStrategy.ONLY_ADD
import com.google.common.truth.Truth.assertThat
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import kotlin.io.path.createTempDirectory
import kotlin.text.Charsets.UTF_8
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

@Suppress("FunctionNaming")
@RunWith(JUnit4::class)
class EditorConfigResolverTest {
private val root = createTempDirectory().toFile()
private val testCharset = StandardCharsets.UTF_16

@Before
@BeforeEach
fun setUp() {
assertThat(Charset.defaultCharset()).isEqualTo(testCharset) // Verify the test JVM flags
assertEquals(testCharset, Charset.defaultCharset()) // Verify the test JVM flags
}

@After
@AfterEach
fun tearDown() {
root.deleteRecursively()
}
Expand All @@ -52,7 +49,7 @@ class EditorConfigResolverTest {
src.parentFile.mkdirs()
src.writeText("", UTF_8)
val resolved = EditorConfigResolver.resolveFormattingOptions(src, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT)
assertEquals(Formatter.GOOGLE_FORMAT, resolved)
}

@Test
Expand All @@ -69,7 +66,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT)
assertEquals(Formatter.GOOGLE_FORMAT, resolved)
}

@Test
Expand All @@ -86,7 +83,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT.copy(maxWidth = 80))
assertEquals(Formatter.GOOGLE_FORMAT.copy(maxWidth = 80), resolved)
}

@Test
Expand All @@ -103,7 +100,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT)
assertEquals(Formatter.GOOGLE_FORMAT, resolved)
}

@Test
Expand All @@ -120,7 +117,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT.copy(blockIndent = 3))
assertEquals(Formatter.GOOGLE_FORMAT.copy(blockIndent = 3), resolved)
}

@Test
Expand All @@ -137,7 +134,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT)
assertEquals(Formatter.GOOGLE_FORMAT, resolved)
}

@Test
Expand All @@ -155,7 +152,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT.copy(blockIndent = 8))
assertEquals(Formatter.GOOGLE_FORMAT.copy(blockIndent = 8), resolved)
}

@Test
Expand All @@ -172,7 +169,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT.copy(blockIndent = 3))
assertEquals(Formatter.GOOGLE_FORMAT.copy(blockIndent = 3), resolved)
}

@Test
Expand All @@ -190,7 +187,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT.copy(blockIndent = 3))
assertEquals(Formatter.GOOGLE_FORMAT.copy(blockIndent = 3), resolved)
}

@Test
Expand All @@ -207,7 +204,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT.copy(continuationIndent = 3))
assertEquals(Formatter.GOOGLE_FORMAT.copy(continuationIndent = 3), resolved)
}

@Test
Expand All @@ -224,7 +221,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT.copy(continuationIndent = 3))
assertEquals(Formatter.GOOGLE_FORMAT.copy(continuationIndent = 3), resolved)
}

@Test
Expand All @@ -242,7 +239,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT.copy(continuationIndent = 3))
assertEquals(Formatter.GOOGLE_FORMAT.copy(continuationIndent = 3), resolved)
}

@Test
Expand All @@ -259,8 +256,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved)
.isEqualTo(Formatter.GOOGLE_FORMAT.copy(trailingCommaManagementStrategy = ONLY_ADD))
assertEquals(Formatter.GOOGLE_FORMAT.copy(trailingCommaManagementStrategy = ONLY_ADD), resolved)
}

@Test
Expand All @@ -277,7 +273,7 @@ class EditorConfigResolverTest {

val file = root.resolve("src/main/kotlin/Example.kt")
val resolved = EditorConfigResolver.resolveFormattingOptions(file, Formatter.GOOGLE_FORMAT)
assertThat(resolved).isEqualTo(Formatter.GOOGLE_FORMAT)
assertEquals(Formatter.GOOGLE_FORMAT, resolved)
}

@Test
Expand Down Expand Up @@ -340,23 +336,33 @@ class EditorConfigResolverTest {
)

val fileInRoot = root.resolve("build.gradle.kts")
assertThat(EditorConfigResolver.resolveFormattingOptions(fileInRoot, Formatter.GOOGLE_FORMAT))
.isEqualTo(rootOptions)
assertEquals(
rootOptions,
EditorConfigResolver.resolveFormattingOptions(fileInRoot, Formatter.GOOGLE_FORMAT),
)

val fileInMain = root.resolve("src/main/kotlin/Example.kt")
assertThat(EditorConfigResolver.resolveFormattingOptions(fileInMain, Formatter.GOOGLE_FORMAT))
.isEqualTo(mainOptions)
assertEquals(
mainOptions,
EditorConfigResolver.resolveFormattingOptions(fileInMain, Formatter.GOOGLE_FORMAT),
)

val fileInTest = root.resolve("src/test/kotlin/ExampleTest.kt")
assertThat(EditorConfigResolver.resolveFormattingOptions(fileInTest, Formatter.GOOGLE_FORMAT))
.isEqualTo(testOptions)
assertEquals(
testOptions,
EditorConfigResolver.resolveFormattingOptions(fileInTest, Formatter.GOOGLE_FORMAT),
)

val ktsInMain = root.resolve("src/main/kotlin/ExampleTest.kts")
assertThat(EditorConfigResolver.resolveFormattingOptions(ktsInMain, Formatter.GOOGLE_FORMAT))
.isEqualTo(rootOptions.copy(maxWidth = 120))
assertEquals(
rootOptions.copy(maxWidth = 120),
EditorConfigResolver.resolveFormattingOptions(ktsInMain, Formatter.GOOGLE_FORMAT),
)

val ktsInTest = root.resolve("src/test/kotlin/ExampleTest.kts")
assertThat(EditorConfigResolver.resolveFormattingOptions(ktsInTest, Formatter.GOOGLE_FORMAT))
.isEqualTo(Formatter.GOOGLE_FORMAT) // root=true stops even non-matching fall-through
assertEquals(
Formatter.GOOGLE_FORMAT,
EditorConfigResolver.resolveFormattingOptions(ktsInTest, Formatter.GOOGLE_FORMAT),
) // root=true stops even non-matching fall-through
}
}
Loading
Loading