Add tests for StackTraceUtils

This commit is contained in:
JeremyStar™ 2024-12-20 02:50:30 +01:00
parent 3b532a2dda
commit 01b2955c39
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 83 additions and 13 deletions

View file

@ -59,8 +59,7 @@ class InfoCrashCategory private constructor() : CrashCategory {
channelSettings: ChannelSettings?, channelSettings: ChannelSettings?,
throwable: Throwable?, throwable: Throwable?,
fatal: Boolean, fatal: Boolean,
): LinkedHashMap<String, Any?> { ): LinkedHashMap<String, Any?> = linkedMapOf(
return linkedMapOf(
Pair("Origin", linkedMapOf<String, Any>( Pair("Origin", linkedMapOf<String, Any>(
Pair("Class", "${call.origin.packageName}.${call.origin.className}"), Pair("Class", "${call.origin.packageName}.${call.origin.className}"),
Pair("Method", call.origin.methodName), Pair("Method", call.origin.methodName),
@ -71,5 +70,4 @@ class InfoCrashCategory private constructor() : CrashCategory {
Pair("Message", call.message), Pair("Message", call.message),
Pair("Stacktrace", if (throwable == null) "Not available." else StackTraceUtils.stacktraceRecursive(throwable)), Pair("Stacktrace", if (throwable == null) "Not available." else StackTraceUtils.stacktraceRecursive(throwable)),
) )
}
} }

View file

@ -0,0 +1,72 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* Licensed under the GNU General Public License v3.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.staropensource.engine.base.utility.misc
import de.staropensource.engine.base.type.Origin
import de.staropensource.engine.testing.TestBase
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import java.lang.NullPointerException
import kotlin.test.Test
import kotlin.test.assertContains
class StackTraceUtilsTest : TestBase() {
@Test
fun methodCaller() {
val origin: Origin = methodCallerOrigin()
assertEquals("de.staropensource.engine.base.utility.misc", origin.packageName)
assertEquals("StackTraceUtilsTest", origin.className)
assertEquals("methodCaller", origin.methodName)
assertEquals(33u, origin.lineNumber)
}
private fun methodCallerOrigin(): Origin = StackTraceUtils.methodCaller()
@Test
@DisplayName("stacktraceHeader (no message)")
fun stacktraceHeaderNoMessage() {
assertEquals("Caused by: java.lang.NullPointerException", StackTraceUtils.stacktraceHeader(NullPointerException()))
}
@Test
@DisplayName("stacktraceHeader (with message)")
fun stacktraceHeaderWithMessage() {
assertEquals("Caused by: java.lang.NullPointerException: Hello World!", StackTraceUtils.stacktraceHeader(NullPointerException("Hello World!")))
}
@Test
@DisplayName("stacktraceBody (no indent)")
fun stacktraceBodyNoIndent() {
val body: String = StackTraceUtils.stacktraceBody(Throwable(), indent = false)
assertTrue(body.startsWith("at "), "Body '${body}' does not start with 'at '")
assertTrue(body.contains("de.staropensource.engine.base.utility.misc.StackTraceUtilsTest"), "Body '${body}' does not contain 'de.staropensource.engine.base.utility.misc.StackTraceUtilsTest")
}
@Test
@DisplayName("stacktraceBody (with indent)")
fun stacktraceBodyWithIndent() {
val body: String = StackTraceUtils.stacktraceBody(Throwable(), indent = true)
assertTrue(body.startsWith("\tat "), "Body '${body}' does not start with '\tat '")
assertTrue(body.contains("de.staropensource.engine.base.utility.misc.StackTraceUtilsTest"), "Body '${body}' does not contain 'de.staropensource.engine.base.utility.misc.StackTraceUtilsTest")
}
}