Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix golden tests generation for Gen instances using filter #1151

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions zio-json-golden/src/main/scala/zio/json/golden/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,20 @@ package object golden {
} yield TestResult(assertion)
}

/**
* Implementation inspired by zio-test [[zio.test#check]]
*/
private def generateSample[A: JsonEncoder](
gen: Gen[Sized, A],
sampleSize: Int
)(implicit trace: Trace): ZIO[Sized, Exception, GoldenSample] =
Gen
.listOfN(sampleSize)(gen)
.sample
gen.sample.forever
.map(_.value)
.map { elements =>
val jsonElements = elements.map(_.toJsonAST).collect { case Right(a) => a }
val jsonArray = new Json.Arr(Chunk.fromIterable(jsonElements))
GoldenSample(jsonArray)
}
.runHead
.someOrFailException
.map(_.toJsonAST)
.collectRight
.take(sampleSize.toLong)
.runCollect
.map(jsonElements => GoldenSample(new Json.Arr(jsonElements)))

private def getName[A](implicit tag: Tag[A]): String =
tag.tag.shortName
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
{
"samples" : [
{
"a" : -6.807778209396064042484608633080144E+37
},
{
"a" : 1.595098341748072691735235811828587E+38
},
{
"a" : 1.459950313422007732470858735428470E+38
},
{
"a" : 3.966890979309949040353919413508109E+37
},
{
"a" : 1.233609631287946015973115353212111E+38
},
{
"a" : 1.473182672254656075288635468340979E+38
},
{
"a" : -4.370307886719313347409020693679809E+36
},
{
"a" : 4.037946164963809325160258151515959E+37
},
{
"a" : 9.654807468219847939517983223821157E+36
},
{
"a" : 9.016039177425153953741714882797374E+37
},
{
"a" : 7.146160681625873270982431039786890E+37
},
{
"a" : 5.715520023941784610636805140974297E+37
},
{
"a" : -4.963886209068154507584885110622067E+37
},
{
"a" : 1.429642469253121453233931676540811E+38
},
{
"a" : 1.347358173024022215551559165152039E+38
},
{
"a" : -1.443197818206812225540268705601235E+38
},
{
"a" : 1.652974714985098210216497615438388E+38
},
{
"a" : 6.854881439246725349714811941975828E+37
},
{
"a" : -1.440269453811987772460377566176060E+38
},
{
"a" : 7.349647466961309857751700891214103E+37
},
{
"a" : 1.442405212658813209066752593353578E+38
},
{
"a" : 3.965771366281331473297193084703031E+37
},
{
"a" : 6.269323508703350877363225990119655E+37
},
{
"a" : -1.758914181069706042842822199462931E+37
},
{
"a" : 1.223935114914264676851645426397511E+38
},
{
"a" : -7.384928634462281384291440360868614E+37
},
{
"a" : 1.693228930919227030554211724500322E+38
},
{
"a" : 4.424974721701017780262797468032916E+37
},
{
"a" : -1.303664216711215462163176389172558E+38
},
{
"a" : 1.404704317795160461166424204194676E+37
},
{
"a" : -1.329057724454004799664476545910425E+38
},
{
"a" : -1.048356224872675570271942686628903E+38
},
{
"a" : -6.290438873817500525528580887751261E+37
},
{
"a" : 1.084524705805485052810712065828027E+38
},
{
"a" : -7.550243566246841354983346269026909E+37
},
{
"a" : 9.340651914763911687206557126706644E+37
},
{
"a" : -2.480333693026775008020223168834843E+37
},
{
"a" : -1.094510132373439403344708257152704E+38
},
{
"a" : -6.786566488714105384835344334324745E+37
},
{
"a" : 1.584399487952599918984676750023353E+37
},
{
"a" : 5.128682031007228158346770228279368E+37
},
{
"a" : 9.348340016921799959253942302421099E+37
},
{
"a" : 7.030935368599146218716189376901462E+37
},
{
"a" : 1.535594588509527431040574927018135E+38
},
{
"a" : -4.934741325633573501964215791643639E+37
},
{
"a" : 9.682312857134232428245162228518594E+37
},
{
"a" : -2.614986691569376906824897939793061E+37
},
{
"a" : -1.014394404488501605028181495043210E+38
},
{
"a" : 5.488115260327267108034268022746715E+37
},
{
"a" : 1.126087665079675493640367522377595E+38
},
{
"a" : -1.039540496026219616948722255179099E+38
},
{
"a" : -9.140747409014218126917907727764322E+37
},
{
"a" : -4.824125603843691307227014286009879E+37
},
{
"a" : -8.145094645621006028171195553277435E+37
},
{
"a" : -5.626674097898124181091040103617824E+37
},
{
"a" : 9.103992673978668331401073051758253E+37
},
{
"a" : -6.434701867932310680029743191675950E+37
},
{
"a" : 1.544388056858114009506299836014695E+37
},
{
"a" : -1.265122848783281340816555691730657E+38
},
{
"a" : -1.038801113752491268000223564191006E+38
},
{
"a" : 1.005113855834286665723509372286190E+38
},
{
"a" : -1.532637041640666300354819845360303E+38
},
{
"a" : 1.460152591686346812838507724273444E+37
},
{
"a" : -1.807215480356152323767541674888201E+37
},
{
"a" : 1.130164367464931673447884683370859E+38
},
{
"a" : -2.206605703514989373349781667972389E+37
},
{
"a" : -7.335310305765769880508310438610933E+37
},
{
"a" : -9.032060789190847023210378392783773E+37
},
{
"a" : -1.434866879379575685137940146522389E+38
},
{
"a" : -8.537143289319745801149293653774310E+37
},
{
"a" : -7.189751213495862580473152406825669E+37
},
{
"a" : -1.116003336173084398830455517338484E+38
},
{
"a" : 4.350602216631428056662100784595331E+37
},
{
"a" : -4.200580225109217264680432341389498E+37
},
{
"a" : 1.519464406997130639901449647551254E+38
},
{
"a" : 1.605847304967111250934189072078944E+38
},
{
"a" : -1.537207767079682775719785647758733E+38
},
{
"a" : -1.279659726955884472452164873447049E+38
},
{
"a" : 6.508277213006463765700394170604852E+37
},
{
"a" : 1.283817488073224657514525683762202E+38
},
{
"a" : 1.339367218367240868953919097436571E+38
},
{
"a" : -1.682265900214633524445901815287998E+37
},
{
"a" : 1.206863685039632397166752202964046E+38
},
{
"a" : -1.084738675922968416633463809761249E+38
},
{
"a" : 5.657051930790282199429825000570743E+37
},
{
"a" : -8.295559405291592878379291027574690E+36
},
{
"a" : 1.759196447669202351875403477421470E+37
},
{
"a" : 1.469253810134078897550508376470893E+38
},
{
"a" : 4.042754662729660664430342317410895E+37
},
{
"a" : -2.203950127770040611464116843938096E+37
},
{
"a" : 1.510320473631250916474309414975180E+38
},
{
"a" : -7.763024290460987157407648151549977E+37
},
{
"a" : 2.621985659558485942857886646450931E+37
},
{
"a" : -7.736585181363804829301628996321853E+37
},
{
"a" : -6.009164305461593191654380749583999E+37
},
{
"a" : -9.519320687479863998450437188934807E+37
},
{
"a" : -3.199912116712397916450907359672628E+37
},
{
"a" : 1.050603568467569482115229536968020E+38
},
{
"a" : -1.259168689102438672889911917677648E+38
},
{
"a" : -1.203951555022847927293048112388223E+38
}
]
}
26 changes: 24 additions & 2 deletions zio-json-golden/src/test/scala/zio/json/golden/GoldenSpec.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package zio.json.golden

