2015-04-20 02:39:58 +00:00
|
|
|
module Data.HexStringSpec where
|
|
|
|
|
2015-04-20 09:26:56 +00:00
|
|
|
import Data.HexString ( hexString
|
2015-04-21 03:21:07 +00:00
|
|
|
, fromBytes
|
|
|
|
, toBytes )
|
2015-04-20 02:39:58 +00:00
|
|
|
|
2015-04-20 09:26:56 +00:00
|
|
|
import qualified Data.ByteString.Char8 as BS8
|
2015-04-20 06:25:01 +00:00
|
|
|
|
2015-04-20 02:39:58 +00:00
|
|
|
import Test.Hspec
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
2015-04-20 09:26:56 +00:00
|
|
|
describe "when constructing a hex string" $ do
|
|
|
|
it "should accept strings that fall within a valid range" $
|
|
|
|
hexString (BS8.pack "0123456789abcdef") `shouldBe` hexString (BS8.pack "0123456789abcdef")
|
2015-04-20 02:39:58 +00:00
|
|
|
|
2015-04-20 09:26:56 +00:00
|
|
|
it "should reject strings outside the range" $ do
|
|
|
|
putStrLn (show (hexString (BS8.pack "/"))) `shouldThrow` anyErrorCall
|
|
|
|
putStrLn (show (hexString (BS8.pack ":"))) `shouldThrow` anyErrorCall
|
|
|
|
putStrLn (show (hexString (BS8.pack "`"))) `shouldThrow` anyErrorCall
|
|
|
|
putStrLn (show (hexString (BS8.pack "g"))) `shouldThrow` anyErrorCall
|
2015-04-21 03:21:07 +00:00
|
|
|
|
|
|
|
describe "when interpreting a hex string" $ do
|
|
|
|
it "should convert the hex string properly when interpreting as bytes" $
|
|
|
|
toBytes (hexString (BS8.pack "ffff")) `shouldBe` BS8.pack "\255\255"
|
|
|
|
it "should convert bytes to the proper hex string" $
|
|
|
|
fromBytes (BS8.pack "\255\255") `shouldBe` hexString (BS8.pack "ffff")
|