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 function Gen.timeSpan #442

Merged
merged 1 commit into from
Jul 30, 2023
Merged
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
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
Loading