Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Property based tests for Refined support (#144)
* Property tests for numeric refined support * Distinguish test names * Format code * Fix 2.11 compilation
- Loading branch information
1 parent
819bda3
commit cbc1cc7
Showing
2 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
zio-config-refined/src/test/scala/zio/config/refined/NumericSupportTest.scala
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,142 @@ | ||
package zio.config.refined | ||
|
||
import eu.timepit.refined.W | ||
import eu.timepit.refined.api.Refined | ||
import eu.timepit.refined.numeric.{ Divisible, Greater, GreaterEqual, Less, LessEqual, NonDivisible } | ||
import zio.ZIO | ||
import zio.config.ConfigDescriptor._ | ||
import zio.config.{ helpers, read, write, BaseSpec, ConfigSource, ReadErrorsVector } | ||
import zio.test.Assertion._ | ||
import zio.test._ | ||
|
||
object NumericSupportTest | ||
extends BaseSpec( | ||
suite("Refined Numeric support")( | ||
testM("Refined config Less roundtrip") { | ||
checkM(Gen.int(1, 9).map(s => Refined.unsafeApply[Int, Less[W.`10`.T]](s))) { p => | ||
val cfg = less[W.`10`.T](int("TEST")) | ||
val p2 = | ||
for { | ||
written <- ZIO.fromEither(write(cfg, p)) | ||
reread <- read(cfg from ConfigSource.fromPropertyTree(written)) | ||
} yield reread | ||
|
||
assertM(p2, equalTo(p)) | ||
} | ||
}, | ||
testM("Refined config Less invalid") { | ||
checkM(Gen.int(10, 100)) { p => | ||
val cfg = less[W.`10`.T](int("TEST")) | ||
val p2: ZIO[Any, ReadErrorsVector[String, String], Refined[Int, Less[W.`10`.T]]] = | ||
read(cfg from ConfigSource.fromMap(Map("TEST" -> p.toString))) | ||
|
||
assertM(p2.either, helpers.assertErrors(_.size == 1)) | ||
} | ||
}, | ||
testM("Refined config Greater roundtrip") { | ||
checkM(Gen.int(11, 100).map(s => Refined.unsafeApply[Int, Greater[W.`10`.T]](s))) { p => | ||
val cfg = greater[W.`10`.T](int("TEST")) | ||
val p2 = | ||
for { | ||
written <- ZIO.fromEither(write(cfg, p)) | ||
reread <- read(cfg from ConfigSource.fromPropertyTree(written)) | ||
} yield reread | ||
|
||
assertM(p2, equalTo(p)) | ||
} | ||
}, | ||
testM("Refined config Greater invalid") { | ||
checkM(Gen.int(1, 10)) { p => | ||
val cfg = greater[W.`10`.T](int("TEST")) | ||
val p2: ZIO[Any, ReadErrorsVector[String, String], Refined[Int, Greater[W.`10`.T]]] = | ||
read(cfg from ConfigSource.fromMap(Map("TEST" -> p.toString))) | ||
|
||
assertM(p2.either, helpers.assertErrors(_.size == 1)) | ||
} | ||
}, | ||
testM("Refined config LessEqual roundtrip") { | ||
checkM(Gen.int(1, 10).map(s => Refined.unsafeApply[Int, LessEqual[W.`10`.T]](s))) { p => | ||
val cfg = lessEqual[W.`10`.T](int("TEST")) | ||
val p2 = | ||
for { | ||
written <- ZIO.fromEither(write(cfg, p)) | ||
reread <- read(cfg from ConfigSource.fromPropertyTree(written)) | ||
} yield reread | ||
|
||
assertM(p2, equalTo(p)) | ||
} | ||
}, | ||
testM("Refined config LessEqual invalid") { | ||
checkM(Gen.int(11, 100)) { p => | ||
val cfg = lessEqual[W.`10`.T](int("TEST")) | ||
val p2: ZIO[Any, ReadErrorsVector[String, String], Refined[Int, LessEqual[W.`10`.T]]] = | ||
read(cfg from ConfigSource.fromMap(Map("TEST" -> p.toString))) | ||
|
||
assertM(p2.either, helpers.assertErrors(_.size == 1)) | ||
} | ||
}, | ||
testM("Refined config GreaterEqual roundtrip") { | ||
checkM(Gen.int(10, 100).map(s => Refined.unsafeApply[Int, GreaterEqual[W.`10`.T]](s))) { p => | ||
val cfg = greaterEqual[W.`10`.T](int("TEST")) | ||
val p2 = | ||
for { | ||
written <- ZIO.fromEither(write(cfg, p)) | ||
reread <- read(cfg from ConfigSource.fromPropertyTree(written)) | ||
} yield reread | ||
|
||
assertM(p2, equalTo(p)) | ||
} | ||
}, | ||
testM("Refined config GreaterEqual invalid") { | ||
checkM(Gen.int(1, 9)) { p => | ||
val cfg = greaterEqual[W.`10`.T](int("TEST")) | ||
val p2: ZIO[Any, ReadErrorsVector[String, String], Refined[Int, GreaterEqual[W.`10`.T]]] = | ||
read(cfg from ConfigSource.fromMap(Map("TEST" -> p.toString))) | ||
|
||
assertM(p2.either, helpers.assertErrors(_.size == 1)) | ||
} | ||
}, | ||
testM("Refined config Divisible roundtrip") { | ||
checkM(Gen.int(1, 10).map(s => Refined.unsafeApply[Int, Divisible[W.`10`.T]](s * 10))) { p => | ||
val cfg = divisible[W.`10`.T](int("TEST")) | ||
val p2 = | ||
for { | ||
written <- ZIO.fromEither(write(cfg, p)) | ||
reread <- read(cfg from ConfigSource.fromPropertyTree(written)) | ||
} yield reread | ||
|
||
assertM(p2, equalTo(p)) | ||
} | ||
}, | ||
testM("Refined config Divisible invalid") { | ||
checkM(Gen.int(1, 10).map(_ * 10 + 1)) { p => | ||
val cfg = divisible[W.`10`.T](int("TEST")) | ||
val p2: ZIO[Any, ReadErrorsVector[String, String], Refined[Int, Divisible[W.`10`.T]]] = | ||
read(cfg from ConfigSource.fromMap(Map("TEST" -> p.toString))) | ||
|
||
assertM(p2.either, helpers.assertErrors(_.size == 1)) | ||
} | ||
}, | ||
testM("Refined config NonDivisible roundtrip") { | ||
checkM(Gen.int(1, 10).map(s => Refined.unsafeApply[Int, NonDivisible[W.`10`.T]](s * 10 + 1))) { p => | ||
val cfg = nonDivisible[W.`10`.T](int("TEST")) | ||
val p2 = | ||
for { | ||
written <- ZIO.fromEither(write(cfg, p)) | ||
reread <- read(cfg from ConfigSource.fromPropertyTree(written)) | ||
} yield reread | ||
|
||
assertM(p2, equalTo(p)) | ||
} | ||
}, | ||
testM("Refined config NonDivisible invalid") { | ||
checkM(Gen.int(1, 10).map(_ * 10)) { p => | ||
val cfg = nonDivisible[W.`10`.T](int("TEST")) | ||
val p2: ZIO[Any, ReadErrorsVector[String, String], Refined[Int, NonDivisible[W.`10`.T]]] = | ||
read(cfg from ConfigSource.fromMap(Map("TEST" -> p.toString))) | ||
|
||
assertM(p2.either, helpers.assertErrors(_.size == 1)) | ||
} | ||
} | ||
) | ||
) |
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