Skip to content

Commit

Permalink
Remove maybeFail
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Dec 16, 2018
1 parent 087b3f4 commit 940165b
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/Fake.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE FlexibleContexts #-}

module Fake (
State,
Expand Down Expand Up @@ -133,13 +134,14 @@ oneOfArray arr = do
-- Number 21.0
--
-- >>> runFakeT Nothing (oneOfArgs [])
-- Nothing
oneOfArgs :: [Expr] -> Fake (Maybe Value)
-- *** Exception: At least 1 argument required
-- ...
oneOfArgs :: [Expr] -> Fake Value
oneOfArgs args = do
rndArg <- rndListItem args
case rndArg of
Nothing -> pure Nothing
Just arg -> Just <$> eval arg
Nothing -> Except.throwError "At least 1 argument required"
Just arg -> eval arg


-- | Create an array with `num` items
Expand Down Expand Up @@ -261,12 +263,6 @@ randomChar = charToString <$> State.state random
charToString :: Char -> Value
charToString = String . T.pack . (: [])

maybeFail :: Monad m => String -> m (Maybe a) -> m a
maybeFail err x = do
x' <- x
case x' of
Nothing -> error err
Just x'' -> pure x''

-- | Create a value getter for an expression
--
Expand All @@ -289,8 +285,7 @@ eval (FunctionCall "randomInt" [lower, upper]) = randomInt lower upper
eval (FunctionCall "randomDouble" [lower, upper]) = randomDouble lower upper
eval (FunctionCall "array" args) = Array . V.fromList <$> mapM eval args
eval (FunctionCall "oneOf" [arg]) = oneOfArray arg
eval (FunctionCall "oneOf" args) =
maybeFail "oneOf requires at least 1 argument" (oneOfArgs args)
eval (FunctionCall "oneOf" args) = oneOfArgs args
eval (FunctionCall "replicate" [num, expr]) = replicate num expr
eval (FunctionCall "object" args) = objectFromArgs args
eval (FunctionCall "fromFile" [fileName]) = fromFile fileName
Expand Down

0 comments on commit 940165b

Please sign in to comment.