diff --git a/Data/Encoding/Base.hs b/Data/Encoding/Base.hs index bc7223a..ae3f7e7 100644 --- a/Data/Encoding/Base.hs +++ b/Data/Encoding/Base.hs @@ -114,7 +114,7 @@ data EncodeState data EncodingException = HasNoRepresentation Char -- ^ Thrown if a specific character -- is not representable in an encoding. - deriving (Show,Typeable) + deriving (Eq,Show,Typeable) -- | This exception type is thrown whenever something went wrong during the -- decoding-process. diff --git a/Test/Tester.hs b/Test/Tester.hs new file mode 100644 index 0000000..35cf58d --- /dev/null +++ b/Test/Tester.hs @@ -0,0 +1,37 @@ +{-# LANGUAGE ExistentialQuantification #-} +module Test.Tester where + +import Data.Encoding +import Test.HUnit +import Data.Word +import Data.ByteString (pack) +import Control.Exception (catchDyn,evaluate) + +data EncodingTest + = forall enc. (Encoding enc,Show enc) => + EncodingTest enc String [Word8] + | forall enc. (Encoding enc,Show enc) => + DecodingError enc [Word8] DecodingException + +instance Testable EncodingTest where + test (EncodingTest enc src trg) = TestList + [TestLabel (show enc ++ " encodable") + (TestCase $ (all (encodable enc) src) @=? True) + ,TestLabel (show enc ++ " encoding") + (TestCase $ (encode enc src) @=? bstr) + ,TestLabel (show enc ++ " decodable") + (TestCase $ (decodable enc bstr) @=? True) + ,TestLabel (show enc ++ " decoding") + (TestCase $ (decode enc bstr) @=? src) + ] + where + bstr = pack trg + test (DecodingError enc trg what) = TestList + [TestLabel (show enc ++ " not decodable") $ + TestCase $ assert $ not $ decodable enc (pack trg) + ,TestLabel (show enc ++ " decoding error") $ TestCase $ + catchDyn (do + evaluate (decode enc (pack trg) == "") + return ()) + (\exc -> exc @=? what) + ]