diff --git a/Hufflepdf.cabal b/Hufflepdf.cabal index 5e2657e..aa84f0c 100644 --- a/Hufflepdf.cabal +++ b/Hufflepdf.cabal @@ -80,3 +80,24 @@ executable getText , zlib ghc-options: -Wall default-language: Haskell2010 + +Test-Suite unitTests + type: exitcode-stdio-1.0 + main-is: Main.hs + other-modules: Object + , Data.ByteString.Char8.Util + , PDF.EOL + , PDF.Parser + , PDF.Object + , PDF.Output + hs-source-dirs: test + , src + build-depends: attoparsec + , base >=4.9 && <4.13 + , bytestring + , containers + , Hufflepdf + , HUnit + , mtl + , text + default-language: Haskell2010 diff --git a/src/Data/ByteString/Char8/Util.hs b/src/Data/ByteString/Char8/Util.hs index b6753fe..03d5b54 100644 --- a/src/Data/ByteString/Char8/Util.hs +++ b/src/Data/ByteString/Char8/Util.hs @@ -23,9 +23,9 @@ import Data.Text.Encoding (decodeUtf16BE) import Prelude hiding (length) import Text.Printf (printf) -newtype B8Int = B8Int ByteString deriving Show -newtype B16Int = B16Int ByteString deriving Show -newtype B256Int = B256Int ByteString deriving Show +newtype B8Int = B8Int ByteString deriving (Eq, Show) +newtype B16Int = B16Int ByteString deriving (Eq, Show) +newtype B256Int = B256Int ByteString deriving (Eq, Show) previous :: Char -> Int -> ByteString -> Int previous char position byteString diff --git a/src/PDF/Object.hs b/src/PDF/Object.hs index f4d077b..55984d3 100644 --- a/src/PDF/Object.hs +++ b/src/PDF/Object.hs @@ -93,7 +93,7 @@ boolean = -- -- Number -- -newtype Number = Number Double deriving Show +newtype Number = Number Double deriving (Eq, Show) instance Output Number where output (Number f) = Output.string $ @@ -113,7 +113,7 @@ number = Number . read . Char8.unpack <$> -- -- StringObject -- -data StringObject = Literal ByteString | Hexadecimal B16Int deriving Show +data StringObject = Literal ByteString | Hexadecimal B16Int deriving (Eq, Show) instance Output StringObject where output (Literal s) = Output.string (printf "(%s)" (Char8.unpack s)) diff --git a/test/Main.hs b/test/Main.hs new file mode 100644 index 0000000..c435a11 --- /dev/null +++ b/test/Main.hs @@ -0,0 +1,9 @@ +{-# LANGUAGE OverloadedStrings #-} +import Test.HUnit +import Object (testNumber, testString) + +main :: IO () +main = runTestTT (TestList [ + testNumber + , testString + ]) *> return () diff --git a/test/Object.hs b/test/Object.hs new file mode 100644 index 0000000..8a73f0d --- /dev/null +++ b/test/Object.hs @@ -0,0 +1,50 @@ +{-# LANGUAGE OverloadedStrings #-} +module Object ( + testNumber + , testString + ) where + +import Data.ByteString.Char8 (pack) +import Data.ByteString.Char8.Util (B16Int(..)) +import PDF.Object (Number(..), StringObject(..), name, number, stringObject) +import PDF.Parser (MonadParser, Parser, evalParser) +import Test.HUnit + +check :: (Eq a, Show a) => Parser () a -> (a, String) -> Test +check parser (aim, input) = + TestCase $ assertEqual message (Right aim) (parse input) + where + message = "parse: " ++ input + parse = evalParser parser () . pack + +testName :: Test +testName = TestLabel "Name" . TestList $ check name <$> [ + ] + +testNumber :: Test +testNumber = TestLabel "Number" . TestList $ check number <$> [ + (Number 123, "123") + , (Number 43445, "43445") + , (Number 17, "+17") + , (Number (-98), "-98") + , (Number 0, "0") + , (Number 34.5, "34.5") + , (Number (-3.62), "-3.62") + , (Number 123.6, "+123.6") + , (Number 4, "4.") + , (Number (-0.002), "-.002") + , (Number 0, "0.0") + ] + +testString :: Test +testString = TestLabel "StringObject" . TestList $ check stringObject <$> [ + (Literal "This is a string", "(This is a string)") + , (Literal "Strings may contain newlines\nand such .", "(Strings may contain newlines\nand such .)") + , (Literal "Strings may contain balanced parentheses ( ) and\nspecial characters ( * ! & } ^ % and so on ) .", "(Strings may contain balanced parentheses ( ) and\nspecial characters ( * ! & } ^ % and so on ) .)") + , (Literal "The following is an empty string .", "(The following is an empty string .)") + , (Literal "", "()") + , (Literal "It has zero ( 0 ) length .", "(It has zero ( 0 ) length .)") + , (Hexadecimal $ B16Int "4E6F762073686D6F7A206B6120706F702E", "<4E6F762073686D6F7A206B6120706F702E>") + , (Hexadecimal $ B16Int "901FA3", "<901FA3>") + , (Hexadecimal $ B16Int "901FA0", "<901FA>") + ]