@@ -11,7 +11,9 @@ module Fake (
1111
1212import qualified Aeson as A
1313import Control.Monad (forM , replicateM )
14- import Control.Monad.IO.Class (liftIO , MonadIO )
14+ import Control.Monad.Except (ExceptT )
15+ import qualified Control.Monad.Except as Except
16+ import Control.Monad.IO.Class (MonadIO , liftIO )
1517import Control.Monad.State.Class (MonadState )
1618import Control.Monad.State.Strict (StateT )
1719import qualified Control.Monad.State.Strict as State
@@ -39,7 +41,7 @@ import qualified Text.Regex.TDFA.ReadRegex as R
3941-- >>> let exec expr = runFakeT (Just 1) (eval expr)
4042
4143
42- newtype Fake a = Fake { runFake :: StateT Env IO a }
44+ newtype Fake a = Fake { runFake :: ExceptT String ( StateT Env IO ) a }
4345 deriving
4446 ( Functor
4547 , Applicative
@@ -51,7 +53,10 @@ newtype Fake a = Fake { runFake :: StateT Env IO a }
5153runFakeT :: Maybe Int -> Fake a -> IO a
5254runFakeT seed fake = do
5355 env <- newEnv seed
54- State. evalStateT (runFake fake) env
56+ result <- State. evalStateT (Except. runExceptT (runFake fake)) env
57+ case result of
58+ Left errorMsg -> error errorMsg
59+ Right result' -> pure result'
5560
5661type State a = StateT Env IO a
5762
@@ -106,7 +111,7 @@ randomDouble lower upper = do
106111--
107112-- >>> exec "randomBool"
108113-- Bool False
109- randomBool :: Monad m => StateT Env m Value
114+ randomBool :: ( RandomGen g , MonadState g m ) => m Value
110115randomBool = Bool <$> State. state random
111116
112117
@@ -249,7 +254,7 @@ fromFile fileName = do
249254--
250255-- >>> exec "randomChar()"
251256-- String "\629160"
252- randomChar :: Monad m => StateT Env m Value
257+ randomChar :: ( RandomGen g , MonadState g m ) => m Value
253258randomChar = charToString <$> State. state random
254259 where
255260 charToString :: Char -> Value
@@ -277,8 +282,8 @@ eval (DoubleLiteral x) = pure $ Number x
277282eval (FunctionCall " uuid4" [] ) = String . UUID. toText <$> State. state random
278283eval (FunctionCall " uuid1" [] ) = String . UUID. toText <$> liftIO uuid1
279284eval (FunctionCall " null" [] ) = pure Null
280- eval (FunctionCall " randomBool" [] ) = Fake randomBool
281- eval (FunctionCall " randomChar" [] ) = Fake randomChar
285+ eval (FunctionCall " randomBool" [] ) = randomBool
286+ eval (FunctionCall " randomChar" [] ) = randomChar
282287eval (FunctionCall " randomInt" [lower, upper]) = randomInt lower upper
283288eval (FunctionCall " randomDouble" [lower, upper]) = randomDouble lower upper
284289eval (FunctionCall " array" args) = Array . V. fromList <$> mapM eval args
0 commit comments