Skip to content

Commit

Permalink
@CaptureSource and sourceOf() support properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mfwgenerics committed Jun 5, 2023
1 parent 784cbc7 commit fba7adb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ If it is present, the block's argument list is considered part of its source tex

You can capture declaration sources using the `@CaptureSource`
annotation. The source of annotated declarations can then be retrieved using
`sourceOf<T>` for class declarations or `sourceOf(::method)` for method
`sourceOf<T>` for class declarations or `sourceOf(::declaration)` for method and property
declarations. The source capture starts at the end of the `@CaptureSource`
annotation.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package io.koalaql.kapshot

@Target(
AnnotationTarget.CLASS,
AnnotationTarget.FUNCTION
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY
)
annotation class CaptureSource(
val location: String = "",
Expand Down
10 changes: 5 additions & 5 deletions kapshot-runtime/src/main/kotlin/io/koalaql/kapshot/sourceOf.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.koalaql.kapshot

import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KType
import kotlin.reflect.typeOf
import kotlin.reflect.*

private fun sourceOf(annotations: List<Annotation>, name: () -> String): Source {
val anno = annotations
Expand All @@ -29,4 +26,7 @@ fun sourceOf(type: KType): Source {
inline fun <reified T : Any> sourceOf() = sourceOf(typeOf<T>())

fun sourceOf(method: KFunction<*>): Source =
sourceOf(method.annotations) { method.name }
sourceOf(method.annotations) { method.name }

fun sourceOf(property: KProperty<*>): Source =
sourceOf(property.annotations) { property.name }
2 changes: 1 addition & 1 deletion readme/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fun Markdown.generateMarkdown() {

-"You can capture declaration sources using the `@${captureAnno.simpleName}`"
-"annotation. The source of annotated declarations can then be retrieved using"
-"`sourceOf<T>` for class declarations or `sourceOf(::method)` for method"
-"`sourceOf<T>` for class declarations or `sourceOf(::declaration)` for method and property"
-"declarations. The source capture starts at the end of the `@${captureAnno.simpleName}`"
-"annotation."

Expand Down
27 changes: 27 additions & 0 deletions testing/src/test/kotlin/CaptureSourceTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,31 @@ class CaptureSourceTests {
sourceOf<CapturedSealed.Companion>().text
)
}

@CaptureSource
@Transient /* test this annotation is present in source */
private val someVal = 5

@CaptureSource
private var someVar: Int get() = 10
set(value) { }

@Test
fun `can capture source of valvars`() {
assertEquals(
"""
@Transient /* test this annotation is present in source */
private val someVal = 5
""".trimIndent(),
sourceOf(::someVal).text
)

assertEquals(
"""
private var someVar: Int get() = 10
set(value) { }
""".trimIndent(),
sourceOf(::someVar).text
)
}
}

0 comments on commit fba7adb

Please sign in to comment.