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

Add LocalDate generator #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions src/main/groovy/spock/genesis/Gen.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import spock.genesis.generators.values.CharacterGenerator
import spock.genesis.generators.values.DateGenerator
import spock.genesis.generators.values.DoubleGenerator
import spock.genesis.generators.values.IntegerGenerator
import spock.genesis.generators.values.LocalDateGenerator
import spock.genesis.generators.values.LongGenerator
import spock.genesis.generators.values.RandomElementGenerator
import spock.genesis.generators.values.StringGenerator
import spock.genesis.generators.values.ValueGenerator

import java.time.LocalDate
import java.util.regex.Pattern

/**
Expand Down Expand Up @@ -347,6 +349,29 @@ class Gen {
new DateGenerator(minDate, maxDate)
}

/**
* Produces a lazy infinite {@link LocalDateGenerator} that generates
* random instances of {@link java.time.LocalDate}
*
* @return an instance of {@link LocalDateGenerator}
*/
static LocalDateGenerator getLocalDate() {
new LocalDateGenerator()
}

/**
* Produces a lazy infinite {@link LocalDateGenerator}. This generator
* will create random instances of {@link java.time.LocalDate} from a minimum
* date to a maximum date.
*
* @param minDate minimum possible date
* @param maxDate maximum possible date
* @return an instance of {@link LocalDateGenerator}
*/
static LocalDateGenerator localDate(LocalDate minDate, LocalDate maxDate) {
new LocalDateGenerator(minDate, maxDate)
}

/**
* Produces a lazy infinite {@link FactoryGenerator}. This
* generator will produce random instances of the values returned
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package spock.genesis.generators.values

import spock.genesis.generators.InfiniteGenerator
import spock.genesis.generators.InfiniteIterator

import java.time.LocalDate

class LocalDateGenerator extends InfiniteGenerator<LocalDate> {
final LongGenerator millisProvider

LocalDateGenerator() {
this.millisProvider = new LongGenerator()
}

LocalDateGenerator(LocalDate minDate, LocalDate maxDate) {
this.millisProvider = new LongGenerator(minDate.toEpochDay(), maxDate.toEpochDay())
}

InfiniteIterator<LocalDate> iterator() {
final Iterator<Long> TIME = millisProvider.iterator()
new InfiniteIterator<LocalDate>() {
@Override
LocalDate next() {
LocalDate.ofEpochDay(TIME.next())
}
}
}

@Override
LocalDateGenerator seed(Long seed) {
super.seed(seed)
millisProvider.seed(seed)
this
}
}
17 changes: 17 additions & 0 deletions src/test/groovy/spock/genesis/SamplesSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import spock.lang.Specification
import spock.lang.Unroll
import spock.util.mop.Use

import java.time.LocalDate

import static spock.genesis.Gen.*


Expand Down Expand Up @@ -103,6 +105,21 @@ class SamplesSpec extends Specification {
}
// end::dates[]

// tag::localDates[]
def 'create a new localDate value range'() {
given: "yesterday's reference and tomorrow's"
def yesterday = LocalDate.now().minusDays(1)
def tomorrow = LocalDate.now().plusDays(1)

when: 'getting a new date'
def newDate = localDate(yesterday, tomorrow).iterator().next()

then: 'new date should be between boundaries'
tomorrow.plusDays(1).isAfter(newDate)
newDate.isAfter(yesterday.minusDays(1))
}
// end::localDates[]

// tag::ampersand[]
def 'create multi source generator with & operator'() {
setup:
Expand Down