Skip to content

Commit 0e967c5

Browse files
author
Serhii Khoma
committed
feat: update
1 parent 40db458 commit 0e967c5

File tree

5 files changed

+103
-71
lines changed

5 files changed

+103
-71
lines changed

Glob/Data/NonEmptyList.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import Batteries.Data.List.Basic
2+
import Init.System.IO
13
import Lean
24
import Lean.Data.RBMap
3-
import Std.Data.HashSet
45
import Lean.Data.RBTree
5-
import Init.System.IO
66
import Lean.Elab.Term
77
import Lean.Parser.Term
8-
import Batteries.Data.List.Basic
8+
import Std.Data.HashSet
99

1010
structure NonEmptyList (α : Type u) where
1111
toList : List α
@@ -19,7 +19,7 @@ instance [BEq α] : BEq (NonEmptyList α) where
1919
beq a b := a.toList == b.toList
2020

2121
instance [ToString α] : ToString (NonEmptyList α) where
22-
toString a := toString a.toList
22+
toString a := "nel!" ++ toString a.toList
2323

2424
instance [Inhabited α] : Inhabited (NonEmptyList α) where
2525
default := ⟨[default], List.cons_ne_nil default []⟩

Glob/Utils/Other.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ open System (FilePath)
1616
private abbrev LNES := List NonEmptyString
1717
private abbrev NELNES := NonEmptyList NonEmptyString
1818

19-
-- \f<< \-> \f>>
20-
@[inline] def FilePath.Lax.«LNES» (p : FilePath) : LNES := ToNE.FilterMap.«LS->LNES» p.components
21-
@[inline] def FilePath.Lax.«NELNES» (p : FilePath) : Option NELNES := ToNE.FilterMap.«LS->NELNES» p.components
22-
@[inline] def FilePath.Strict.«LNES» (p : FilePath) : Option LNES := ToNE.Traverse.«LS->LNES» p.components
23-
@[inline] def FilePath.Strict.«NELNES» (p : FilePath) : Option NELNES := ToNE.Traverse.«LS->NELNES» p.components
19+
-- \f<< -> \f>>
20+
@[inline] def FilePath.Lax.«->LNES» (p : FilePath) : LNES := ToNE.FilterMap.«LS->LNES» p.components
21+
@[inline] def FilePath.Lax.«->NELNES» (p : FilePath) : Option NELNES := ToNE.FilterMap.«LS->NELNES» p.components
22+
@[inline] def FilePath.Strict.«->LNES» (p : FilePath) : Option LNES := ToNE.Traverse.«LS->LNES» p.components
23+
@[inline] def FilePath.Strict.«->NELNES» (p : FilePath) : Option NELNES := ToNE.Traverse.«LS->NELNES» p.components

Test/GlobSpec.lean

Lines changed: 49 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Glob
2+
import Glob.Data.NonEmptyList
23
import Glob.Data.Tree
34
import Glob.WF.IO
45
import Glob.WF.Tree
@@ -23,26 +24,26 @@ def glob (pattern : PatternValidated) (tree : Tree) : Option Tree := none
2324
def globMany (patterns : NonEmptyList PatternValidated) (tree : Tree) : Option Tree := none
2425

2526
-- Helper to assert glob results match expected Tree
26-
def assertGlobResult (name : String) (pattern : String) (tree : Tree) (expected : Option Tree) : IO Unit := do
27+
def assertGlobResult (pattern : String) (tree : Tree) (expected : Option Tree) : IO Unit := do
2728
let patternValidated ← PatternValidated.patternStrictIO! pattern
2829
let actual := glob patternValidated tree
2930
unless actual == expected do
30-
IO.println s!"❌ {name} failed:"
31+
IO.println s!"❌ {pattern} failed:"
3132
IO.println s!" Pattern: {pattern}"
3233
IO.println s!" Expected: {reprStr expected}"
3334
IO.println s!" Actual: {reprStr actual}"
34-
throw <| IO.Error.userError s!"Assertion failed: {name}"
35+
throw <| IO.Error.userError s!"Assertion failed: {pattern}"
3536

3637
-- Helper to assert globMany results match expected Tree
37-
def assertGlobManyResult (name : String) (patternsNel : NonEmptyList String) (tree : Tree) (expected : Option Tree) : IO Unit := do
38+
def assertGlobManyResult (patternsNel : NonEmptyList String) (tree : Tree) (expected : Option Tree) : IO Unit := do
3839
let patternsNel' ← patternsNel.mapM PatternValidated.patternStrictIO!
3940
let actual := globMany patternsNel' tree
4041
unless actual == expected do
41-
IO.println s!"❌ {name} failed:"
42+
IO.println s!"❌ {toString patternsNel} failed:"
4243
IO.println s!" Patterns: {patternsNel}"
4344
IO.println s!" Expected: {reprStr expected}"
4445
IO.println s!" Actual: {reprStr actual}"
45-
throw <| IO.Error.userError s!"Assertion failed: {name}"
46+
throw <| IO.Error.userError s!"Assertion failed: {toString patternsNel}"
4647

