diff --git a/Test/Tester.hs b/Test/Tester.hs index 35cf58d..31f5166 100644 --- a/Test/Tester.hs +++ b/Test/Tester.hs @@ -4,7 +4,8 @@ module Test.Tester where import Data.Encoding import Test.HUnit import Data.Word -import Data.ByteString (pack) +import qualified Data.ByteString as BS +import qualified Data.ByteString.Lazy as LBS import Control.Exception (catchDyn,evaluate) data EncodingTest @@ -12,26 +13,56 @@ data EncodingTest EncodingTest enc String [Word8] | forall enc. (Encoding enc,Show enc) => DecodingError enc [Word8] DecodingException + | forall enc. (Encoding enc,Show enc) => + EncodingError enc String EncodingException instance Testable EncodingTest where test (EncodingTest enc src trg) = TestList [TestLabel (show enc ++ " encodable") (TestCase $ (all (encodable enc) src) @=? True) - ,TestLabel (show enc ++ " encoding") + ,TestLabel (show enc ++ " encoding (strict)") (TestCase $ (encode enc src) @=? bstr) + ,TestLabel (show enc ++ " encoding (lazy)") + (TestCase $ (encodeLazy enc src) @=? lbstr) ,TestLabel (show enc ++ " decodable") (TestCase $ (decodable enc bstr) @=? True) - ,TestLabel (show enc ++ " decoding") + ,TestLabel (show enc ++ " decoding (strict)") (TestCase $ (decode enc bstr) @=? src) + ,TestLabel (show enc ++ " decoding (lazy)") + (TestCase $ (decodeLazy enc lbstr) @=? src) ] where - bstr = pack trg + bstr = BS.pack trg + lbstr = LBS.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 $ + [TestLabel (show what++" not decodable in "++show enc) $ + TestCase $ assert $ not $ decodable enc (BS.pack trg) + ,TestLabel (show enc ++ " decoding error (strict)") $ TestCase $ catchDyn (do - evaluate (decode enc (pack trg) == "") - return ()) + mapM_ evaluate (decode enc (BS.pack trg)) + assertFailure "No exception thrown" + ) + (\exc -> exc @=? what) + ,TestLabel (show enc ++ " decoding error (lazy)") $ TestCase $ + catchDyn (do + mapM_ evaluate (decodeLazy enc (LBS.pack trg)) + assertFailure "No exception thrown" + ) + (\exc -> exc @=? what) + ] + test (EncodingError enc src what) = TestList + [TestLabel (show src ++ " not encodable in " ++ show enc) $ + TestCase $ assert $ not $ all (encodable enc) src + ,TestLabel (show enc ++ " encoding error (strict)") $ TestCase $ + catchDyn (do + evaluate (encode enc src) + assertFailure "No exception thrown" + ) + (\exc -> exc @=? what) + ,TestLabel (show enc ++ " encoding error (lazy)") $ TestCase $ + catchDyn (do + evaluate (encodeLazy enc src) + assertFailure "No exception thrown" + ) (\exc -> exc @=? what) ]