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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
-- This template expects CPP definitions for:
-- PLATFORM_NAME = Posix | Windows
-- PLATFORM_PATH = PosixPath | WindowsPath
-- PLATFORM_STRING = PosixString | WindowsString
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}
-- | Test functions that are common to Posix and Windows
module Common.PLATFORM_NAME
(spec
,parseFails
,parseSucceeds
,parserTest
) where
import Control.Applicative ((<|>))
import Control.Monad (forM_, void)
import Control.Monad.Catch (MonadThrow)
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.List.NonEmpty as NonEmpty
import Data.Maybe (fromJust, isNothing)
import System.OsPath.PLATFORM_NAME (PLATFORM_PATH)
import qualified System.OsPath.PLATFORM_NAME as OsPath
import Test.Hspec
import OsPath.PLATFORM_NAME
import OsPath.Internal.PLATFORM_NAME
import System.OsString.Compat.PLATFORM_NAME (PLATFORM_STRING)
import qualified System.OsString.Compat.PLATFORM_NAME as OsString
currentDir :: Path Rel Dir
currentDir = (fromJust . parseRelDir) [OsString.pstr|.|]
drives :: NonEmpty (Path Abs Dir)
drives = (fromJust . traverse parseAbsDir) drives_
relDir :: Path Rel Dir
relDir = (fromJust . parseRelDir) [OsString.pstr|directory|]
relFile :: Path Rel File
relFile = (fromJust . parseRelFile) [OsString.pstr|file|]
spec :: Spec
spec = do
describe "Operations: (</>)" operationAppend
describe "Operations: dirname" operationDirname
describe "Operations: filename" operationFilename
describe "Operations: parent" operationParent
describe "Operations: toOsPath" operationToOsPath
describe "Operations: isProperPrefixOf" operationIsProperPrefixOf
describe "Operations: stripProperPrefix" operationStripProperPrefix
describe "Operations: isDrive" operationIsDrive
describe "Operations: splitDrive" operationSplitDrive
describe "Operations: extensions" extensionOperations
-- | The 'dirname' operation.
operationDirname :: Spec
operationDirname = do
it
"dirname (relDir </> relDir) == dirname relDir"
(dirname (relDir </> relDir) == dirname relDir)
it
"dirname \".\" == dirname \".\""
(dirname currentDir == currentDir)
forDrives $ \drive -> do
let absDir = drive </> relDir
it
"dirname (absDir </> relDir) == dirname relDir"
(dirname (absDir </> relDir) == dirname relDir)
it
"dirname of a drive must be a Rel path"
(isNothing (parseAbsDir . toOsPath . dirname $ drive))
-- | The 'filename' operation.
operationFilename :: Spec
operationFilename = do
it
"filename (relDir </> relFile) == filename relFile"
(filename (relDir </> relFile) == filename relFile)
forDrives $ \drive -> do
let absDir = drive </> relDir
it
"filename (absDir </> relFile) == filename relFile"
(filename (absDir </> relFile) == filename relFile)
-- | The 'parent' operation.
operationParent :: Spec
operationParent = do
it
"parent relDir == \".\""
(parent relDir == currentDir)
it
"parent \".\" == \".\""
(parent currentDir == currentDir)
forDrives $ \drive -> do
let absDir = drive </> relDir
it
"parent (absDir </> relDir) == absDir"
(parent (absDir </> relDir) == absDir)
it
"parent \"/name\" == drive"
(parent absDir == drive)
it
"parent drive == drive"
(parent drive == drive)
-- | The 'splitDrive' operation.
operationSplitDrive :: Spec
operationSplitDrive = forDrives $ \drive -> do
let absDir = drive </> relDir
absFile = drive </> relFile
it
"splitDrive absDir == (drive, Just relDir)"
(splitDrive absDir == (drive, Just relDir))
it
"splitDrive absFile == (drive, Just relFile)"
(splitDrive absFile == (drive, Just relFile))
it
"splitDrive drive == (drive, Nothing)"
(splitDrive drive == (drive, Nothing))
-- | The 'isDrive' operation.
operationIsDrive :: Spec
operationIsDrive = forDrives $ \drive -> do
let absDir = drive </> relDir
it
"isDrive drive"
(isDrive drive)
it
"not (isDrive absDir)"
(not (isDrive absDir))
-- | The 'isProperPrefixOf' operation.
operationIsProperPrefixOf :: Spec
operationIsProperPrefixOf = do
it
"isProperPrefixOf relDir (relDir </> relDir)"
(isProperPrefixOf relDir (relDir </> relDir))
it
"not (relDir `isProperPrefixOf` relDir)"
(not (isProperPrefixOf relDir relDir))
forDrives $ \drive -> do
let absDir = drive </> relDir
it
"isProperPrefixOf absDir (absDir </> relDir)"
(isProperPrefixOf absDir (absDir </> relDir))
it
"not (drive `isProperPrefixOf` drive)"
(not (isProperPrefixOf drive drive))
-- | The 'stripProperPrefix' operation.
operationStripProperPrefix :: Spec
operationStripProperPrefix = do
it
"stripProperPrefix relDir (relDir </> relDir) == Just relDir"
(stripProperPrefix relDir (relDir </> relDir) == Just relDir)
forDrives $ \drive -> do
let absDir = drive </> relDir
it
"stripProperPrefix absDir (absDir </> relDir) == Just relDir"
(stripProperPrefix absDir (absDir </> relDir) == Just relDir)
it
"stripProperPrefix absDir absDir == Nothing"
(isNothing (stripProperPrefix absDir absDir))
-- | The '</>' operation.
operationAppend :: Spec
operationAppend = do
let Path relDir' = relDir
Path relFile' = relFile
it
"RelDir + RelDir == RelDir"
(relDir </> relDir == Path (relDir' OsPath.</> relDir'))
it
"\".\" + \".\" == \".\""
(currentDir </> currentDir == currentDir)
it
"\".\" + relDir == relDir"
(currentDir </> relDir == relDir)
it
"relDir + \".\" == x"
(relDir </> currentDir == relDir)
it
"RelDir + RelFile == RelFile"
(relDir </> relFile == Path (relDir' OsPath.</> relFile'))
forDrives $ \drive -> do
let absDir@(Path absDir') = drive </> relDir
it
"AbsDir + RelDir == AbsDir"
(absDir </> relDir == Path (absDir' OsPath.</> relDir'))
it
"AbsDir + RelFile == AbsFile"
(absDir </> relFile == Path (absDir' OsPath.</> relFile'))
-- | The 'toOsPath' operation.
operationToOsPath :: Spec
operationToOsPath = do
let expected = relRoot
it
("toOsPath \".\" == " ++ show expected)
(toOsPath currentDir == expected)
it
("show \".\" == " ++ (show . show) expected)
(show currentDir == show expected)
-- | Testing operations related to extensions.
extensionOperations :: Spec
extensionOperations = do
describe "Only filenames and extensions" $
forM_ filenames $ \file -> do
forM_ validExtensions $ \ext -> do
runTests parseRelFile file ext
describe "Relative dir paths" $
forM_ dirnames $ \dir -> do
forM_ filenames $ \file -> do
forM_ validExtensions $ \ext -> do
let ospath =
dir <> OsString.singleton OsPath.pathSeparator <> file
runTests parseRelFile ospath ext
describe "Absolute dir paths" $
forM_ drives_ $ \drive -> do
forM_ dirnames $ \dir -> do
forM_ filenames $ \file -> do
forM_ validExtensions $ \ext -> do
let ospath = drive <> dir <> pathSep <> file
runTests parseAbsFile ospath ext
-- Invalid extensions
forM_ invalidExtensions $ \ext -> do
it ("throws InvalidExtension when extension is " ++ show ext) $
addExtension ext (Path [OsString.pstr|name|])
`shouldThrow` (== InvalidExtension ext)
where
runTests :: (forall m . MonadThrow m => PLATFORM_PATH -> m (Path b File))
-> PLATFORM_PATH
-> PLATFORM_STRING
-> Spec
runTests parse file ext = do
let maybePathFile = parse file
let maybePathFileWithExt = parse (file <> ext)
case (maybePathFile, maybePathFileWithExt) of
(Just pathFile, Just pathFileWithExt) -> validExtensionsSpec ext pathFile pathFileWithExt
_ -> it ("Files " ++ show file ++ " and/or " ++ show (file <> ext) ++ " should parse successfully.") $
expectationFailure $
show file ++ " parsed to " ++ show maybePathFile ++ ", "
++ show (file <> ext) ++ " parsed to " ++ show maybePathFileWithExt
filenames :: [PLATFORM_PATH]
filenames =
[ [OsString.pstr|name|]
, [OsString.pstr|name.|]
, [OsString.pstr|name..|]
, [OsString.pstr|.name|]
, [OsString.pstr|..name|]
, [OsString.pstr|name.name|]
, [OsString.pstr|name..name|]
, [OsString.pstr|...|]
]
dirnames :: [PLATFORM_PATH]
dirnames = filenames ++ [ [OsString.pstr|.|] ]
invalidExtensions :: [PLATFORM_STRING]
invalidExtensions =
[ [OsString.pstr||]
, [OsString.pstr|.|]
, [OsString.pstr|x|]
, [OsString.pstr|..|]
, [OsString.pstr|...|]
, [OsString.pstr|xy|]
, [OsString.pstr|foo|]
, [OsString.pstr|foo.|]
, [OsString.pstr|foo..|]
, [OsString.pstr|..foo|]
, [OsString.pstr|...foo|]
, [OsString.pstr|.foo.bar|]
, [OsString.pstr|.foo|] <> pathSep <> [OsString.pstr|bar|]
]
validExtensions :: [PLATFORM_STRING]
validExtensions =
[ [OsString.pstr|.foo|]
, [OsString.pstr|.foo.|]
, [OsString.pstr|.foo..|]
]
validExtensionsSpec :: PLATFORM_STRING -> Path b File -> Path b File -> Spec
validExtensionsSpec ext file fext = do
let f = show $ toOsPath file
let fx = show $ toOsPath fext
it ("addExtension " ++ show ext ++ " " ++ f ++ " == " ++ fx) $
addExtension ext file `shouldReturn` fext
it ("fileExtension " ++ fx ++ " == " ++ show ext) $
fileExtension fext `shouldReturn` ext
it ("replaceExtension " ++ show ext ++ " " ++ fx ++ " == " ++ fx) $
replaceExtension ext fext `shouldReturn` fext
forDrives :: (Path Abs Dir -> Spec) -> Spec
forDrives f = case drives of
(drive :| []) -> f drive
_ -> forM_ drives $ \drive ->
describe ("Drive " ++ show drive) (f drive)
parseFails :: PLATFORM_PATH -> Spec
parseFails x = it (show x ++ " should be rejected")
(isNothing (void (parseAbsDir x) <|>
void (parseRelDir x) <|>
void (parseAbsFile x) <|>
void (parseRelFile x)))
parseSucceeds :: PLATFORM_PATH -> Path Rel Dir -> Spec
parseSucceeds x with = parserTest parseRelDir x (Just with)
-- | Parser test.
parserTest :: (Show a, Show b, Eq b)
=> (a -> Maybe b) -> a -> Maybe b -> Spec
parserTest parser input expected =
it (message1 ++ "Parsing " ++ show input ++ " " ++ message2)
(parser input `shouldBe` expected)
where message1
| isNothing expected = "Failing: "
| otherwise = "Succeeding: "
message2 = case expected of
Nothing -> "should fail."
Just x -> "should succeed with: " ++ show x
|