Test.hs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. module Test (tests) where
  2. import Distribution.TestSuite
  3. import System.IO.Error
  4. import Control.Monad.Trans.Either
  5. import Data.Either.Combinators
  6. import Control.Monad.Trans.Interruptible
  7. simpleTest :: String -> IO Progress -> Test
  8. simpleTest n t =
  9. let test = TestInstance
  10. {run = t',
  11. name = n,
  12. tags = [],
  13. options = [],
  14. setOption = \_ _ -> Right test
  15. }
  16. in Test test
  17. where
  18. t' :: IO Progress
  19. t' = catchIOError t (
  20. \e -> return . Finished . Fail $ "Raised exception: " ++ show e
  21. )
  22. tests :: IO [Test]
  23. tests = return [
  24. simpleTest "resume" (tres),
  25. simpleTest "resume2" (tres2),
  26. simpleTest "resume3" (tres3),
  27. simpleTest "resume4" (tres4),
  28. simpleTest "resume5" (tres5),
  29. simpleTest "intercalate1" (int1),
  30. simpleTest "intercalate5" (int5)
  31. ]
  32. tres :: IO Progress
  33. tres = do
  34. let f = (\x -> return $ x + 1) :: Int -> EitherT () IO Int
  35. r <- resume f (Right 1)
  36. let v = fromRight 0 r
  37. Finished <$> if v == 2 then return Pass else return $ Fail $ "Wrong value: " ++ show v
  38. tres2 :: IO Progress
  39. tres2 = do
  40. let f = (\x -> return $ x + 1) :: Int -> EitherT () (EitherT () IO) Int
  41. r <- resume2 f (Right . Right $ 1)
  42. let v = fromRight 0 . fromRight (Left ()) $ r
  43. Finished <$> if v == 2 then return Pass else return $ Fail $ "Wrong value: " ++ show v
  44. tres3 :: IO Progress
  45. tres3 = do
  46. let f = (\x -> return $ x + 1) :: Int -> EitherT () (EitherT () (EitherT () IO)) Int
  47. r <- resume3 f (Right . Right . Right $ 1)
  48. let v = fromRight 0 . fromRight (Left ()) . fromRight (Left ()) $ r
  49. Finished <$> if v == 2 then return Pass else return $ Fail $ "Wrong value: " ++ show v
  50. tres4 :: IO Progress
  51. tres4 = do
  52. let f = (\x -> return $ x + 1) :: Int -> EitherT () (EitherT () (EitherT () (EitherT () IO))) Int
  53. r <- resume4 f (Right . Right . Right . Right $ 1)
  54. let v = fromRight 0 . fromRight (Left ()) . fromRight (Left ()) . fromRight (Left ()) $ r
  55. Finished <$> if v == 2 then return Pass else return $ Fail $ "Wrong value: " ++ show v
  56. tres5 :: IO Progress
  57. tres5 = do
  58. let f = (\x -> return $ x + 1) :: Int -> EitherT () (EitherT () (EitherT () (EitherT () (EitherT () IO)))) Int
  59. r <- resume5 f (Right . Right . Right . Right . Right $ 1)
  60. let v = fromRight 0 . fromRight (Left ()) . fromRight (Left ()) . fromRight (Left ()) . fromRight (Left ()) $ r
  61. Finished <$> if v == 2 then return Pass else return $ Fail $ "Wrong value: " ++ show v
  62. int1 :: IO Progress
  63. int1 = do
  64. let f = (\x y -> return $ x + y) :: Int -> Int -> EitherT () IO Int
  65. r <- intercalateWith resume f [1, 2, 3] (map Right [10, 20])
  66. let v = map (fromRight 0) r
  67. Finished <$> if v == [16, 26] then return Pass else return $ Fail $ "Wrong value: " ++ show v
  68. int5 :: IO Progress
  69. int5 = do
  70. let f = (\x y -> return $ x + y) :: Int -> Int -> EitherT () (EitherT () (EitherT () (EitherT () (EitherT () IO)))) Int
  71. r <- intercalateWith resume5 f [1, 2, 3] (map (Right . Right . Right . Right . Right) [10, 20])
  72. let v = map (fromRight 0 . fromRight (Left ()) . fromRight (Left ()) . fromRight (Left ()) . fromRight (Left ())) r
  73. Finished <$> if v == [16, 26] then return Pass else return $ Fail $ "Wrong value: " ++ show v