on master help sign in
assure VerifyTerminal can actually work as a regression test for issue #42
commit838dd51d734b79217ca14557503e302bb02bd15e authorCorey O'Connor <corey@ooyala.com> dateWed 14 Aug 2013, 22:32 (13 years ago) parentada920d
5 files changed+185−149
test/VerifyImageOps.hs+15 test/VerifyTerminal.hs+51−102 test/VerifyTerminfoBased.hs−43 test/VerifyUsingMockTerminal.hs+115 vty.cabal+4−4
test/VerifyImageOps.hs+15
@@ -7,6 +7,8 @@ import Verify.Graphics.Vty.Image
77
88import Verify
99
10+import Control.DeepSeq
11+
1012two_sw_horiz_concat :: SingleColumnChar -> SingleColumnChar -> Bool
1113two_sw_horiz_concat (SingleColumnChar c1) (SingleColumnChar c2) =
1214 image_width (char def_attr c1 <|> char def_attr c2) == 2
@@ -135,6 +137,16 @@ crop_bottom_limits_height :: Image -> Int -> Property
135137crop_bottom_limits_height i v = v >= 0 ==>
136138 v >= image_height (crop_bottom v i)
137139
140+-- rediculous tests just to satisfy my desire for nice code coverage :-P
141+can_show_image :: Image -> Bool
142+can_show_image i = length (show i) > 0
143+
144+can_rnf_image :: Image -> Bool
145+can_rnf_image i = rnf i == ()
146+
147+can_pp_image :: Image -> Bool
148+can_pp_image i = length (pp_image_structure i) > 0
149+
138150tests :: IO [Test]
139151tests = return
140152 [ verify "two_sw_horiz_concat" two_sw_horiz_concat
@@ -156,5 +168,8 @@ tests = return
156168 , verify "crop right limits width" crop_right_limits_width
157169 , verify "crop top limits height" crop_top_limits_height
158170 , verify "crop bottom limits height" crop_bottom_limits_height
171+ , verify "can show image" can_show_image
172+ , verify "can rnf image" can_rnf_image
173+ , verify "can pp image" can_pp_image
159174 ]
160175
test/VerifyTerminal.hs+51−102
@@ -1,115 +1,64 @@
11{-# LANGUAGE ScopedTypeVariables #-}
2+{- We setup the environment to envoke certain terminals of interest.
3+ - This assumes appropriate definitions exist in the current environment for the terminals of
4+ - interest.
5+ -}
26module VerifyTerminal where
3−
4−import Verify.Graphics.Vty.DisplayRegion
5−import Verify.Graphics.Vty.Picture
6−import Verify.Graphics.Vty.Image
7−import Verify.Graphics.Vty.Span
8−import Graphics.Vty.Terminal
9−import Graphics.Vty.Terminal.Mock
10−
11−import Graphics.Vty.Debug
12−
137import Verify
148
15−import qualified Data.ByteString as BS
16−import Data.IORef
17−import qualified Data.String.UTF8 as UTF8
9+import Graphics.Vty
1810
19−import System.IO
11+import Verify.Graphics.Vty.Image
12+import Verify.Graphics.Vty.Terminal
2013
21−compare_bytes out_bytes expected_bytes =
22− if out_bytes /= expected_bytes
23− then return $ failed { reason = "bytes\n" ++ show out_bytes
24− ++ "\nare not the expected bytes\n"
25− ++ show expected_bytes
26− }
27− else return succeeded
14+import Control.Monad
2815
29−unit_image_unit_bounds :: UnitImage -> Property
30−unit_image_unit_bounds (UnitImage _ i) = liftIOResult $ do
31− (_,t) <- mock_terminal (DisplayRegion 1 1)
32− dc <- display_bounds t >>= display_context t
33− let pic = pic_for_image i
34− output_picture dc pic
35− return succeeded
16+import qualified System.Console.Terminfo as Terminfo
17+import System.Posix.Env
18+import System.IO
3619
37−unit_image_arb_bounds :: UnitImage -> MockWindow -> Property
38−unit_image_arb_bounds (UnitImage _ i) (MockWindow w h) = liftIOResult $ do
39− (_,t) <- mock_terminal (DisplayRegion w h)
40− dc <- display_bounds t >>= display_context t
41− let pic = pic_for_image i
42− output_picture dc pic
43− return succeeded
20+tests :: IO [Test]
21+tests = concat <$> forM terminals_of_interest (\term_name -> do
22+ -- check if that terminfo exists
23+ putStrLn $ "testing end to end for terminal: " ++ term_name
24+ mti <- try $ Terminfo.setupTerm term_name
25+ case mti of
26+ Left (_ :: SomeException) -> return []
27+ Right _ -> return [ verify ("verify " ++ term_name ++ " could output a picture")
28+ (smoke_test_term_non_mac term_name)
29+ -- this is excessive.
30+ , verify ("verify " ++ term_name ++ " could output a picture on a Mac.")
31+ (smoke_test_term_mac term_name)
32+ ]
33+ )
4434
45−single_T_row :: MockWindow -> Property
46−single_T_row (MockWindow w h) = liftIOResult $ do
47− (mock_data,t) <- mock_terminal (DisplayRegion w h)
48− dc <- display_bounds t >>= display_context t
49− -- create an image that contains just the character T repeated for a single row
50− let i = horiz_cat $ replicate (fromEnum w) (char def_attr 'T')
51− pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
52− output_picture dc pic
53− out_bytes <- readIORef mock_data >>= return . UTF8.toRep
54− -- The UTF8 string that represents the output bytes a single line containing the T string:
55− let expected = "HD" ++ "MA" ++ replicate (fromEnum w) 'T'
56− -- Followed by h - 1 lines of a change to the background attribute and then the background
57− -- character
58− ++ concat (replicate (fromEnum h - 1) $ "MA" ++ replicate (fromEnum w) 'B')
59− expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
60− compare_bytes out_bytes expected_bytes
61−
62−many_T_rows :: MockWindow -> Property
63−many_T_rows (MockWindow w h) = liftIOResult $ do
64− (mock_data, t) <- mock_terminal (DisplayRegion w h)
65− dc <- display_bounds t >>= display_context t
66− -- create an image that contains the character 'T' repeated for all the rows
67− let i = vert_cat $ replicate (fromEnum h) $ horiz_cat $ replicate (fromEnum w) (char def_attr 'T')
68− pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
69− output_picture dc pic
70− out_bytes <- readIORef mock_data >>= return . UTF8.toRep
71− -- The UTF8 string that represents the output bytes is h repeats of a move, 'M', followed by an
72− -- attribute change. 'A', followed by w 'T's
73− let expected = "HD" ++ concat (replicate (fromEnum h) $ "MA" ++ replicate (fromEnum w) 'T')
74− expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
75− compare_bytes out_bytes expected_bytes
35+smoke_test_term_non_mac :: String -> Image -> Property
36+smoke_test_term_non_mac term_name i = liftIOResult $ do
37+ -- unset the TERM_PROGRAM environment variable if set.
38+ -- Required to execute regression test for #42 on a mac
39+ unsetEnv "TERM_PROGRAM"
40+ smoke_test_term term_name i
7641
77−many_T_rows_cropped_width :: MockWindow -> Property
78−many_T_rows_cropped_width (MockWindow w h) = liftIOResult $ do
79− (mock_data,t) <- mock_terminal (DisplayRegion w h)
80− dc <- display_bounds t >>= display_context t
81− -- create an image that contains the character 'T' repeated for all the rows
82− let i = vert_cat $ replicate (fromEnum h) $ horiz_cat $ replicate (fromEnum w * 2) (char def_attr 'T')
83− pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
84− output_picture dc pic
85− out_bytes <- readIORef mock_data >>= return . UTF8.toRep
86− -- The UTF8 string that represents the output bytes is h repeats of a move, 'M', followed by an
87− -- attribute change. 'A', followed by w 'T's
88− let expected = "HD" ++ concat (replicate (fromEnum h) $ "MA" ++ replicate (fromEnum w) 'T')
89− expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
90− compare_bytes out_bytes expected_bytes
42+smoke_test_term_mac :: String -> Image -> Property
43+smoke_test_term_mac term_name i = liftIOResult $ do
44+ setEnv "TERM_PROGRAM" "Apple_Terminal" True
45+ smoke_test_term term_name i
9146
92−many_T_rows_cropped_height :: MockWindow -> Property
93−many_T_rows_cropped_height (MockWindow w h) = liftIOResult $ do
94− (mock_data,t) <- mock_terminal (DisplayRegion w h)
95− dc <- display_bounds t >>= display_context t
96− -- create an image that contains the character 'T' repeated for all the rows
97− let i = vert_cat $ replicate (fromEnum h * 2) $ horiz_cat $ replicate (fromEnum w) (char def_attr 'T')
98− pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
47+smoke_test_term :: String -> Image -> IO Result
48+smoke_test_term term_name i = do
49+ null_out <- openFile "/dev/null" WriteMode
50+ t <- terminal_with_name_and_io term_name null_out
51+ putStrLn $ "context color count: " ++ show (context_color_count t)
52+ reserve_display t
53+ dc <- display_context t (DisplayRegion 100 100)
54+ -- always show the cursor to produce tests for terminals with no cursor support.
55+ let pic = (pic_for_image i) { pic_cursor = Cursor 0 0 }
9956 output_picture dc pic
100− out_bytes <- readIORef mock_data >>= return . UTF8.toRep
101− -- The UTF8 string that represents the output bytes is h repeats of a move, 'M', followed by an
102− -- attribute change. 'A', followed by w count 'T's
103− let expected = "HD" ++ concat (replicate (fromEnum h) $ "MA" ++ replicate (fromEnum w) 'T')
104− expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
105− compare_bytes out_bytes expected_bytes
106−
107−tests :: IO [Test]
108−tests = return [ verify "unit_image_unit_bounds" unit_image_unit_bounds
109− , verify "unit_image_arb_bounds" unit_image_arb_bounds
110− , verify "single_T_row" single_T_row
111− , verify "many_T_rows" many_T_rows
112− , verify "many_T_rows_cropped_width" many_T_rows_cropped_width
113− , verify "many_T_rows_cropped_height" many_T_rows_cropped_height
114− ]
57+ set_cursor_pos t 0 0
58+ when (supports_cursor_visibility t) $ do
59+ hide_cursor t
60+ show_cursor t
61+ release_display t
62+ release_terminal t
63+ return succeeded
11564
test/VerifyTerminfoBased.hsdeleted−43
@@ -1,43 +0,0 @@
1−{-# LANGUAGE ScopedTypeVariables #-}
2−{- We setup the environment to envoke certain terminals of interest.
3− - This assumes appropriate definitions exist in the current environment for the terminals of
4− - interest.
5− -}
6−module VerifyTerminfoBased where
7−import Verify
8−
9−import Graphics.Vty
10−import Graphics.Vty.Terminal.TerminfoBased as TerminfoBased
11−
12−import Verify.Graphics.Vty.Image
13−import Verify.Graphics.Vty.Terminal
14−
15−import qualified System.Console.Terminfo as Terminfo
16−import System.IO
17−
18−tests :: IO [Test]
19−tests = concat <$> forM terminals_of_interest (\term_name -> do
20− -- check if that terminfo exists
21− putStrLn $ "testing end to end for terminal: " ++ term_name
22− mti <- try $ Terminfo.setupTerm term_name
23− case mti of
24− Left (_ :: SomeException) -> return []
25− Right _ -> return [ verify ("verify " ++ term_name ++ " could output a picture")
26− (smoke_test_term term_name)
27− ]
28− )
29−
30−smoke_test_term :: String -> Image -> Property
31−smoke_test_term term_name i = liftIOResult $ do
32− null_out <- openFile "/dev/null" WriteMode
33− t <- TerminfoBased.reserve_terminal term_name null_out
34− putStrLn $ "context color count: " ++ show (context_color_count t)
35− reserve_display t
36− dc <- display_context t (DisplayRegion 100 100)
37− -- always show the cursor to produce tests for terminals with no cursor support.
38− let pic = (pic_for_image i) { pic_cursor = Cursor 0 0 }
39− output_picture dc pic
40− release_display t
41− release_terminal t
42− return succeeded
43−
test/VerifyUsingMockTerminal.hsnew file+115
@@ -0,0 +1,115 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+module VerifyUsingMockTerminal where
3+
4+import Verify.Graphics.Vty.DisplayRegion
5+import Verify.Graphics.Vty.Picture
6+import Verify.Graphics.Vty.Image
7+import Verify.Graphics.Vty.Span
8+import Graphics.Vty.Terminal
9+import Graphics.Vty.Terminal.Mock
10+
11+import Graphics.Vty.Debug
12+
13+import Verify
14+
15+import qualified Data.ByteString as BS
16+import Data.IORef
17+import qualified Data.String.UTF8 as UTF8
18+
19+import System.IO
20+
21+compare_bytes out_bytes expected_bytes =
22+ if out_bytes /= expected_bytes
23+ then return $ failed { reason = "bytes\n" ++ show out_bytes
24+ ++ "\nare not the expected bytes\n"
25+ ++ show expected_bytes
26+ }
27+ else return succeeded
28+
29+unit_image_unit_bounds :: UnitImage -> Property
30+unit_image_unit_bounds (UnitImage _ i) = liftIOResult $ do
31+ (_,t) <- mock_terminal (DisplayRegion 1 1)
32+ dc <- display_bounds t >>= display_context t
33+ let pic = pic_for_image i
34+ output_picture dc pic
35+ return succeeded
36+
37+unit_image_arb_bounds :: UnitImage -> MockWindow -> Property
38+unit_image_arb_bounds (UnitImage _ i) (MockWindow w h) = liftIOResult $ do
39+ (_,t) <- mock_terminal (DisplayRegion w h)
40+ dc <- display_bounds t >>= display_context t
41+ let pic = pic_for_image i
42+ output_picture dc pic
43+ return succeeded
44+
45+single_T_row :: MockWindow -> Property
46+single_T_row (MockWindow w h) = liftIOResult $ do
47+ (mock_data,t) <- mock_terminal (DisplayRegion w h)
48+ dc <- display_bounds t >>= display_context t
49+ -- create an image that contains just the character T repeated for a single row
50+ let i = horiz_cat $ replicate (fromEnum w) (char def_attr 'T')
51+ pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
52+ output_picture dc pic
53+ out_bytes <- readIORef mock_data >>= return . UTF8.toRep
54+ -- The UTF8 string that represents the output bytes a single line containing the T string:
55+ let expected = "HD" ++ "MA" ++ replicate (fromEnum w) 'T'
56+ -- Followed by h - 1 lines of a change to the background attribute and then the background
57+ -- character
58+ ++ concat (replicate (fromEnum h - 1) $ "MA" ++ replicate (fromEnum w) 'B')
59+ expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
60+ compare_bytes out_bytes expected_bytes
61+
62+many_T_rows :: MockWindow -> Property
63+many_T_rows (MockWindow w h) = liftIOResult $ do
64+ (mock_data, t) <- mock_terminal (DisplayRegion w h)
65+ dc <- display_bounds t >>= display_context t
66+ -- create an image that contains the character 'T' repeated for all the rows
67+ let i = vert_cat $ replicate (fromEnum h) $ horiz_cat $ replicate (fromEnum w) (char def_attr 'T')
68+ pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
69+ output_picture dc pic
70+ out_bytes <- readIORef mock_data >>= return . UTF8.toRep
71+ -- The UTF8 string that represents the output bytes is h repeats of a move, 'M', followed by an
72+ -- attribute change. 'A', followed by w 'T's
73+ let expected = "HD" ++ concat (replicate (fromEnum h) $ "MA" ++ replicate (fromEnum w) 'T')
74+ expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
75+ compare_bytes out_bytes expected_bytes
76+
77+many_T_rows_cropped_width :: MockWindow -> Property
78+many_T_rows_cropped_width (MockWindow w h) = liftIOResult $ do
79+ (mock_data,t) <- mock_terminal (DisplayRegion w h)
80+ dc <- display_bounds t >>= display_context t
81+ -- create an image that contains the character 'T' repeated for all the rows
82+ let i = vert_cat $ replicate (fromEnum h) $ horiz_cat $ replicate (fromEnum w * 2) (char def_attr 'T')
83+ pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
84+ output_picture dc pic
85+ out_bytes <- readIORef mock_data >>= return . UTF8.toRep
86+ -- The UTF8 string that represents the output bytes is h repeats of a move, 'M', followed by an
87+ -- attribute change. 'A', followed by w 'T's
88+ let expected = "HD" ++ concat (replicate (fromEnum h) $ "MA" ++ replicate (fromEnum w) 'T')
89+ expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
90+ compare_bytes out_bytes expected_bytes
91+
92+many_T_rows_cropped_height :: MockWindow -> Property
93+many_T_rows_cropped_height (MockWindow w h) = liftIOResult $ do
94+ (mock_data,t) <- mock_terminal (DisplayRegion w h)
95+ dc <- display_bounds t >>= display_context t
96+ -- create an image that contains the character 'T' repeated for all the rows
97+ let i = vert_cat $ replicate (fromEnum h * 2) $ horiz_cat $ replicate (fromEnum w) (char def_attr 'T')
98+ pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
99+ output_picture dc pic
100+ out_bytes <- readIORef mock_data >>= return . UTF8.toRep
101+ -- The UTF8 string that represents the output bytes is h repeats of a move, 'M', followed by an
102+ -- attribute change. 'A', followed by w count 'T's
103+ let expected = "HD" ++ concat (replicate (fromEnum h) $ "MA" ++ replicate (fromEnum w) 'T')
104+ expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
105+ compare_bytes out_bytes expected_bytes
106+
107+tests :: IO [Test]
108+tests = return [ verify "unit_image_unit_bounds" unit_image_unit_bounds
109+ , verify "unit_image_arb_bounds" unit_image_arb_bounds
110+ , verify "single_T_row" single_T_row
111+ , verify "many_T_rows" many_T_rows
112+ , verify "many_T_rows_cropped_width" many_T_rows_cropped_width
113+ , verify "many_T_rows_cropped_height" many_T_rows_cropped_height
114+ ]
115+
vty.cabal+4−4
@@ -120,14 +120,14 @@ test-suite verify-attribute-ops
120120 vector >= 0.7
121121
122122
123−test-suite verify-terminal
123+test-suite verify-using-mock-terminal
124124 default-language: Haskell2010
125125
126126 type: detailed-0.9
127127
128128 hs-source-dirs: test
129129
130− test-module: VerifyTerminal
130+ test-module: VerifyUsingMockTerminal
131131
132132 other-modules: Verify
133133 Verify.Graphics.Vty.Attributes
@@ -155,14 +155,14 @@ test-suite verify-terminal
155155 utf8-string >= 0.3 && < 0.4,
156156 vector >= 0.7
157157
158−test-suite verify-terminfo-based
158+test-suite verify-terminal
159159 default-language: Haskell2010
160160
161161 type: detailed-0.9
162162
163163 hs-source-dirs: test
164164
165− test-module: VerifyTerminfoBased
165+ test-module: VerifyTerminal
166166
167167 other-modules: Verify
168168 Verify.Graphics.Vty.Attributes