packages feed

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

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


import qualified Data.ByteString as BS
import qualified Data.Either as E
import qualified Data.Text as T
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-streaming.any.js" ~: U.TestList
    [ testFatal Utf8 0xC0
    , testFatal Utf16le 0x00
    , testFatal Utf16be 0x00
    , testReplace Utf8 0xC0
    , testReplace Utf16le 0xC0
    , testReplace Utf16be 0xC0
    -- Can't test streaming since it's implemented via native laziness rather than separate calls
    ]


testFatal :: Encoding -> W.Word8 -> U.Test
testFatal enc byte = "Unterminated " ++ show enc ~:
    not (null . filter E.isLeft . decodeEnc enc $ BS.singleton byte) ~?
    "unterminated sequence should throw"

testReplace :: Encoding -> W.Word8 -> U.Test
testReplace enc byte = "Unterminated but non-fatal " ++ show enc ~:
    decodeEnc' enc (BS.singleton byte) ~?= T.singleton '\xFFFD'