11{-# language LambdaCase #-}
22{-# language OverloadedStrings #-}
3+ {-# language ScopedTypeVariables #-}
34
45import Control.Monad (when )
56import Data.ByteString.Short.Internal (ShortByteString (SBS ))
67import Data.Bytes (Bytes )
78import Data.Primitive (ByteArray (ByteArray ))
89import Data.Scientific (Scientific ,scientific )
10+ import Test.QuickCheck ((===) )
911import Data.Text.Short (ShortText )
1012import Test.Tasty (defaultMain ,testGroup ,TestTree )
1113import Test.Tasty.HUnit ((@=?) )
@@ -20,7 +22,9 @@ import qualified Data.Number.Scientific as SCI
2022import qualified Data.Text.Short as TS
2123import qualified GHC.Exts as Exts
2224import qualified Json as J
25+ import qualified Test.QuickCheck as QC
2326import qualified Test.Tasty.HUnit as THU
27+ import qualified Test.Tasty.QuickCheck as TQC
2428
2529main :: IO ()
2630main = defaultMain tests
@@ -77,6 +81,26 @@ tests = testGroup "Tests"
7781 BChunks. concat (Builder. run 1 (J. encode (J. String " Hello\n World" )))
7882 @=?
7983 Bytes. fromLatinString " \" Hello\\ nWorld\" "
84+ , TQC. testProperty " M" $ QC. forAll (jsonFromPrintableStrings <$> QC. vectorOf 10 QC. arbitrary) $ \ val0 -> do
85+ let enc = BChunks. concat (Builder. run 128 (J. encode val0))
86+ case J. decode enc of
87+ Left _ -> QC. property False
88+ Right val1 -> val0 === val1
89+ , TQC. testProperty " N" $ QC. forAll (jsonFromPrintableStrings <$> QC. vectorOf 400 QC. arbitrary) $ \ val0 -> do
90+ let enc = BChunks. concat (Builder. run 128 (J. encode val0))
91+ case J. decode enc of
92+ Left e -> QC. counterexample (show e) False
93+ Right val1 -> val0 === val1
94+ , TQC. testProperty " O" $ QC. forAll (jsonFromAsciiStrings <$> QC. vectorOf 10 QC. arbitrary) $ \ val0 -> do
95+ let enc = BChunks. concat (Builder. run 128 (J. encode val0))
96+ case J. decode enc of
97+ Left _ -> QC. property False
98+ Right val1 -> val0 === val1
99+ , TQC. testProperty " P" $ QC. forAll (jsonFromAsciiStrings <$> QC. vectorOf 400 QC. arbitrary) $ \ val0 -> do
100+ let enc = BChunks. concat (Builder. run 128 (J. encode val0))
101+ case J. decode enc of
102+ Left e -> QC. counterexample (show e) False
103+ Right val1 -> val0 === val1
80104 , THU. testCase " Twitter100" $
81105 case J. decode (Bytes. fromByteArray encodedTwitter100) of
82106 Left _ -> fail " nope"
@@ -91,6 +115,12 @@ tests = testGroup "Tests"
91115 Right j' -> when (j /= j') (fail " document was not the same after roundtrip" )
92116 ]
93117
118+ jsonFromPrintableStrings :: [QC. PrintableString ] -> J. Value
119+ jsonFromPrintableStrings xs = J. Array (Exts. fromList (map (J. String . TS. pack . QC. getPrintableString) xs))
120+
121+ jsonFromAsciiStrings :: [QC. ASCIIString ] -> J. Value
122+ jsonFromAsciiStrings xs = J. Array (Exts. fromList (map (J. String . TS. pack . QC. getASCIIString) xs))
123+
94124toBadSci :: SCI. Scientific -> Scientific
95125toBadSci = SCI. withExposed
96126 (\ a b -> scientific (fromIntegral a) b)
0 commit comments