hooky-1.0.9: test/Hooky/Utils/GlobSpec.hs
{-# LANGUAGE OverloadedStrings #-}
module Hooky.Utils.GlobSpec (spec) where
import Data.Text (Text)
import Data.Text qualified as Text
import Hooky.Utils.Glob (
matchesGlob,
matchesGlobs,
toGlob,
)
import Skeletest
import Skeletest.Predicate qualified as P
import Skeletest.Prop qualified as Prop
import Skeletest.Prop.Gen qualified as Gen
import Skeletest.Prop.Range qualified as Range
spec :: Spec
spec = do
globSpec
globSpec :: Spec
globSpec = do
describe "Glob" $ do
describe "matchesGlob" $ do
let
glob `matches` input = \expected ->
it (show glob <> " matches " <> show input <> " ==> " <> show expected) $ do
matchesGlob (toGlob glob) input `shouldBe` expected
(==>) = ($)
"*" `matches` "Foo.hs" ==> True
"*.hs" `matches` "Foo.hs" ==> True
"*.hs" `matches` "Foo.hs.txt" ==> False
"*.hs" `matches` "Foo/Bar.hs" ==> True
"*.hs" `matches` "Foo/Bar.txt" ==> False
"*.hs" `matches` "Foo/Bar.hs/Baz" ==> False
"Foo*.hs" `matches` "Foo.hs" ==> True
"Foo*.hs" `matches` "FooBar.hs" ==> True
"Foo*.hs" `matches` "FooBar.hs.txt" ==> False
"!*.hs" `matches` "Foo.hs" ==> False
"!*.hs" `matches` "Foo/Bar.hs" ==> False
"!*.hs" `matches` "Foo/Bar.txt" ==> True
"!*.hs" `matches` "Foo/Bar.hs/Baz" ==> True
"/*.hs" `matches` "Foo.hs" ==> True
"/*.hs" `matches` "Foo/Bar.hs" ==> False
"Foo/Bar.hs" `matches` "Foo/Bar.hs" ==> True
"Foo/Bar.hs" `matches` "path/to/Foo/Bar.hs" ==> True
"/Foo/Bar.hs" `matches` "Foo/Bar.hs" ==> True
"/Foo/Bar.hs" `matches` "path/to/Foo/Bar.hs" ==> False
"**/*.hs" `matches` "Foo.hs" ==> True
"**/*.hs" `matches` "Foo/Bar.hs" ==> True
"**/*.hs" `matches` "Foo/Bar/Baz.hs" ==> True
"**/*.hs" `matches` "Foo.txt" ==> False
"**/*.hs" `matches` "Foo/Bar/Baz.txt" ==> False
"!**/*.hs" `matches` "Foo.hs" ==> False
"!**/*.hs" `matches` "Foo/Bar.hs" ==> False
"!**/*.hs" `matches` "Foo/Bar/Baz.hs" ==> False
"!**/*.hs" `matches` "Foo.txt" ==> True
"!**/*.hs" `matches` "Foo/Bar/Baz.txt" ==> True
-- Brace expansion
"{foo,bar}.{hs,txt}" `matches` "foo.hs" ==> True
"{foo,bar}.{hs,txt}" `matches` "bar.hs" ==> True
"{foo,bar}.{hs,txt}" `matches` "foo.txt" ==> True
"{foo,bar}.{hs,txt}" `matches` "bar.txt" ==> True
"!{foo,bar}.{hs,txt}" `matches` "bar.txt" ==> False
"!{foo,bar}.{hs,txt}" `matches` "baz.txt" ==> True
-- Hidden files/directories
"*" `matches` ".gitignore" ==> True
"**/*" `matches` ".foo/test" ==> True
prop "'<path>' and '**/<path>' are equivalent" $ do
Prop.setTestLimit 10000
g <- forAll genRelGlob
let g1 = toGlob g
g2 = toGlob ("**/" <> g)
(matchesGlob g1 P.=== matchesGlob g2) `shouldSatisfy` P.isoWith genRelPath
describe "matchesGlobs" $ do
let
globs `matches` input = \expected ->
it (show globs <> " matches " <> show input <> " ==> " <> show expected) $ do
matchesGlobs (map toGlob globs) input `shouldBe` expected
(==>) = ($)
["**/*"] `matches` "Foo.hs" ==> True
["!**/*.golden"] `matches` "Foo.hs" ==> True
["!**/*.golden", "**/*"] `matches` "Foo.hs" ==> True
pure () -- TODO: property test where matchesGlobs [g] === matchesGlob g
where
genRelPathLike :: Gen Text -> Gen Text
genRelPathLike g = Text.intercalate "/" <$> Gen.list (Range.linear 1 10) g
genRelPath :: Gen Text
genRelPath = genRelPathLike $ Gen.text (Range.linear 1 30) Gen.unicode
genRelGlob :: Gen Text
genRelGlob =
genRelPathLike $
fmap Text.concat . Gen.list (Range.linear 1 5) $
Gen.frequency
[ (9, Gen.text (Range.linear 1 5) genChar)
, (1, pure "*")
]
genChar = Gen.filter (/= '!') Gen.unicode