Skip to content

Commit

Permalink
chore(dsl): Refacto exception constructor (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrouand authored Dec 19, 2024
1 parent 576d2ec commit 89d6bf2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ class ChutneyScenarioExecutionContext(

private fun convertExecuteException(t: Throwable, scenarioDescriptor: ChutneyScenarioDescriptor): Throwable {
return when (t) {
is UnresolvedEnvironmentException -> UnresolvedScenarioEnvironmentException(t.message + " Please, specify a name or declare only one environment.")
is NoEnvironmentFoundException -> UnresolvedScenarioEnvironmentException(t.message + " Please, declare one.")
is EnvironmentNotFoundException -> UnresolvedScenarioEnvironmentException("Environment [${scenarioDescriptor.environmentName}] not found. ${t.message}")
is UnresolvedEnvironmentException -> UnresolvedScenarioEnvironmentException(t)
is NoEnvironmentFoundException -> UnresolvedScenarioEnvironmentException(t)
is EnvironmentNotFoundException -> UnresolvedScenarioEnvironmentException(t, scenarioDescriptor.environmentName)
else -> AssertionError(t)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ class StepExecutionFailedException(step: Step) :
}"
)

class UnresolvedScenarioEnvironmentException(message: String?) :
NoStackTraceAssertionError(message ?: "Cannot resolve environment")
class UnresolvedScenarioEnvironmentException(
throwable: Throwable,
environmentName: String? = null
) : NoStackTraceAssertionError(
environmentName?.let { "${throwable.message}: Environment [$it] not found." }
?: "${throwable.message}: Please, specify a name or declare only one environment."
)

fun Step.findSubStepPath(toBeFound: Step): List<Step> {
if (this == toBeFound) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: 2017-2024 Enedis
*
* SPDX-License-Identifier: Apache-2.0
*
*/

package com.chutneytesting.kotlin.junit.engine.execution

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class UnresolvedScenarioEnvironmentExceptionTest {

@Test
fun exceptionMessageWithEnvironmentName() {
val throwable = Throwable("Error occurred")
val exception = UnresolvedScenarioEnvironmentException(throwable, "test-environment")

assertEquals("Error occurred: Environment [test-environment] not found.", exception.message)
}

@Test
fun exceptionMessageWithoutEnvironmentName() {
val throwable = Throwable("Error occurred")
val exception = UnresolvedScenarioEnvironmentException(throwable)

assertEquals("Error occurred: Please, specify a name or declare only one environment.", exception.message)
}
}

0 comments on commit 89d6bf2

Please sign in to comment.