Skip to content

Commit

Permalink
misc: Use fewerBraces
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxxel committed Apr 19, 2024
1 parent 8d5b2c0 commit 9976c6c
Show file tree
Hide file tree
Showing 24 changed files with 165 additions and 314 deletions.
7 changes: 6 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ newlines.source = keep

rewrite.scala3 {
convertToNewSyntax = yes
removeOptionalBraces = yes
removeOptionalBraces {
"enabled": true,
"fewerBracesMinSpan": 2,
"fewerBracesMaxSpan": 600,
"oldSyntaxToo": yes
}
}

align {
Expand Down
3 changes: 1 addition & 2 deletions borer/src/io/github/iltotore/iron/borer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ object borer:
encoder.asInstanceOf[Encoder[A :| B]]

inline given [A, B](using inline decoder: Decoder[A], inline constraint: Constraint[A, B]): Decoder[A :| B] =
Decoder { r =>
Decoder: r =>
decoder.read(r).refineEither match
case Left(msg) => r.validationFailure(msg)
case Right(x) => x
}

inline given [T](using m: RefinedTypeOps.Mirror[T], ev: Encoder[m.IronType]): Encoder[T] =
ev.asInstanceOf[Encoder[T]]
Expand Down
12 changes: 4 additions & 8 deletions borer/test/src/io/github/iltotore/iron/BorerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@ object BorerSuite extends TestSuite:

val tests: Tests = Tests {

test("opaque alias encoding") {
test("opaque alias encoding"):
Json.encode(Temperature(15.0)).toUtf8String ==> "15.0"
}

test("opaque alias decoding") {
test("opaque alias decoding"):
Json.decode("15.0".getBytes).to[Temperature].valueEither ==> Right(Temperature(15.0))
Json.decode("-15.0".getBytes).to[Temperature].valueEither.left.map(_.getMessage) ==>
Left("Should be strictly positive (input position 0)")
}

test("transparent alias encoding") {
test("transparent alias encoding"):
Json.encode(15.0: Moisture).toUtf8String ==> "15.0"
}

test("transparent alias decoding") {
test("transparent alias decoding"):
Json.decode("15.0".getBytes).to[Moisture].valueEither ==> Right(15.0: Moisture)
Json.decode("-15.0".getBytes).to[Moisture].valueEither.left.map(_.getMessage) ==>
Left("Should be strictly positive (input position 0)")
}
}
42 changes: 14 additions & 28 deletions cats/test/src/io/github/iltotore/iron/CatsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ object CatsSuite extends TestSuite:

val tests: Tests = Tests {

test("Cats instances are resolved for String iron types") {
test("Cats instances are resolved for String iron types"):
Eq[String :| NameR]
Hash[String :| NameR]
Order[String :| NameR]
PartialOrder[String :| NameR]
Show[String :| NameR]
}

test("Cats instances are resolved for new types") {
test("Cats instances are resolved for new types"):
Eq[Temperature]
Hash[Temperature]
Order[Temperature]
Expand All @@ -53,76 +52,66 @@ object CatsSuite extends TestSuite:
Hash[Moisture]
Order[Moisture]
Show[Moisture]
}

test("Cats instances are resolved for Int iron types") {
test("Cats instances are resolved for Int iron types"):
Eq[Int :| AgeR]
Hash[Int :| AgeR]
UpperBounded[Int :| AgeR]
Order[Int :| AgeR]
PartialOrder[Int :| AgeR]
Show[Int :| AgeR]
LowerBounded[Int :| AgeR]
}

test("Cats instances are resolved for a case class with iron types") {
test("Cats instances are resolved for a case class with iron types"):
Eq[Person]
Order[Person]
Show[Person]
}

test("alley") {
test("commutativeMonoid") {
test("int") {
test("int"):
test("pos") - assert(CommutativeMonoid[Int :| Positive].combine(1, 5) == 6)
test("neg") - assert(CommutativeMonoid[Int :| Negative].combine(-1, -5) == -6)
}

test("long") {
test("long"):
test("pos") - assert(CommutativeMonoid[Long :| Positive].combine(1, 5) == 6)
test("neg") - assert(CommutativeMonoid[Long :| Negative].combine(-1, -5) == -6)
}

test("float") {
test("float"):
test("pos") - assert(CommutativeMonoid[Float :| Positive].combine(1, 5) == 6)
test("neg") - assert(CommutativeMonoid[Float :| Negative].combine(-1, -5) == -6)
}

test("double") {
test("double"):
test("pos") - assert(CommutativeMonoid[Double :| Positive].combine(1, 5) == 6)
test("neg") - assert(CommutativeMonoid[Double :| Negative].combine(-1, -5) == -6)
}
}
}

test("eitherNec") {
test("eitherNec"):
import io.github.iltotore.iron.cats.*

val eitherNecWithFailingPredicate = Temperature.eitherNec(-5.0)
assert(eitherNecWithFailingPredicate == Left(NonEmptyChain.one("Should be strictly positive")), "'eitherNec' returns left if predicate fails")
val eitherNecWithSucceedingPredicate = Temperature.eitherNec(100)
assert(eitherNecWithSucceedingPredicate == Right(Temperature(100)), "right should contain result of 'apply'")
}

test("eitherNel") {
test("eitherNel"):
import io.github.iltotore.iron.cats.*

val eitherNelWithFailingPredicate = Temperature.eitherNel(-5.0)
assert(eitherNelWithFailingPredicate == Left(NonEmptyList.one("Should be strictly positive")), "'eitherNel' returns left if predicate fails")
val eitherNelWithSucceedingPredicate = Temperature.eitherNel(100)
assert(eitherNelWithSucceedingPredicate == Right(Temperature(100)), "right should contain result of 'apply'")
}

test("validated") {
test("validated"):
import io.github.iltotore.iron.cats.*

val validatedWithFailingPredicate = Temperature.validated(-5.0)
assert(validatedWithFailingPredicate == Invalid("Should be strictly positive"), "'eitherNec' returns left if predicate fails")
val validatedWithSucceedingPredicate = Temperature.validated(100)
assert(validatedWithSucceedingPredicate == Valid(Temperature(100)), "right should contain result of 'apply'")
}

test("validatedNec") {
test("validatedNec"):
import io.github.iltotore.iron.cats.*

val validatedNecWithFailingPredicate = Temperature.validatedNec(-5.0)
Expand All @@ -132,9 +121,8 @@ object CatsSuite extends TestSuite:
)
val validatedNecWithSucceedingPredicate = Temperature.validatedNec(100)
assert(validatedNecWithSucceedingPredicate == Valid(Temperature(100)), "valid should contain result of 'apply'")
}

test("validatedNel") {
test("validatedNel"):
import io.github.iltotore.iron.cats.*

val validatedNelWithFailingPredicate = Temperature.validatedNel(-5.0)
Expand All @@ -144,10 +132,8 @@ object CatsSuite extends TestSuite:
)
val validatedNelWithSucceedingPredicate = Temperature.validatedNel(100)
assert(validatedNelWithSucceedingPredicate == Valid(Temperature(100)), "valid should contain result of 'apply'")
}

