willow-0.1.0.0: test/Test/Willow/WebPlatformTests/Manual/Encoding/ApiSurrogatesUtf8.hs
{-|
Description: Emulating tests from @encoding/api-surrogates-utf8.any.js@
Copyright: (c) 2020 Samuel May
License: MPL-2.0
Maintainer: ag.eitilt@gmail.com
Stability: experimental
Portability: portable
-}
module Test.Willow.WebPlatformTests.Manual.Encoding.ApiSurrogatesUtf8
( tests
) where
import qualified Data.ByteString as BS
import qualified Data.Text as T
import qualified Test.HUnit as U
import Test.HUnit ( (~:), (@?=) )
import Web.Willow.Common.Encoding
tests :: U.Test
tests = "api-surrogates-utf8.any.js" ~: U.TestList
[ "Invalid surrogates encoded into UTF-8" ~: U.TestList (map surrogates badStrings)
]
surrogates :: SurrogateTest -> U.Test
surrogates s = name s ~: U.TestCase $ do
fst (encodeUtf8 $ T.pack $ input s) @?= expected s
fst (decodeUtf8' $ expected s) @?= decoded s
data SurrogateTest = SurrogateTest
{ input :: String
, expected :: BS.ByteString
, decoded :: T.Text
, name :: String
}
badStrings :: [SurrogateTest]
badStrings =
[ SurrogateTest
{ input = "abc123"
, expected = BS.pack [0x61, 0x62, 0x63, 0x31, 0x32, 0x33]
, decoded = T.pack "abc123"
, name = "Sanity check"
}
, SurrogateTest
{ input = "\xD800"
, expected = BS.pack [0xef, 0xbf, 0xbd]
, decoded = T.pack "\xFFFD"
, name = "Surrogate half (low)"
}
, SurrogateTest
{ input = "\xDC00"
, expected = BS.pack [0xef, 0xbf, 0xbd]
, decoded = T.pack "\xFFFD"
, name = "Surrogate half (high)"
}
, SurrogateTest
{ input = "abc\xD800\&123"
, expected = BS.pack [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33]
, decoded = T.pack "abc\xFFFD\&123"
, name = "Surrogate half (low), in a string"
}
, SurrogateTest
{ input = "abc\xDC00\&123"
, expected = BS.pack [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33]
, decoded = T.pack "abc\xFFFD\&123"
, name = "Surrogate half (high), in a string"
}
, SurrogateTest
{ input = "\xDC00\xD800"
, expected = BS.pack [0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd]
, decoded = T.pack "\xFFFD\xFFFD"
, name = "Wrong order"
}
]