Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@dieproht
Copy link
Contributor

@dieproht dieproht commented Nov 7, 2019

This is an extension of Gen implementing generators for basic time datatypes: ZIO duration, Instant, LocalDateTime and OffsetDateTime. The implementation is done in the way like diskussed with @adamgfraser at Berlin hackathon.

/**
* A generator of ZIO duration values. Shrinks toward Duration.Zero.
*/
final def anyDuration: Gen[Random, Duration] = Gen.long(0L, Long.MaxValue).map(Duration.Finite(_))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anyFiniteDuration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I've updated the PR

Copy link
Contributor

@adamgfraser adamgfraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Just a few minor comments. Do you want to add any more generators? We could definitely do generators for other date / time classes if you had interest, but also fine to merge with what we have here after these changes.

/**
* A generator of ZIO duration values inside the specified range: [min, max].
*/
final def duration(min: Duration, max: Duration): Gen[Random, Duration] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finiteDuration? Should we add a generator of infinite values (it would just be a constant generator) and possibly a generator of duration values that produces both? Not sure what ratio of finite to infinite values would make sense.

final def anyFiniteDuration: Gen[Random, Duration] = Gen.long(0L, Long.MaxValue).map(Duration.Finite(_))

/**
* A generator java.time.Instant values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"generator of" instead of "generator". I would put the class names like java.time.Instant in back ticks. Also briefly state what the generator shrinks towards.

object TimeVariantsSpec extends AsyncBaseSpec {

val run: List[Async[(Boolean, String)]] = List(
label(anyFiniteDurationShrinksToZero, "anyFiniteDuration shrinks to zero"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add properties about what any of the other generators shrink to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback! I'm still working on improvements...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. Just reach out whenever you're ready for another review.

@dieproht
Copy link
Contributor Author

Hi @adamgfraser, generators for (finite) Duration, Instant, LocalDatetime and OffsetDateTime are ready for another review now. I would not add more datatypes in this PR because one can derive many similar types from the existing ones in a trivial way (i.e. LocalDateTime.toLocalTime).

However I would like to add anyDuration, but therefor we have to combine a constant generator that emits Infinity with the existing anyFiniteDuration generator reasonably and it's unclear to me how to do that. Do you have an clue about this? Otherwise let`s add that later in another PR...

Copy link
Contributor

@adamgfraser adamgfraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Congratulations on your first contribution!

@adamgfraser adamgfraser merged commit 9025ac4 into zio:master Nov 15, 2019
@adamgfraser
Copy link
Contributor

@dieproht To combine a generator of finite values with a generator of infinite values we could either do something like booleans.flatMap(p => if (p) finite else infinite) (if we want an equal distribution between finite and infinite values) or weighted which lets us specify some percentage of finite versus infinite values to generate. So mechanically it seems straightforward. The harder question is what ratio to generate. The only non-arbitrary ratio seems to be half and half, but that also feels like potentially "too many" infinite values. Maybe that is a reason to just leave it up to the user to combine them in the appropriate ratio for them or maybe we try to just pick some ratio so we can have a generator that covers both finite and infinite values for easy testing of functions that use duration concepts. What do you think?

Twizty pushed a commit to Twizty/zio that referenced this pull request Nov 18, 2019
* Add generators for time datatypes

* renamed anyDuration anyFiniteDuration

* improved generators and tests
@dieproht
Copy link
Contributor Author

Thank you @adamgfraser for supporting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants