{-| Description: Copyright: (c) 2020 Samuel May License: MPL-2.0 Maintainer: ag.eitilt@gmail.com Stability: experimental Portability: portable -} module Test.Willow.Unit.Encoding.Utf8 ( tests ) where import qualified Data.ByteString.Builder as BS.B import qualified Data.ByteString.Lazy as BS.L import qualified Data.Either as E import qualified Test.HUnit as U import Test.HUnit ( (~:), (~?=) ) import Web.Willow.Common.Encoding import Test.Willow.Unit.Encoding.Common tests :: U.Test tests = "UTF-8 encoding" ~: U.TestList [ "decode" ~: U.TestList $ [ "ASCII" ~: U.TestList $ map (decodeAscii Utf8) ['\NUL'..'\DEL'] , "failures" ~: U.TestList $ map (\(c, b) -> decodeTest Utf8 c b Nothing) [ ([toEnum i], [fromIntegral i]) | i <- [0x80..0xC1] ++ [0xF5..0xFF] ] ++ ["surrogates" ~: U.TestList $ [ surrogatesInvalid c | c <- ['\xD800'..'\xDFFF'] ]] ] , "encode" ~: U.TestList $ [ "ASCII" ~: U.TestList $ map (encodeAscii Utf8) ['\NUL'..'\DEL'] ] ] surrogatesInvalid :: Char -> U.Test surrogatesInvalid surrogate = show surrogate ~: filter E.isRight (fst . decodeUtf8NoBom . BS.L.toStrict . BS.B.toLazyByteString $ BS.B.charUtf8 surrogate) ~?= []