4748
-- Test data
4849
def globTestExample1 := tree! "Glob" { "A" { "X" { } }, "B" { "Y" { } } }
@@ -58,198 +59,185 @@ def globTestExample2 := tree! "Root" {
5859
def testBasicGlob : IO Unit := do
5960
IO.println "Testing basic glob patterns..."
6061

61-
assertGlobResult "Glob root match" "Glob"
62+
assertGlobResult "Glob"
6263
(tree! "Glob" { "A" { } })
6364
(some (tree! "Glob" {}))
6465

65-
assertGlobResult "Glob/A match" "Glob/A"
66+
assertGlobResult "Glob/A"
6667
(tree! "Glob" { "A" { } })
6768
(some (tree! "Glob" { "A" { } }))
6869

69-
assertGlobResult "Glob/A match without children" "Glob/A"
70+
assertGlobResult "Glob/A"
7071
(tree! "Glob" { "A" })
7172
(some (tree! "Glob" { "A" }))
7273

73-
assertGlobResult "Glob/B no match" "Glob/B"
74+
assertGlobResult "Glob/B"
7475
(tree! "Glob" { "A" { } })
7576
none
7677

7778
-- Wildcard tests
7879
def testWildcardGlob : IO Unit := do
7980
IO.println "Testing wildcard patterns..."
8081

81-
assertGlobResult "Double star match" "**"
82+
assertGlobResult "**"
8283
globTestExample1
8384
(some (tree! "Glob" { "A" { "X" {} }, "B" { "Y" {} } }))
8485

85-
assertGlobResult "**/X recursive match" "**/X"
86+
assertGlobResult "**/X"
8687
globTestExample1
8788
(some (tree! "Glob" { "A" { "X" {} } }))
8889

89-
assertGlobResult "**/Y recursive match" "**/Y"
90+
assertGlobResult "**/Y"
9091
globTestExample1
9192
(some (tree! "Glob" { "B" { "Y" {} } }))
9293

93-
assertGlobResult "**/Z no match" "**/Z"
94+
assertGlobResult "**/Z"
9495
globTestExample1
9596
none
9697

97-
assertGlobResult "Glob/* single level" "Glob/*"
98+
assertGlobResult "Glob/*"
9899
globTestExample1
99100
(some (tree! "Glob" { "A" {}, "B" {} }))
100101

101-
assertGlobResult "Glob/** recursive" "Glob/**"
102+
assertGlobResult "Glob/**"
102103
globTestExample1
103104
(some globTestExample1)
104105

105106
-- Specific path tests
106107
def testSpecificPaths : IO Unit := do
107108
IO.println "Testing specific path patterns..."
108109

109-
assertGlobResult "Glob/A/* match" "Glob/A/*"
110+
assertGlobResult "Glob/A/*"
110111
globTestExample1
111112
(some (tree! "Glob" { "A" { "X" {} } }))
112113

113-
assertGlobResult "Glob/A/** recursive" "Glob/A/**"
114+
assertGlobResult "Glob/A/**"
114115
globTestExample1
115116
(some (tree! "Glob" { "A" { "X" { } } }))
116117

117-
assertGlobResult "Glob/A/X exact match" "Glob/A/X"
118+
assertGlobResult "Glob/A/X"
118119
globTestExample1
119120
(some (tree! "Glob" { "A" { "X" {} } }))
120121

121-
assertGlobResult "Glob/B/** recursive" "Glob/B/**"
122+
assertGlobResult "Glob/B/**"
122123
globTestExample1
123124
(some (tree! "Glob" { "B" { "Y" {} } }))
124125

125-
assertGlobResult "Glob/C no match" "Glob/C"
126+
assertGlobResult "Glob/C"
126127
globTestExample1
127128
none
128129

129130
-- Simple tree tests
130131
def testSimpleTrees : IO Unit := do
131132
IO.println "Testing simple tree patterns..."
132133

133-
assertGlobResult "Single star match" "*"
134+
assertGlobResult "*"
134135
(tree! "foo" {})
135136
(some (tree! "foo" {}))
136137

137-
assertGlobResult "Double star on simple tree" "**"
138+
assertGlobResult "**"
138139
(tree! "root" {})
139140
(some (tree! "root" {}))
140141

141142
-- Complex tree tests
142143
def testComplexTree : IO Unit := do
143144
IO.println "Testing complex tree patterns..."
144145

145-
assertGlobResult "**/baz.txt deep search" "**/baz.txt"
146+
assertGlobResult "**/baz.txt"
146147
globTestExample2
147148
(some (tree! "Root" { "foo" { "bar" { "baz.txt" } } }))
148149

149-
assertGlobResult "**/delta.txt deep search" "**/delta.txt"
150+
assertGlobResult "**/delta.txt"
150151
globTestExample2
151152
(some (tree! "Root" { "alpha" { "beta" { "gamma" { "delta.txt" } } } }))
152153

153-
assertGlobResult "**/file.txt search" "**/file.txt"
154+
assertGlobResult "**/file.txt"
154155
globTestExample2
155156
(some (tree! "Root" { "foo" { "file.txt" } }))
156157

157-
assertGlobResult "**/qux.md search" "**/qux.md"
158+
assertGlobResult "**/qux.md"
158159
globTestExample2
159160
(some (tree! "Root" { "foo" { "bar" { "qux.md" } } }))
160161

161-
assertGlobResult "Root exact match" "Root"
162+
assertGlobResult "Root"
162163
globTestExample2
163164
(some (tree! "Root" {}))
164165

165-
assertGlobResult "Root/* first level" "Root/*"
166+
assertGlobResult "Root/*"
166167
globTestExample2
167168
(some (Tree.dir "Root" [Tree.dir "foo" [], Tree.dir "foo2" [], Tree.dir "alpha" [], Tree.dir "zeta" []]))
168169

169-
assertGlobResult "Root/** full tree" "Root/**"
170+
assertGlobResult "Root/**"
170171
globTestExample2
171172
(some globTestExample2)
172173

173174
-- Multi-level pattern tests
174175
def testMultiLevelPatterns : IO Unit := do
175176
IO.println "Testing multi-level patterns..."
176177

177-
assertGlobResult "Root/**/bar/* nested wildcard" "Root/**/bar/*"
178+
assertGlobResult "Root/**/bar/*"
178179
globTestExample2
179180
(some (tree! "Root" { "foo" { "bar" { "baz.txt", "qux.md" } }, "foo2" { "bar" { "baz2.txt", "qux2.md" } } }))
180181

181-
assertGlobResult "Root/**/delta.txt deep nested" "Root/**/delta.txt"
182+
assertGlobResult "Root/**/delta.txt"
182183
globTestExample2
183184
(some (tree! "Root" { "alpha" { "beta" { "gamma" { "delta.txt" } } } }))
184185

185-
assertGlobResult "Root/**/file.txt nested search" "Root/**/file.txt"
186+
assertGlobResult "Root/**/file.txt"
186187
globTestExample2
187188
(some (tree! "Root" { "foo" { "file.txt" } }))
188189

189-
assertGlobResult "Root/*/*/*/delta.txt exact depth" "Root/*/*/*/delta.txt"
190+
assertGlobResult "Root/*/*/*/delta.txt"
190191
globTestExample2
191192
(some (tree! "Root" { "alpha" { "beta" { "gamma" { "delta.txt" } } } }))
192193

193194
-- Specific path combinations
194195
def testSpecificCombinations : IO Unit := do
195196
IO.println "Testing specific path combinations..."
196197

197-
assertGlobResult "Root/foo/**/baz.txt" "Root/foo/**/baz.txt"
198+
assertGlobResult "Root/foo/**/baz.txt"
198199
globTestExample2
199200
(some (tree! "Root" { "foo" { "bar" { "baz.txt" } } }))
200201

201-
assertGlobResult "Root/foo/*/baz.txt" "Root/foo/*/baz.txt"
202+
assertGlobResult "Root/foo/*/baz.txt"
202203
globTestExample2
203204
(some (tree! "Root" { "foo" { "bar" { "baz.txt" } } }))
204205

205-
assertGlobResult "Root/foo/*/doesntexist.txt no match" "Root/foo/*/doesntexist.txt"
206+
assertGlobResult "Root/foo/*/doesntexist.txt"
206207
globTestExample2
207208
none
208209

209-
assertGlobResult "Root/foo/bar/baz.txt exact" "Root/foo/bar/baz.txt"
210+
assertGlobResult "Root/foo/bar/baz.txt"
210211
globTestExample2
211212
(some (tree! "Root" { "foo" { "bar" { "baz.txt" } } }))
212213

213-
assertGlobResult "Root/foo/bar/baz.txt/extra invalid" "Root/foo/bar/baz.txt/extra"
214+
assertGlobResult "Root/foo/bar/baz.txt/extra"
214215
globTestExample2
215216
none
216217

217-
assertGlobResult "Root/foo/bar/notfound.txt no match" "Root/foo/bar/notfound.txt"
218+
assertGlobResult "Root/foo/bar/notfound.txt"
218219
globTestExample2
219220
none
220221

221222
-- GlobMany tests using assertGlobManyResult
222223
def testGlobMany : IO Unit := do
223224
IO.println "Testing globMany patterns..."
224225

225-
assertGlobManyResult "GlobMany test 1" nel!["Glob/A", "Glob/B"]
226-
(tree! "Glob" { "A" {}, "B" {} })
227-
(some (tree! "Glob" { "A" {}, "B" {} }))
228-
229-
assertGlobManyResult "GlobMany test 2" nel!["Glob/A", "Glob/C"]
230-
(tree! "Glob" { "A" {}, "B" {} })
231-
(some (tree! "Glob" { "A" {} }))
232-
233-
assertGlobManyResult "GlobMany test 3" nel!["**/X", "**/Y"]
234-
globTestExample1
235-
(some (tree! "Glob" { "A" { "X" {} }, "B" { "Y" {} } }))
236-
237-
assertGlobManyResult "GlobMany test 4" nel!["**/baz.txt", "**/delta.txt"]
226+
assertGlobManyResult nel!["Glob/A", "Glob/B"] (tree! "Glob" { "A" {}, "B" {} }) (some (tree! "Glob" { "A" {}, "B" {} }))
227+
assertGlobManyResult nel!["Glob/A", "Glob/C"] (tree! "Glob" { "A" {}, "B" {} }) (some (tree! "Glob" { "A" {} }))
228+
assertGlobManyResult nel!["**/X", "**/Y"] globTestExample1 (some (tree! "Glob" { "A" { "X" {} }, "B" { "Y" {} } }))
229+
assertGlobManyResult nel!["**/baz.txt", "**/delta.txt"]
238230
globTestExample2
239231
(some (tree! "Root" {
240232
"foo" { "bar" { "baz.txt" } },
241233
"alpha" { "beta" { "gamma" { "delta.txt" } } }
242234
}))
243235

244-
assertGlobManyResult "GlobMany test 5" nel!["**/file.txt", "**/qux.md"]
245-
globTestExample2
246-
(some (tree! "Root" { "foo" { "file.txt", "bar" { "qux.md" } } }))
236+
assertGlobManyResult nel!["**/file.txt", "**/qux.md"] globTestExample2 (some (tree! "Root" { "foo" { "file.txt", "bar" { "qux.md" } } }))
247237

248-
assertGlobManyResult "GlobMany test 6" nel!["**/doesntexist.txt", "**/missing.txt"]
249-
globTestExample2
250-
none
238+
assertGlobManyResult nel!["**/doesntexist.txt", "**/missing.txt"] globTestExample2 none
251239

252-
assertGlobManyResult "GlobMany test 7" nel!["Root/foo/bar/baz.txt", "Root/foo2/bar/qux2.md"]
240+
assertGlobManyResult nel!["Root/foo/bar/baz.txt", "Root/foo2/bar/qux2.md"]
253241
globTestExample2
254242
(some (tree! "Root" {
255243
"foo" { "bar" { "baz.txt" } },
@@ -275,4 +263,4 @@ def runGlobTests : IO Unit := do
275263
IO.println s!"❌ Glob tests failed: {e}"
276264
throw e
277265

278-
end
266+
end GlobSpec

Test/Main.lean

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ open System (FilePath)
2323
`mkTestGlob pat t expected` returns a pair
2424
```lean
2525
(testName, IO Unit)
26+
```
27+
2628
where testName is derived from pat, and the IO Unit runs runGlobTest.
2729
2830
pat is the pattern string (the first argument to #testGlob)
@@ -31,8 +33,9 @@ t is the Tree to search in
3133
3234
expected is the Option Tree you expect back
3335
-/
36+
3437
-- def main : IO Unit := return
3538
def main : IO Unit := do
3639
GlobSpec.runGlobTests
3740
-- runTests #[
38-
-- ("FindRecursive", fun (currentTmpDir : FilePath) => do -- TODO: this is a limitation of glob-posix, no support of recursion
41+
-- ("FindRecursive", fun (currentTmpDir : FilePath) => do -- TODO: this is a limitation of glob-posix, no support of recursion

0 commit comments

Comments
 (0)