on ae06339 help sign in
vty/test/verify_debug_terminal.hs
115 lines 5.8 kB history raw
1{-# LANGUAGE ScopedTypeVariables #-}
2module Main where
4import Verify.Graphics.Vty.DisplayRegion
5import Verify.Graphics.Vty.Picture
6import Verify.Graphics.Vty.Image
7import Verify.Graphics.Vty.Span
8import Graphics.Vty.Terminal
9import Graphics.Vty.Terminal.Debug
11import Graphics.Vty.Debug
13import Verify
15import qualified Data.ByteString as BS
16import Data.IORef
17import qualified Data.String.UTF8 as UTF8
19import System.IO
21unit_image_unit_bounds :: UnitImage -> Property
22unit_image_unit_bounds (UnitImage _ i) = liftIOResult $ do
23 t <- terminal_instance (DisplayRegion 1 1)
24 d <- display_bounds t >>= display_context t
25 let pic = pic_for_image i
26 output_picture d pic
27 return succeeded
29unit_image_arb_bounds :: UnitImage -> DebugWindow -> Property
30unit_image_arb_bounds (UnitImage _ i) (DebugWindow w h) = liftIOResult $ do
31 t <- terminal_instance (DisplayRegion w h)
32 d <- display_bounds t >>= display_context t
33 let pic = pic_for_image i
34 output_picture d pic
35 return succeeded
37single_T_row :: DebugWindow -> Property
38single_T_row (DebugWindow w h) = liftIOResult $ do
39 t <- terminal_instance (DisplayRegion w h)
40 d <- display_bounds t >>= display_context t
41 -- create an image that contains just the character T repeated for a single row
42 let i = horiz_cat $ replicate (fromEnum w) (char def_attr 'T')
43 pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
44 output_picture d pic
45 out_bytes <- readIORef (debug_terminal_last_output $ dehandle t) >>= return . UTF8.toRep
46 -- The UTF8 string that represents the output bytes a single line containing the T string:
47 let expected = "HD" ++ "MA" ++ replicate (fromEnum w) 'T'
48 -- Followed by h - 1 lines of a change to the background attribute and then the background
49 -- character
50 ++ concat (replicate (fromEnum h - 1) $ "MA" ++ replicate (fromEnum w) 'B')
51 expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
52 if out_bytes /= expected_bytes
53 then return $ failed $ result { reason = "\n" ++ show out_bytes ++ "\n\n" ++ show expected_bytes }
54 else return succeeded
56many_T_rows :: DebugWindow -> Property
57many_T_rows (DebugWindow w h) = liftIOResult $ do
58 t <- terminal_instance (DisplayRegion w h)
59 d <- display_bounds t >>= display_context t
60 -- create an image that contains the character 'T' repeated for all the rows
61 let i = vert_cat $ replicate (fromEnum h) $ horiz_cat $ replicate (fromEnum w) (char def_attr 'T')
62 pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
63 output_picture d pic
64 out_bytes <- readIORef (debug_terminal_last_output $ dehandle t) >>= return . UTF8.toRep
65 -- The UTF8 string that represents the output bytes is h repeats of a move, 'M', followed by an
66 -- attribute change. 'A', followed by w 'T's
67 let expected = "HD" ++ concat (replicate (fromEnum h) $ "MA" ++ replicate (fromEnum w) 'T')
68 expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
69 if out_bytes /= expected_bytes
70 then return $ failed $ result { reason = "\n" ++ show out_bytes ++ "\n\n" ++ show expected_bytes }
71 else return succeeded
73many_T_rows_cropped_width :: DebugWindow -> Property
74many_T_rows_cropped_width (DebugWindow w h) = liftIOResult $ do
75 t <- terminal_instance (DisplayRegion w h)
76 d <- display_bounds t >>= display_context t
77 -- create an image that contains the character 'T' repeated for all the rows
78 let i = vert_cat $ replicate (fromEnum h) $ horiz_cat $ replicate (fromEnum w * 2) (char def_attr 'T')
79 pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
80 output_picture d pic
81 out_bytes <- readIORef (debug_terminal_last_output $ dehandle t) >>= return . UTF8.toRep
82 -- The UTF8 string that represents the output bytes is h repeats of a move, 'M', followed by an
83 -- attribute change. 'A', followed by w 'T's
84 let expected = "HD" ++ concat (replicate (fromEnum h) $ "MA" ++ replicate (fromEnum w) 'T')
85 expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
86 if out_bytes /= expected_bytes
87 then return $ failed $ result { reason = "\n" ++ show out_bytes ++ "\n\n" ++ show expected_bytes }
88 else return succeeded
90many_T_rows_cropped_height :: DebugWindow -> Property
91many_T_rows_cropped_height (DebugWindow w h) = liftIOResult $ do
92 t <- terminal_instance (DisplayRegion w h)
93 d <- display_bounds t >>= display_context t
94 -- create an image that contains the character 'T' repeated for all the rows
95 let i = vert_cat $ replicate (fromEnum h * 2) $ horiz_cat $ replicate (fromEnum w) (char def_attr 'T')
96 pic = (pic_for_image i) { pic_background = Background 'B' def_attr }
97 output_picture d pic
98 out_bytes <- readIORef (debug_terminal_last_output $ dehandle t) >>= return . UTF8.toRep
99 -- The UTF8 string that represents the output bytes is h repeats of a move, 'M', followed by an
100 -- attribute change. 'A', followed by w count 'T's
101 let expected = "HD" ++ concat (replicate (fromEnum h) $ "MA" ++ replicate (fromEnum w) 'T')
102 expected_bytes :: BS.ByteString = UTF8.toRep $ UTF8.fromString expected
103 if out_bytes /= expected_bytes
104 then return $ failed $ result { reason = "\n" ++ show out_bytes ++ "\n\n" ++ show expected_bytes }
105 else return succeeded
107main = run_test $ do
108 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 return ()