on master help sign in
fix terminfo cap parser tests
76bffb4Corey O'Connor
test/Verify/Data/Terminfo/Parse.hs+9−10
@@ -8,6 +8,7 @@ import Data.Terminfo.Eval
8import Verify
8import Verify
9
9
10import Data.Word
10import Data.Word
11+import qualified Data.Vector.Unboxed as Vector
11
12
12import Numeric
13import Numeric
13
14
@@ -46,26 +47,26 @@ instance Arbitrary IncFirstTwoCap where
46 return $ IncFirstTwoCap s' bytes
47 return $ IncFirstTwoCap s' bytes
47 ) `suchThat` ( \(IncFirstTwoCap str _) -> length str < 255 )
48 ) `suchThat` ( \(IncFirstTwoCap str _) -> length str < 255 )
48
49
49−data PushParamCap = PushParamCap String Word [Word8]
50+data PushParamCap = PushParamCap String Int [Word8]
50 deriving ( Show )
51 deriving ( Show )
51
52
52instance Arbitrary PushParamCap where
53instance Arbitrary PushParamCap where
53 arbitrary
54 arbitrary
54 = ( do
55 = ( do
55 NonParamCapString s <- arbitrary
56 NonParamCapString s <- arbitrary
56− n :: Word <- choose (1,9) >>= return . toEnum
57+ n <- choose (1,9)
57 (s', bytes) <- insert_escape_op ("p" ++ show n) [] s
58 (s', bytes) <- insert_escape_op ("p" ++ show n) [] s
58 return $ PushParamCap s' n bytes
59 return $ PushParamCap s' n bytes
59 ) `suchThat` ( \(PushParamCap str _ _) -> length str < 255 )
60 ) `suchThat` ( \(PushParamCap str _ _) -> length str < 255 )
60
61
61−data DecPrintCap = DecPrintCap String Word [Word8]
62+data DecPrintCap = DecPrintCap String Int [Word8]
62 deriving ( Show )
63 deriving ( Show )
63
64
64instance Arbitrary DecPrintCap where
65instance Arbitrary DecPrintCap where
65 arbitrary
66 arbitrary
66 = ( do
67 = ( do
67 NonParamCapString s <- arbitrary
68 NonParamCapString s <- arbitrary
68− n :: Word <- choose (1,9) >>= return . toEnum
69+ n <- choose (1,9)
69 (s', bytes) <- insert_escape_op ("p" ++ show n ++ "%d") [] s
70 (s', bytes) <- insert_escape_op ("p" ++ show n ++ "%d") [] s
70 return $ DecPrintCap s' n bytes
71 return $ DecPrintCap s' n bytes
71 ) `suchThat` ( \(DecPrintCap str _ _) -> length str < 255 )
72 ) `suchThat` ( \(DecPrintCap str _ _) -> length str < 255 )
@@ -87,15 +88,13 @@ is_bytes_op :: CapOp -> Bool
87is_bytes_op (Bytes {}) = True
88is_bytes_op (Bytes {}) = True
88-- is_bytes_op _ = False
89-- is_bytes_op _ = False
89
90
90−bytes_for_range cap offset c
91+bytes_for_range cap offset count
91− = take (fromEnum c)
92+ = Vector.toList $ Vector.take count $ Vector.drop offset $ cap_bytes cap
92− $ drop (fromEnum offset)
93− $ ( map ( toEnum . fromEnum ) $! source_string cap )
94
93
95collect_bytes :: CapExpression -> [Word8]
94collect_bytes :: CapExpression -> [Word8]
96collect_bytes e = concat [ bytes
95collect_bytes e = concat [ bytes
97− | Bytes offset c _ <- cap_ops e
96+ | Bytes offset count <- cap_ops e
98− , let bytes = bytes_for_range e offset c
97+ , let bytes = bytes_for_range e offset count
99 ]
98 ]
100
99
101
100
test/VerifyEvalTerminfoCaps.hs+8−6
@@ -2,7 +2,7 @@
2{-# LANGUAGE NamedFieldPuns #-}
2{-# LANGUAGE NamedFieldPuns #-}
3module VerifyEvalTerminfoCaps where
3module VerifyEvalTerminfoCaps where
4
4
5−import Blaze.ByteString.Builder.Internal.Write (runWrite)
5+import Blaze.ByteString.Builder.Internal.Write (runWrite, getBound)
6import Data.Terminfo.Eval
6import Data.Terminfo.Eval
7import Data.Terminfo.Parse
7import Data.Terminfo.Parse
8import Control.DeepSeq
8import Control.DeepSeq
@@ -20,6 +20,8 @@ import Control.Monad ( mapM_, forM, forM_ )
20import Data.Maybe ( fromJust )
20import Data.Maybe ( fromJust )
21import Data.Word
21import Data.Word
22
22
23+import Foreign.Marshal.Alloc (mallocBytes)
24+import Foreign.Ptr (Ptr, minusPtr)
23import Numeric
25import Numeric
24
26
25-- If a terminal defines one of the caps then it's expected to be parsable.
27-- If a terminal defines one of the caps then it's expected to be parsable.
@@ -60,8 +62,7 @@ tests = do
60 Just cap_def -> do
62 Just cap_def -> do
61 putStrLn $ "\tadding test for cap: " ++ cap_name
63 putStrLn $ "\tadding test for cap: " ++ cap_name
62 let test_name = term_name ++ "(" ++ cap_name ++ ")"
64 let test_name = term_name ++ "(" ++ cap_name ++ ")"
63− parse_result <- parse_cap_expression cap_def
65+ case parse_cap_expression cap_def of
64− case parse_result of
65 Left error -> return [verify test_name (failed {reason = "parse error " ++ show error})]
66 Left error -> return [verify test_name (failed {reason = "parse error " ++ show error})]
66 Right !cap_expr -> return [verify test_name (verify_eval_cap eval_buffer cap_expr)]
67 Right !cap_expr -> return [verify test_name (verify_eval_cap eval_buffer cap_expr)]
67 Nothing -> do
68 Nothing -> do
@@ -71,11 +72,12 @@ tests = do
71verify_eval_cap :: Ptr Word8 -> CapExpression -> Int -> Property
72verify_eval_cap :: Ptr Word8 -> CapExpression -> Int -> Property
72verify_eval_cap eval_buffer expr !junk_int = do
73verify_eval_cap eval_buffer expr !junk_int = do
73 forAll (vector 9) $ \input_values ->
74 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
75 in liftIOResult $ do
77 in liftIOResult $ do
76 let start_ptr :: Ptr Word8 = eval_buffer
78 let start_ptr :: Ptr Word8 = eval_buffer
77− forM_ [0..100] $ \i -> runWrite (write_cap_expr expr input_values) start_ptr
79+ forM_ [0..100] $ \i -> runWrite write start_ptr
78− end_ptr <- runWrite(write_cap_expr expr input_values) start_ptr
80+ end_ptr <- runWrite write start_ptr
79 case end_ptr `minusPtr` start_ptr of
81 case end_ptr `minusPtr` start_ptr of
80 count | count < 0 ->
82 count | count < 0 ->
81 return $ failed { reason = "End pointer before start pointer." }
83 return $ failed { reason = "End pointer before start pointer." }
test/VerifyParseTerminfoCaps.hs+15−25
@@ -48,7 +48,7 @@ tests = do
48 liftIO $ putStrLn case_name
48 liftIO $ putStrLn case_name
49 return $ case Terminfo.getCapability ti (Terminfo.tiGetStr cap_name) of
49 return $ case Terminfo.getCapability ti (Terminfo.tiGetStr cap_name) of
50 Just cap_def -> [verify (case_name ++ " -> " ++ show cap_def)
50 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)]
52 Nothing -> []
52 Nothing -> []
53 )
53 )
54 )
54 )
@@ -59,46 +59,36 @@ tests = do
59 , verify "parse cap string with %pN op" push_param_caps
59 , verify "parse cap string with %pN op" push_param_caps
60 ] ++ parse_tests
60 ] ++ parse_tests
61
61
62−verify_parse_cap cap_string on_parse = liftIOResult $ do
62+verify_parse_cap cap_string on_parse =
63− parse_result <- parse_cap_expression cap_string
63+ case parse_cap_expression cap_string of
64− case parse_result of
65− Left error -> return $ failed { reason = "parse error " ++ show error }
64+ Left error -> failed { reason = "parse error " ++ show error }
66− Right e -> on_parse e
65+ Right e -> on_parse e
67
66
68non_paramaterized_caps (NonParamCapString cap) = do
67non_paramaterized_caps (NonParamCapString cap) = do
69 verify_parse_cap cap $ \e ->
68 verify_parse_cap cap $ \e ->
70− let expected_count :: Word8 = toEnum $ length cap
71− expected_bytes = map (toEnum . fromEnum) cap
69+ let expected_bytes = map (toEnum . fromEnum) cap
72− out_bytes = bytes_for_range e 0 expected_count
70+ out_bytes = bytes_for_range e 0 (length cap)
73− in return $ verify_bytes_equal out_bytes expected_bytes
71+ in verify_bytes_equal out_bytes expected_bytes
74
72
75literal_percent_caps (LiteralPercentCap cap_string expected_bytes) = do
73literal_percent_caps (LiteralPercentCap cap_string expected_bytes) = do
76− verify_parse_cap cap_string $ \e ->
74+ verify_parse_cap cap_string $ \e -> verify_bytes_equal (collect_bytes e) expected_bytes
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
80
75
81inc_first_two_caps (IncFirstTwoCap cap_string expected_bytes) = do
76inc_first_two_caps (IncFirstTwoCap cap_string expected_bytes) = do
82− verify_parse_cap cap_string $ \e ->
77+ verify_parse_cap cap_string $ \e -> verify_bytes_equal (collect_bytes e) expected_bytes
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
86
78
87push_param_caps (PushParamCap cap_string expected_param_count expected_bytes) = do
79push_param_caps (PushParamCap cap_string expected_param_count expected_bytes) = do
88 verify_parse_cap cap_string $ \e ->
80 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
91 out_param_count = param_count e
82 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
93 then verify_bytes_equal out_bytes expected_bytes
84 then verify_bytes_equal out_bytes expected_bytes
94 else failed { reason = "out param count /= expected param count" }
85 else failed { reason = "out param count /= expected param count" }
95
86
96dec_print_param_caps (DecPrintCap cap_string expected_param_count expected_bytes) = do
87dec_print_param_caps (DecPrintCap cap_string expected_param_count expected_bytes) = do
97 verify_parse_cap cap_string $ \e ->
88 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
100 out_param_count = param_count e
90 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
102 then verify_bytes_equal out_bytes expected_bytes
92 then verify_bytes_equal out_bytes expected_bytes
103 else failed { reason = "out param count /= expected param count" }
93 else failed { reason = "out param count /= expected param count" }
104
94
@@ -106,6 +96,6 @@ print_cap ti cap_name = do
106 putStrLn $ cap_name ++ ": " ++ show (from_capname ti cap_name)
96 putStrLn $ cap_name ++ ": " ++ show (from_capname ti cap_name)
107
97
108print_expression ti cap_name = do
98print_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
110 putStrLn $ cap_name ++ ": " ++ show parse_result
100 putStrLn $ cap_name ++ ": " ++ show parse_result
111
101
test/vty-examples.cabal−2
@@ -33,7 +33,6 @@ executable vty-interactive-terminal-test
33 containers,
33 containers,
34 data-default >= 0.5.3,
34 data-default >= 0.5.3,
35 deepseq >= 1.1 && < 1.4,
35 deepseq >= 1.1 && < 1.4,
36− ghc-prim,
37 lens,
36 lens,
38 mtl >= 1.1.1.0 && < 2.2,
37 mtl >= 1.1.1.0 && < 2.2,
39 parallel >= 2.2 && < 3.3,
38 parallel >= 2.2 && < 3.3,
@@ -100,7 +99,6 @@ executable vty-benchmark
100 containers,
99 containers,
101 data-default >= 0.5.3,
100 data-default >= 0.5.3,
102 deepseq >= 1.1 && < 1.4,
101 deepseq >= 1.1 && < 1.4,
103− ghc-prim,
104 lens,
102 lens,
105 mtl >= 1.1.1.0 && < 2.2,
103 mtl >= 1.1.1.0 && < 2.2,
106 parallel >= 2.2 && < 3.3,
104 parallel >= 2.2 && < 3.3,
vty.cabal+1
@@ -269,6 +269,7 @@ test-suite verify-eval-terminfo-caps
269 QuickCheck >= 2.4,
269 QuickCheck >= 2.4,
270 random == 1.0.*,
270 random == 1.0.*,
271 base >= 4 && < 5,
271 base >= 4 && < 5,
272+ blaze-builder >= 0.3.3.2 && < 0.4,
272 bytestring,
273 bytestring,
273 containers,
274 containers,
274 deepseq >= 1.1 && < 1.4,
275 deepseq >= 1.1 && < 1.4,