on master help sign in
decodeFile with test written
commit2cdd30d1a80bf4f2151429dd2730af0fdb07f484 authorMichael Snoyman <michael@snoyman.com> dateTue 12 Jan 2010, 13:14 (17 years ago) parent5b5ad3f
9 files changed+33−50
Text/Libyaml.hs+9−9 runtests.hs+18 test/.gitignore−3 test/hebrew.yaml−1 test/input.yaml−2 test/largest-string.yaml+6 test/tags.hs−8 test/tags.yaml−3 test/test.hs−24
Text/Libyaml.hs+9−9
@@ -20,8 +20,7 @@ module Text.Libyaml
2020 , encode
2121 , decode
2222 -- FIXME , encodeFile
23− -- FIXME , decodeFile
24− , withFileParser
23+ , decodeFile
2524 ) where
2625
2726import qualified Data.ByteString.Internal as B
@@ -157,7 +156,9 @@ foreign import ccall unsafe "fclose"
157156 c_fclose :: File
158157 -> IO ()
159158
160−withFileParser :: FilePath -> (Parser -> IO a) -> IO a
159+withFileParser :: FilePath
160+ -> (Parser -> IO (Either YamlException a))
161+ -> IO (Either YamlException a)
161162withFileParser fp f = allocaBytes parserSize $ \p ->
162163 do
163164 res <- c_yaml_parser_initialize p
@@ -519,9 +520,8 @@ decode :: B.ByteString
519520 -> IO (Either YamlException a)
520521decode bs fold accum = withParser bs $ parserParse fold accum
521522
522−{- FIXME
523−decodeFile :: MonadFailure YamlException m
524− => FilePath
525− -> IO (m [Event])
526−decodeFile fp = withFileParser fp parserParse
527−-}
523+decodeFile :: FilePath
524+ -> (a -> Event -> Either a a)
525+ -> a
526+ -> IO (Either YamlException a)
527+decodeFile fp fold accum = withFileParser fp $ parserParse fold accum
runtests.hs+18
@@ -12,6 +12,7 @@ import Test.HUnit hiding (Test, path)
1212main :: IO ()
1313main = defaultMain
1414 [ testCase "count scalars" caseCountScalars
15+ , testCase "largest string" caseLargestString
1516 ]
1617
1718caseCountScalars :: Assertion
@@ -27,3 +28,20 @@ caseCountScalars = do
2728 fold res EventNone = Left res
2829 fold res _ = Right res
2930 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