Fix Setup.hs `--dependency` example
[cabal.git] / cabal-install / tests / UnitTests / Distribution / Client / Glob.hs
blob8d77b6784ef3f4762e37997b1e852408a0c561d9
1 {-# LANGUAGE CPP #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module UnitTests.Distribution.Client.Glob (tests) where
6 import Distribution.Client.Compat.Prelude hiding (last)
7 import Prelude ()
9 import Distribution.Client.Glob
10 import Distribution.Utils.Structured (structureHash)
11 import UnitTests.Distribution.Client.ArbitraryInstances ()
13 import GHC.Fingerprint (Fingerprint (..))
14 import Test.Tasty
15 import Test.Tasty.HUnit
16 import Test.Tasty.QuickCheck
18 tests :: [TestTree]
19 tests =
20 [ testProperty "print/parse roundtrip" prop_roundtrip_printparse
21 , testCase "parse examples" testParseCases
22 , testGroup
23 "Structured hashes"
24 [ testCase "GlobPiece" $ structureHash (Proxy :: Proxy GlobPiece) @?= Fingerprint 0xd5e5361866a30ea2 0x31fbfe7b58864782
25 , testCase "FilePathGlobRel" $ structureHash (Proxy :: Proxy FilePathGlobRel) @?= Fingerprint 0x76fa5bcb865a8501 0xb152f68915316f98
26 , testCase "FilePathRoot" $ structureHash (Proxy :: Proxy FilePathRoot) @?= Fingerprint 0x713373d51426ec64 0xda7376a38ecee5a5
27 , testCase "FilePathGlob" $ structureHash (Proxy :: Proxy FilePathGlob) @?= Fingerprint 0x3c11c41f3f03a1f0 0x96e69d85c37d0024
31 -- TODO: [nice to have] tests for trivial globs, tests for matching,
32 -- tests for windows style file paths
34 prop_roundtrip_printparse :: FilePathGlob -> Property
35 prop_roundtrip_printparse pathglob =
36 counterexample (prettyShow pathglob) $
37 eitherParsec (prettyShow pathglob) === Right pathglob
39 -- first run, where we don't even call updateMonitor
40 testParseCases :: Assertion
41 testParseCases = do
42 FilePathGlob (FilePathRoot "/") GlobDirTrailing <- testparse "/"
43 FilePathGlob FilePathHomeDir GlobDirTrailing <- testparse "~/"
45 FilePathGlob (FilePathRoot "A:\\") GlobDirTrailing <- testparse "A:/"
46 FilePathGlob (FilePathRoot "Z:\\") GlobDirTrailing <- testparse "z:/"
47 FilePathGlob (FilePathRoot "C:\\") GlobDirTrailing <- testparse "C:\\"
48 FilePathGlob FilePathRelative (GlobFile [Literal "_:"]) <- testparse "_:"
50 FilePathGlob
51 FilePathRelative
52 (GlobFile [Literal "."]) <-
53 testparse "."
55 FilePathGlob
56 FilePathRelative
57 (GlobFile [Literal "~"]) <-
58 testparse "~"
60 FilePathGlob
61 FilePathRelative
62 (GlobDir [Literal "."] GlobDirTrailing) <-
63 testparse "./"
65 FilePathGlob
66 FilePathRelative
67 (GlobFile [Literal "foo"]) <-
68 testparse "foo"
70 FilePathGlob
71 FilePathRelative
72 ( GlobDir
73 [Literal "foo"]
74 (GlobFile [Literal "bar"])
75 ) <-
76 testparse "foo/bar"
78 FilePathGlob
79 FilePathRelative
80 ( GlobDir
81 [Literal "foo"]
82 (GlobDir [Literal "bar"] GlobDirTrailing)
83 ) <-
84 testparse "foo/bar/"
86 FilePathGlob
87 (FilePathRoot "/")
88 ( GlobDir
89 [Literal "foo"]
90 (GlobDir [Literal "bar"] GlobDirTrailing)
91 ) <-
92 testparse "/foo/bar/"
94 FilePathGlob
95 (FilePathRoot "C:\\")
96 ( GlobDir
97 [Literal "foo"]
98 (GlobDir [Literal "bar"] GlobDirTrailing)
99 ) <-
100 testparse "C:\\foo\\bar\\"
102 FilePathGlob
103 FilePathRelative
104 (GlobFile [WildCard]) <-
105 testparse "*"
107 FilePathGlob
108 FilePathRelative
109 (GlobFile [WildCard, WildCard]) <-
110 testparse "**" -- not helpful but valid
111 FilePathGlob
112 FilePathRelative
113 (GlobFile [WildCard, Literal "foo", WildCard]) <-
114 testparse "*foo*"
116 FilePathGlob
117 FilePathRelative
118 (GlobFile [Literal "foo", WildCard, Literal "bar"]) <-
119 testparse "foo*bar"
121 FilePathGlob
122 FilePathRelative
123 (GlobFile [Union [[WildCard], [Literal "foo"]]]) <-
124 testparse "{*,foo}"
126 parseFail "{"
127 parseFail "}"
128 parseFail ","
129 parseFail "{"
130 parseFail "{{}"
131 parseFail "{}"
132 parseFail "{,}"
133 parseFail "{foo,}"
134 parseFail "{,foo}"
136 return ()
138 testparse :: String -> IO FilePathGlob
139 testparse s =
140 case eitherParsec s of
141 Right p -> return p
142 Left err -> throwIO $ HUnitFailure Nothing ("expected parse of: " ++ s ++ " -- " ++ err)
144 parseFail :: String -> Assertion
145 parseFail s =
146 case eitherParsec s :: Either String FilePathGlob of
147 Right p -> throwIO $ HUnitFailure Nothing ("expected no parse of: " ++ s ++ " -- " ++ show p)
148 Left _ -> return ()