@@ -2,6 +2,7 @@
2{-# LANGUAGE PackageImports #-}
2{-# LANGUAGE PackageImports #-}
3{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE OverloadedStrings #-}
5+{-# LANGUAGE TemplateHaskell #-}
6import qualified Text.Libyaml as Y
7import qualified Text.Libyaml as Y
7import qualified Data.ByteString.Char8 as B8
8import qualified Data.ByteString.Char8 as B8
@@ -21,6 +22,21 @@ import Data.Yaml (object, array, (.=))
21import Data.Maybe
22import Data.Maybe
22import qualified Data.HashMap.Strict as M
23import qualified Data.HashMap.Strict as M
23import qualified Data.Text as T
24import qualified Data.Text as T
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
32+data TestJSON = TestJSON
35+ , anArray :: Vector Text
36+ , hash :: HashMap Text Text
37+ } deriving (Show, Eq)
39+deriveJSON id ''TestJSON
25main :: IO ()
41main :: IO ()
26main = hspec $ do
42main = hspec $ do
@@ -70,23 +86,38 @@ main = hspec $ do
70 checkNull "NULL"
86 checkNull "NULL"
71 checkNull "~"
87 checkNull "~"
72 checkNull ""
88 checkNull ""
73 describe "pretty output" $ do
90 describe "pretty output" $ do
74 it "simple nulls" $ D.encode (object ["foo" .= D.Null]) `shouldBe` "foo: null\n"
91 it "simple nulls" $ D.encode (object ["foo" .= D.Null]) `shouldBe` "foo: null\n"
75 it "simple numbers" $ D.encode (object ["foo" .= (4 :: Int)]) `shouldBe` "foo: 4\n"
92 it "simple numbers" $ D.encode (object ["foo" .= (4 :: Int)]) `shouldBe` "foo: 4\n"
76 it "True" $ D.encode (object ["foo" .= True]) `shouldBe` "foo: true\n"
93 it "True" $ D.encode (object ["foo" .= True]) `shouldBe` "foo: true\n"
77 it "False" $ D.encode (object ["foo" .= False]) `shouldBe` "foo: false\n"
94 it "False" $ D.encode (object ["foo" .= False]) `shouldBe` "foo: false\n"
78 it "simple string" $ D.encode (object ["foo" .= ("bar" :: T.Text)]) `shouldBe` "foo: bar\n"
95 it "simple string" $ D.encode (object ["foo" .= ("bar" :: T.Text)]) `shouldBe` "foo: bar\n"
79 describe "special keys" $ do
97 describe "special keys" $ do
80 let tester key = it (T.unpack key) $
98 let tester key = it (T.unpack key) $
81 let value = object [key .= True]
99 let value = object [key .= True]
82 in D.decode (D.encode value) `shouldBe` Just value
100 in D.decode (D.encode value) `shouldBe` Just value
83 mapM_ tester specialStrings
101 mapM_ tester specialStrings
84 describe "special values" $ do
103 describe "special values" $ do
85 let tester value = it (T.unpack value) $
104 let tester value = it (T.unpack value) $
86 let value' = object ["foo" .= value]
105 let value' = object ["foo" .= value]
87 in D.decode (D.encode value') `shouldBe` Just value'
106 in D.decode (D.encode value') `shouldBe` Just value'
88 mapM_ tester specialStrings
107 mapM_ tester specialStrings
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
115+ , anArray = V.fromList ["a", "b"]
116+ , hash = HM.fromList [("key1", "value1"), ("key2", "value2")]
90specialStrings :: [T.Text]
121specialStrings :: [T.Text]
91specialStrings =
122specialStrings =
92 [ "fo\"o"
123 [ "fo\"o"