Skip to content

Commit

Permalink
data: introduce result.contains (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil authored Oct 12, 2024
1 parent eb8cd98 commit cca7c91
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
12 changes: 12 additions & 0 deletions kyo-data/shared/src/main/scala/kyo/Result.scala
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,18 @@ object Result:
case Success(v) => Result.fail(v)
case _ => self.asInstanceOf[Result[A, E]]

/** Checks if the Result is a Success and contains the given value.
*
* @param value
* The value to check for
* @return
* true if the Result is a Success and contains the given value, false otherwise
*/
def contains(value: A)(using CanEqual[A, A]): Boolean =
self match
case Success(`value`) => true
case _ => false

/** Returns a string representation of the Result.
*
* @return
Expand Down
69 changes: 69 additions & 0 deletions kyo-data/shared/src/test/scala/kyo/ResultTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,75 @@ class ResultTest extends Test:

}

"contains" - {
"should return true for Success with matching value" in {
val result = Result.success(42)
assert(result.contains(42))
}

"should return false for Success with non-matching value" in {
val result = Result.success(42)
assert(!result.contains(43))
}

"should return false for Fail" in {
val result = Result.fail[String, Int]("error")
assert(!result.contains(42))
}

"should return false for Panic" in {
val result = Result.panic[String, Int](new Exception("panic"))
assert(!result.contains(42))
}

"should work with custom types" in {
case class Person(name: String, age: Int) derives CanEqual
val person = Person("Alice", 30)
val result = Result.success(person)
assert(result.contains(person))
assert(!result.contains(Person("Bob", 25)))
}

"should work with Maybe values" in {
val someMaybeResult = Result.success(Maybe(42))
assert(someMaybeResult.contains(Maybe(42)))
assert(!someMaybeResult.contains(Maybe(43)))
assert(!someMaybeResult.contains(Maybe.empty))

val noneMaybeResult = Result.success(Maybe.empty)
assert(noneMaybeResult.contains(Maybe.empty))
assert(!noneMaybeResult.contains(Maybe(42)))

val failResult: Result[String, Maybe[Int]] = Result.fail("error")
assert(!failResult.contains(Maybe(42)))
assert(!failResult.contains(Maybe.empty))

val panicResult: Result[String, Maybe[Int]] = Result.panic(new Exception("panic"))
assert(!panicResult.contains(Maybe(42)))
assert(!panicResult.contains(Maybe.empty))
}

"should work with nested Result values" in {
val nestedSuccessResult: Result[String, Result[Int, String]] = Result.success(Result.success("nested"))
assert(nestedSuccessResult.contains(Result.success("nested")))
assert(!nestedSuccessResult.contains(Result.success("other")))
assert(!nestedSuccessResult.contains(Result.fail(42)))

val nestedFailResult: Result[String, Result[Int, String]] = Result.success(Result.fail(42))
assert(nestedFailResult.contains(Result.fail(42)))
assert(!nestedFailResult.contains(Result.success("nested")))

val deeplyNestedResult: Result[String, Result[Int, Result[Double, String]]] =
Result.success(Result.success(Result.success("deeply nested")))
assert(deeplyNestedResult.contains(Result.success(Result.success("deeply nested"))))
assert(!deeplyNestedResult.contains(Result.success(Result.fail(3.14))))

val outerFailResult: Result[String, Result[Int, String]] = Result.fail("outer error")
assert(!outerFailResult.contains(Result.success("nested")))
assert(!outerFailResult.contains(Result.fail(42)))
}
}

"show" - {
"Success" in {
assert(Result.success(42).show == "Success(42)")
Expand Down

0 comments on commit cca7c91

Please sign in to comment.