Skip to content

Commit

Permalink
Use Fuzz.oneOfValues when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
miniBill committed Dec 14, 2024
1 parent 4eb5a4b commit 7aa9e1b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Internal/Builtin/Fuzzer.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Internal.Builtin.Fuzzer exposing (codeGen)

import CodeGenerator exposing (CodeGenerator)
import Elm.CodeGen as CG
import Elm.Syntax.Expression as Expression
import Elm.Syntax.Node exposing (Node(..))
import Maybe.Extra
import String.Extra
import TypePattern exposing (TypePattern(..))

Expand All @@ -19,7 +22,15 @@ codeGen =
, typePattern = Typed [ "Fuzz" ] "Fuzzer" [ Target ]
, makeName = \name -> String.Extra.decapitalize name ++ "Fuzzer"
}
[ CodeGenerator.customType (\_ exps -> CG.apply [ fuzz "oneOf", CG.list (List.map Tuple.second exps) ])
[ CodeGenerator.customType
(\_ exps ->
case Maybe.Extra.combineMap extractFromConstant exps of
Just constants ->
CG.apply [ fuzz "oneOfValues", CG.list constants ]

Nothing ->
CG.apply [ fuzz "oneOf", CG.list (List.map Tuple.second exps) ]
)
, CodeGenerator.pipeline (\c -> CG.apply [ fuzz "constant", c ]) (\m -> CG.apply [ fuzz "andMap", m ])
, CodeGenerator.mapN 8 (\name a bs -> CG.apply (fuzz name :: a :: bs))
, CodeGenerator.map (\a b -> CG.apply [ fuzz "map", a, b ])
Expand All @@ -31,3 +42,13 @@ codeGen =
, CodeGenerator.char (fuzz "char")
, CodeGenerator.lambdaBreaker (\inner -> CG.apply [ fuzz "lazy", CG.lambda [ CG.unitPattern ] inner ])
]


extractFromConstant : ( String, CG.Expression ) -> Maybe CG.Expression
extractFromConstant ( _, expr ) =
case expr of
Expression.Application [ Node _ (Expression.FunctionOrValue [ "Fuzz" ] "constant"), Node _ inner ] ->
Just inner

_ ->
Nothing
22 changes: 22 additions & 0 deletions tests/FuzzerCodeGenTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,27 @@ columnConstraintFuzzer =
innerColumnConstraintFuzzer : Fuzzer CreateTable.InnerColumnConstraint
innerColumnConstraintFuzzer =
Debug.todo ""
"""
, codeGenTest "Issue #17"
[ elmJson ]
[]
[ """module A exposing (..)
import Fuzz exposing (Fuzzer)
type Semaphore = Red | Yellow | Green
fuzzer : Fuzzer Semaphore
fuzzer =
Debug.todo ""
"""
]
"""module A exposing (..)
import Fuzz exposing (Fuzzer)
type Semaphore = Red | Yellow | Green
fuzzer : Fuzzer Semaphore
fuzzer =
Fuzz.oneOfValues [ Red, Yellow, Green ]
"""
]

0 comments on commit 7aa9e1b

Please sign in to comment.