{-# LANGUAGE CPP #-}
module Test.Sandwich.WebDriver.Internal.Util where
import Control.Monad
import Control.Monad.IO.Unlift
import qualified Data.Text as T
import qualified System.Random as R
import Test.Sandwich (expectationFailure)
import UnliftIO.Exception
leftOnException :: (MonadUnliftIO m) => m (Either T.Text a) -> m (Either T.Text a)
leftOnException :: forall (m :: * -> *) a.
MonadUnliftIO m =>
m (Either Text a) -> m (Either Text a)
leftOnException = (SomeException -> m (Either Text a))
-> m (Either Text a) -> m (Either Text a)
forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
(e -> m a) -> m a -> m a
handle (\(SomeException
e :: SomeException) -> Either Text a -> m (Either Text a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either Text a -> m (Either Text a))
-> Either Text a -> m (Either Text a)
forall a b. (a -> b) -> a -> b
$ Text -> Either Text a
forall a b. a -> Either a b
Left (Text -> Either Text a) -> Text -> Either Text a
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ SomeException -> String
forall a. Show a => a -> String
show SomeException
e)
exceptionOnLeft :: (MonadUnliftIO m) => m (Either T.Text a) -> m a
exceptionOnLeft :: forall (m :: * -> *) a. MonadUnliftIO m => m (Either Text a) -> m a
exceptionOnLeft m (Either Text a)
action = m (Either Text a)
action m (Either Text a) -> (Either Text a -> m a) -> m a
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left Text
err -> String -> m a
forall (m :: * -> *) a. (HasCallStack, MonadIO m) => String -> m a
expectationFailure (Text -> String
T.unpack Text
err)
Right a
x -> a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
leftOnException' :: (MonadUnliftIO m) => m a -> m (Either T.Text a)
leftOnException' :: forall (m :: * -> *) a. MonadUnliftIO m => m a -> m (Either Text a)
leftOnException' m a
action = m (Either Text a)
-> (SomeException -> m (Either Text a)) -> m (Either Text a)
forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catch (a -> Either Text a
forall a b. b -> Either a b
Right (a -> Either Text a) -> m a -> m (Either Text a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
action) (\(SomeException
e :: SomeException) -> Either Text a -> m (Either Text a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either Text a -> m (Either Text a))
-> Either Text a -> m (Either Text a)
forall a b. (a -> b) -> a -> b
$ Text -> Either Text a
forall a b. a -> Either a b
Left (Text -> Either Text a) -> Text -> Either Text a
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ SomeException -> String
forall a. Show a => a -> String
show SomeException
e)
whenJust :: (Monad m) => Maybe a -> (a -> m b) -> m ()
whenJust :: forall (m :: * -> *) a b. Monad m => Maybe a -> (a -> m b) -> m ()
whenJust Maybe a
Nothing a -> m b
_ = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
whenJust (Just a
x) a -> m b
action = m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m b -> m ()) -> m b -> m ()
forall a b. (a -> b) -> a -> b
$ a -> m b
action a
x
whenLeft :: (Monad m) => Either a b -> (a -> m ()) -> m ()
whenLeft :: forall (m :: * -> *) a b.
Monad m =>
Either a b -> (a -> m ()) -> m ()
whenLeft (Left a
x) a -> m ()
action = a -> m ()
action a
x
whenLeft (Right b
_) a -> m ()
_ = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
whenRight :: (Monad m) => Either a b -> (b -> m ()) -> m ()
whenRight :: forall (m :: * -> *) a b.
Monad m =>
Either a b -> (b -> m ()) -> m ()
whenRight (Left a
_) b -> m ()
_ = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
whenRight (Right b
x) b -> m ()
action = b -> m ()
action b
x
makeUUID :: IO T.Text
makeUUID :: IO Text
makeUUID = (String -> Text
T.pack (String -> Text) -> (StdGen -> String) -> StdGen -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
10 (String -> String) -> (StdGen -> String) -> StdGen -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char, Char) -> StdGen -> String
forall g. RandomGen g => (Char, Char) -> g -> String
forall a g. (Random a, RandomGen g) => (a, a) -> g -> [a]
R.randomRs (Char
'a',Char
'z')) (StdGen -> Text) -> IO StdGen -> IO Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO StdGen
forall (m :: * -> *). MonadIO m => m StdGen
R.newStdGen