-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
krefty-core/src/commonTest/kotlin/dev/ustits/krefty/core/RefinedTest.kt
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
krefty-core/src/commonTest/kotlin/dev/ustits/krefty/core/RefinementTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package dev.ustits.krefty.core | ||
|
||
import dev.ustits.krefty.predicate.string.Blank | ||
import dev.ustits.krefty.predicate.string.Length | ||
import dev.ustits.krefty.predicate.string.NotBlank | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.result.shouldBeFailure | ||
import io.kotest.matchers.result.shouldBeSuccess | ||
import io.kotest.matchers.shouldBe | ||
|
||
class RefinementTest : StringSpec ({ | ||
|
||
"returns value" { | ||
val refinement = refine(NotBlank(), "oil") | ||
refinement.getOrThrow() shouldBe "oil" | ||
refinement.getOrNull() shouldBe "oil" | ||
refinement.getOrElse { "gas" } shouldBe "oil" | ||
refinement.getOrError() shouldBeSuccess "oil" | ||
} | ||
|
||
"returns default value" { | ||
val refinement = refine(Blank(), "oil") | ||
refinement.getOrElse { "gas" } shouldBe "gas" | ||
} | ||
|
||
"returns null" { | ||
val refinement = refine(NotBlank(), "") | ||
refinement.getOrNull() shouldBe null | ||
} | ||
|
||
"returns error" { | ||
val refinement = refine(NotBlank(), "") | ||
refinement.getOrError() shouldBeFailure RefinementException("") | ||
} | ||
|
||
"throws exception" { | ||
val refinement = refine(NotBlank(), "") | ||
shouldThrow<RefinementException> { | ||
refinement.getOrThrow() | ||
} | ||
} | ||
|
||
"maps if can be refined" { | ||
val refinement = refine(NotBlank(), "oil").map { "gas" } | ||
refinement.getOrThrow() shouldBe "gas" | ||
} | ||
|
||
"doesn't map if can't be refined" { | ||
val refinement = refine(NotBlank(), "").map { "gas" } | ||
refinement.getOrNull() shouldBe null | ||
} | ||
|
||
"flatMaps if can be refined" { | ||
val refinement = refine(NotBlank(), "oil").flatMap { | ||
refine(Length(3), it) | ||
} | ||
refinement.getOrThrow() shouldBe "oil" | ||
} | ||
|
||
"doesn't flatMap if can't be refined" { | ||
val refinement = refine(NotBlank(), "").flatMap { | ||
refine(Length(0), it) | ||
} | ||
refinement.getOrNull() shouldBe null | ||
} | ||
|
||
"returns error if after flatMap can't be refined" { | ||
val refinement = refine(NotBlank(), "oil").flatMap { | ||
refine(Length(2), it) | ||
} | ||
refinement.getOrNull() shouldBe null | ||
} | ||
}) |