File: Text.hs

package info (click to toggle)
haskell-io-streams 1.5.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 480 kB
  • sloc: haskell: 4,470; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download | duplicates (7)
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
69
{-# LANGUAGE OverloadedStrings #-}

module System.IO.Streams.Tests.Text (tests) where

------------------------------------------------------------------------------
import           Control.Monad                  ((>=>))
import           Data.Text.Encoding.Error
import qualified System.IO.Streams.Internal     as Streams
import qualified System.IO.Streams.List         as Streams
import           System.IO.Streams.Tests.Common
import qualified System.IO.Streams.Text         as Streams
import           Test.Framework
import           Test.Framework.Providers.HUnit
import           Test.HUnit                     hiding (Test)
------------------------------------------------------------------------------

tests :: [Test]
tests = [ testDecodeOK
        , testStrictDecodeError
        , testEncode
        ]


------------------------------------------------------------------------------
testEncode :: Test
testEncode = testCase "text/encodeUtf8" $ do
    is <- Streams.fromList ["\x3BC", "ok", ""]
    Streams.outputToList (Streams.encodeUtf8 >=> Streams.connect is)
        >>= assertEqual "ok encode" ["\xCE\xBC", "ok", ""]


------------------------------------------------------------------------------
testDecodeOK :: Test
testDecodeOK = testCase "text/decodeUtf8/wholeChunk" $ do
    Streams.fromList ["\xCE\xBC", "ok", ""]
        >>= Streams.decodeUtf8
        >>= Streams.toList
        >>= assertEqual "ok decode" ["\x3BC", "ok", ""]

    Streams.fromList ["\xCE", "\xBC", "ok", "foo\xCE", "\xBC"]
        >>= Streams.decodeUtf8
        >>= Streams.toList
        >>= assertEqual "ok decode 2" ["\x3BC", "ok", "foo", "\x3BC"]

    Streams.fromList ["\xE2\xB6", "\x8E"]
        >>= Streams.decodeUtf8
        >>= Streams.toList
        >>= assertEqual "ok decode 3" ["\x2D8E"]

    Streams.fromList ["\xF0\x90\x80\x83"]
        >>= Streams.decodeUtf8
        >>= Streams.toList
        >>= assertEqual "ok decode 4" ["\x10003"]

    Streams.fromList []
        >>= Streams.decodeUtf8With strictDecode
        >>= Streams.toList
        >>= assertEqual "ok strict empty" []


------------------------------------------------------------------------------
testStrictDecodeError :: Test
testStrictDecodeError = testCase "text/decodeUtf8/error" $ do
    expectExceptionH (Streams.fromList ["\x87"] >>=
                      Streams.decodeUtf8With strictDecode >>=
                      Streams.toList)
    expectExceptionH (Streams.fromList ["o\x87\x87"] >>=
                      Streams.decodeUtf8With strictDecode >>=
                      Streams.toList)