packages feed

willow-0.1.0.0: test/Test/Willow/WebPlatformTests/Manual/Encoding/FatalSingleByte.hs

{-|
Description:    Emulating tests from @encoding/textdecoder-fatal-single-byte.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.FatalSingleByte
    ( tests
    ) where

import qualified Data.ByteString as BS
import qualified Data.Either as E
import qualified Data.HashMap.Strict as M
import qualified Data.Maybe as Y
import qualified Data.Word as W

import qualified Test.HUnit as U

import Test.HUnit ( (~:), (~?=) )

import Web.Willow.Common.Encoding

import Test.Willow.WebPlatformTests.Manual.Common


tests :: U.Test
tests = "textdecoder-fatal-single-byte.any.js" ~: U.TestList $ map test
    [ Ibm866
    , Iso8859_2
    , Iso8859_3
    , Iso8859_4
    , Iso8859_5
    , Iso8859_6
    , Iso8859_7
    , Iso8859_8
    , Iso8859_8i
    , Iso8859_10
    , Iso8859_13
    , Iso8859_14
    , Iso8859_15
    , Iso8859_16
    , Koi8R
    , Koi8U
    , Macintosh
    , MacintoshCyrillic
    , Windows874
    , Windows1250
    , Windows1251
    , Windows1252
    , Windows1253
    , Windows1254
    , Windows1255
    , Windows1256
    , Windows1257
    , Windows1258
    ]

test :: Encoding -> U.Test
test enc = show enc ~: U.TestList $ map (testByte enc) [0x00..0xFF]

testByte :: Encoding -> W.Word8 -> U.Test
testByte enc byte =
    null (filter E.isLeft . decodeEnc enc $ BS.singleton byte) ~?= notElem byte bad
  where bad = Y.fromMaybe [] $ M.lookup enc badBytes


badBytes :: M.HashMap Encoding [W.Word8]
badBytes = M.fromList
    [ (Iso8859_3, [0xA5, 0xAE, 0xBE, 0xC3, 0xD0, 0xE3, 0xF0])
    , (Iso8859_6,
        [ 0xA1, 0xA2, 0xA3, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9
        , 0xAA, 0xAB, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3
        , 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBC
        , 0xBD, 0xBE, 0xC0, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF
        , 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA
        , 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
        ])
    , (Iso8859_7, [0xAE, 0xD2, 0xFF])
    , (Iso8859_8,
        [ 0xA1, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5
        , 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD
        , 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5
        , 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD
        , 0xDE, 0xFB, 0xFC, 0xFF
        ])
    , (Iso8859_8i,
        [ 0xA1, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5
        , 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD
        , 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5
        , 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD
        , 0xDE, 0xFB, 0xFC, 0xFF
        ])
    , (Windows874, [0xDB, 0xDC, 0xDD, 0xDE, 0xFC, 0xFD, 0xFE, 0xFF])
    , (Windows1253, [0xAA, 0xD2, 0xFF])
    , (Windows1255, [0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xFB, 0xFC, 0xFF])
    , (Windows1257, [0xA1, 0xA5])
    ]