Skip to content

Commit

Permalink
fixup! Add Fuzz.filterMap
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaechler committed Jan 20, 2024
1 parent 55c4cd2 commit 099edd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/Fuzz.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ module Fuzz exposing
, array, maybe, result
, bool, unit, order, weightedBool
, oneOf, oneOfValues, frequency, frequencyValues
, constant, invalid, filter
, constant, invalid, filter, filterMap
, map, map2, map3, map4, map5, map6, map7, map8, andMap
, andThen, lazy, sequence, traverse
, fromGenerator
, filterMap
)

{-| This is a library of _fuzzers_ you can use to supply values to your fuzz
Expand Down Expand Up @@ -1336,23 +1335,33 @@ a risk of infinite loop depending on the predicate), you can use this pattern:
-}
filter : (a -> Bool) -> Fuzzer a -> Fuzzer a
filter predicate = filterMap (\a -> if predicate a then Just a else Nothing)
filter predicate =
filterMap
(\a ->
if predicate a then
Just a

else
Nothing
)


{-| A fuzzer that applies a function returning a Maybe on a given fuzzer and
output values, as List.filterMap does.
Example usage:
type UnicodeNonLetter = UnicodeNonLetter Char
type UnicodeNonLetter
= UnicodeNonLetter Char
fromChar : Char -> Maybe UnicodeNonLetter
fromChar c =
if (c |> Unicode.isLower |> not) && (c |> Unicode.isUpper |> not) then
UnicodeNonLetter |> Just
else
Nothing
fuzz : Fuzzer UnicodeNonLetter
fuzz =
Fuzz.char |> Fuzz.filterMap fromChar
Expand Down
8 changes: 7 additions & 1 deletion tests/src/FuzzerTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1145,8 +1145,14 @@ fuzzerSpecificationTests =
intsNotDivBy5 : Fuzzer Int
intsNotDivBy5 =
Fuzz.int
|> Fuzz.filterMap (\i -> if isDivBy5 i then Nothing else Just i)
|> Fuzz.filterMap
(\i ->
if isDivBy5 i then
Nothing

else
Just i
)
in
[ rejects "impossible func (always Nothing)"
(Fuzz.int |> Fuzz.filterMap (\_ -> Nothing))
Expand Down

0 comments on commit 099edd4

Please sign in to comment.