|
1 | 1 | package com.avsystem.commons.misc
|
2 | 2 |
|
| 3 | +import com.avsystem.commons.CommonAliases.* |
| 4 | +import com.avsystem.commons.SharedExtensions.* |
3 | 5 | import org.scalatest.funsuite.AnyFunSuite
|
4 | 6 | import org.scalatest.matchers.should.Matchers
|
5 | 7 |
|
6 |
| -import com.avsystem.commons.SharedExtensions._ |
7 |
| -import com.avsystem.commons.CommonAliases._ |
8 |
| - |
9 | 8 | class SharedExtensionsTest extends AnyFunSuite with Matchers {
|
10 | 9 | test("mkMap") {
|
11 | 10 | List.range(0, 3).mkMap(identity, _.toString) shouldEqual
|
@@ -81,7 +80,7 @@ class SharedExtensionsTest extends AnyFunSuite with Matchers {
|
81 | 80 | }
|
82 | 81 |
|
83 | 82 | test("Future.transformWith") {
|
84 |
| - import com.avsystem.commons.concurrent.RunNowEC.Implicits._ |
| 83 | + import com.avsystem.commons.concurrent.RunNowEC.Implicits.* |
85 | 84 | val ex = new Exception
|
86 | 85 | assert(Future.successful(42).transformWith(t => Future.successful(t.get - 1)).value.contains(Success(41)))
|
87 | 86 | assert(Future.successful(42).transformWith(_ => Future.failed(ex)).value.contains(Failure(ex)))
|
@@ -206,4 +205,34 @@ class SharedExtensionsTest extends AnyFunSuite with Matchers {
|
206 | 205 | | abc
|
207 | 206 | | abc""".stripMargin)
|
208 | 207 | }
|
| 208 | + |
| 209 | + test("Try.tapFailure - Success case") { |
| 210 | + var actionCalled = false |
| 211 | + val successTry = Success(42) |
| 212 | + val result = successTry.tapFailure(_ => actionCalled = true) |
| 213 | + |
| 214 | + assert(!actionCalled, "Action should not be called for Success") |
| 215 | + assert(result === successTry, "Original Success should be returned") |
| 216 | + } |
| 217 | + |
| 218 | + test("Try.tapFailure - Failure case") { |
| 219 | + var capturedThrowable: Throwable = null |
| 220 | + val exception = new RuntimeException("test exception") |
| 221 | + val failureTry = Failure(exception) |
| 222 | + |
| 223 | + val result = failureTry.tapFailure(t => capturedThrowable = t) |
| 224 | + |
| 225 | + assert(capturedThrowable === exception, "Action should be called with the exception") |
| 226 | + assert(result === failureTry, "Original Failure should be returned") |
| 227 | + } |
| 228 | + |
| 229 | + test("Try.tapFailure - Exception in action") { |
| 230 | + val originalException = new RuntimeException("original exception") |
| 231 | + val actionException = new RuntimeException("action exception") |
| 232 | + val failureTry = Failure(originalException) |
| 233 | + |
| 234 | + val result = failureTry.tapFailure(_ => throw actionException) |
| 235 | + |
| 236 | + assert(result === failureTry, "Original Failure should be returned even if action throws") |
| 237 | + } |
209 | 238 | }
|
0 commit comments