File: NullaryConstructors.hs

package info (click to toggle)
haskell-aeson 2.1.2.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,988 kB
  • sloc: haskell: 11,933; ansic: 123; makefile: 11
file content (68 lines) | stat: -rw-r--r-- 3,163 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}

{-# OPTIONS_GHC -fno-warn-deprecations #-}

module UnitTests.NullaryConstructors
    (
      nullaryConstructors
    ) where

import Prelude.Compat

import Data.Aeson (decode, eitherDecode, fromEncoding, Value)
import Data.Aeson.Internal (IResult (..), iparse)
import Data.Aeson.Types (Parser)
import Data.ByteString.Builder (toLazyByteString)
import Data.Foldable (for_)
import Data.Maybe (fromJust)
import Encoders
import Test.Tasty.HUnit ((@=?), Assertion)
import Types
import qualified Data.ByteString.Lazy.Char8 as L

nullaryConstructors :: [Assertion]
nullaryConstructors =
  [ dec "\"C1\""           @=? thNullaryToJSONString C1
  , dec "\"C1\""           @=? gNullaryToJSONString C1
  , dec "{\"c1\":[]}"      @=? thNullaryToJSONObjectWithSingleField C1
  , dec "{\"c1\":[]}"      @=? gNullaryToJSONObjectWithSingleField C1
  , dec "[\"c1\",[]]"      @=? gNullaryToJSON2ElemArray C1
  , dec "[\"c1\",[]]"      @=? thNullaryToJSON2ElemArray C1
  , dec "{\"tag\":\"c1\"}" @=? thNullaryToJSONTaggedObject C1
  , dec "{\"tag\":\"c1\"}" @=? gNullaryToJSONTaggedObject C1

  , decE "\"C1\""           @=? enc (gNullaryToEncodingString C1)
  , decE "\"C1\""           @=? enc (thNullaryToEncodingString C1)
  , decE "[\"c1\",[]]"      @=? enc (gNullaryToEncoding2ElemArray C1)
  , decE "[\"c1\",[]]"      @=? enc (thNullaryToEncoding2ElemArray C1)
  , decE "{\"c1\":[]}"      @=? enc (thNullaryToEncodingObjectWithSingleField C1)
  , decE "{\"c1\":[]}"      @=? enc (gNullaryToEncodingObjectWithSingleField C1)
  , decE "{\"tag\":\"c1\"}" @=? enc (thNullaryToEncodingTaggedObject C1)
  , decE "{\"tag\":\"c1\"}" @=? enc (gNullaryToEncodingTaggedObject C1)

  , ISuccess C1 @=? parse thNullaryParseJSONTaggedObject          (dec "{\"tag\":\"c1\"}")
  , ISuccess C1 @=? parse gNullaryParseJSONTaggedObject           (dec "{\"tag\":\"c1\"}")

  , ISuccess C1 @=? parse thNullaryParseJSONString                (dec "\"C1\"")
  , ISuccess C1 @=? parse gNullaryParseJSONString                 (dec "\"C1\"")
  , ISuccess C1 @=? parse thNullaryParseJSON2ElemArray            (dec  "[\"c1\",[]]")
  , ISuccess C1 @=? parse gNullaryParseJSON2ElemArray             (dec  "[\"c1\",[]]")
  , ISuccess C1 @=? parse thNullaryParseJSONObjectWithSingleField (dec  "{\"c1\":[]}")
  , ISuccess C1 @=? parse gNullaryParseJSONObjectWithSingleField  (dec  "{\"c1\":[]}")
    -- Make sure that the old `"contents" : []' is still allowed
  , ISuccess C1 @=? parse thNullaryParseJSONTaggedObject          (dec "{\"tag\":\"c1\",\"contents\":[]}")
  , ISuccess C1 @=? parse gNullaryParseJSONTaggedObject           (dec "{\"tag\":\"c1\",\"contents\":[]}")

  , for_ [("kC1", C1), ("kC2", C2), ("kC3", C3)] $ \(jkey, key) -> do
      Right   jkey @=? gNullaryToJSONKey key
      ISuccess key @=? parse gNullaryFromJSONKey jkey
  ]
  where
    enc = eitherDecode . toLazyByteString . fromEncoding
    dec :: L.ByteString -> Value
    dec = fromJust . decode
    decE :: L.ByteString -> Either String Value
    decE = eitherDecode
    parse :: (a -> Parser b) -> a -> IResult b
    parse parsejson v = iparse parsejson v