File: GeneralisedUTF8.hs

package info (click to toggle)
haskell-serialise 0.2.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 596 kB
  • sloc: haskell: 6,811; makefile: 3
file content (37 lines) | stat: -rw-r--r-- 935 bytes parent folder | download | duplicates (2)
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
module Tests.GeneralisedUTF8 where

import GHC.Exts
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.ByteString as BS

import Test.Tasty
import Test.Tasty.QuickCheck

import qualified Codec.CBOR.ByteArray as BA
import Codec.Serialise.Internal.GeneralisedUTF8

testEncoder :: String -> Property
testEncoder s =
    case encodeGenUTF8 s of
      (ba, enc) ->
          toList ba === BS.unpack (T.encodeUtf8 $ T.pack s)
          .&&.
          enc === correctEnc
  where
    correctEnc
      | any isSurrogate s = GeneralisedUTF8
      | otherwise         = ConformantUTF8

testDecoder :: String -> Property
testDecoder s =
    decodeGenUTF8 ba === s
  where
    BA.BA ba = BA.fromByteString $ T.encodeUtf8 $ T.pack s

testTree :: TestTree
testTree =
    testGroup "Generalised UTF-8 codec"
        [ testProperty "encoder" testEncoder
        , testProperty "decoder" testDecoder
        ]