forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.livecodescript
More file actions
322 lines (271 loc) · 9.69 KB
/
Copy pathfilter.livecodescript
File metadata and controls
322 lines (271 loc) · 9.69 KB
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
script "CoreArrayFilter"
/*
Copyright (C) 2016 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/*
Filter wildcard special characters:
* - matches any number of occurrences of any character
? - matches a single occurrence of any character
[abc] - matches any of a, b or c
[a-m] - matches any lowercase character in the first half of the alphabet
[!abc] - matches any character which is not a, b or c
The last three of these can be combined to create more complex character classes, for
example [!abcA-C] - matches any character which is not a, b or c (upper or lower case)
*/
on __testFilterRangeAndOptions pIsNative, pLower, pMid, pUpper, pKeys
local tTestType
if pIsNative then
put "native" into tTestType
else
put "non-native" into tTestType
end if
if pKeys then
put "array keys " before tTestType
else
put "array elements " before tTestType
end if
local tSource, tPattern
# Option tests
put pLower into tSource[pLower]
put "[" & pLower & pMid & "]" into tPattern
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter with matching option pattern" && tTestType, the keys of tSource is pLower
put empty into tSource
put pUpper into tSource[pUpper]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter with non-matching option pattern" && tTestType, the keys of tSource is empty
put "[!" & pLower & pMid & "]" into tPattern
put empty into tSource
put pUpper into tSource[pUpper]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter with matching negated option pattern" && tTestType, the keys of tSource is pUpper
# Range tests
put empty into tSource
put pMid into tSource[pMid]
put "[" & pLower & "-" & pUpper & "]" into tPattern
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter with match within range pattern" && tTestType, the keys of tSource is pMid
put empty into tSource
put pLower into tSource[pLower]
put "[" & pLower & "-" & pMid & "]" into tPattern
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter with match on range pattern lower bound" && tTestType, the keys of tSource is pLower
put empty into tSource
put pMid into tSource[pMid]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter with match on range pattern upper bound" && tTestType, the keys of tSource is pMid
put empty into tSource
put pUpper into tSource[pUpper]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestDiagnostic tSource
TestAssert "filter with non-match outside range pattern" && tTestType, the keys of tSource is empty
put empty into tSource
put pUpper into tSource[pUpper]
put "[!" & pLower & "-" & pMid & "]" into tPattern
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter with match outside negated range pattern" && tTestType, the keys of tSource is pUpper
# Range options
put empty into tSource
put pUpper into tSource[pUpper]
put "[" & pLower & "-" & pMid & pLower & "-" & pUpper & "]" into tPattern
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter with match within option range pattern" && tTestType, the keys of tSource is pUpper
# Range + options
put empty into tSource
put pUpper into tSource[pUpper]
put "[" & pLower & "-" & pMid & pLower & pMid & pUpper & "]" into tPattern
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter with matching option after range pattern" && tTestType, the keys of tSource is pUpper
end __testFilterRangeAndOptions
on __testFilterWildcard pIsNative, pString, pKeys
if pString is empty then
TestFail "filter wildcard string not valid"
exit __testFilterWildcard
end if
# Single character wildcard
local tSource, tPattern, tTestType
if pKeys then
put "array keys" into tTestType
else
put "array elements" into tTestType
end if
put "?" into tPattern
if pIsNative then
put empty into tSource[empty]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter single char wildcard doesn't match empty" && tTestType, the keys of tSource is empty
end if
if pIsNative then
put " native" after tTestType
else
put " non-native" after tTestType
end if
local tChar
put any char of pString into tChar
put empty into tSource
put tChar into tSource[tChar]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter single char wildcard matches single char" && tTestType, the keys of tSource is tChar
put empty into tSource
put tChar & tChar into tSource[tChar & tChar]
filter tSource with tPattern
TestAssert "filter single char wildcard doesn't match multiple chars" && tTestType, the keys of tSource is empty
put pString into tPattern
put "?" into any char of tPattern
put pString into tSource[pString]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter single char wildcard matches in string" && tTestType, the keys of tSource is pString
# Asterisk
put "*" into tPattern
if pIsNative then
put empty into tSource
put empty into tSource[empty]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter asterisk matches empty", the number of elements of tSource is 1
end if
put empty into tSource
put pString into tSource[pString]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter asterisk matches multiple chars" && tTestType, the keys of tSource is pString
put pString & "**" into tPattern
put empty into tSource
put pString into tSource[pString]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter asterisks at end don't affect match" && tTestType, the keys of tSource is pString
put "*" & pString into tPattern
put empty into tSource
put pString into tSource[pString]
if pKeys then
filter keys of tSource with tPattern
else
filter elements of tSource with tPattern
end if
TestAssert "filter simple asterisk recursion" && tTestType, the keys of tSource is pString
end __testFilterWildcard
on TestFilterWildcardNative
__testFilterRangeAndOptions true, "a", "b", "c", false
__testFilterWildcard true, "abc", false
__testFilterRangeAndOptions true, "a", "b", "c", true
__testFilterWildcard true, "abc", true
end TestFilterWildcardNative
on TestFilterWildcardNonNative
local tGClef, tFClef, tCClef
put numToCodepoint(0x1d11e) into tGClef
put numToCodepoint(0x1d121) into tCClef
put numToCodepoint(0x1d122) into tFClef
__testFilterRangeAndOptions false, tGClef, tCClef, tFClef, true
__testFilterWildcard false, tGClef & tCClef & tFClef, true
__testFilterRangeAndOptions false, tGClef, tCClef, tFClef, false
__testFilterWildcard false, tGClef & tCClef & tFClef, false
end TestFilterWildcardNonNative
on TestFilterSourceConversion
local tSource, tTest
put "foo" into tSource["foo"]
put "bar" into tSource["bar"]
filter tSource with "f*" into tTest
TestAssert "filter implicit lines of array source", tTest is empty
filter lines of tSource with "f*" into tTest
TestAssert "filter explicit lines of array source", tTest is empty
filter tSource with "f*" into tTest
TestAssert "filter explicit items of array source", tTest is empty
put "foo" & return & "bar" into tSource
filter keys of tSource with "f*" into tTest
TestAssert "filter explicit keys of string source", tTest is empty
filter elements of tSource with "f*" into tTest
TestAssert "filter explicit elements of string source", tTest is empty
end TestFilterSourceConversion
on TestFilterRegex
local tSource, tTest
put "foo" into tSource["foo"]
put "bar" into tSource["bar"]
filter keys of tSource with regex pattern "f.*" into tTest
TestAssert "filter keys of array with regex", the keys of tTest is "foo"
put "foo" into tSource["foo"]
put "bar" into tSource["bar"]
filter elements of tSource with regex pattern "f.*" into tTest
TestAssert "filter elements of array with regex", the keys of tTest is "foo"
end TestFilterRegex
on TestFilterExpression
local tSource, tTest
put "foo" into tSource["foo"]
put "bar" into tSource["bar"]
filter keys of tSource where each begins with "f" into tTest
TestAssert "filter keys of array where expression", the keys of tTest is "foo"
put "foo" into tSource["foo"]
put "bar" into tSource["bar"]
filter elements of tSource where each begins with "f" into tTest
TestAssert "filter elements of array where expression", the keys of tTest is "foo"
end TestFilterExpression