Skip to content

Commit

Permalink
Merge pull request #163 from jwChung/address-non-characters
Browse files Browse the repository at this point in the history
fix for returning noncharacters from Gen.unicode
  • Loading branch information
moodmosaic authored Jun 26, 2018
2 parents 099343c + e510079 commit a32a8bc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Hedgehog/Gen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,17 @@ module Gen =
let latin1 : Gen<char> =
char '\000' '\255'

/// Generates a Unicode character, excluding invalid standalone surrogates:
/// '\000'..'\65535' (excluding '\55296'..'\57343')
/// Generates a Unicode character, excluding noncharacters
/// ('\65534', '\65535') and invalid standalone surrogates
/// ('\000'..'\65535' excluding '\55296'..'\57343').
[<CompiledName("Unicode")>]
let unicode : Gen<char> =
filter (not << System.Char.IsSurrogate) unicodeAll
let isNoncharacter x =
x = Operators.char 65534
|| x = Operators.char 65535
unicodeAll
|> filter (not << isNoncharacter)
|> filter (not << System.Char.IsSurrogate)

// Generates a random alpha character.
[<CompiledName("Alpha")>]
Expand Down
13 changes: 13 additions & 0 deletions tests/Hedgehog.Tests/GenTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ let ``dateTime creates System.DateTime instances`` count =
|> List.distinct
|> List.length
=! actual.Length

[<Fact>]
let ``unicode doesn't return any surrogate`` () =
let actual = Gen.sample 100 100000 Gen.unicode
[] =! List.filter System.Char.IsSurrogate actual

[<Theory>]
[<InlineData(65534)>]
[<InlineData(65535)>]
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

0 comments on commit a32a8bc

Please sign in to comment.