Skip to content

Commit

Permalink
✅ Add tests for SampleSourceFile.Companion.checkSingleClass
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Nov 9, 2024
1 parent 22359f5 commit 223b675
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,54 @@ import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.fail

class SampleSourceFileTest {
@Test
fun `checkSingleClass should pass with a single class in it`() {
val name = "/sample/kotlin/Valid.kt"
this::class.java.getResource(name)
?.toURI()
?.let(::File)
?.let(SampleSourceFile.Companion::orThrow)
?.checkSingleClass()
?: fail("'$name' file not found.")
}

@Test
fun `checkSingleClass should fail without a class`() {
val name = "/sample/kotlin/NoClass.kt"
val file: File = this::class.java.getResource(name)
?.toURI()
?.let(::File)
?: fail("'$name' file not found.")
val sampleSourceFile: SampleSourceFile = SampleSourceFile.orThrow(file)
val actual: String? = assertFailsWith<IllegalStateException>(
block = sampleSourceFile::checkSingleClass
).message
val expected: String =
"The '${file.name}' file should have a single class.\n" +
"File location: ${file.path}"
assertEquals(expected, actual)
}

@Test
fun `checkSingleClass should fail with multiple classes in it`() {
val name = "/sample/kotlin/MultipleClasses.kt"
val file: File = this::class.java.getResource(name)
?.toURI()
?.let(::File)
?: fail("'$name' file not found.")
val sampleSourceFile: SampleSourceFile = SampleSourceFile.orThrow(file)
val actual: String? = assertFailsWith<IllegalStateException>(
block = sampleSourceFile::checkSingleClass
).message
val expected: String =
"The '${file.name}' file should have a single class.\n" +
"File location: ${file.path}"
assertEquals(expected, actual)
}
}

class SampleSourceFileCompanionTest {
@Test
Expand Down
5 changes: 5 additions & 0 deletions jvm/src/test/resources/sample/kotlin/MultipleClasses.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sample.kotlin

class A
class B
class C
1 change: 1 addition & 0 deletions jvm/src/test/resources/sample/kotlin/NoClass.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package sample.kotlin
8 changes: 8 additions & 0 deletions jvm/src/test/resources/sample/kotlin/Valid.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class HelloKotlinSample {
fun greet() {
val name = "Sample"
val greeting: String = greet(name)
val expected = "Hello $name!"
assertEquals(expected, greeting)
}
}

0 comments on commit 223b675

Please sign in to comment.