2010-01-13 02:57:13 +03:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
2009-11-09 08:34:36 +03:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2012-10-10 07:58:00 +04:00
|
|
|
module VerifyEvalTerminfoCaps where
|
2009-11-09 08:34:36 +03:00
|
|
|
|
2014-01-31 09:12:01 +04:00
|
|
|
import Blaze.ByteString.Builder.Internal.Write (runWrite, getBound)
|
2017-01-22 09:21:58 +03:00
|
|
|
import Data.Terminfo.Eval
|
2009-11-09 08:34:36 +03:00
|
|
|
import Data.Terminfo.Parse
|
2010-01-13 02:57:13 +03:00
|
|
|
import Control.DeepSeq
|
2009-11-09 08:34:36 +03:00
|
|
|
|
|
|
|
import qualified System.Console.Terminfo as Terminfo
|
|
|
|
|
|
|
|
import Verify
|
2013-12-20 10:24:56 +04:00
|
|
|
import Verify.Graphics.Vty.Output
|
2009-11-09 08:34:36 +03:00
|
|
|
|
2012-10-10 07:58:00 +04:00
|
|
|
import Control.Applicative ( (<$>) )
|
2009-11-09 08:34:36 +03:00
|
|
|
import Control.Exception ( try, SomeException(..) )
|
|
|
|
|
2012-10-10 07:58:00 +04:00
|
|
|
import Control.Monad ( mapM_, forM, forM_ )
|
2009-11-09 08:34:36 +03:00
|
|
|
|
|
|
|
import Data.Maybe ( fromJust )
|
|
|
|
import Data.Word
|
|
|
|
|
2014-01-31 09:12:01 +04:00
|
|
|
import Foreign.Marshal.Alloc (mallocBytes)
|
|
|
|
import Foreign.Ptr (Ptr, minusPtr)
|
2009-11-09 08:34:36 +03:00
|
|
|
import Numeric
|
|
|
|
|
2017-01-22 09:54:37 +03:00
|
|
|
-- If a terminal defines one of the caps then it's expected to be
|
|
|
|
-- parsable.
|
2017-01-22 09:21:58 +03:00
|
|
|
capsOfInterest =
|
2009-11-09 08:34:36 +03:00
|
|
|
[ "cup"
|
|
|
|
, "sc"
|
|
|
|
, "rc"
|
|
|
|
, "setf"
|
|
|
|
, "setb"
|
|
|
|
, "setaf"
|
|
|
|
, "setab"
|
|
|
|
, "op"
|
|
|
|
, "cnorm"
|
|
|
|
, "civis"
|
|
|
|
, "smcup"
|
|
|
|
, "rmcup"
|
|
|
|
, "clear"
|
|
|
|
, "hpa"
|
|
|
|
, "vpa"
|
|
|
|
, "sgr"
|
|
|
|
, "sgr0"
|
|
|
|
]
|
|
|
|
|
2014-04-12 04:51:13 +04:00
|
|
|
fromCapname ti name = fromJust $ Terminfo.getCapability ti (Terminfo.tiGetStr name)
|
2009-11-09 08:34:36 +03:00
|
|
|
|
2012-10-10 07:58:00 +04:00
|
|
|
tests :: IO [Test]
|
|
|
|
tests = do
|
2017-01-22 09:54:37 +03:00
|
|
|
-- 1 MB should be big enough for any termcaps ;-)
|
|
|
|
evalBuffer :: Ptr Word8 <- mallocBytes (1024 * 1024)
|
2014-04-12 04:51:13 +04:00
|
|
|
fmap concat $ forM terminalsOfInterest $ \termName -> do
|
|
|
|
putStrLn $ "adding tests for terminal: " ++ termName
|
|
|
|
mti <- try $ Terminfo.setupTerm termName
|
2012-10-10 07:58:00 +04:00
|
|
|
case mti of
|
2017-01-22 09:21:58 +03:00
|
|
|
Left (_e :: SomeException)
|
2012-10-10 07:58:00 +04:00
|
|
|
-> return []
|
|
|
|
Right ti -> do
|
2014-04-12 04:51:13 +04:00
|
|
|
fmap concat $ forM capsOfInterest $ \capName -> do
|
|
|
|
case Terminfo.getCapability ti (Terminfo.tiGetStr capName) of
|
|
|
|
Just capDef -> do
|
|
|
|
putStrLn $ "\tadding test for cap: " ++ capName
|
|
|
|
let testName = termName ++ "(" ++ capName ++ ")"
|
|
|
|
case parseCapExpression capDef of
|
|
|
|
Left error -> return [verify testName (failed {reason = "parse error " ++ show error})]
|
|
|
|
Right !cap_expr -> return [verify testName (verifyEvalCap evalBuffer cap_expr)]
|
2012-10-10 07:58:00 +04:00
|
|
|
Nothing -> do
|
|
|
|
return []
|
2009-11-09 08:34:36 +03:00
|
|
|
|
2014-04-12 04:51:13 +04:00
|
|
|
{-# NOINLINE verifyEvalCap #-}
|
|
|
|
verifyEvalCap :: Ptr Word8 -> CapExpression -> Int -> Property
|
|
|
|
verifyEvalCap evalBuffer expr !junkInt = do
|
2017-01-22 09:21:58 +03:00
|
|
|
forAll (vector 9) $ \inputValues ->
|
2014-04-12 04:51:13 +04:00
|
|
|
let write = writeCapExpr expr inputValues
|
|
|
|
!byteCount = getBound write
|
2010-01-13 02:57:13 +03:00
|
|
|
in liftIOResult $ do
|
2014-04-12 04:51:13 +04:00
|
|
|
let startPtr :: Ptr Word8 = evalBuffer
|
|
|
|
forM_ [0..100] $ \i -> runWrite write startPtr
|
|
|
|
endPtr <- runWrite write startPtr
|
|
|
|
case endPtr `minusPtr` startPtr of
|
2017-01-22 09:21:58 +03:00
|
|
|
count | count < 0 ->
|
2011-07-15 05:23:02 +04:00
|
|
|
return $ failed { reason = "End pointer before start pointer." }
|
2017-01-22 09:21:58 +03:00
|
|
|
| toEnum count > byteCount ->
|
|
|
|
return $ failed { reason = "End pointer past end of buffer by "
|
|
|
|
++ show (toEnum count - byteCount)
|
2011-07-15 05:23:02 +04:00
|
|
|
}
|
2017-01-22 09:21:58 +03:00
|
|
|
| otherwise ->
|
2010-01-13 02:57:13 +03:00
|
|
|
return succeeded
|