-
Notifications
You must be signed in to change notification settings - Fork 0
/
2018-11-13_1.hs
67 lines (53 loc) · 2.45 KB
/
2018-11-13_1.hs
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
{-# OPTIONS -Weverything -fno-warn-implicit-prelude #-}
import qualified Data.ByteString
import qualified Data.ByteString.Char8
import qualified Data.Text
import qualified Data.Text.Encoding
import qualified Test.Hspec as Test
import Data.Either (isLeft)
import Data.ByteString (ByteString)
import Data.Text.Encoding.Error (UnicodeException)
import Control.Exception (evaluate)
-- "Hello world"
helloAscii :: ByteString
helloAscii =
Data.ByteString.pack [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
-- "你好世界" (Chinese)
helloUtf8 :: ByteString
helloUtf8 = Data.ByteString.pack
[228, 189, 160, 229, 165, 189, 228, 184, 150, 231, 149, 140]
-- Dodgy unicode (invalid 2 octet sequence)
helloInvalidUtf8 :: ByteString
helloInvalidUtf8 = Data.ByteString.pack [195, 40] <> helloUtf8
main :: IO ()
main = Test.hspec $ do
Test.describe "Data.ByteString.Char8" $ do
let decode :: ByteString -> String
decode = Data.ByteString.Char8.unpack
Test.it "works fine for ascii text" $ do
decode helloAscii `Test.shouldBe` "Hello world"
Test.it "garbles unicode" $ do
decode helloUtf8 `Test.shouldNotBe` "你好世界"
Test.it "garbles bad unicode" $ do
decode helloInvalidUtf8 `Test.shouldNotBe` "你好世界"
Test.describe "Data.Text.Encoding.decodeUtf8" $ do
let decode :: ByteString -> String
decode = Data.Text.unpack . Data.Text.Encoding.decodeUtf8
unicodeException :: Test.Selector UnicodeException
unicodeException _ = True
Test.it "works fine for ascii text" $ do
decode helloAscii `Test.shouldBe` "Hello world"
Test.it "works fine for unicode" $ do
decode helloUtf8 `Test.shouldBe` "你好世界"
Test.it "throws an exception for bad unicode" $ do
evaluate (decode helloInvalidUtf8)
`Test.shouldThrow` unicodeException
Test.describe "Data.Text.Encoding.decodeUtf8'" $ do
let decode :: ByteString -> Either UnicodeException String
decode = fmap Data.Text.unpack . Data.Text.Encoding.decodeUtf8'
Test.it "works fine for ascii text" $ do
decode helloAscii `Test.shouldBe` Right "Hello world"
Test.it "works fine for unicode" $ do
decode helloUtf8 `Test.shouldBe` Right "你好世界"
Test.it "indicates bad unicode via Left value" $ do
decode helloInvalidUtf8 `Test.shouldSatisfy` isLeft