Evgenii Akentev
·
2023-07-23
Main.hs
1{-# LANGUAGE OverloadedStrings #-}
2
3module Main (main) where
4
5import System.IO
6import System.IO.LineIndexedCursor
7import Data.Foldable
8
9import Test.Hspec
10
11main :: IO ()
12main = hspec $ do
13 let
14 mkCursor capacity = do
15 h <- openFile "test/testdata" ReadMode
16 c <- mkLineIndexedCursorWithCapacity h capacity
17 pure (h, c)
18
19 forM_ [0 :: Integer ..20] $ \capacity -> do
20 before (mkCursor capacity) . after (\(h, _) -> hClose h)
21 $ describe ("System.IO.LineIndexedCursor with list capacity " ++ show capacity) $ do
22
23 it "getCurrentLine works" $ \(_, c) -> do
24 l <- getCurrentLine c
25 l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
26
27 ln <- goToLine c 3
28 ln `shouldBe` 3
29
30 l' <- getCurrentLine c
31 l' `shouldBe` Just "Curabitur nec mi sit amet justo condimentum gravida."
32
33 l'' <- getCurrentLine c
34 l'' `shouldBe` Just "Pellentesque accumsan dolor at nisl pulvinar, ut bibendum diam egestas."
35
36 it "goToLine works" $ \(_, c) -> do
37 ln <- goToLine c 10
38 ln `shouldBe` 10
39
40 l <- getCurrentLine c
41 l `shouldBe` Just "Sed elementum velit sit amet orci mollis tincidunt."
42
43 it "goToLine is negative" $ \(_, c) -> do
44 ln <- goToLine c (-10)
45 ln `shouldBe` 0
46
47 l <- getCurrentLine c
48 l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
49
50 it "goToLine is too big" $ \(_, c) -> do
51 l <- getCurrentLine c
52 l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
53
54 ln <- goToLine c 30
55 ln `shouldBe` 20
56
57 l' <- getCurrentLine c
58 l' `shouldBe` Nothing
59
60 it "fullScan works" $ \(_, c) -> do
61 l <- getCurrentLine c
62 l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
63
64 doFullScan c
65
66 cln <- getCurrentLineNumber c
67 cln `shouldBe` 20
68
69 l' <- getCurrentLine c
70 l' `shouldBe` Nothing
71
72 s <- getCursorState c
73 s `shouldBe` [0,57,117,191,244,316,384,429,511,561,616,668,715,761,799,851,907,941,981,1024,1068]
74
75 it "read line, then go to the beginning and forth" $ \(_, c) -> do
76 cln <- getCurrentLineNumber c
77 cln `shouldBe` 0
78
79 l <- getCurrentLine c
80 l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
81
82 cln' <- getCurrentLineNumber c
83 cln' `shouldBe` 1
84
85 _ <- getCurrentLine c
86 _ <- getCurrentLine c
87 _ <- getCurrentLine c
88 _ <- getCurrentLine c
89 _ <- getCurrentLine c
90
91 cln'' <- getCurrentLineNumber c
92 cln'' `shouldBe` 6
93
94 ln <- goToLine c 0
95 ln `shouldBe` 0
96
97 cln''' <- getCurrentLineNumber c
98 cln''' `shouldBe` 0
99
100 l' <- getCurrentLine c
101 l' `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
102
103 ln' <- goToLine c 5
104 ln' `shouldBe` 5
105
106 l'' <- getCurrentLine c
107 l'' `shouldBe` Just "Curabitur nec dui posuere, tincidunt turpis vitae, tincidunt magna."
108
109 ln'' <- goToLine c 6
110 ln'' `shouldBe` 6
111
112 ln''' <- goToLine c 7
113 ln''' `shouldBe` 7
114
115 ln'''' <- goToLine c 10
116 ln'''' `shouldBe` 10
117
118 ln''''' <- goToLine c 3
119 ln''''' `shouldBe` 3
120
121 ln'''''' <- goToLine c 2
122 ln'''''' `shouldBe` 2
123
124 ln''''''' <- goToLine c 1
125 ln''''''' `shouldBe` 1
126
127 ln'''''''' <- goToLine c 0
128 ln'''''''' `shouldBe` 0