@@ -33,6 +33,10 @@ import Data.Object.Raw
33import System.IO.Unsafe
33import System.IO.Unsafe
34import Control.Arrow (first, (***))
34import Control.Arrow (first, (***))
35import Control.Monad (replicateM)
35import Control.Monad (replicateM)
37+import Control.Monad.Trans
38+import Control.Monad.Attempt
39+import Control.Monad.Attempt.Class
37import Test.Framework (testGroup, Test)
41import Test.Framework (testGroup, Test)
38import Test.Framework.Providers.HUnit
42import Test.Framework.Providers.HUnit
@@ -43,18 +47,22 @@ import Test.QuickCheck
43encode :: (StrictByteString bs, ToObject o Raw Raw) => o -> bs
47encode :: (StrictByteString bs, ToObject o Raw Raw) => o -> bs
44encode = unsafePerformIO . encode'
48encode = unsafePerformIO . encode'
46−decode :: (MonadFail m, StrictByteString bs, FromObject o Raw Raw)
50+decode :: (StrictByteString bs, FromObject o Raw Raw, MonadAttempt m)
49−decode = unsafePerformIO . decode'
53+decode bs = unsafePerformIO $ do
54+ a <- runAttemptT $ decode' bs -- FIXME
56+ Success s -> return $ success s
57+ Failure e -> return $ failure e
51encodeFile :: ToObject o Raw Raw => FilePath -> o -> IO ()
59encodeFile :: ToObject o Raw Raw => FilePath -> o -> IO ()
52encodeFile path o = encode' o >>= B.writeFile path
60encodeFile path o = encode' o >>= B.writeFile path
54−decodeFile :: (MonadFail m, FromObject o Raw Raw) => FilePath -> IO (m o)
62+decodeFile :: (FromObject o Raw Raw, MonadAttempt m, MonadIO m)
55−decodeFile path = do
65+decodeFile path = liftIO (B.readFile path) >>= decode'
59encode' :: (StrictByteString bs, ToObject o Raw Raw) => o -> IO bs
67encode' :: (StrictByteString bs, ToObject o Raw Raw) => o -> IO bs
60encode' o =
68encode' o =
@@ -62,13 +70,15 @@ encode' o =
62 result = withEmitter $ flip emitEvents $ events
70 result = withEmitter $ flip emitEvents $ events
63 in fromStrictByteString `fmap` result
71 in fromStrictByteString `fmap` result
65−decode' :: (MonadFail m, StrictByteString bs, FromObject o Raw Raw)
73+decode' :: (StrictByteString bs, FromObject o Raw Raw, MonadIO m,
67− -> IO (m o)
76+ -> m o
68decode' bs = do
77decode' bs = do
69− let bs' = toStrictByteString bs
78+ events <- liftIO $ withParser (toStrictByteString bs) parserParse
70− events = withParser bs' parserParse
71− (fromObject . eventsToRawObject) `fmap` events
79+ case fromObject $ eventsToRawObject events of -- FIXME something better in attempt
80+ Failure e -> failure e
81+ Success s -> success s
73objectToEvents :: RawObject -> [Event]
83objectToEvents :: RawObject -> [Event]
74objectToEvents y = concat
84objectToEvents y = concat
@@ -126,7 +136,8 @@ instance FromScalar MyString Raw where
126 fromScalar raw = MyString `fmap` fromScalar raw
136 fromScalar raw = MyString `fmap` fromScalar raw
128propEncodeDecode :: Object MyString MyString -> Bool
138propEncodeDecode :: Object MyString MyString -> Bool
129−propEncodeDecode o = decode (encode o :: B.ByteString) == Just o
139+propEncodeDecode o = fromAttempt (decode (encode o :: B.ByteString))
131caseEmptyStrings :: Assertion
142caseEmptyStrings :: Assertion
132caseEmptyStrings = do
143caseEmptyStrings = do
@@ -135,12 +146,12 @@ caseEmptyStrings = do
135 , ("baz", "")
146 , ("baz", "")
136 , ("bin", "")
147 , ("bin", "")
139 let m' = map (toStrictByteString *** toStrictByteString) m
149 let m' = map (toStrictByteString *** toStrictByteString) m
140 let m'' = map (toLazyByteString *** toLazyByteString) m
150 let m'' = map (toLazyByteString *** toLazyByteString) m
141− Just ma @=? decode (encode ma :: String)
151+ let test' x = Just x @=? fromAttempt (decode (encode x :: String))
142− Just m' @=? decode (encode m' :: String)
152+ test' m
143− Just m'' @=? decode (encode m'' :: String)
153+ test' m'
145testSuite :: Test
156testSuite :: Test
146testSuite = testGroup "Text.Yaml"
157testSuite = testGroup "Text.Yaml"