Skip to content

Commit

Permalink
Merge pull request hedgehogqa#165 from jwChung/address-datetime-inclu…
Browse files Browse the repository at this point in the history
…ding-sub-seconds

Address generating datetime to include sub seconds
  • Loading branch information
moodmosaic authored Jul 13, 2018
2 parents f13fa4d + aeec579 commit 5f14a9c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
25 changes: 9 additions & 16 deletions src/Hedgehog/Gen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -496,23 +496,16 @@ module Gen =
/// Generates a random instant in time expressed as a date and time of day.
[<CompiledName("DateTime")>]
let dateTime : Gen<System.DateTime> =
let yMin = System.DateTime.MinValue.Year
let yMax = System.DateTime.MaxValue.Year
let minTicks =
System.DateTime.MinValue.Ticks
let maxTicks =
System.DateTime.MaxValue.Ticks
gen {
let! y =
integral <| Range.linearFrom 2000 yMin yMax
let! m =
integral <| Range.constant 1 12
let! d =
integral <| Range.constant 1 (System.DateTime.DaysInMonth (y, m))
let! h =
integral <| Range.constant 0 23
let! min =
integral <| Range.constant 0 59
let! sec =
integral <| Range.constant 0 59

return System.DateTime (y, m, d, h, min, sec)
let! ticks =
Range.constantFrom
(System.DateTime (2000, 1, 1)).Ticks minTicks maxTicks
|> integral
return System.DateTime ticks
}

//
Expand Down
34 changes: 33 additions & 1 deletion tests/Hedgehog.Tests/GenTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,36 @@ let ``unicode doesn't return any surrogate`` () =
let ``unicode doesn't return any noncharacter`` nonchar =
let isNoncharacter = (=) <| Operators.char nonchar
let actual = Gen.sample 100 100000 Gen.unicode
[] =! List.filter isNoncharacter actual
[] =! List.filter isNoncharacter actual

[<Fact>]
let ``dateTime randomly generates value between max and min ticks`` () =
let seed0 = Seed.random()
let (seed1, _) = Seed.split seed0
let range =
Range.constant
System.DateTime.MinValue.Ticks
System.DateTime.MaxValue.Ticks
let ticks =
Random.integral range
|> Random.run seed1 0
let expected = System.DateTime ticks

let actual = Gen.dateTime

let result = actual |> Gen.toRandom |> Random.run seed0 0 |> Tree.outcome
expected =! result

[<Fact>]
let ``dateTime shrinks to correct mid-value`` () =
let result =
property {
let! actual = Gen.dateTime
System.DateTime.Now =! actual
}
|> Property.report
|> Report.render
|> (fun x -> x.Split System.Environment.NewLine)
|> Array.item 1
|> System.DateTime.Parse
System.DateTime (2000, 1, 1) =! result

0 comments on commit 5f14a9c

Please sign in to comment.