on master help sign in
fix terminfo cap parser tests
commit76bffb44d8821e5c25599aa2b9fbb96ba474de95 authorCorey O'Connor <corey@ooyala.com> dateThu 30 Jan 2014, 21:12 (13 years ago) parentf0dd4aa
5 files changed+33−43
test/Verify/Data/Terminfo/Parse.hs+9−10 test/VerifyEvalTerminfoCaps.hs+8−6 test/VerifyParseTerminfoCaps.hs+15−25 test/vty-examples.cabal−2 vty.cabal+1
test/Verify/Data/Terminfo/Parse.hs+9−10
@@ -8,6 +8,7 @@ import Data.Terminfo.Eval
88import Verify
99
1010import Data.Word
11+import qualified Data.Vector.Unboxed as Vector
1112
1213import Numeric
1314
@@ -46,26 +47,26 @@ instance Arbitrary IncFirstTwoCap where
4647 return $ IncFirstTwoCap s' bytes
4748 ) `suchThat` ( \(IncFirstTwoCap str _) -> length str < 255 )
4849
49−data PushParamCap = PushParamCap String Word [Word8]
50+data PushParamCap = PushParamCap String Int [Word8]
5051 deriving ( Show )
5152
5253instance Arbitrary PushParamCap where
5354 arbitrary
5455 = ( do
5556 NonParamCapString s <- arbitrary
56− n :: Word <- choose (1,9) >>= return . toEnum
57+ n <- choose (1,9)
5758 (s', bytes) <- insert_escape_op ("p" ++ show n) [] s
5859 return $ PushParamCap s' n bytes
5960 ) `suchThat` ( \(PushParamCap str _ _) -> length str < 255 )
6061
61−data DecPrintCap = DecPrintCap String Word [Word8]
62+data DecPrintCap = DecPrintCap String Int [Word8]
6263 deriving ( Show )
6364
6465instance Arbitrary DecPrintCap where
6566 arbitrary
6667 = ( do
6768 NonParamCapString s <- arbitrary
68− n :: Word <- choose (1,9) >>= return . toEnum
69+ n <- choose (1,9)
6970 (s', bytes) <- insert_escape_op ("p" ++ show n ++ "%d") [] s
7071 return $ DecPrintCap s' n bytes
7172 ) `suchThat` ( \(DecPrintCap str _ _) -> length str < 255 )
@@ -87,15 +88,13 @@ is_bytes_op :: CapOp -> Bool
8788is_bytes_op (Bytes {}) = True
8889-- is_bytes_op _ = False
8990
90−bytes_for_range cap offset c
91− = take (fromEnum c)
92− $ drop (fromEnum offset)
93− $ ( map ( toEnum . fromEnum ) $! source_string cap )
91+bytes_for_range cap offset count
92+ = Vector.toList $ Vector.take count $ Vector.drop offset $ cap_bytes cap
9493
9594collect_bytes :: CapExpression -> [Word8]
9695collect_bytes e = concat [ bytes
97− | Bytes offset c _ <- cap_ops e
98− , let bytes = bytes_for_range e offset c
96+ | Bytes offset count <- cap_ops e
97+ , let bytes = bytes_for_range e offset count
9998 ]
10099
101100
test/VerifyEvalTerminfoCaps.hs+8−6
@@ -2,7 +2,7 @@
22{-# LANGUAGE NamedFieldPuns #-}
33module VerifyEvalTerminfoCaps where
44
5−import Blaze.ByteString.Builder.Internal.Write (runWrite)
5+import Blaze.ByteString.Builder.Internal.Write (runWrite, getBound)
66import Data.Terminfo.Eval
77import Data.Terminfo.Parse
88import Control.DeepSeq
@@ -20,6 +20,8 @@ import Control.Monad ( mapM_, forM, forM_ )
2020import Data.Maybe ( fromJust )
2121import Data.Word
2222
23+import Foreign.Marshal.Alloc (mallocBytes)
24+import Foreign.Ptr (Ptr, minusPtr)
2325import Numeric
2426
2527-- If a terminal defines one of the caps then it's expected to be parsable.
@@ -60,8 +62,7 @@ tests = do
6062 Just cap_def -> do
6163 putStrLn $ "\tadding test for cap: " ++ cap_name
6264 let test_name = term_name ++ "(" ++ cap_name ++ ")"
63− parse_result <- parse_cap_expression cap_def
64− case parse_result of
65+ case parse_cap_expression cap_def of
6566 Left error -> return [verify test_name (failed {reason = "parse error " ++ show error})]
6667 Right !cap_expr -> return [verify test_name (verify_eval_cap eval_buffer cap_expr)]
6768 Nothing -> do
@@ -71,11 +72,12 @@ tests = do
7172verify_eval_cap :: Ptr Word8 -> CapExpression -> Int -> Property
7273verify_eval_cap eval_buffer expr !junk_int = do
7374 forAll (vector 9) $ \input_values ->
74− let !byte_count = cap_expression_required_bytes expr input_values
75+ let write = write_cap_expr expr input_values
76+ !byte_count = getBound write
7577 in liftIOResult $ do
7678 let start_ptr :: Ptr Word8 = eval_buffer
77− forM_ [0..100] $ \i -> runWrite (write_cap_expr expr input_values) start_ptr
78− end_ptr <- runWrite(write_cap_expr expr input_values) start_ptr
79+ forM_ [0..100] $ \i -> runWrite write start_ptr
80+ end_ptr <- runWrite write start_ptr
7981 case end_ptr `minusPtr` start_ptr of
8082 count | count < 0 ->
8183 return $ failed { reason = "End pointer before start pointer." }
test/VerifyParseTerminfoCaps.hs+15−25
@@ -48,7 +48,7 @@ tests = do
4848 liftIO $ putStrLn case_name
4949 return $ case Terminfo.getCapability ti (Terminfo.tiGetStr cap_name) of
5050 Just cap_def -> [verify (case_name ++ " -> " ++ show cap_def)
51− (verify_parse_cap cap_def $ const (return succeeded))]
51+ (verify_parse_cap cap_def $ const succeeded)]
5252 Nothing -> []
5353 )
5454 )
@@ -59,46 +59,36 @@ tests = do
5959 , verify "parse cap string with %pN op" push_param_caps
6060 ] ++ parse_tests
6161
62−verify_parse_cap cap_string on_parse = liftIOResult $ do
63− parse_result <- parse_cap_expression cap_string
64− case parse_result of
65− Left error -> return $ failed { reason = "parse error " ++ show error }
66− Right e -> on_parse e
62+verify_parse_cap cap_string on_parse =
63+ case parse_cap_expression cap_string of
64+ Left error -> failed { reason = "parse error " ++ show error }
65+ Right e -> on_parse e
6766
6867non_paramaterized_caps (NonParamCapString cap) = do
6968 verify_parse_cap cap $ \e ->
70− let expected_count :: Word8 = toEnum $ length cap
71− expected_bytes = map (toEnum . fromEnum) cap
72− out_bytes = bytes_for_range e 0 expected_count
73− in return $ verify_bytes_equal out_bytes expected_bytes
69+ let expected_bytes = map (toEnum . fromEnum) cap
70+ out_bytes = bytes_for_range e 0 (length cap)
71+ in verify_bytes_equal out_bytes expected_bytes
7472
7573literal_percent_caps (LiteralPercentCap cap_string expected_bytes) = do
76− verify_parse_cap cap_string $ \e ->
77− let expected_count :: Word8 = toEnum $ length expected_bytes
78− out_bytes = collect_bytes e
79− in return $ verify_bytes_equal out_bytes expected_bytes
74+ verify_parse_cap cap_string $ \e -> verify_bytes_equal (collect_bytes e) expected_bytes
8075
8176inc_first_two_caps (IncFirstTwoCap cap_string expected_bytes) = do
82− verify_parse_cap cap_string $ \e ->
83− let expected_count :: Word8 = toEnum $ length expected_bytes
84− out_bytes = collect_bytes e
85− in return $ verify_bytes_equal out_bytes expected_bytes
77+ verify_parse_cap cap_string $ \e -> verify_bytes_equal (collect_bytes e) expected_bytes
8678
8779push_param_caps (PushParamCap cap_string expected_param_count expected_bytes) = do
8880 verify_parse_cap cap_string $ \e ->
89− let expected_count :: Word8 = toEnum $ length expected_bytes
90− out_bytes = collect_bytes e
81+ let out_bytes = collect_bytes e
9182 out_param_count = param_count e
92− in return $ if out_param_count == expected_param_count
83+ in if out_param_count == expected_param_count
9384 then verify_bytes_equal out_bytes expected_bytes
9485 else failed { reason = "out param count /= expected param count" }
9586
9687dec_print_param_caps (DecPrintCap cap_string expected_param_count expected_bytes) = do
9788 verify_parse_cap cap_string $ \e ->
98− let expected_count :: Word8 = toEnum $ length expected_bytes
99− out_bytes = collect_bytes e
89+ let out_bytes = collect_bytes e
10090 out_param_count = param_count e
101− in return $ if out_param_count == expected_param_count
91+ in if out_param_count == expected_param_count
10292 then verify_bytes_equal out_bytes expected_bytes
10393 else failed { reason = "out param count /= expected param count" }
10494
@@ -106,6 +96,6 @@ print_cap ti cap_name = do
10696 putStrLn $ cap_name ++ ": " ++ show (from_capname ti cap_name)
10797
10898print_expression ti cap_name = do
109− parse_result <- parse_cap_expression $ from_capname ti cap_name
99+ let parse_result = parse_cap_expression $ from_capname ti cap_name
110100 putStrLn $ cap_name ++ ": " ++ show parse_result
111101
test/vty-examples.cabal−2
@@ -33,7 +33,6 @@ executable vty-interactive-terminal-test
3333 containers,
3434 data-default >= 0.5.3,
3535 deepseq >= 1.1 && < 1.4,
36− ghc-prim,
3736 lens,
3837 mtl >= 1.1.1.0 && < 2.2,
3938 parallel >= 2.2 && < 3.3,
@@ -100,7 +99,6 @@ executable vty-benchmark
10099 containers,
101100 data-default >= 0.5.3,
102101 deepseq >= 1.1 && < 1.4,
103− ghc-prim,
104102 lens,
105103 mtl >= 1.1.1.0 && < 2.2,
106104 parallel >= 2.2 && < 3.3,
vty.cabal+1
@@ -269,6 +269,7 @@ test-suite verify-eval-terminfo-caps
269269 QuickCheck >= 2.4,
270270 random == 1.0.*,
271271 base >= 4 && < 5,
272+ blaze-builder >= 0.3.3.2 && < 0.4,
272273 bytestring,
273274 containers,
274275 deepseq >= 1.1 && < 1.4,