git.iko.soy/typeable/pandoc
on rc/test4 help sign in
114 lines 4.5 kB history raw
1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE OverloadedStrings #-}
3{- |
4 Module : Tests.Shared
5 Copyright : © 2006-2020 John MacFarlane
6 License : GNU GPL, version 2 or above
8 Maintainer : John MacFarlane <jgm@berkeley@edu>
9 Stability : alpha
10 Portability : portable
12Tests for functions used in many parts of the library.
13-}
14module Tests.Shared (tests) where
16import Prelude
17import System.FilePath.Posix (joinPath)
18import Test.Tasty
19import Test.Tasty.HUnit (assertBool, testCase, (@?=))
20import Text.Pandoc.Arbitrary ()
21import Text.Pandoc.Builder
22import Text.Pandoc.Shared
23import Text.Pandoc.Writers.Shared (toLegacyTable)
25tests :: [TestTree]
26tests = [ testGroup "compactifyDL"
27 [ testCase "compactifyDL with empty def" $
28 assertBool "compactifyDL"
29 (let x = [(str "word", [para (str "def"), mempty])]
30 in compactifyDL x == x)
31 ]
32 , testGroup "collapseFilePath" testCollapse
33 , testGroup "toLegacyTable" testLegacyTable
34 ]
36testCollapse :: [TestTree]
37testCollapse = map (testCase "collapse")
38 [ collapseFilePath (joinPath [ ""]) @?= joinPath [ ""]
39 , collapseFilePath (joinPath [ ".","foo"]) @?= joinPath [ "foo"]
40 , collapseFilePath (joinPath [ ".",".","..","foo"]) @?= joinPath [ joinPath ["..", "foo"]]
41 , collapseFilePath (joinPath [ "..","foo"]) @?= joinPath [ "..","foo"]
42 , collapseFilePath (joinPath [ "","bar","..","baz"]) @?= joinPath [ "","baz"]
43 , collapseFilePath (joinPath [ "","..","baz"]) @?= joinPath [ "","..","baz"]
44 , collapseFilePath (joinPath [ ".","foo","..",".","bar","..",".",".","baz"]) @?= joinPath [ "baz"]
45 , collapseFilePath (joinPath [ ".",""]) @?= joinPath [ ""]
46 , collapseFilePath (joinPath [ ".",".",""]) @?= joinPath [ ""]
47 , collapseFilePath (joinPath [ "..",""]) @?= joinPath [ ".."]
48 , collapseFilePath (joinPath [ "..",".",""]) @?= joinPath [ ".."]
49 , collapseFilePath (joinPath [ ".","..",""]) @?= joinPath [ ".."]
50 , collapseFilePath (joinPath [ "..","..",""]) @?= joinPath [ "..",".."]
51 , collapseFilePath (joinPath [ "parent","foo","baz","..","bar"]) @?= joinPath [ "parent","foo","bar"]
52 , collapseFilePath (joinPath [ "parent","foo","baz","..","..","bar"]) @?= joinPath [ "parent","bar"]
53 , collapseFilePath (joinPath [ "parent","foo",".."]) @?= joinPath [ "parent"]
54 , collapseFilePath (joinPath [ "","parent","foo","..","..","bar"]) @?= joinPath [ "","bar"]
55 , collapseFilePath (joinPath [ "",".","parent","foo"]) @?= joinPath [ "","parent","foo"]]
57testLegacyTable :: [TestTree]
58testLegacyTable =
59 [ testCase "decomposes a table with head" $ gen1 @?= expect1
60 , testCase "decomposes a table without head" $ gen2 @?= expect2
61 ]
62 where
63 pln = toList . plain . str
64 cl a h w = Cell ("", [], []) AlignDefault h w $ pln a
65 rws = map $ Row nullAttr
66 th = TableHead nullAttr . rws
67 tb n x y = TableBody nullAttr n (rws x) (rws y)
68 tf = TableFoot nullAttr . rws
70 headRows1 =
71 [[cl "a" 1 1, cl "b" 2 2]
72 ,[cl "c" 1 1]
73 ]
74 body1 = tb 1
75 [[cl "e" 3 1,cl "f" 3 2]
76 ,[]
77 ,[]
78 ]
79 [[emptyCell,emptyCell,emptyCell]
80 ,[cl "g" 1 1,emptyCell,emptyCell]
81 ]
82 footRows1 =
83 [[cl "h" 1 2,cl "i" 2 1]
84 ,[cl "j" 1 2]]
85 caption1 = simpleCaption $ plain "caption"
86 spec1 = replicate 2 (AlignDefault, ColWidth 0.5) ++ [(AlignRight, ColWidthDefault)]
87 expect1 = ( [Str "caption"]
88 , replicate 2 AlignDefault ++ [AlignRight]
89 , replicate 2 0.5 ++ [0]
90 , [pln "a", pln "b", mempty]
91 , [[pln "c", mempty, mempty]
92 ,[pln "e", pln "f", mempty]
93 ,[mempty, mempty, mempty]
94 ,[mempty, mempty, mempty]
95 ,[mempty, mempty, mempty]
96 ,[pln "g", mempty, mempty]
97 ,[pln "h", mempty, pln "i"]
98 ,[pln "j", mempty, mempty]]
99 )
100 gen1 = toLegacyTable caption1 spec1 (th headRows1) [body1] (tf footRows1)
102 expect2 = ( []
103 , replicate 2 AlignDefault ++ [AlignRight]
104 , replicate 2 0.5 ++ [0]
105 , []
106 , [[pln "e", pln "f", mempty]
107 ,[mempty, mempty, mempty]
108 ,[mempty, mempty, mempty]
109 ,[mempty, mempty, mempty]
110 ,[pln "g", mempty, mempty]
111 ,[pln "h", mempty, pln "i"]
112 ,[pln "j", mempty, mempty]]
113 )
114 gen2 = toLegacyTable emptyCaption spec1 (th []) [body1] (tf footRows1)