on master help sign in
Tests for the scalar tag rendering controls
commit3004598c33845984ccd992022c605574fbe301b9 authorgmorpheme <github@gmorpheme.net> dateFri 1 Feb 2019, 10:16 (8 years ago) parentda70eaf
1 file changed+79−49
yaml/test/Data/YamlSpec.hs+79−49
yaml/test/Data/YamlSpec.hs+79−49
@@ -56,36 +56,13 @@ shouldDecode bs expected = do
5656 actual <- D.decodeThrow bs
5757 actual `shouldBe` expected
5858
59−shouldEmit :: [Y.Event] -> BS.ByteString -> IO ()
60−shouldEmit es expected = do
61− actual <- runConduitRes (CL.sourceList events .| Y.encode)
62− actual `shouldBe` expected
59+testEncodeWith :: Y.FormatOptions -> [Y.Event] -> IO BS.ByteString
60+testEncodeWith opts es = runConduitRes (CL.sourceList events .| Y.encodeWith opts)
6361 where
6462 events =
6563 [Y.EventStreamStart, Y.EventDocumentStart] ++ es ++
6664 [Y.EventDocumentEnd, Y.EventStreamEnd]
6765
68−shouldEmitWithTagsOn :: [Y.Event] -> BS.ByteString -> IO ()
69−shouldEmitWithTagsOn es expected = do
70− actual <- runConduitRes (CL.sourceList events .| Y.encodeWith opts)
71− actual `shouldBe` expected
72− where
73− events =
74− [Y.EventStreamStart, Y.EventDocumentStart] ++
75− es ++ [Y.EventDocumentEnd, Y.EventStreamEnd]
76− opts = Y.setCollectionTagRendering Y.renderAll $ Y.defaultFormatOptions
77−
78−shouldEmitWithUriTagsOn :: [Y.Event] -> BS.ByteString -> IO ()
79−shouldEmitWithUriTagsOn es expected = do
80− actual <- runConduitRes (CL.sourceList events .| Y.encodeWith opts)
81− actual `shouldBe` expected
82− where
83− events =
84− [Y.EventStreamStart, Y.EventDocumentStart] ++
85− es ++ [Y.EventDocumentEnd, Y.EventStreamEnd]
86− opts = Y.setCollectionTagRendering Y.renderUriTags $ Y.defaultFormatOptions
87−
88−
8966main :: IO ()
9067main = hspec spec
9168
@@ -237,56 +214,109 @@ spec = do
237214 go "1230.15" (1230.15 :: Scientific)
238215
239216 describe "Text.Libyaml with collection tags off" $ do
217+ let enc = testEncodeWith Y.defaultFormatOptions
240218 it "elides custom sequence tags" $
241− taggedSequence `shouldEmit` "[]\n"
219+ enc taggedSequence `shouldReturn` "[]\n"
242220 it "elides custom mapping tags" $
243− taggedMapping `shouldEmit` "{}\n"
221+ enc taggedMapping `shouldReturn` "{}\n"
244222 it "elides default sequence tags" $
245− defaultTaggedSequence `shouldEmit` "[]\n"
223+ enc defaultTaggedSequence `shouldReturn` "[]\n"
246224 it "elides default mapping tags" $
247− defaultTaggedMapping `shouldEmit` "{}\n"
225+ enc defaultTaggedMapping `shouldReturn` "{}\n"
248226 it "handles NoTag on sequences" $
249− untaggedSequence `shouldEmit` "[]\n"
227+ enc untaggedSequence `shouldReturn` "[]\n"
250228 it "handles NoTag on mappings" $
251− untaggedMapping `shouldEmit` "{}\n"
229+ enc untaggedMapping `shouldReturn` "{}\n"
252230 it "handles mixed tag usages but elides all mapping and sequence tags" $
253− mixedTagSampleA `shouldEmit` "- {}\n"
231+ enc mixedTagSampleA `shouldReturn` "- {}\n"
254232 it "in combination of tags, anchors and styles, outputs only the scalar tags" $
255− mixedTagSampleB `shouldEmit` "&a\n&b !<bar> foo: &c [&d !!null '']\n"
233+ enc mixedTagSampleB `shouldReturn` "&a\n&b !<bar> foo: &c [&d !!null '']\n"
256234 describe "Text.Libyaml with collection tags on" $ do
235+ let enc = testEncodeWith $ Y.setCollectionTagRendering Y.renderAll Y.defaultFormatOptions
257236 it "will output custom sequence tags" $
258− taggedSequence `shouldEmitWithTagsOn` "!foo []\n"
237+ enc taggedSequence `shouldReturn` "!foo []\n"
259238 it "will output custom mapping tags" $
260− taggedMapping `shouldEmitWithTagsOn` "!foo {}\n"
239+ enc taggedMapping `shouldReturn` "!foo {}\n"
261240 it "will output default sequence tags" $
262− defaultTaggedSequence `shouldEmitWithTagsOn` "!!seq []\n"
241+ enc defaultTaggedSequence `shouldReturn` "!!seq []\n"
263242 it "will output default mapping tags" $
264− defaultTaggedMapping `shouldEmitWithTagsOn` "!!map {}\n"
243+ enc defaultTaggedMapping `shouldReturn` "!!map {}\n"
265244 it "handles NoTag on sequences" $
266− untaggedSequence `shouldEmitWithTagsOn` "[]\n"
245+ enc untaggedSequence `shouldReturn` "[]\n"
267246 it "handles NoTag on mappings" $
268− untaggedMapping `shouldEmitWithTagsOn` "{}\n"
247+ enc untaggedMapping `shouldReturn` "{}\n"
269248 it "handles mixed tag usages outputting all mapping and sequence tags" $
270− mixedTagSampleA `shouldEmitWithTagsOn` "- !foo {}\n"
249+ enc mixedTagSampleA `shouldReturn` "- !foo {}\n"
271250 it "in combination of tags, anchors and styles, outputs all the tags" $
272− mixedTagSampleB `shouldEmitWithTagsOn` "&a\n&b !<bar> foo: &c !baz [&d !!null '']\n"
251+ enc mixedTagSampleB `shouldReturn` "&a\n&b !<bar> foo: &c !baz [&d !!null '']\n"
273252 describe "Text.Libyaml with collection uri tags on" $ do
253+ let enc = testEncodeWith $ Y.setCollectionTagRendering Y.renderUriTags Y.defaultFormatOptions
274254 it "will output custom sequence tags" $
275− taggedSequence `shouldEmitWithUriTagsOn` "!foo []\n"
255+ enc taggedSequence `shouldReturn` "!foo []\n"
276256 it "will output custom mapping tags" $
277− taggedMapping `shouldEmitWithUriTagsOn` "!foo {}\n"
257+ enc taggedMapping `shouldReturn` "!foo {}\n"
278258 it "will output default sequence tags" $
279− defaultTaggedSequence `shouldEmitWithUriTagsOn` "[]\n"
259+ enc defaultTaggedSequence `shouldReturn` "[]\n"
280260 it "will output default mapping tags" $
281− defaultTaggedMapping `shouldEmitWithUriTagsOn` "{}\n"
261+ enc defaultTaggedMapping `shouldReturn` "{}\n"
282262 it "handles NoTag on sequences" $
283− untaggedSequence `shouldEmitWithUriTagsOn` "[]\n"
263+ enc untaggedSequence `shouldReturn` "[]\n"
284264 it "handles NoTag on mappings" $
285− untaggedMapping `shouldEmitWithUriTagsOn` "{}\n"
265+ enc untaggedMapping `shouldReturn` "{}\n"
286266 it "handles mixed tag usages outputting all mapping and sequence tags" $
287− mixedTagSampleA `shouldEmitWithUriTagsOn` "- !foo {}\n"
267+ enc mixedTagSampleA `shouldReturn` "- !foo {}\n"
288268 it "in combination of tags, anchors and styles, outputs all the tags" $
289− mixedTagSampleB `shouldEmitWithUriTagsOn` "&a\n&b !<bar> foo: &c !baz [&d !!null '']\n"
269+ enc mixedTagSampleB `shouldReturn` "&a\n&b !<bar> foo: &c !baz [&d !!null '']\n"
270+ describe "Text.Libyaml with plain scalar tags on" $ do
271+ let enc = testEncodeWith $ Y.setPlainTagRendering Y.renderAll Y.defaultFormatOptions
272+ it "outputs plain tags" $
273+ enc [Y.EventScalar "foo" Y.StrTag Y.Plain Nothing] `shouldReturn` "!!str foo\n"
274+ it "respects PlainNoTag tags" $
275+ enc [Y.EventScalar "foo" Y.StrTag Y.PlainNoTag Nothing] `shouldReturn` "foo\n"
276+ describe "Text.Libyaml with plain scalar tags off" $ do
277+ let enc = testEncodeWith $ Y.setPlainTagRendering Y.renderNone Y.defaultFormatOptions
278+ it "outputs plain tags" $
279+ enc [Y.EventScalar "foo" Y.StrTag Y.Plain Nothing] `shouldReturn` "foo\n"
280+ it "respects PlainNoTag tags" $
281+ enc [Y.EventScalar "foo" Y.StrTag Y.PlainNoTag Nothing] `shouldReturn` "foo\n"
282+ describe "Text.Libyaml with quoted scalar tags on" $ do
283+ let enc = testEncodeWith $ Y.setQuotedTagRendering Y.renderAll Y.defaultFormatOptions
284+ it "outputs tags when double quoted" $
285+ enc [Y.EventScalar "foo" Y.StrTag Y.DoubleQuoted Nothing] `shouldReturn` "!!str \"foo\"\n"
286+ it "outputs tags when single quoted" $
287+ enc [Y.EventScalar "foo" Y.StrTag Y.SingleQuoted Nothing] `shouldReturn` "!!str 'foo'\n"
288+ it "outputs tags on literal text" $
289+ enc [Y.EventScalar "foo" Y.StrTag Y.Literal Nothing] `shouldReturn` "!!str |-\n foo\n"
290+ it "outputs tags on folded text" $
291+ enc [Y.EventScalar "foo" Y.StrTag Y.Folded Nothing] `shouldReturn` "!!str >-\n foo\n"
292+ describe "Text.Libyaml with quoted scalar tags off" $ do
293+ let enc = testEncodeWith $ Y.setQuotedTagRendering Y.renderNone Y.defaultFormatOptions
294+ it "elides tags when double quoted" $
295+ enc [Y.EventScalar "foo" Y.StrTag Y.DoubleQuoted Nothing] `shouldReturn` "\"foo\"\n"
296+ it "elides tags when single quoted" $
297+ enc [Y.EventScalar "foo" Y.StrTag Y.SingleQuoted Nothing] `shouldReturn` "'foo'\n"
298+ it "elides tags on literal text" $
299+ enc [Y.EventScalar "foo" Y.StrTag Y.Literal Nothing] `shouldReturn` "|-\n foo\n"
300+ it "elides tags on folded text" $
301+ enc [Y.EventScalar "foo" Y.StrTag Y.Folded Nothing] `shouldReturn` ">-\n foo\n"
302+ describe "Text.Libyaml with only UriTags set to render " $ do
303+ let enc =
304+ testEncodeWith $
305+ Y.setCollectionTagRendering Y.renderUriTags $
306+ Y.setPlainTagRendering Y.renderUriTags $
307+ Y.setQuotedTagRendering Y.renderUriTags Y.defaultFormatOptions
308+ it "outputs only UriTags" $
309+ enc
310+ [ Y.EventSequenceStart Y.NoTag Y.FlowSequence Nothing
311+ , Y.EventScalar "foo" Y.StrTag Y.DoubleQuoted Nothing
312+ , Y.EventScalar "99" Y.IntTag Y.Plain Nothing
313+ , Y.EventScalar "99.99" Y.FloatTag Y.Plain Nothing
314+ , Y.EventScalar "bar" Y.NoTag Y.Plain Nothing
315+ , Y.EventScalar "foo" (Y.UriTag "!foo") Y.DoubleQuoted Nothing
316+ , Y.EventScalar "foo" (Y.UriTag "!foo") Y.Plain Nothing
317+ , Y.EventSequenceEnd
318+ ] `shouldReturn`
319+ "[\"foo\", 99, 99.99, bar, !foo \"foo\", !foo foo]\n"
290320
291321specialStrings :: [T.Text]
292322specialStrings =