Skip to content

Commit

Permalink
Merge pull request #442 from njlr/feature/issue-440-gen-timespan
Browse files Browse the repository at this point in the history
Add function Gen.timeSpan
  • Loading branch information
TysonMN committed Jul 30, 2023
2 parents 6271360 + 2c0879e commit 09bdbd2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Hedgehog/Gen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,15 @@ module Gen =
let! offsetMinutes = int32 (Range.linearFrom 0 (Operators.int minOffsetMinutes) (Operators.int maxOffsetMinutes))
return DateTimeOffset(ticks, TimeSpan.FromMinutes (Operators.float offsetMinutes))
}

#if !FABLE_COMPILER
/// Generates a random TimeSpan using the specified range.
let timeSpan (range : Range<TimeSpan>) : Gen<TimeSpan> =
range
|> Range.map (fun x -> x.Ticks (* Fable can't do this *))
|> int64
|> map TimeSpan.FromTicks
#endif

//
// Sampling
Expand Down
17 changes: 17 additions & 0 deletions tests/Hedgehog.Tests/GenTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ let genTests = testList "Gen tests" [
|> List.distinct
|> List.length
=! actual.Length

#if !FABLE_COMPILER
// See production code
yield! testCases "timeSpan creates TimeSpan instances"
[ 8; 16; 32; 64; 128; 256; 512 ] <| fun count ->

let actual =
(Range.constant TimeSpan.MinValue TimeSpan.MaxValue)
|> Gen.timeSpan
|> Gen.sample 0 count
|> Seq.toList

actual
|> List.distinct
|> List.length
=! actual.Length
#endif

testCase "unicode does not return any surrogate" <| fun _ ->
let actual =
Expand Down

0 comments on commit 09bdbd2

Please sign in to comment.