Skip to content

Commit

Permalink
Close #312 - Add unicode encoding for the error message from NonBlank…
Browse files Browse the repository at this point in the history
…String.from
  • Loading branch information
kevin-lee committed Apr 21, 2024
1 parent 916fdc1 commit 27c1c33
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ object strings {
override def invalidReason(a: String): String =
expectedMessage("not all whitespace non-empty String")

@SuppressWarnings(Array("org.wartremover.warts.ToString"))
override def from(a: String): Either[String, Type] =
super
.from(a)

Check warning on line 72 in modules/refined4s-core/shared/src/main/scala/refined4s/types/strings.scala

View check run for this annotation

Codecov / codecov/patch

modules/refined4s-core/shared/src/main/scala/refined4s/types/strings.scala#L71-L72

Added lines #L71 - L72 were not covered by tests
.left
.map(_ => "Invalid value: [" + a + s"], unicode=[${a.map(c => "\\u%04x".format(c.toInt)).mkString}]. " + invalidReason(a))

override def predicate(a: String): Boolean = a != "" && !a.forall(c => WhitespaceCharRange.exists((min, max) => c >= min && c <= max))

extension (inline thisNbs: Type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ object stringsSpec extends Properties {
) =>
})
.log(
s"Compilation should have been failed but it didn't for NonBlankString(s) (non-literal String). Errors: ${shouldFail5.map(_.toString).mkString("[", ", ", "]")}"
"Compilation should have been failed but it didn't for NonBlankString(s) (non-literal String). " +
s"Errors: ${shouldFail5.map(_.toString).mkString("[", ", ", "]")}"
),
)
)
Expand All @@ -269,12 +270,14 @@ object stringsSpec extends Properties {
s <-
Gen
.frequency1(
5 -> Gen.constant(""),
95 -> Gen.string(hedgehog.extra.Gens.genCharByRange(strings.WhitespaceCharRange), Range.linear(1, 10)),
5 -> Gen.constant(""),
)
.log("s")
} yield {
val expected = s"Invalid value: [$s]. It must be not all whitespace non-empty String"
val expected =
s"Invalid value: [$s], unicode=[${s.map(c => "\\u%04x".format(c.toInt)).mkString}]. " +
s"It must be not all whitespace non-empty String"
val actual = NonBlankString.from(s)
actual ==== Left(expected)
}
Expand Down Expand Up @@ -302,7 +305,9 @@ object stringsSpec extends Properties {
)
.log("s")
} yield {
val expected = s"Invalid value: [$s]. It must be not all whitespace non-empty String"
val expected =
s"Invalid value: [$s], unicode=[${s.map(c => "\\u%04x".format(c.toInt)).mkString}]. " +
s"It must be not all whitespace non-empty String"
try {
NonBlankString.unsafeFrom(s)
Result
Expand Down

0 comments on commit 27c1c33

Please sign in to comment.