Skip to content

Commit

Permalink
refactor(analysis): move mockStartSession to own class #3483
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktMehl committed Feb 19, 2025
1 parent dca5d4d commit 5284289
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package de.maibornwolff.codecharta.filter.mergefilter

import com.varabyte.kotter.runtime.Session
import com.varabyte.kotterx.test.foundation.testSession
import de.maibornwolff.codecharta.filter.mergefilter.MergeFilter.Companion.main
import de.maibornwolff.codecharta.serialization.ProjectDeserializer
import de.maibornwolff.codecharta.tools.interactiveparser.startSession
import de.maibornwolff.codecharta.tools.interactiveparser.SessionMock
import io.mockk.every
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
Expand All @@ -28,7 +25,7 @@ class MergeFilterTest {

@BeforeEach
fun beforeTest() {
mockStartSession()
SessionMock.mockStartSession()
}

@AfterEach
Expand Down Expand Up @@ -612,19 +609,4 @@ class MergeFilterTest {
).contains("Input project structure doesn't have '/root/' as a base folder. If that's intended open an issue.")
}
}

private fun mockStartSession() {
mockkStatic("de.maibornwolff.codecharta.tools.interactiveparser.ParserDialogInterfaceKt")
every { startSession(any<Session.() -> Any>()) } answers {
startTestSession { firstArg<Session.() -> Any>()(this) }
}
}

