Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/2.5.1 #98

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@

import com.atiurin.ultron.annotations.ExperimentalUltronApi
import com.atiurin.ultron.core.test.UltronTest
import com.atiurin.ultron.log.UltronLog
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class UltronTestFlowTest : UltronTest() {
companion object {
var order = 0
var beforeAllTestCounter = -1
var beforeFirstTestCounter = 0
var commonBeforeOrder = -1
var commonAfterOrder = -1
var afterOrder = -1

}

@OptIn(ExperimentalUltronApi::class)
override val beforeFirstTest = {
beforeAllTestCounter = order
beforeFirstTestCounter++
UltronLog.info("Before Class")
}

Expand All @@ -39,6 +38,7 @@ class UltronTestFlowTest : UltronTest() {
var goOrder = -1
order++
before {
assertTrue(beforeFirstTestCounter == 1, message = "beforeFirstTest block should run before all test")
beforeOrder = order
order++
UltronLog.info("Before TestMethod 1")
Expand All @@ -49,8 +49,6 @@ class UltronTestFlowTest : UltronTest() {
}.after {
afterOrder = order
order++
assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run before all test")
assertTrue(beforeAllTestCounter < commonBeforeOrder, message = "beforeAllTests block should run before commonBefore block")
assertTrue(commonBeforeOrder < beforeOrder, message = "beforeOrder block should run after commonBefore block")
assertTrue(beforeOrder < goOrder, message = "Before block should run before 'go'")
assertTrue(goOrder < afterOrder, message = "After block should run after 'go'")
Expand All @@ -64,14 +62,48 @@ class UltronTestFlowTest : UltronTest() {
}.after {
UltronLog.info("After TestMethod 2")
}.go {
assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run only once")
assertTrue(beforeFirstTestCounter == 1, message = "beforeFirstTest block should run only once")
UltronLog.info("Run TestMethod 2")
}
}

@Test
fun simpleTest() = test {
assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run only once")
assertTrue(beforeFirstTestCounter == 1, message = "beforeFirstTest block should run only once")
UltronLog.info("UltronTest simpleTest")
}


@Test
fun afterBlockExecutedOnFailedTest() {
var isAfterExecuted = false
runCatching {
test {
go {
throw RuntimeException("test exception")
}
after {
isAfterExecuted = true
}
}
}
assertTrue(isAfterExecuted)
}

@Test
fun testExceptionMessageThrownOnFailedTest() {
val testExceptionMessage = "test exception"
runCatching {
test {
go {
throw RuntimeException(testExceptionMessage)
}
after {
throw RuntimeException("Another after exception")
}
}
}.onFailure { ex ->
assertEquals(ex.message, testExceptionMessage)
}
}
}
14 changes: 8 additions & 6 deletions composeApp/src/commonTest/kotlin/UltronTestFlowTest2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import com.atiurin.ultron.annotations.ExperimentalUltronApi
import com.atiurin.ultron.core.test.UltronTest
import com.atiurin.ultron.log.UltronLog
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class UltronTestFlowTest2 : UltronTest() {
var order = 0
var beforeAllTestCounter = 0
companion object {
var order = 0
var beforeFirstTestCounter = 0
}

@OptIn(ExperimentalUltronApi::class)
override val beforeFirstTest = {
beforeAllTestCounter = order
beforeFirstTestCounter++
order++
UltronLog.info("Before Class")
}
Expand All @@ -22,6 +25,7 @@ class UltronTestFlowTest2 : UltronTest() {
var goOrder = -1
order++
before {
assertEquals(1, beforeFirstTestCounter, message = "beforeFirstTest block should run before all test")
beforeOrder = order
order++
UltronLog.info("Before TestMethod 1")
Expand All @@ -31,8 +35,6 @@ class UltronTestFlowTest2 : UltronTest() {
UltronLog.info("Run TestMethod 1")
}.after {
afterOrder = order
assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run before all test")
assertTrue(beforeOrder > beforeAllTestCounter, message = "Before block should run after 'Before All'")
assertTrue(beforeOrder < goOrder, message = "Before block should run before 'go'")
assertTrue(goOrder < afterOrder, message = "After block should run after 'go'")
}
Expand All @@ -45,7 +47,7 @@ class UltronTestFlowTest2 : UltronTest() {
}.after {
UltronLog.info("After TestMethod 2")
}.go {
assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run only once")
assertEquals(1, beforeFirstTestCounter, message = "beforeFirstTest block should run before all test")
UltronLog.info("Run TestMethod 2")
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ kotlin.mpp.enableCInteropCommonization=true

GROUP=com.atiurin
POM_ARTIFACT_ID=ultron
VERSION_NAME=2.5.0-alpha16
VERSION_NAME=2.5.2
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package com.atiurin.sampleapp.tests.testlifecycle
import com.atiurin.sampleapp.tests.BaseTest
import com.atiurin.ultron.annotations.ExperimentalUltronApi
import com.atiurin.ultron.log.UltronLog
import org.junit.Assert
import org.junit.Test

class UltronTestFlowTest : BaseTest() {
companion object {
var order = 0
var beforeAllTestCounter = -1
var isBeforeFirstTestCounter = 0
var commonBeforeOrder = -1
var commonAfterOrder = -1
var afterOrder = -1
Expand All @@ -17,7 +18,7 @@ class UltronTestFlowTest : BaseTest() {

@OptIn(ExperimentalUltronApi::class)
override val beforeFirstTest = {
beforeAllTestCounter = order
isBeforeFirstTestCounter++
UltronLog.info("Before Class")
}

Expand All @@ -39,6 +40,7 @@ class UltronTestFlowTest : BaseTest() {
var goOrder = -1
order++
before {
assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeFirstTest block should run before commonBefore block" })
beforeOrder = order
order++
UltronLog.info("Before TestMethod 1")
Expand All @@ -49,8 +51,6 @@ class UltronTestFlowTest : BaseTest() {
}.after {
afterOrder = order
order++
assert(beforeAllTestCounter == 0, lazyMessage = { "beforeAllTests block should run before all test" })
assert(beforeAllTestCounter < commonBeforeOrder, lazyMessage = { "beforeAllTests block should run before commonBefore block" })
assert(commonBeforeOrder < beforeOrder, lazyMessage = { "beforeOrder block should run after commonBefore block" })
assert(beforeOrder < goOrder, lazyMessage = { "Before block should run before 'go'" })
assert(goOrder < afterOrder, lazyMessage = { "After block should run after 'go'" })
Expand All @@ -61,17 +61,35 @@ class UltronTestFlowTest : BaseTest() {
fun someTest2() = test(suppressCommonBefore = true) {
before {
UltronLog.info("Before TestMethod 2")
}.after {
}
after {
UltronLog.info("After TestMethod 2")
}.go {
assert(beforeAllTestCounter == 0, lazyMessage = { "beforeAllTests block should run only once" })
}
go {
assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeFirstTest block should run only once" })
UltronLog.info("Run TestMethod 2")
}
}

@Test
fun simpleTest() = test {
assert(beforeAllTestCounter == 0, lazyMessage = { "beforeAllTests block should run only once" })
assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeAllTests block should run only once" })
UltronLog.info("UltronTest simpleTest")
}

@Test
fun afterBlockExecutedOnFailedTest() {
var isAfterExecuted = false
runCatching {
test {
go{
throw RuntimeException("test exception")
}
after {
isAfterExecuted = true
}
}
}
Assert.assertTrue(isAfterExecuted)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ class TestMethod(testContext: UltronTestContext) {
private var test: TestMethod.() -> Unit = {}

internal fun attack() {
var throwable: Throwable? = null
beforeTest()
test()
afterTest()
runCatching(test).onFailure { ex ->
throwable = ex
}
runCatching(afterTest).onFailure { ex ->
throwable?.let { throw it }
throw ex
}
throwable?.let { throw it }
}

fun before(block: TestMethod.() -> Unit) = apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open class UltronTest(
*
* @throws UltronException if the test class is anonymous.
*/
private val className = this::class.qualifiedName
private val className = this::class.simpleName
?: throw UltronException("Don't use anonymous class for UltronTest")

/**
Expand Down Expand Up @@ -72,15 +72,25 @@ open class UltronTest(
if (!suppressCommonBefore) {
beforeTest()
}

var throwable: Throwable? = null
// Configure and execute the test block
configureTestBlock()
attack()
runCatching {
configureTestBlock()
attack()
}.onFailure { ex ->
throwable = ex
}

// Execute common `afterTest` logic if not suppressed
if (!suppressCommonAfter) {
afterTest()
runCatching(afterTest).onFailure { ex ->
throwable?.let { throw it }
throw ex
}
}
throwable?.let { throw it }
}
}


}