-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lua
110 lines (89 loc) · 2.96 KB
/
test.lua
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
--[[
Run these tests with `:so`
--]]
package.loaded['ufind.query'] = nil
package.loaded['ufind.arg'] = nil
package.loaded['ufind.ansi'] = nil
package.loaded['ufind.util'] = nil -- clear out transitive deps as well
local split_cmd = require'ufind.arg'.split_cmd_aux
local parse = require'ufind.ansi'.parse
local query = require'ufind.query'
local match = query.match
local find_last_newline = require'ufind.util'.find_last_newline
local default_match = require'ufind.matcher.default'
local function asserteq(a, b)
assert(
vim.deep_equal(a, b),
string.format('Expected %s to equal %s', vim.inspect(a), vim.inspect(b))
)
end
local find_min_subsequence = require'ufind.helper.find_min_subsequence'
asserteq(
find_min_subsequence('~/foo/bar/foo.txt', 'foo'),
{11, 12, 13}
)
asserteq(
find_min_subsequence('~/foo/bar/frodo.txt', 'foo'),
{3, 4, 5}
)
asserteq(
find_min_subsequence('~/foo/bar/frodo.txt', 'oof'),
{4, 5, 11}
)
asserteq(
find_min_subsequence('~/foo/bar/frodo.txt', 'to'),
nil
)
local pat = require'ufind.arg'.inject_empty_captures '^(.*)$'
local pat_colon = require'ufind.arg'.inject_empty_captures '^([^:]-):(.*)$'
asserteq(
match({'x'}, {'x.lua', 'y.lua'}, pat, default_match),
{{index = 1, positions = {1}, score = 1}}
)
asserteq(
match({'fil', 'foo'}, {'file.lua: print(foo)'}, pat_colon, default_match),
{{index = 1, positions = {1, 2, 3, 17, 18, 19}, score = 12}}
)
asserteq(
match({'lua$', 'a'}, {'file.lua: a', 'file.c: x'}, pat_colon, default_match),
{{index = 1, positions = {11}, score = 1}}
)
asserteq(
match({'', 'zxczxc'}, {'file: a', 'file: x'}, pat_colon, default_match),
{}
)
asserteq(
match({'', 'zxc'}, {'file: zxc', 'file: x'}, pat_colon, default_match),
{{index = 1, positions = {7, 8, 9}, score = 5}}
)
asserteq(
match({'', 'sub'}, {'file: s:sub()', 'file: x'}, pat_colon, default_match),
{{index = 1, positions = {9, 10, 11}, score = 5}}
)
-- Exact match fails
asserteq(
match({"'la"}, {'foo.lua', 'bar.lua'}, pat, default_match),
{}
)
-- Exact match succeeds
asserteq(
match({"'fo"}, {'foo.lua', 'bar.lua'}, pat, default_match),
{{index = 1, positions = {}, score = 0}}
)
asserteq(split_cmd('rg -f'), {'rg', '-f'})
asserteq(split_cmd(' rg -f '), {'rg', '-f'})
asserteq(split_cmd('rg -f x'), {'rg', '-f', 'x'})
asserteq(split_cmd([[rg -f "/Some path/"]]), {'rg', '-f', '/Some path/'})
asserteq(split_cmd([[rg -f '/Some path/']]), {'rg', '-f', '/Some path/'})
asserteq(split_cmd([[rg -f "/Some 'path'/"]]), {'rg', '-f', "/Some 'path'/"})
asserteq(parse({[[[35mtest:[mYo]]}), {'test:Yo'})
asserteq(parse({[[[01;31m[Kex[m[K =]]}), {'ex ='})
asserteq(
parse({[[[0m[35mufind.lua[0m:[0m[32m25[0m: open({'~/[0m[1m[31mfoo[0m'})]]}),
{"ufind.lua:25: open({'~/foo'})"}
)
asserteq(find_last_newline("aa\naa"), 3)
asserteq(find_last_newline("aaaa\n"), 5)
asserteq(find_last_newline("\na"), 1)
asserteq(find_last_newline("aaaa"), nil)
print('ok')