From 82b0a26ec93dabba6a420a3ad34f497894ef7411 Mon Sep 17 00:00:00 2001 From: Henning Guenther Date: Mon, 31 Dec 2007 03:47:27 -0800 Subject: [PATCH] Implemented Unit Test Framework darcs-hash:20071231114727-a4fee-7bb01bb4f8b044c8cf29a84cb0f64a392c7f0588 --- Data/Encoding/Base.hs | 2 +- Test/Tester.hs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 Test/Tester.hs 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) + ]