forked from dhall-lang/dhall-haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackage.hs
More file actions
212 lines (179 loc) · 6.65 KB
/
Package.hs
File metadata and controls
212 lines (179 loc) · 6.65 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Dhall.Test.Package where
import Control.Exception (Exception, displayException, try)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Void (Void)
import Dhall.Core
( Directory (..)
, Expr (..)
, File (..)
, FilePrefix (..)
, Import (..)
, ImportHashed (..)
, ImportMode (..)
, ImportType (..)
, makeRecordField
)
import qualified Dhall.Map as Map
import Dhall.Package
import System.FilePath ((</>))
import Test.Tasty
import Test.Tasty.HUnit
tests :: TestTree
tests = testGroup "Package"
[ packagePackageFile
, packageCustomPackageFile
, packageSingleFile
, packageEmptyDirectory
, packageSingleDirectory
, packageNested
, packageMissingFile
, packageFilesDifferentDirs
, packageIncompatibleFiles
]
packagePackageFile :: TestTree
packagePackageFile = testCase "package file" $ do
let path = "./tests/package" </> "package.dhall"
let package :: Expr Void Import
package = RecordLit Map.empty
(output, expr) <- getPackagePathAndContent Nothing ("./tests/package/package.dhall" :| [])
assertEqual "path" path output
assertEqual "content" package expr
packageCustomPackageFile :: TestTree
packageCustomPackageFile = testCase "custom package file" $ do
let path = "./tests/package" </> "custom.dhall"
let package :: Expr Void Import
package = RecordLit $
Map.singleton "package" $ makeRecordField $ Embed packageDhall
(output, expr) <- getPackagePathAndContent (Just "custom.dhall") ("./tests/package/package.dhall" :| [])
assertEqual "path" path output
assertEqual "content" package expr
packageSingleFile :: TestTree
packageSingleFile = testCase "single file" $ do
let path = "./tests/package/dir" </> "package.dhall"
let package :: Expr Void Import
package = RecordLit $
Map.singleton "test" $ makeRecordField $ Embed testDhall
(output, expr) <- getPackagePathAndContent Nothing ("./tests/package/dir/test.dhall" :| [])
assertEqual "path" path output
assertEqual "content" package expr
packageEmptyDirectory :: TestTree
packageEmptyDirectory = testCase "empty directory" $ do
let path = "./tests/package/empty" </> "package.dhall"
let package :: Expr Void Import
package = RecordLit Map.empty
(output, expr) <- getPackagePathAndContent Nothing ("./tests/package/empty" :| [])
assertEqual "path" path output
assertEqual "content" package expr
packageSingleDirectory :: TestTree
packageSingleDirectory = testCase "single directory" $ do
let path = "./tests/package/dir" </> "package.dhall"
let package :: Expr Void Import
package = RecordLit $ Map.singleton "test" $
makeRecordField $ Embed testDhall
(output, expr) <- getPackagePathAndContent Nothing ("./tests/package/dir" :| [])
assertEqual "path" path output
assertEqual "content" package expr
packageNested :: TestTree
packageNested = testCase "nested files" $ do
let path = "./tests/package" </> "package.dhall"
let package :: Expr Void Import
package = RecordLit $ Map.fromList
[ ("dir", makeRecordField $ RecordLit $ Map.fromList
[ ("test", makeRecordField $ Embed dirTestDhall)
]
)
, ("other", makeRecordField $ Embed otherPackageDhall)
, ("test", makeRecordField $ Embed testDhall)
]
(output, expr) <- getPackagePathAndContent Nothing
( "./tests/package/test.dhall" :|
[ "./tests/package/dir/test.dhall"
, "./tests/package/other/package.dhall"
])
assertEqual "path" path output
assertEqual "content" package expr
packageMissingFile :: TestTree
packageMissingFile = testCase "missing file" $ do
let action :: IO (FilePath, Expr Void Import)
action = getPackagePathAndContent Nothing ("./tests/package/missing.dhall" :| [])
assertThrow action $ \case
InvalidPath "./tests/package/missing.dhall" -> True
_ -> False
packageFilesDifferentDirs :: TestTree
packageFilesDifferentDirs = testCase "files from different directories" $ do
let action :: IO (FilePath, Expr Void Import)
action = getPackagePathAndContent Nothing ("./tests/package/dir/test.dhall" :| ["./tests/package/test/test.dhall"])
assertThrow action $ \case
AmbiguousOutputDirectory "./tests/package/dir" "./tests/package/test" -> True
_ -> False
packageIncompatibleFiles :: TestTree
packageIncompatibleFiles = testCase "files that are incompatible" $ do
let action :: IO (FilePath, Expr Void Import)
action = getPackagePathAndContent Nothing ("./tests/package/test.dhall" :| ["./tests/package/test/test.dhall"])
assertThrow action $ \case
IncompatiblePaths xs -> xs == [ testDhall , testTestDhall ]
_ -> False
packageDhall :: Import
packageDhall = Import
{ importHashed = ImportHashed
{ hash = Nothing
, importType = Local Here File
{ directory = Directory []
, file = "package.dhall"
}
}
, importMode = Code
}
testDhall :: Import
testDhall = Import
{ importHashed = ImportHashed
{ hash = Nothing
, importType = Local Here File
{ directory = Directory []
, file = "test.dhall"
}
}
, importMode = Code
}
dirTestDhall :: Import
dirTestDhall = Import
{ importHashed = ImportHashed
{ hash = Nothing
, importType = Local Here $ File
{ directory = Directory {components = ["dir"]}
, file = "test.dhall"
}
}
, importMode = Code
}
otherPackageDhall :: Import
otherPackageDhall = Import
{ importHashed = ImportHashed
{ hash = Nothing
, importType = Local Here $ File
{ directory = Directory {components = ["other"]}
, file = "package.dhall"
}
}
, importMode = Code
}
testTestDhall :: Import
testTestDhall = Import
{ importHashed = ImportHashed
{ hash = Nothing
, importType = Local Here (File
{ directory = Directory {components = ["test"]}
, file = "test.dhall"
})
}
, importMode = Code
}
assertThrow :: (Exception e, Show a) => IO a -> (e -> Bool) -> IO ()
assertThrow k p = do
result <- try k
case result of
Left e | p e -> return ()
Left e -> assertFailure $ "Predicate did not match: " <> displayException e
Right result' -> assertFailure $ "Expected exception, but got: " <> show result'