import zio._
import zio.json._
import zio.json.golden._
import zio.test._
import zio.test.magnolia.DeriveGen
import zio._

object GoldenSpec extends ZIOSpecDefault {

Expand All @@ -29,6 +28,25 @@ object GoldenSpec extends ZIOSpecDefault {
implicit val jsonCodec: JsonCodec[RecordType] = DeriveJsonCodec.gen
}

final case class FilteredGenType(a: java.math.BigDecimal)
object FilteredGenType {
implicit val jsonCodec: JsonCodec[FilteredGenType] = DeriveJsonCodec.gen

val anyFilteredGenType: Gen[Any, FilteredGenType] = {

/**
* Copied from zio-json/shared/src/test/scala/zio/json/Gens.scala
*/
val genBigDecimal: Gen[Any, java.math.BigDecimal] =
Gen
.bigDecimal((BigDecimal(2).pow(128) - 1) * -1, BigDecimal(2).pow(128) - 1)
.map(_.bigDecimal)
.filter(_.toBigInteger.bitLength < 128)

genBigDecimal.map(FilteredGenType.apply)
}
}

def spec: Spec[TestEnvironment with Scope, Any] = suite("GoldenSpec")(
goldenTest(DeriveGen[Int]),
goldenTest(DeriveGen[SumType]),
Expand All @@ -41,6 +59,10 @@ object GoldenSpec extends ZIOSpecDefault {
}, {
implicit val config: GoldenConfiguration = GoldenConfiguration.default.copy(relativePath = "recordtype")
goldenTest(DeriveGen[RecordType])
}, {
implicit val config: GoldenConfiguration =
GoldenConfiguration.default.copy(relativePath = "filteredgentype", sampleSize = 100)
goldenTest(FilteredGenType.anyFilteredGenType)
}
)

Expand Down
Loading