2021-03-31 00:16:32 +03:00
|
|
|
module GlobTests exposing (all)
|
|
|
|
|
2021-04-29 00:05:08 +03:00
|
|
|
import DataSource.Glob as Glob
|
2021-03-31 00:16:32 +03:00
|
|
|
import Expect
|
2021-04-02 23:54:42 +03:00
|
|
|
import Test exposing (Test, describe, test)
|
2021-03-31 00:16:32 +03:00
|
|
|
|
|
|
|
|
2021-04-02 23:54:42 +03:00
|
|
|
all : Test
|
2021-03-31 00:16:32 +03:00
|
|
|
all =
|
2021-04-01 05:55:28 +03:00
|
|
|
describe "glob"
|
|
|
|
[ test "literal" <|
|
|
|
|
\() ->
|
|
|
|
Glob.literal "hello"
|
2021-04-26 05:39:54 +03:00
|
|
|
|> expect "hello"
|
2021-04-26 06:38:48 +03:00
|
|
|
{ expectedMatch = "hello"
|
2021-04-01 05:55:28 +03:00
|
|
|
, expectedPattern = "hello"
|
|
|
|
}
|
|
|
|
, test "capture" <|
|
|
|
|
\() ->
|
|
|
|
Glob.succeed identity
|
2021-04-20 23:53:53 +03:00
|
|
|
|> Glob.capture Glob.wildcard
|
2021-05-20 04:20:47 +03:00
|
|
|
|> Glob.match (Glob.literal ".txt")
|
2021-04-26 05:39:54 +03:00
|
|
|
|> expect "my-file.txt"
|
2021-04-26 06:38:48 +03:00
|
|
|
{ expectedMatch = "my-file"
|
2021-04-01 05:55:28 +03:00
|
|
|
, expectedPattern = "*.txt"
|
|
|
|
}
|
|
|
|
, test "oneOf" <|
|
|
|
|
\() ->
|
|
|
|
Glob.succeed Tuple.pair
|
2021-04-20 23:53:53 +03:00
|
|
|
|> Glob.capture Glob.wildcard
|
2021-05-20 04:20:47 +03:00
|
|
|
|> Glob.match (Glob.literal ".")
|
2021-04-20 23:53:53 +03:00
|
|
|
|> Glob.capture
|
2021-04-01 05:55:28 +03:00
|
|
|
(Glob.oneOf
|
|
|
|
( ( "yml", Yml )
|
|
|
|
, [ ( "json", Json )
|
|
|
|
]
|
2021-03-31 01:37:03 +03:00
|
|
|
)
|
2021-04-01 05:55:28 +03:00
|
|
|
)
|
|
|
|
-- https://runkit.com/embed/05epbnc0c7g1
|
2021-04-26 05:39:54 +03:00
|
|
|
|> expect "data-file.json"
|
2021-04-26 06:38:48 +03:00
|
|
|
{ expectedMatch = ( "data-file", Json )
|
2021-04-01 05:55:28 +03:00
|
|
|
, expectedPattern = "*.(yml|json)"
|
|
|
|
}
|
|
|
|
, test "at least one" <|
|
|
|
|
\() ->
|
|
|
|
Glob.succeed identity
|
2021-05-20 04:20:47 +03:00
|
|
|
|> Glob.match Glob.wildcard
|
|
|
|
|> Glob.match (Glob.literal ".")
|
2021-04-20 23:53:53 +03:00
|
|
|
|> Glob.capture
|
2021-04-01 05:55:28 +03:00
|
|
|
(Glob.atLeastOne
|
|
|
|
( ( "yml", Yml )
|
|
|
|
, [ ( "json", Json )
|
|
|
|
]
|
2021-03-31 23:56:56 +03:00
|
|
|
)
|
2021-04-01 05:55:28 +03:00
|
|
|
)
|
|
|
|
-- https://runkit.com/embed/05epbnc0c7g1
|
2021-04-26 05:39:54 +03:00
|
|
|
|> expect "data-file.jsonymljsonjson"
|
2021-04-26 06:38:48 +03:00
|
|
|
{ expectedMatch = ( Json, [ Yml, Json, Json ] )
|
2021-04-01 05:55:28 +03:00
|
|
|
, expectedPattern = "*.+(yml|json)"
|
|
|
|
}
|
|
|
|
, test "optional group - no match" <|
|
|
|
|
\() ->
|
|
|
|
zeroOrMoreGlob
|
2021-04-26 05:39:54 +03:00
|
|
|
|> expect "test/a/x.js"
|
2021-04-01 05:55:28 +03:00
|
|
|
-- test/a/x.js
|
|
|
|
-- https://github.com/micromatch/micromatch/blob/fe4858b0c63b174fd3ae22674db39119b8fa4392/test/api.capture.js#L42
|
2021-04-26 06:38:48 +03:00
|
|
|
{ expectedMatch = Nothing
|
2021-04-01 05:55:28 +03:00
|
|
|
, expectedPattern = "test/a*(a|b)/x.js"
|
|
|
|
}
|
|
|
|
, test "optional group - single match" <|
|
|
|
|
\() ->
|
|
|
|
zeroOrMoreGlob
|
2021-04-26 05:39:54 +03:00
|
|
|
|> expect "test/ab/x.js"
|
2021-04-01 05:55:28 +03:00
|
|
|
-- test/ab/x.js
|
|
|
|
-- https://github.com/micromatch/micromatch/blob/fe4858b0c63b174fd3ae22674db39119b8fa4392/test/api.capture.js#L44
|
2021-04-26 06:38:48 +03:00
|
|
|
{ expectedMatch = Just "b"
|
2021-04-01 05:55:28 +03:00
|
|
|
, expectedPattern = "test/a*(a|b)/x.js"
|
|
|
|
}
|
|
|
|
, test "optional group - multiple matches" <|
|
|
|
|
\() ->
|
|
|
|
zeroOrMoreGlob
|
2021-04-26 05:39:54 +03:00
|
|
|
|> expect "test/aba/x.js"
|
2021-04-01 05:55:28 +03:00
|
|
|
-- test/aba/x.js
|
|
|
|
-- https://github.com/micromatch/micromatch/blob/fe4858b0c63b174fd3ae22674db39119b8fa4392/test/api.capture.js#L45
|
2021-04-26 06:38:48 +03:00
|
|
|
{ expectedMatch = Just "ba"
|
2021-04-01 05:55:28 +03:00
|
|
|
, expectedPattern = "test/a*(a|b)/x.js"
|
|
|
|
}
|
|
|
|
, test "new star" <|
|
|
|
|
\() ->
|
|
|
|
Glob.wildcard
|
2021-04-26 05:39:54 +03:00
|
|
|
|> expect "star-pattern"
|
2021-04-26 06:38:48 +03:00
|
|
|
{ expectedMatch = "star-pattern"
|
2021-04-01 05:55:28 +03:00
|
|
|
, expectedPattern = "*"
|
|
|
|
}
|
|
|
|
, test "new star with literal" <|
|
|
|
|
\() ->
|
|
|
|
Glob.succeed Tuple.pair
|
2021-04-20 23:53:53 +03:00
|
|
|
|> Glob.capture Glob.wildcard
|
2021-05-20 04:20:47 +03:00
|
|
|
|> Glob.match (Glob.literal "/")
|
2021-04-20 23:53:53 +03:00
|
|
|
|> Glob.capture (Glob.wildcard |> Glob.map String.toUpper)
|
2021-05-20 04:20:47 +03:00
|
|
|
|> Glob.match (Glob.literal ".txt")
|
2021-04-26 05:39:54 +03:00
|
|
|
|> expect "before-slash/after-slash.txt"
|
2021-04-26 06:38:48 +03:00
|
|
|
{ expectedMatch = ( "before-slash", "AFTER-SLASH" )
|
2021-04-01 05:55:28 +03:00
|
|
|
, expectedPattern = "*/*.txt"
|
|
|
|
}
|
|
|
|
, test "recursive match" <|
|
|
|
|
\() ->
|
|
|
|
Glob.succeed Tuple.pair
|
2021-04-20 23:53:53 +03:00
|
|
|
|> Glob.capture Glob.recursiveWildcard
|
2021-05-20 04:20:47 +03:00
|
|
|
|> Glob.match (Glob.literal "/")
|
2021-04-20 23:53:53 +03:00
|
|
|
|> Glob.capture Glob.wildcard
|
2021-05-20 04:20:47 +03:00
|
|
|
|> Glob.match (Glob.literal ".txt")
|
2021-04-26 05:39:54 +03:00
|
|
|
|> expect "a/b/c/d.txt"
|
2021-05-20 17:30:26 +03:00
|
|
|
{ expectedMatch = ( [ "a", "b", "c" ], "d" )
|
2021-04-01 05:55:28 +03:00
|
|
|
, expectedPattern = "**/*.txt"
|
|
|
|
}
|
2021-05-20 19:35:28 +03:00
|
|
|
, test "wildcard and recursiveWildcard in one pattern" <|
|
|
|
|
\() ->
|
|
|
|
Glob.succeed Tuple.pair
|
|
|
|
|> Glob.match (Glob.literal "content/")
|
|
|
|
|> Glob.capture Glob.recursiveWildcard
|
|
|
|
|> Glob.match (Glob.literal "/")
|
|
|
|
|> Glob.capture Glob.wildcard
|
|
|
|
|> Glob.match (Glob.literal ".md")
|
|
|
|
|> expectAll
|
|
|
|
[ ( "content/about.md", ( [], "about" ) )
|
|
|
|
, ( "content/community/meetups.md", ( [ "community" ], "meetups" ) )
|
|
|
|
]
|
2021-04-01 05:55:28 +03:00
|
|
|
]
|
2021-03-31 02:04:05 +03:00
|
|
|
|
|
|
|
|
2021-03-31 05:57:34 +03:00
|
|
|
zeroOrMoreGlob : Glob.Glob (Maybe String)
|
2021-03-31 03:50:45 +03:00
|
|
|
zeroOrMoreGlob =
|
2021-03-31 05:55:52 +03:00
|
|
|
Glob.succeed identity
|
2021-05-20 04:20:47 +03:00
|
|
|
|> Glob.match (Glob.literal "test/a")
|
2021-04-20 23:53:53 +03:00
|
|
|
|> Glob.capture (Glob.zeroOrMore [ "a", "b" ])
|
2021-05-20 04:20:47 +03:00
|
|
|
|> Glob.match (Glob.literal "/x.js")
|
2021-03-31 00:16:32 +03:00
|
|
|
|
|
|
|
|
2021-03-31 01:37:03 +03:00
|
|
|
type DataExtension
|
|
|
|
= Yml
|
|
|
|
| Json
|
|
|
|
|
|
|
|
|
2021-03-31 00:16:32 +03:00
|
|
|
expect :
|
2021-04-26 05:39:54 +03:00
|
|
|
String
|
|
|
|
->
|
2021-04-26 06:38:48 +03:00
|
|
|
{ expectedMatch : match
|
2021-04-26 05:39:54 +03:00
|
|
|
, expectedPattern : String
|
|
|
|
}
|
2021-03-31 05:57:34 +03:00
|
|
|
-> Glob.Glob match
|
2021-03-31 05:43:37 +03:00
|
|
|
-> Expect.Expectation
|
2021-04-26 06:38:48 +03:00
|
|
|
expect filePath { expectedMatch, expectedPattern } glob =
|
2021-03-31 05:43:37 +03:00
|
|
|
glob
|
2021-04-26 05:39:54 +03:00
|
|
|
|> Glob.run filePath
|
2021-03-31 05:43:37 +03:00
|
|
|
|> Expect.equal
|
|
|
|
{ pattern = expectedPattern
|
|
|
|
, match = expectedMatch
|
|
|
|
}
|
2021-05-20 19:35:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
expectAll :
|
|
|
|
List ( String, match )
|
|
|
|
-> Glob.Glob match
|
|
|
|
-> Expect.Expectation
|
|
|
|
expectAll expectedPairs glob =
|
|
|
|
expectedPairs
|
|
|
|
|> List.map
|
|
|
|
(\( filePath, expectedMatch ) ->
|
|
|
|
( filePath
|
|
|
|
, glob
|
|
|
|
|> Glob.run filePath
|
|
|
|
|> .match
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> Expect.equalLists expectedPairs
|