packages feed

willow-0.1.0.0: test/Test/Willow/Property/Encoding/EucKr.hs

{-|
Description:    

Copyright:      (c) 2020 Samuel May
License:        MPL-2.0
Maintainer:     ag.eitilt@gmail.com

Stability:      experimental
Portability:    portable
-}
module Test.Willow.Property.Encoding.EucKr
    ( tests
    ) where


import qualified Hedgehog as H
import qualified Hedgehog.Gen as H.G
import qualified Hedgehog.Range as H.R

import Web.Willow.Common.Encoding

import Test.Willow.Property.Common


tests :: H.Group
tests = packGroup "Web.Willow.Common.Encoding.EucKr"
    -- Single byte tests handled in "unit" due to limited number
    [ decodeEndOfStream
    , decodeTwoByteInvalid
    , decodeOneByteLookup
    , decodeTwoByteLookup
    ]


decodeEndOfStream :: Test
decodeEndOfStream = packTest "incomplete character" $ do
    first <- H.forAll . H.G.word8 $ H.R.linear 0x81 0xFE
    checkEndOfStream EucKr [first]


decodeTwoByteInvalid :: Test
decodeTwoByteInvalid = packTest "invalid second byte" $ do
    first <- H.forAll . H.G.word8 $ H.R.linear 0x81 0xFE
    second <- H.forAll $ H.G.choice
        [ H.G.word8 $ H.R.linear 0x00 0x40
        , H.G.constant 0xFF
        ]
    let check = if second <= 0x7F
            then checkInvalid
            else checkInvalidAll
    check EucKr [first, second]


decodeOneByteLookup :: Test
decodeOneByteLookup = packTest "one-byte sequence" $ do
    first <- H.forAll $ H.G.choice
        [ H.G.word8 $ H.R.linear 0x00 0x80
        , H.G.constant 0xFF
        ]
    checkTrailing EucKr [first]

decodeTwoByteLookup :: Test
decodeTwoByteLookup = packTest "two-byte sequence" $ do
    first <- H.forAll . H.G.word8 $ H.R.linear 0x81 0xFE
    second <- H.forAll . H.G.word8 $ H.R.linear 0x41 0xFE
    let check = if second <= 0x7F
            then checkTrailing
            else checkTrailingAll
    check EucKr [first, second]