on master help sign in
decodeFile with test written
2cdd30dMichael Snoyman
Text/Libyaml.hs+9−9
@@ -20,8 +20,7 @@ module Text.Libyaml
20 , encode
20 , encode
21 , decode
21 , decode
22 -- FIXME , encodeFile
22 -- FIXME , encodeFile
23− -- FIXME , decodeFile
23+ , decodeFile
24− , withFileParser
25 ) where
24 ) where
26
25
27import qualified Data.ByteString.Internal as B
26import qualified Data.ByteString.Internal as B
@@ -157,7 +156,9 @@ foreign import ccall unsafe "fclose"
157 c_fclose :: File
156 c_fclose :: File
158 -> IO ()
157 -> IO ()
159
158
160−withFileParser :: FilePath -> (Parser -> IO a) -> IO a
159+withFileParser :: FilePath
160+ -> (Parser -> IO (Either YamlException a))
161+ -> IO (Either YamlException a)
161withFileParser fp f = allocaBytes parserSize $ \p ->
162withFileParser fp f = allocaBytes parserSize $ \p ->
162 do
163 do
163 res <- c_yaml_parser_initialize p
164 res <- c_yaml_parser_initialize p
@@ -519,9 +520,8 @@ decode :: B.ByteString
519 -> IO (Either YamlException a)
520 -> IO (Either YamlException a)
520decode bs fold accum = withParser bs $ parserParse fold accum
521decode bs fold accum = withParser bs $ parserParse fold accum
521
522
522−{- FIXME
523−decodeFile :: MonadFailure YamlException m
524− => FilePath
523+decodeFile :: FilePath
525− -> IO (m [Event])
524+ -> (a -> Event -> Either a a)
525+ -> a
526+ -> IO (Either YamlException a)
526−decodeFile fp = withFileParser fp parserParse
527+decodeFile fp fold accum = withFileParser fp $ parserParse fold accum
527−-}
runtests.hs+18
@@ -12,6 +12,7 @@ import Test.HUnit hiding (Test, path)
12main :: IO ()
12main :: IO ()
13main = defaultMain
13main = defaultMain
14 [ testCase "count scalars" caseCountScalars
14 [ testCase "count scalars" caseCountScalars
15+ , testCase "largest string" caseLargestString
15 ]
16 ]
16
17
17caseCountScalars :: Assertion
18caseCountScalars :: Assertion
@@ -27,3 +28,20 @@ caseCountScalars = do
27 fold res EventNone = Left res
28 fold res EventNone = Left res
28 fold res _ = Right res
29 fold res _ = Right res
29 accum = (0, 0, 0) :: (Int, Int, Int)
30 accum = (0, 0, 0) :: (Int, Int, Int)
31+
32+caseLargestString :: Assertion
33+caseLargestString = do
34+ Right res <- decodeFile filePath fold accum
35+ res @?= (length expected, expected)
36+ where
37+ expected = "this one is just a little bit bigger than the others"
38+ filePath = "test/largest-string.yaml"
39+ fold (i, s) (EventScalar bs _ _) =
40+ let s' = B8.unpack bs
41+ i' = length s'
42+ in if i' > i
43+ then Right (i', s')
44+ else Right (i, s)
45+ fold res EventNone = Left res
46+ fold res _ = Right res
47+ accum = (0, "no strings found")
test/.gitignoredeleted−3
@@ -1,3 +0,0 @@
1−hebrew.txt
2−output.yaml
3−output2.yaml
test/hebrew.yamldeleted−1
@@ -1 +0,0 @@
1−שלום
test/input.yamldeleted−2
@@ -1,2 +0,0 @@
1−this is a testש: foobar
2−this is too: foobarbaz
test/largest-string.yamlnew file+6
@@ -0,0 +1,6 @@
1+this is a long string: but this one is even longer
2+this is shorter:
3+ - many
4+ - tiny
5+ - string
6+one the other hand: this one is just a little bit bigger than the others
test/tags.hsdeleted−8
@@ -1,8 +0,0 @@
1−import Text.Yaml
2−import qualified Data.ByteString as B
3−
4−main = do
5− bs <- B.readFile "tags.yaml"
6− yo <- decodeYaml' bs
7− print yo
8− B.putStrLn $ encodeYaml' yo
test/tags.yamldeleted−3
@@ -1,3 +0,0 @@
1−this: !!int 5
2−that: !!float "89"
3−'other': !mine foobarbaz
test/test.hsdeleted−24
@@ -1,24 +0,0 @@
1−{-# LANGUAGE MultiParamTypeClasses #-}
2−{-# LANGUAGE FlexibleInstances #-}
3−import Text.Yaml
4−import qualified Data.ByteString as B
5−import Control.Monad
6−import Data.Convertible.Text
7−import Data.Object
8−import Data.Object.Text
9−
10−main = do
11− contents <- B.readFile "input.yaml"
12− o <- decodeText' contents
13− let contents2 = encodeText o
14− B.writeFile "output.yaml" contents2
15− o2 <- decodeText' contents2
16− print o2
17−
18− contents3 <- B.readFile "hebrew.yaml"
19− o3 <- decodeText contents3
20− print o3
21− B.writeFile "hebrew.txt" o3
22−
23−instance ConvertAttempt (Object Text Text) B.ByteString where
24− convertAttempt = fromObject