Skip to content

Commit

Permalink
Add more test for anonymous class stripping from tag
Browse files Browse the repository at this point in the history
  • Loading branch information
eygraber committed Aug 12, 2021
1 parent adae9b8 commit d89a839
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions timber/src/test/java/timber/log/TimberTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,85 @@ class TimberTest {
.hasNoMoreMessages()
}

@Suppress("ObjectLiteralToLambda") // Lambdas != anonymous classes.
@Test fun debugTreeTagGenerationStripsAnonymousClassMarkerWithInnerSAMLambda() {
Timber.plant(Timber.DebugTree())
object : Runnable {
override fun run() {
Timber.d("Hello, world!")

Runnable { Timber.d("Hello, world!") }.run()
}
}.run()

assertLog()
.hasDebugMessage("TimberTest\$debugTreeTag", "Hello, world!")
.hasDebugMessage("TimberTest\$debugTreeTag", "Hello, world!")
.hasNoMoreMessages()
}

@Suppress("ObjectLiteralToLambda") // Lambdas != anonymous classes.
@Test fun debugTreeTagGenerationStripsAnonymousClassMarkerWithOuterSAMLambda() {
Timber.plant(Timber.DebugTree())

Runnable {
Timber.d("Hello, world!")

object : Runnable {
override fun run() {
Timber.d("Hello, world!")
}
}.run()
}.run()

assertLog()
.hasDebugMessage("TimberTest", "Hello, world!")
.hasDebugMessage("TimberTest\$debugTreeTag", "Hello, world!")
.hasNoMoreMessages()
}

// NOTE: this will fail on some future version of Kotlin when lambdas are compiled using invokedynamic
// Fix will be to expect the tag to be "TimberTest" as opposed to "TimberTest\$debugTreeTag"
@Test
fun debugTreeTagGenerationStripsAnonymousLambdaClassMarker() {
Timber.plant(Timber.DebugTree())

val outer = {
Timber.d("Hello, world!")

val inner = {
Timber.d("Hello, world!")
}

inner()
}

outer()

assertLog()
.hasDebugMessage("TimberTest\$debugTreeTag", "Hello, world!")
.hasDebugMessage("TimberTest\$debugTreeTag", "Hello, world!")
.hasNoMoreMessages()
}

@Test
fun debugTreeTagGenerationForSAMLambdasUsesClassName() {
Timber.plant(Timber.DebugTree())

Runnable {
Timber.d("Hello, world!")

Runnable {
Timber.d("Hello, world!")
}.run()
}.run()

assertLog()
.hasDebugMessage("TimberTest", "Hello, world!")
.hasDebugMessage("TimberTest", "Hello, world!")
.hasNoMoreMessages()
}

private class ClassNameThatIsReallyReallyReallyLong {
init {
Timber.i("Hello, world!")
Expand Down

0 comments on commit d89a839

Please sign in to comment.