@@ -1,13 +1,42 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Tests.Readers.Org.Directive (tests) where
33
4+import Control.Arrow (second)
5+import Data.Time (UTCTime (UTCTime), secondsToDiffTime)
6+import Data.Time.Calendar (Day (ModifiedJulianDay))
47import Test.Tasty (TestTree, testGroup)
5−import Tests.Helpers ((=?>))
8+import Tests.Helpers ((=?>), ToString, purely, test)
69import Tests.Readers.Org.Shared ((=:), tagSpan)
710import Text.Pandoc
811import Text.Pandoc.Builder
12+import qualified Data.ByteString as BS
13+import qualified Data.Map as Map
914import qualified Data.Text as T
1015
16+testWithFiles :: (ToString c)
17+ => [(FilePath, BS.ByteString)]
18+ -> String -- ^ name of test case
19+ -> (T.Text, c) -- ^ (input, expected value)
20+ -> TestTree
21+testWithFiles fileDefs = test (orgWithFiles fileDefs)
22+ where
23+orgWithFiles :: [(FilePath, BS.ByteString)] -> T.Text -> Pandoc
24+orgWithFiles fileDefs input =
25+ let readOrg' = readOrg def{ readerExtensions = getDefaultExtensions "org" }
26+ in flip purely input $ \inp -> do
27+ modifyPureState (\st -> st { stFiles = files fileDefs })
28+ readOrg' inp
29+
30+
31+files :: [(FilePath, BS.ByteString)] -> FileTree
32+files fileDefs =
33+ let dummyTime = UTCTime (ModifiedJulianDay 125) (secondsToDiffTime 0)
34+ fileInfo content = FileInfo
35+ { infoFileMTime = dummyTime
36+ , infoFileContents = content
37+ }
38+ in FileTree (Map.fromList (map (second fileInfo) fileDefs))
39+
1140tests :: [TestTree]
1241tests =
1342 [ testGroup "export options"
@@ -125,4 +154,51 @@ tests =
125154 ] =?>
126155 headerWith ("headline", [], mempty) 1 "Headline"
127156 ]
157+
158+ , testGroup "Include"
159+ [ testWithFiles [("./other.org", "content of other file\n")]
160+ "file inclusion"
161+ (T.unlines [ "#+include: \"other.org\"" ] =?>
162+ plain "content of other file")
163+
164+ , testWithFiles [("./world.org", "World\n\n")]
165+ "Included file belongs to item"
166+ (T.unlines [ "- Hello,\n #+include: \"world.org\"" ] =?>
167+ bulletList [para "Hello," <> para "World"])
168+
169+ , testWithFiles [("./level3.org", "*** Level3\n\n")]
170+ "Default include preserves level"
171+ (T.unlines [ "#+include: \"level3.org\"" ] =?>
172+ headerWith ("level3", [], []) 3 "Level3")
173+
174+ , testWithFiles [("./level3.org", "*** Level3\n\n")]
175+ "Minlevel shifts level"
176+ (T.unlines [ "#+include: \"level3.org\" :minlevel 1" ] =?>
177+ headerWith ("level3", [], []) 1 "Level3")
178+
179+ , testWithFiles [("./src.hs", "putStrLn outString\n")]
180+ "Include file as source code snippet"
181+ (T.unlines [ "#+include: \"src.hs\" src haskell" ] =?>
182+ codeBlockWith ("", ["haskell"], []) "putStrLn outString\n")
183+
184+ , testWithFiles [("./export-latex.org", "\\emph{Hello}\n")]
185+ "Include file as export snippet"
186+ (T.unlines [ "#+include: \"export-latex.org\" export latex" ] =?>
187+ rawBlock "latex" "\\emph{Hello}\n")
188+
189+ , testWithFiles [("./subdir/foo-bar.latex", "foo\n"),
190+ ("./hello.lisp", "(print \"Hello!\")\n")
191+ ]
192+ "include directive is limited to one line"
193+ (T.unlines [ "#+INCLUDE: \"hello.lisp\" src lisp"
194+ , "#+include: \"subdir/foo-bar.latex\" export latex"
195+ , "bar"
196+ ] =?>
197+ mconcat
198+ [ codeBlockWith ("", ["lisp"], []) "(print \"Hello!\")\n"
199+ , rawBlock "latex" "foo\n"
200+ , para "bar"
201+ ]
202+ )
203+ ]
128204 ]