<commit_before>
module Test.Monad(main) where

import Test.Type
import Development.Shake.Internal.Core.Monad

import Data.IORef
import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.IO.Class




run :: ro -> rw -> RAW ro rw a -> IO a
run ro rw m = do
    res <- newEmptyMVar
    runRAW ro rw m $ void . tryPutMVar res
    either throwIO return =<< readMVar res


main = testSimple $ do
    let conv x = either (Left . fromException) Right x :: Either (Maybe ArithException) Int
    let dump rw = liftIO . (=== rw) =<< getRW

    -- test the basics plus exception handling
    run 1 "test" $ do
        dump "test"
        putRW "more"
        dump "more"
        res <- tryRAW $ do
            dump "more"
            modifyRW (++ "x")
            dump "morex"
            return 100
        liftIO $ conv res === Right 100
        dump "morex"
        putRW "new"
        dump "new"
        res <- tryRAW $ do
            dump "new"
            modifyRW (++ "z")
            dump "newz"
            throwRAW Overflow
            error "Should not have reached here"
            return 9
        liftIO $ conv res === Left (Just Overflow)
        dump "newz"
        catchRAW (catchRAW (throwRAW Overflow) $ \_ -> modifyRW (++ "x")) $
            \_ -> modifyRW (++ "y")
        dump "newzx"
        catchRAW (catchRAW (throwRAW Overflow) $ \e -> modifyRW (++ "x") >> throwRAW e) $
            \_ -> modifyRW (++ "y")
        dump "newzxxy"

    -- test capture
    run 1 "test" $ do
        i <- captureRAW $ \k -> k $ Right 1
        liftIO $ i === 1
        i <- tryRAW $ captureRAW $ \k -> k $ Left $ toException Overflow
        liftIO $ conv i === Left (Just Overflow)
        captureRAW $ \k -> k $ Right ()
        i <- tryRAW $ throwRAW Underflow
        liftIO $ conv i === Left (Just Underflow)

    -- catch does not scope too far
    res <- try $ run 1 "test" $
        fmap (either show id) $ tryRAW $ captureRAW $ \_ -> throwIO Overflow
    res === Left Overflow
    res <- try $ run 1 "test" $ do
        captureRAW $ \_ -> throwIO Overflow
        return "x"
    res === Left Overflow
    -- test for GHC bug 11555
    runRAW 1 "test" (throw Overflow :: RAW Int String ()) $ \res ->
        either (Left . fromException) Right res === Left (Just Overflow)

    -- catch works properly if continuation called multiple times
    ref <- newIORef []
    run 1 "test" $
        flip catchRAW (const $ liftIO $ modifyIORef ref ('x':)) $ do<commit_msg>Use mapLeft in a few places<commit_after>
module Test.Monad(main) where

import Test.Type
import Development.Shake.Internal.Core.Monad

import Data.Either.Extra
import Data.IORef
import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.IO.Class




run :: ro -> rw -> RAW ro rw a -> IO a
run ro rw m = do
    res <- newEmptyMVar
    runRAW ro rw m $ void . tryPutMVar res
    either throwIO return =<< readMVar res


main = testSimple $ do
    let conv x = mapLeft fromException x :: Either (Maybe ArithException) Int
    let dump rw = liftIO . (=== rw) =<< getRW

    -- test the basics plus exception handling
    run 1 "test" $ do
        dump "test"
        putRW "more"
        dump "more"
        res <- tryRAW $ do
            dump "more"
            modifyRW (++ "x")
            dump "morex"
            return 100
        liftIO $ conv res === Right 100
        dump "morex"
        putRW "new"
        dump "new"
        res <- tryRAW $ do
            dump "new"
            modifyRW (++ "z")
            dump "newz"
            throwRAW Overflow
            error "Should not have reached here"
            return 9
        liftIO $ conv res === Left (Just Overflow)
        dump "newz"
        catchRAW (catchRAW (throwRAW Overflow) $ \_ -> modifyRW (++ "x")) $
            \_ -> modifyRW (++ "y")
        dump "newzx"
        catchRAW (catchRAW (throwRAW Overflow) $ \e -> modifyRW (++ "x") >> throwRAW e) $
            \_ -> modifyRW (++ "y")
        dump "newzxxy"

    -- test capture
    run 1 "test" $ do
        i <- captureRAW $ \k -> k $ Right 1
        liftIO $ i === 1
        i <- tryRAW $ captureRAW $ \k -> k $ Left $ toException Overflow
        liftIO $ conv i === Left (Just Overflow)
        captureRAW $ \k -> k $ Right ()
        i <- tryRAW $ throwRAW Underflow
        liftIO $ conv i === Left (Just Underflow)

    -- catch does not scope too far
    res <- try $ run 1 "test" $
        fmap (either show id) $ tryRAW $ captureRAW $ \_ -> throwIO Overflow
    res === Left Overflow
    res <- try $ run 1 "test" $ do
        captureRAW $ \_ -> throwIO Overflow
        return "x"
    res === Left Overflow
    -- test for GHC bug 11555
    runRAW 1 "test" (throw Overflow :: RAW Int String ()) $ \res ->
        mapLeft fromException res === Left (Just Overflow)

    -- catch works properly if continuation called multiple times
    ref <- newIORef []
    run 1 "test" $
        flip catchRAW (const $ liftIO $ modifyIORef ref ('x':)) $ do
{'fraction_non_alphanumeric': 0.11370436864153202, 'fraction_numerical': 0.007979253939756632, 'mean_word_length': 1.8327683615819208, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7012369', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1491, 'n_words': 556}