test("refineAll") {
test("refineAll"):
test - assert(Temperature.optionAll(NonEmptyList.of(1, 2, -3)).isEmpty)
test - assert(Temperature.optionAll(NonEmptyList.of(1, 2, 3)).contains(NonEmptyList.of(Temperature(1), Temperature(2), Temperature(3))))
}
}
12 changes: 4 additions & 8 deletions ciris/test/src/io/github/iltotore/iron/CirisSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ import io.github.iltotore.iron.constraint.numeric.Positive
import utest.*

object CirisSuite extends TestSuite:
val tests: Tests = Tests {
val tests: Tests = Tests:

test("decoder") {
test("ironType") {
test("decoder"):
test("ironType"):
test("success") - assert(summon[ConfigDecoder[String, Int :| Positive]].decode(None, "5") == Right(5))
test("failure") - assert(summon[ConfigDecoder[String, Int :| Positive]].decode(None, "-5").isLeft)
}

test("newType") {
test("newType"):
test("success") - assert(summon[ConfigDecoder[String, Temperature]].decode(None, "5") == Right(Temperature(5)))
test("failure") - assert(summon[ConfigDecoder[String, Temperature]].decode(None, "-5").isLeft)
}
}
}
12 changes: 4 additions & 8 deletions decline/test/src/io/github/iltotore/iron/DeclineSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import io.github.iltotore.iron.constraint.numeric.Positive
import utest.*