private fun <T> startTestSession(block: Session.() -> T): T {
var returnValue: T? = null
testSession {
returnValue = block()
}
return returnValue ?: throw IllegalStateException("Session did not return a value.")
}
}
7 changes: 7 additions & 0 deletions analysis/tools/InteractiveParser/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ repositories {

dependencies {
implementation(libs.kotter)
implementation(libs.kotter.test)
implementation(libs.picocli)

testImplementation(libs.kotlin.test)
testRuntimeOnly(libs.kotlin.reflect)
testImplementation(libs.assertj.core)
testImplementation(libs.mockk)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.maibornwolff.codecharta.tools.interactiveparser

import com.varabyte.kotter.runtime.Session
import com.varabyte.kotterx.test.foundation.testSession
import io.mockk.every
import io.mockk.mockkStatic

class SessionMock {
companion object {
fun mockStartSession() {
mockkStatic("de.maibornwolff.codecharta.tools.interactiveparser.ParserDialogInterfaceKt")
every { startSession(any<Session.() -> Any>()) } answers {
startTestSession { firstArg<Session.() -> Any>()(this) }
}
}

private fun <T> startTestSession(block: Session.() -> T): T {
var returnValue: T? = null
testSession {
returnValue = block()
}
return returnValue ?: throw IllegalStateException("Session did not return a value.")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package de.maibornwolff.codecharta.ccsh

import com.varabyte.kotter.runtime.Session
import com.varabyte.kotterx.test.foundation.testSession
import de.maibornwolff.codecharta.tools.ccsh.Ccsh
import de.maibornwolff.codecharta.tools.ccsh.parser.InteractiveDialog
import de.maibornwolff.codecharta.tools.ccsh.parser.InteractiveParserSuggestion
import de.maibornwolff.codecharta.tools.ccsh.parser.ParserService
import de.maibornwolff.codecharta.tools.interactiveparser.startSession
import de.maibornwolff.codecharta.tools.interactiveparser.SessionMock.Companion.mockStartSession
import de.maibornwolff.codecharta.util.Logger
import io.mockk.every
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -394,19 +391,4 @@ class CcshTest {
assertThat(exitCode).isZero()
assertThat(mergedOutputFile).exists()
}

private fun mockStartSession() {
mockkStatic("de.maibornwolff.codecharta.tools.interactiveparser.ParserDialogInterfaceKt")
every { startSession(any<Session.() -> Any>()) } answers {
startTestSession { firstArg<Session.() -> Any>()(this) }
}
}

private fun <T> startTestSession(block: Session.() -> T): T {
var returnValue: T? = null
testSession {
returnValue = block()
}
return returnValue ?: throw IllegalStateException("Session did not return a value.")
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package de.maibornwolff.codecharta.ccsh.parser

import com.varabyte.kotter.runtime.Session
import com.varabyte.kotterx.test.foundation.testSession
import de.maibornwolff.codecharta.tools.ccsh.Ccsh
import de.maibornwolff.codecharta.tools.ccsh.parser.InteractiveDialog
import de.maibornwolff.codecharta.tools.ccsh.parser.InteractiveParserSuggestion
import de.maibornwolff.codecharta.tools.ccsh.parser.ParserService
import de.maibornwolff.codecharta.tools.interactiveparser.startSession
import de.maibornwolff.codecharta.tools.interactiveparser.SessionMock
import io.mockk.every
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import org.assertj.core.api.Assertions
Expand Down Expand Up @@ -47,7 +44,7 @@ class InteractiveParserSuggestionTest {

@BeforeEach
fun beforeTest() {
mockStartSession()
SessionMock.mockStartSession()
}

@AfterEach
Expand Down Expand Up @@ -162,19 +159,4 @@ class InteractiveParserSuggestionTest {
Assertions.assertThat(selectedParsers).isEmpty()
Assertions.assertThat(errorOut.toString()).contains("Specified invalid or empty path to analyze! Aborting...")
}

private fun mockStartSession() {
mockkStatic("de.maibornwolff.codecharta.tools.interactiveparser.ParserDialogInterfaceKt")
every { startSession(any<Session.() -> Any>()) } answers {
startTestSession { firstArg<Session.() -> Any>()(this) }
}
}

private fun <T> startTestSession(block: Session.() -> T): T {
var returnValue: T? = null
testSession {
returnValue = block()
}
return returnValue ?: throw IllegalStateException("Session did not return a value.")
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package de.maibornwolff.codecharta.ccsh.parser

import com.varabyte.kotter.runtime.Session
import com.varabyte.kotterx.test.foundation.testSession
import de.maibornwolff.codecharta.tools.ccsh.Ccsh
import de.maibornwolff.codecharta.tools.ccsh.parser.InteractiveDialog
import de.maibornwolff.codecharta.tools.ccsh.parser.ParserService
import de.maibornwolff.codecharta.tools.ccsh.parser.repository.PicocliParserRepository
import de.maibornwolff.codecharta.tools.interactiveparser.InteractiveParser
import de.maibornwolff.codecharta.tools.interactiveparser.ParserDialogInterface
import de.maibornwolff.codecharta.tools.interactiveparser.startSession
import de.maibornwolff.codecharta.tools.interactiveparser.SessionMock
import io.mockk.every
import io.mockk.mockkClass
import io.mockk.mockkConstructor
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import org.assertj.core.api.Assertions
Expand Down Expand Up @@ -88,7 +86,7 @@ class ParserServiceTest {
val expectedParserCommand =
"ccsh " + selectedParser + " " + collectedArgs.map { x -> '"' + x + '"' }.joinToString(" ")

mockStartSession()
SessionMock.mockStartSession()
val selectedParserList = listOf(selectedParser)
val mockPicocliParserRepository = mockParserRepository(selectedParser, emptyList())

Expand All @@ -113,7 +111,7 @@ class ParserServiceTest {
mockkObject(InteractiveDialog)
every { InteractiveDialog.askParserToExecute(any(), any()) } returns "$fakeParser $fakeParserDescription"

mockStartSession()
SessionMock.mockStartSession()

val selectedParser = ParserService.selectParser(cmdLine, PicocliParserRepository())

Expand Down Expand Up @@ -178,7 +176,7 @@ class ParserServiceTest {
"csvimport"
)

mockStartSession()
SessionMock.mockStartSession()

val mockPicocliParserRepository = mockParserRepository(selectedParserList[0], emptyList())

Expand All @@ -198,7 +196,7 @@ class ParserServiceTest {
@MethodSource("providerParserArguments")
fun `should execute parser`(parser: String) {
mockParserObject(parser)
mockStartSession()
SessionMock.mockStartSession()

ParserService.executeSelectedParser(cmdLine, parser)

Expand Down Expand Up @@ -234,7 +232,7 @@ class ParserServiceTest {
@ParameterizedTest
@MethodSource("providerParserArguments")
fun `should output message informing about which parser is being configured`(parser: String) {
mockStartSession()
SessionMock.mockStartSession()

val mockParserRepository = mockParserRepository(parser, listOf(parser))

Expand All @@ -243,13 +241,6 @@ class ParserServiceTest {
assertThat(outContent.toString()).contains("Now configuring $parser.")
}

private fun mockStartSession() {
mockkStatic("de.maibornwolff.codecharta.tools.interactiveparser.ParserDialogInterfaceKt")
every { startSession(any<Session.() -> Any>()) } answers {
startTestSession { firstArg<Session.() -> Any>()(this) }
}
}

private fun mockParserObject(name: String): InteractiveParser {
val obj = cmdLine.subcommands[name]!!.commandSpec.userObject() as InteractiveParser
mockkObject(obj)
Expand Down Expand Up @@ -284,12 +275,4 @@ class ParserServiceTest {

return obj
}

private fun <T> startTestSession(block: Session.() -> T): T {
var returnValue: T? = null
testSession {
returnValue = block()
}
return returnValue ?: throw IllegalStateException("Session did not return a value.")
}
}

0 comments on commit 5284289

Please sign in to comment.