on 5.19 help sign in
vty/test/VerifyUsingMockTerminal.hs
108 lines 4.6 kB history raw
1module VerifyUsingMockTerminal where
3import Verify.Graphics.Vty.Prelude
5import Verify.Graphics.Vty.Picture
6import Verify.Graphics.Vty.Image
7import Verify.Graphics.Vty.Attributes
8import Verify.Graphics.Vty.Span
9import Verify.Graphics.Vty.Output
10import Graphics.Vty.Output
11import Graphics.Vty.Output.Interface
12import Graphics.Vty.Output.Mock
14import Graphics.Vty.Debug
16import Verify
18import qualified Data.ByteString as BS
19import Data.IORef
20import qualified Data.String.UTF8 as UTF8
22import System.IO
24unitImageUnitBounds :: UnitImage -> Property
25unitImageUnitBounds (UnitImage _ i) = liftIOResult $ do
26 (_,t) <- mockTerminal (1,1)
27 dc <- displayBounds t >>= displayContext t
28 let pic = picForImage i
29 outputPicture dc pic
30 return succeeded
32unitImageArbBounds :: UnitImage -> MockWindow -> Property
33unitImageArbBounds (UnitImage _ i) (MockWindow w h) = liftIOResult $ do
34 (_,t) <- mockTerminal (w,h)
35 dc <- displayBounds t >>= displayContext t
36 let pic = picForImage i
37 outputPicture dc pic
38 return succeeded
40singleTRow :: MockWindow -> Property
41singleTRow (MockWindow w h) = liftIOResult $ do
42 (mockData,t) <- mockTerminal (w,h)
43 dc <- displayBounds t >>= displayContext t
44 -- create an image that contains just the character T repeated for a
45 -- single row
46 let i = horizCat $ replicate (fromEnum w) (char defAttr 'T')
47 pic = (picForImage i) { picBackground = Background 'B' defAttr }
48 outputPicture dc pic
49 -- The mock output string that represents the output bytes a single
50 -- line containing the T string: Followed by h - 1 lines of a change
51 -- to the background attribute and then the background character
52 let expected = "H" ++ "MDA" ++ replicate (fromEnum w) 'T'
53 ++ concat (replicate (fromEnum h - 1) $ "MDA" ++ replicate (fromEnum w) 'B')
54 compareMockOutput mockData expected
56manyTRows :: MockWindow -> Property
57manyTRows (MockWindow w h) = liftIOResult $ do
58 (mockData, t) <- mockTerminal (w,h)
59 dc <- displayBounds t >>= displayContext t
60 -- create an image that contains the character 'T' repeated for all
61 -- the rows
62 let i = vertCat $ replicate (fromEnum h) $ horizCat $ replicate (fromEnum w) (char defAttr 'T')
63 pic = (picForImage i) { picBackground = Background 'B' defAttr }
64 outputPicture dc pic
65 -- The UTF8 string that represents the output bytes is h repeats of
66 -- a move, 'M', followed by an attribute change. 'A', followed by w
67 -- 'T's
68 let expected = "H" ++ concat (replicate (fromEnum h) $ "MDA" ++ replicate (fromEnum w) 'T')
69 compareMockOutput mockData expected
71manyTRowsCroppedWidth :: MockWindow -> Property
72manyTRowsCroppedWidth (MockWindow w h) = liftIOResult $ do
73 (mockData,t) <- mockTerminal (w,h)
74 dc <- displayBounds t >>= displayContext t
75 -- create an image that contains the character 'T' repeated for all
76 -- the rows
77 let i = vertCat $ replicate (fromEnum h) $ horizCat $ replicate (fromEnum w * 2) (char defAttr 'T')
78 pic = (picForImage i) { picBackground = Background 'B' defAttr }
79 outputPicture dc pic
80 -- The UTF8 string that represents the output bytes is h repeats of
81 -- a move, 'M', followed by an attribute change. 'A', followed by w
82 -- 'T's
83 let expected = "H" ++ concat (replicate (fromEnum h) $ "MDA" ++ replicate (fromEnum w) 'T')
84 compareMockOutput mockData expected
86manyTRowsCroppedHeight :: MockWindow -> Property
87manyTRowsCroppedHeight (MockWindow w h) = liftIOResult $ do
88 (mockData,t) <- mockTerminal (w,h)
89 dc <- displayBounds t >>= displayContext t
90 -- create an image that contains the character 'T' repeated for all
91 -- the rows
92 let i = vertCat $ replicate (fromEnum h * 2) $ horizCat $ replicate (fromEnum w) (char defAttr 'T')
93 pic = (picForImage i) { picBackground = Background 'B' defAttr }
94 outputPicture dc pic
95 -- The UTF8 string that represents the output bytes is h repeats of
96 -- a move, 'M', followed by an attribute change. 'A', followed by w
97 -- count 'T's
98 let expected = "H" ++ concat (replicate (fromEnum h) $ "MDA" ++ replicate (fromEnum w) 'T')
99 compareMockOutput mockData expected
101tests :: IO [Test]
102tests = return [ verify "unitImageUnitBounds" unitImageUnitBounds
103 , verify "unitImageArbBounds" unitImageArbBounds
104 , verify "singleTRow" singleTRow
105 , verify "manyTRows" manyTRows
106 , verify "manyTRowsCroppedWidth" manyTRowsCroppedWidth
107 , verify "manyTRowsCroppedHeight" manyTRowsCroppedHeight
108 ]