@@ -27,6 +27,7 @@ import System.Directory (createDirectory, createDirectoryIfMissing)
2727import Test.Mockery.Directory
2828
2929import qualified Data.Yaml as D
30+import qualified Data.Yaml.Builder as B
3031import qualified Data.Yaml.Internal as Internal
3132import qualified Data.Yaml.Pretty as Pretty
3233import Data.Yaml (object, array, (.=))
@@ -65,15 +66,28 @@ testJSON = TestJSON
6566
6667shouldDecode :: (Show a, D.FromJSON a, Eq a) => B8.ByteString -> a -> IO ()
6768shouldDecode bs expected = do
68− actual <- D.decodeThrow bs
69− actual `shouldBe` expected
69+ actual <- D.decodeThrow bs
70+ actual `shouldBe` expected
71+
72+shouldDecodeEvents :: B8.ByteString -> [Y.Event] -> IO ()
73+shouldDecodeEvents bs expected = do
74+ actual <- runConduitRes $ Y.decode bs .| CL.consume
75+ map anyStyle actual `shouldBe` map anyStyle expected
76+
77+anyStyle :: Y.Event -> Y.Event
78+anyStyle (Y.EventScalar bs tag _ anchor) = Y.EventScalar bs tag Y.Any anchor
79+anyStyle (Y.EventSequenceStart tag _ anchor) = Y.EventSequenceStart tag Y.AnySequence anchor
80+anyStyle (Y.EventMappingStart tag _ anchor) = Y.EventMappingStart tag Y.AnyMapping anchor
81+anyStyle event = event
7082
7183testEncodeWith :: Y.FormatOptions -> [Y.Event] -> IO BS.ByteString
72−testEncodeWith opts es = runConduitRes (CL.sourceList events .| Y.encodeWith opts)
73− where
74− events =
75− [Y.EventStreamStart, Y.EventDocumentStart] ++ es ++
76− [Y.EventDocumentEnd, Y.EventStreamEnd]
84+testEncodeWith opts es = runConduitRes $ CL.sourceList (eventStream es) .| Y.encodeWith opts
85+
86+eventStream :: [Y.Event] -> [Y.Event]
87+eventStream events =
88+ [Y.EventStreamStart, Y.EventDocumentStart]
89+ ++ events
90+ ++ [Y.EventDocumentEnd, Y.EventStreamEnd]
7791
7892main :: IO ()
7993main = hspec spec
@@ -112,6 +126,9 @@ spec = do
112126 it "encode/decode" caseEncodeDecodeDataPretty
113127 it "encode/decode strings" caseEncodeDecodeStringsPretty
114128 it "processes datatypes" caseDataTypesPretty
129+ describe "Data.Yaml.Builder" $ do
130+ it "encode/decode" caseEncodeDecodeDataBuilder
131+ it "encode/decode complex mapping" caseEncodeDecodeComplexMappingBuilder
115132 describe "Data.Yaml aliases" $ do
116133 it "simple scalar alias" caseSimpleScalarAlias
117134 it "simple sequence alias" caseSimpleSequenceAlias
@@ -478,13 +495,9 @@ caseEncodeDecode = do
478495caseEncodeDecodeEvents :: Assertion
479496caseEncodeDecodeEvents = do
480497 let events = Internal.objToEvents D.defaultStringStyle testJSON []
481− result <- Internal.decodeHelper_ . CL.sourceList $ toStream events
498+ result <- Internal.decodeHelper_ . CL.sourceList $ eventStream events
482499 let (_, value) = either (error . show) id result
483500 value @?= testJSON
484− where
485− toStream es =
486− [Y.EventStreamStart, Y.EventDocumentStart] ++ es ++
487− [Y.EventDocumentEnd, Y.EventStreamEnd]
488501
489502caseEncodeDecodeFile :: Assertion
490503caseEncodeDecodeFile = withFile "" $ \tmpPath -> do
@@ -535,6 +548,24 @@ sample = array
535548 , ("bar3", D.String "")
536549 ]
537550 , D.String ""
551+ , D.Number 1
552+ , D.Number 0.1
553+ , D.Bool True
554+ , D.Null
555+ ]
556+
557+sampleBuilder :: B.YamlBuilder
558+sampleBuilder = B.array
559+ [ B.string "foo"
560+ , B.mapping
561+ [ ("bar1", B.string "bar2")
562+ , ("bar3", B.string "")
563+ ]
564+ , B.string ""
565+ , B.scientific 1
566+ , B.scientific 0.1
567+ , B.bool True
568+ , B.null
538569 ]
539570
540571caseEncodeDecodeData :: Assertion
@@ -544,6 +575,28 @@ caseEncodeDecodeDataPretty :: Assertion
544575caseEncodeDecodeDataPretty =
545576 Pretty.encodePretty Pretty.defConfig sample `shouldDecode` sample
546577
578+caseEncodeDecodeDataBuilder :: Assertion
579+caseEncodeDecodeDataBuilder = do
580+ let events = B.unYamlBuilder sampleBuilder []
581+ bs <- testEncodeWith Y.defaultFormatOptions events
582+ bs `shouldDecodeEvents` eventStream events
583+
584+caseEncodeDecodeComplexMappingBuilder :: Assertion
585+caseEncodeDecodeComplexMappingBuilder = do
586+ let events = B.unYamlBuilder builder []
587+ bs <- testEncodeWith Y.defaultFormatOptions events
588+ bs `shouldDecodeEvents` eventStream events
589+ where
590+ builder :: B.YamlBuilder
591+ builder = B.mappingComplex
592+ [ ( B.mapping
593+ [ ("foo", B.scientific 1)
594+ , ("bar", B.scientific 2)
595+ ]
596+ , B.bool True
597+ )
598+ ]
599+
547600caseEncodeDecodeFileData :: Assertion
548601caseEncodeDecodeFileData = withFile "" $ \fp -> do
549602 D.encodeFile fp sample