packages feed

hex-text-0.1.0.8: test/Main.hs

import Text.Hex (decodeHex, encodeHex, lazilyEncodeHex)

import Test.Hspec (hspec, describe, it, shouldBe, Spec)

import Prelude ((.), ($), cycle, IO, fmap, Maybe (..))
import Data.String (fromString)

import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LazyByteString
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LazyText

main :: IO ()
main = hspec $ do
    encodeHexTests
    decodeHexTests
    lazilyEncodeHexTests

encodeHexTests :: Spec
encodeHexTests = describe "encodeHex" $ do
    it "can encode a single byte" $ do
        let f = encodeHex . ByteString.singleton
        f 192 `shouldBe` fromString "c0"
        f 168 `shouldBe` fromString "a8"
    it "can encode many bytes" $ do
        let f = encodeHex . ByteString.pack
        f [192, 168, 1, 2] `shouldBe` fromString "c0a80102"

decodeHexTests :: Spec
decodeHexTests = describe "decodeHex" $ do
    let f = fmap ByteString.unpack . decodeHex . Text.pack
    it "can decode" $ do
        f "c0a80102" `shouldBe` Just [192,168,1,2]
        f "C0A80102" `shouldBe` Just [192,168,1,2]
    it "can fail" $ do
        f "c0a8010" `shouldBe` Nothing
        f "x0a80102" `shouldBe` Nothing

lazilyEncodeHexTests :: Spec
lazilyEncodeHexTests = describe "lazilyEncodeHex" $ do
    let f = lazilyEncodeHex . LazyByteString.pack
    it "can decode part of an infinite list" $
        (LazyText.take 8 . f . cycle) [1, 2, 3] `shouldBe` fromString "01020301"