object DeclineSuite extends TestSuite:
val tests: Tests = Tests {
val tests: Tests = Tests:

test("Argument") {
test("ironType") {
test("Argument"):
test("ironType"):
test("success") - assert(summon[Argument[Int :| Positive]].read("5") == Valid(5))
test("failure") - assert(summon[Argument[Int :| Positive]].read("-5").isInvalid)
}

test("newType") {
test("newType"):
test("success") - assert(summon[Argument[Temperature]].read("5") == Valid(5))
test("failure") - assert(summon[Argument[Temperature]].read("-5").isInvalid)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object HttpServer:
* The main logic of our mini web server.
* Register the given user passed as a `POST` [[Request]] to `/register` then return it as a `Response`
*/
val service = HttpRoutes.of[IO] { // Can be replaced with colon + indentation in Scala 3.3
val service = HttpRoutes.of[IO]:
case request @ POST -> Root / "register" =>
val routine =
for
Expand All @@ -68,4 +68,3 @@ object HttpServer:
routine.handleErrorWith(handleError)

case unknown => NotFound()
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ object HttpServer:
* Register the given user passed as a `POST` [[Request]] to `/register` then return it as a `Response`
*/
val app: HttpApp[Any, Throwable] =
Http.collectZIO[Request] {
Http.collectZIO[Request]:
case req @ Method.POST -> !! / "register" =>
req.body.asString
.map(_.fromJson[Account])
.map(_.fold(badApiRequest, account => Response.json(account.toJson)))
}
12 changes: 4 additions & 8 deletions jsoniter/test/src/io/github/iltotore/iron/JsoniterSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@ object JsoniterSuite extends TestSuite:
def assertDecodingSuccess[A](expected: A)(using JsonValueCodec[A]): Unit = assert(Try(readFromString[A](value)).fold(_ => false, _ == expected))
def assertDecodingFailure[A](using JsonValueCodec[A]): Unit = assert(Try(readFromString[A](value)).isFailure)

val tests: Tests = Tests {
test("encoding") {
val tests: Tests = Tests:
test("encoding"):
test - zero.assertEncoding("0")
test - Number(0).assertEncoding("""{"zero":0}""")
}

test("decoding - valid predicate") {
test("decoding - valid predicate"):
test - "0".assertDecodingSuccess[Int :| Zero](zero)
test - """{"zero":0}""".assertDecodingSuccess[Number](Number(0))
}

test("decoding - invalid predicate") {
test("decoding - invalid predicate"):
test - "1".assertDecodingFailure[Zero]
test - """{"zero":1}""".assertDecodingFailure[Number]
}
}
24 changes: 8 additions & 16 deletions main/test/src/io/github/iltotore/iron/testing/AnySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,43 @@ object AnySuite extends TestSuite:

val tests: Tests = Tests {

test("constant") {
test("constant"):
test("true") - Dummy.assertRefine[True]
test("false") - Dummy.assertNotRefine[False]
}

test("describedAs") {
test("describedAs"):
test - Dummy.assertRefine[True DescribedAs "test"]
test - Dummy.assertNotRefine[False DescribedAs "test"]
}

test("not") {
test("not"):
test - Dummy.assertRefine[Not[False]]
test - Dummy.assertNotRefine[Not[True]]
}

test("xor") {
test("xor"):
test - Dummy.assertRefine[Xor[True, False]]
test - Dummy.assertNotRefine[Xor[True, True]]
test - Dummy.assertNotRefine[Xor[False, False]]
}

test("union") {
test("union"):
test - Dummy.assertRefine[True | True]
test - Dummy.assertRefine[True | False]
test - Dummy.assertNotRefine[False | False]
}

test("intersection") {
test("intersection"):
test - Dummy.assertRefine[True & True]
test - Dummy.assertNotRefine[True & False]
test - Dummy.assertNotRefine[False & False]
}

test("strictEqual") {
test("strictEqual"):
test - 0.assertRefine[StrictEqual[0]]
test - 1.assertNotRefine[StrictEqual[0]]
test - BigDecimal(0).assertRefine[StrictEqual[0]]
test - BigDecimal(1).assertNotRefine[StrictEqual[0]]
test - BigInt(0).assertRefine[StrictEqual[0]]
test - BigInt(1).assertNotRefine[StrictEqual[0]]
}

test("in") {
test("in"):
test - 0.assertRefine[In[(0, 1)]]
test - 1.assertRefine[In[(0, 1)]]
test - 2.assertNotRefine[In[(0, 1)]]
}
}
18 changes: 6 additions & 12 deletions main/test/src/io/github/iltotore/iron/testing/CharSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object CharSuite extends TestSuite:

val tests: Tests = Tests {

test("blank") { // See https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Character.html#isWhitespace(char)
test("blank"): // See https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Character.html#isWhitespace(char)
test - ' '.assertRefine[Whitespace]
test - '\t'.assertRefine[Whitespace]
test - '\n'.assertRefine[Whitespace]
Expand All @@ -20,41 +20,35 @@ object CharSuite extends TestSuite:
test - '\u001E'.assertRefine[Whitespace]
test - '\u001F'.assertRefine[Whitespace]
test - 'a'.assertNotRefine[Whitespace]
}

test("lowercase") {
test("lowercase"):
test - 'a'.assertRefine[LowerCase]
test - 'A'.assertNotRefine[LowerCase]
test - ' '.assertNotRefine[LowerCase]
test - '1'.assertNotRefine[LowerCase]
}

test("uppercase") {
test("uppercase"):
test - 'A'.assertRefine[UpperCase]
test - 'a'.assertNotRefine[UpperCase]
test - ' '.assertNotRefine[UpperCase]
test - '1'.assertNotRefine[UpperCase]
}

test("digit") {
test("digit"):
test - '1'.assertRefine[Digit]
test - 'a'.assertNotRefine[Digit]
test - 'A'.assertNotRefine[Digit]
test - '-'.assertNotRefine[Digit]
}

test("letter") {
test("letter"):
test - 'a'.assertRefine[Letter]
test - 'A'.assertRefine[Letter]
test - '1'.assertNotRefine[Letter]
test - '-'.assertNotRefine[Letter]
}

test("special") {
test("special"):
test - ' '.assertRefine[Special]
test - '%'.assertRefine[Special]
test - 'a'.assertNotRefine[Special]
test - 'A'.assertNotRefine[Special]
test - '1'.assertNotRefine[Special]
}
}
Loading

0 comments on commit 9976c6c

Please sign in to comment.