From d89a83917644d254319885375d8a10ea995c8df6 Mon Sep 17 00:00:00 2001 From: Eliezer Graber Date: Thu, 12 Aug 2021 17:31:36 -0400 Subject: [PATCH] Add more test for anonymous class stripping from tag --- timber/src/test/java/timber/log/TimberTest.kt | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/timber/src/test/java/timber/log/TimberTest.kt b/timber/src/test/java/timber/log/TimberTest.kt index f298a2cba..cbfecf236 100644 --- a/timber/src/test/java/timber/log/TimberTest.kt +++ b/timber/src/test/java/timber/log/TimberTest.kt @@ -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!")