From 27311faa9d265ceb890d8766200d5a17921c58fd Mon Sep 17 00:00:00 2001 From: Alyssa Date: Sun, 29 Sep 2024 13:04:29 +0100 Subject: [PATCH] fix up tests --- core/src/main/kotlin/logging/Logger.kt | 14 +++++++++++++- core/src/test/kotlin/logging/LoggerTest.kt | 7 ++++--- .../logging/LoggerUncaughtExceptionHandlerTest.kt | 11 +++++------ test-core/src/main/kotlin/testCore/TestUtils.kt | 4 ++-- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/core/src/main/kotlin/logging/Logger.kt b/core/src/main/kotlin/logging/Logger.kt index 4fec5de..c941a33 100644 --- a/core/src/main/kotlin/logging/Logger.kt +++ b/core/src/main/kotlin/logging/Logger.kt @@ -3,8 +3,10 @@ package logging import ch.qos.logback.classic.Level import ch.qos.logback.classic.spi.ILoggingEvent import getPercentage +import java.lang.reflect.Field import java.util.concurrent.ConcurrentHashMap import kotlin.math.floor +import net.logstash.logback.marker.MapEntriesAppendingMarker import net.logstash.logback.marker.Markers import org.slf4j.Marker @@ -85,6 +87,16 @@ val ILoggingEvent.loggingCode: String? fun ILoggingEvent.findLogField(key: String): Any? = getLogFields()[key] -fun ILoggingEvent.getLogFields() = this.keyValuePairs.associate { it.key to it.value } +private val mapEntriesAppendingMarkerField: Field = + MapEntriesAppendingMarker::class.java.getDeclaredField("map").apply { isAccessible = true } + +fun ILoggingEvent.getLogFields(): Map { + val marker = this.markerList?.firstOrNull() + return if (marker is MapEntriesAppendingMarker) { + mapEntriesAppendingMarkerField.get(marker) as Map + } else { + emptyMap() + } +} fun ILoggingEvent.level() = this.throwableProxy diff --git a/core/src/test/kotlin/logging/LoggerTest.kt b/core/src/test/kotlin/logging/LoggerTest.kt index 11e772b..df4baa5 100644 --- a/core/src/test/kotlin/logging/LoggerTest.kt +++ b/core/src/test/kotlin/logging/LoggerTest.kt @@ -21,7 +21,7 @@ class LoggerTest : AbstractTest() { record.loggingCode shouldBe loggingCode record.message shouldBe "A thing happened" record.throwableProxy shouldBe null - record.getLogFields().size shouldBe 0 + record.shouldContainKeyValues("loggingCode" to "some.event") } @Test @@ -51,7 +51,6 @@ class LoggerTest : AbstractTest() { record.loggingCode shouldBe loggingCode record.message shouldBe "A slightly bad thing happened" record.throwableProxy shouldBe null - record.keyValuePairs.size shouldBe 0 } @Test @@ -64,9 +63,11 @@ class LoggerTest : AbstractTest() { val record = getLastLog() record.level shouldBe Level.ERROR - record.throwableProxy shouldBe throwable + record.throwableProxy.message shouldBe "Boo" record.loggingCode shouldBe loggingCode record.shouldContainKeyValues("other.info" to 60, KEY_EXCEPTION_MESSAGE to "Boo") + + errorLogged() shouldBe true } @Test diff --git a/core/src/test/kotlin/logging/LoggerUncaughtExceptionHandlerTest.kt b/core/src/test/kotlin/logging/LoggerUncaughtExceptionHandlerTest.kt index 599f651..d8de444 100644 --- a/core/src/test/kotlin/logging/LoggerUncaughtExceptionHandlerTest.kt +++ b/core/src/test/kotlin/logging/LoggerUncaughtExceptionHandlerTest.kt @@ -1,6 +1,7 @@ package logging import ch.qos.logback.classic.Level +import io.kotest.matchers.maps.shouldNotContainKey import io.kotest.matchers.shouldBe import io.kotest.matchers.string.shouldContain import main.kotlin.testCore.shouldContainKeyValues @@ -33,11 +34,9 @@ class LoggerUncaughtExceptionHandlerTest : AbstractTest() { handler.uncaughtException(Thread.currentThread(), ex) val log = verifyLog("uncaughtException", Level.ERROR) - log.throwableProxy shouldBe ex - log.shouldContainKeyValues( - KEY_THREAD to Thread.currentThread().toString(), - KEY_EXCEPTION_MESSAGE to null - ) + log.throwableProxy.message shouldBe null + log.shouldContainKeyValues(KEY_THREAD to Thread.currentThread().toString()) + log.getLogFields().shouldNotContainKey(KEY_EXCEPTION_MESSAGE) log.message shouldContain "Uncaught exception: $ex" } @@ -50,7 +49,7 @@ class LoggerUncaughtExceptionHandlerTest : AbstractTest() { handler.uncaughtException(t, ex) val log = verifyLog("uncaughtException", Level.ERROR) - log.throwableProxy shouldBe ex + log.throwableProxy.message shouldBe "Argh" log.shouldContainKeyValues(KEY_THREAD to t.toString(), KEY_EXCEPTION_MESSAGE to "Argh") log.message shouldContain "Uncaught exception: $ex" } diff --git a/test-core/src/main/kotlin/testCore/TestUtils.kt b/test-core/src/main/kotlin/testCore/TestUtils.kt index 2e1f392..ceeaaf4 100644 --- a/test-core/src/main/kotlin/testCore/TestUtils.kt +++ b/test-core/src/main/kotlin/testCore/TestUtils.kt @@ -5,7 +5,7 @@ import ch.qos.logback.classic.spi.ILoggingEvent import com.github.alyssaburlton.swingtest.findAll import com.github.alyssaburlton.swingtest.findWindow import com.github.alyssaburlton.swingtest.flushEdt -import io.kotest.matchers.maps.shouldContainExactly +import io.kotest.matchers.maps.shouldContainAll import io.kotest.matchers.shouldBe import io.mockk.verify import java.time.Instant @@ -16,7 +16,7 @@ import logging.LogRecord import logging.getLogFields fun ILoggingEvent.shouldContainKeyValues(vararg values: Pair) { - this.getLogFields().shouldContainExactly(mapOf(*values)) + this.getLogFields().shouldContainAll(mapOf(*values)) } fun makeLogRecord(