Hufflepdf/test/Object.hs

51 lines
1.9 KiB
Haskell
Raw Normal View History

{-# 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>")
]