-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParserSpec.hs
More file actions
65 lines (54 loc) · 2.4 KB
/
ParserSpec.hs
File metadata and controls
65 lines (54 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
module KDL.ParserSpec (spec) where
import Control.Monad (forM_)
import Data.Maybe (isJust)
import Data.Text.IO qualified as Text
import KDL qualified
import Skeletest
import Skeletest.Predicate qualified as P
import System.Directory (findExecutable, listDirectory)
import System.FilePath (takeExtension, (</>))
import System.IO.Temp (withSystemTempDirectory)
import System.IO.Unsafe (unsafePerformIO)
import System.Process (callProcess)
spec :: Spec
spec = do
let exampleConfig = "foo hello=world 1.0 { bar; }"
describe "parse" $ do
it "parses a KDL document" $ do
KDL.parse exampleConfig `shouldSatisfy` P.right P.matchesSnapshot
describe "error messages" $ do
let test msg input = do
it msg $ KDL.parse input `shouldSatisfy` P.left P.matchesSnapshot
-- TODO: add more
test "Unquoted numeric prop name" "foo 123=123"
describe "parseWith" $ do
it "parses a KDL document with spans" $ do
KDL.parseWith KDL.def{KDL.includeSpans = True} "foo 1 2 {\n bar 3\n}"
`shouldSatisfy` P.right P.matchesSnapshot
-- Most behavior tested in `parse` tests
describe "parseFile" $ do
it "parses a KDL document from a filepath" $ do
withSystemTempDirectory "" $ \tmpdir -> do
let file = tmpdir ++ "/test.kdl"
Text.writeFile file exampleConfig
Right actual <- KDL.parseFile file
Right expected <- pure $ KDL.parse exampleConfig
-- tested in `parse`
actual `shouldBe` expected
(if dotSlashInstalled then id else skip "dotslash not installed") . describe "kdl-test examples" $ do
it "decodes correctly" $ do
decoder <- findExecutable "kdl-hs-test-decoder" >>= maybe (error "Could not find kdl-hs-test-decoder") pure
callProcess "tools/kdl-test" ["run", "--decoder", decoder]
it "roundtrips successfully" $ do
FixtureTmpDir tmpdir <- getFixture
let dir = tmpdir </> "kdl-examples"
callProcess "tools/kdl-test" ["extract", "--dir", dir]
files <- filter ((== ".kdl") . takeExtension) <$> listDirectory (dir </> "valid")
forM_ files $ \file -> do
context file $ do
content <- Text.readFile (dir </> "valid" </> file)
(fmap KDL.render . KDL.parse) content `shouldBe` Right content
dotSlashInstalled :: Bool
dotSlashInstalled = unsafePerformIO $ isJust <$> findExecutable "dotslash"