on master help sign in
decodeFileEither test
commitf717edc43f4917a2132e8077aabdea915f13b0ae authorGreg Weber <greg@gregweber.info> dateMon 5 Aug 2013, 10:44 (13 years ago) parent1343cf4
4 files changed+42
.gitignore+1 test/json.yaml+8 test/main.hs+31 yaml.cabal+2
.gitignore+1
@@ -2,3 +2,4 @@ dist
22*.swp
33tmp.yaml
44tmp2.yaml
5+cabal-dev/
test/json.yamlnew file+8
@@ -0,0 +1,8 @@
1+string: str
2+number: 2
3+anArray:
4+ - a
5+ - b
6+hash:
7+ key1: value1
8+ key2: value2
test/main.hs+31
@@ -2,6 +2,7 @@
22{-# LANGUAGE PackageImports #-}
33{-# LANGUAGE ScopedTypeVariables #-}
44{-# LANGUAGE OverloadedStrings #-}
5+{-# LANGUAGE TemplateHaskell #-}
56
67import qualified Text.Libyaml as Y
78import qualified Data.ByteString.Char8 as B8
@@ -21,6 +22,21 @@ import Data.Yaml (object, array, (.=))
2122import Data.Maybe
2223import qualified Data.HashMap.Strict as M
2324import qualified Data.Text as T
25+import Data.Aeson.TH
26+import Data.Text (Text)
27+import Data.Vector (Vector)
28+import qualified Data.Vector as V
29+import Data.HashMap.Strict (HashMap)
30+import qualified Data.HashMap.Strict as HM
31+
32+data TestJSON = TestJSON
33+ { string :: Text
34+ , number :: Int
35+ , anArray :: Vector Text
36+ , hash :: HashMap Text Text
37+ } deriving (Show, Eq)
38+
39+deriveJSON id ''TestJSON
2440
2541main :: IO ()
2642main = hspec $ do
@@ -70,23 +86,38 @@ main = hspec $ do
7086 checkNull "NULL"
7187 checkNull "~"
7288 checkNull ""
89+
7390 describe "pretty output" $ do
7491 it "simple nulls" $ D.encode (object ["foo" .= D.Null]) `shouldBe` "foo: null\n"
7592 it "simple numbers" $ D.encode (object ["foo" .= (4 :: Int)]) `shouldBe` "foo: 4\n"
7693 it "True" $ D.encode (object ["foo" .= True]) `shouldBe` "foo: true\n"
7794 it "False" $ D.encode (object ["foo" .= False]) `shouldBe` "foo: false\n"
7895 it "simple string" $ D.encode (object ["foo" .= ("bar" :: T.Text)]) `shouldBe` "foo: bar\n"
96+
7997 describe "special keys" $ do
8098 let tester key = it (T.unpack key) $
8199 let value = object [key .= True]
82100 in D.decode (D.encode value) `shouldBe` Just value
83101 mapM_ tester specialStrings
102+
84103 describe "special values" $ do
85104 let tester value = it (T.unpack value) $
86105 let value' = object ["foo" .= value]
87106 in D.decode (D.encode value') `shouldBe` Just value'
88107 mapM_ tester specialStrings
89108
109+ describe "yamlFileJSONparse" $
110+ it "loads YAML through JSON into Haskell data" $ do
111+ tj <- either error id `fmap` D.decodeFileEither "test/json.yaml"
112+ tj `shouldBe` TestJSON
113+ { string = "str"
114+ , number = 2
115+ , anArray = V.fromList ["a", "b"]
116+ , hash = HM.fromList [("key1", "value1"), ("key2", "value2")]
117+ }
118+
119+
120+
90121specialStrings :: [T.Text]
91122specialStrings =
92123 [ "fo\"o"
yaml.cabal+2
@@ -76,7 +76,9 @@ test-suite test
7676 , conduit
7777 , yaml
7878 , text
79+ , aeson
7980 , unordered-containers
81+ , vector
8082 ghc-options: -Wall
8183
8284source-repository head