From 2c0879e5dc678f48ef98aa218198e6fc483d0598 Mon Sep 17 00:00:00 2001 From: njlr Date: Tue, 25 Jul 2023 19:26:22 +0100 Subject: [PATCH] Add function Gen.timeSpan --- src/Hedgehog/Gen.fs | 9 +++++++++ tests/Hedgehog.Tests/GenTests.fs | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Hedgehog/Gen.fs b/src/Hedgehog/Gen.fs index f525e511..c71b780a 100644 --- a/src/Hedgehog/Gen.fs +++ b/src/Hedgehog/Gen.fs @@ -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) : Gen = + range + |> Range.map (fun x -> x.Ticks (* Fable can't do this *)) + |> int64 + |> map TimeSpan.FromTicks +#endif // // Sampling diff --git a/tests/Hedgehog.Tests/GenTests.fs b/tests/Hedgehog.Tests/GenTests.fs index acc89127..a62f0824 100644 --- a/tests/Hedgehog.Tests/GenTests.fs +++ b/tests/Hedgehog.Tests/GenTests.fs @@ -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 =