Skip to content

Commit a8743ef

Browse files
committed
test: move common code to helpers
1 parent eae3048 commit a8743ef

File tree

4 files changed

+74
-121
lines changed

4 files changed

+74
-121
lines changed

test/functional/core_spec.lua

+9-46
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,18 @@
11
local helpers = require('helpers')
2-
local Screen = require('test.functional.ui.screen')
3-
local clear, command, eval = helpers.clear, helpers.command, helpers.eval
4-
local feed, alter_slashes = helpers.feed, helpers.alter_slashes
2+
local command, eval = helpers.command, helpers.eval
3+
local feed = helpers.feed
54
local insert = helpers.insert
6-
local eq, neq, ok = helpers.eq, helpers.neq, helpers.ok
5+
local eq, neq = helpers.eq, helpers.neq
76
local sleep = vim and vim.uv and vim.uv.sleep or helpers.sleep
87
local exec_lua = helpers.exec_lua
8+
local setup_test_snippets = helpers.setup_test_snippets
99

1010
describe('Core', function()
1111
local screen
12-
local snippy_src = os.getenv('SNIPPY_PATH') or '.'
13-
14-
local function setup_test_snippets()
15-
exec_lua(string.format(
16-
[[
17-
snippy.setup({
18-
snippet_dirs = '%s',
19-
enable_auto = true,
20-
expand_options = {
21-
c = function()
22-
return vim.startswith(vim.api.nvim_get_current_line(), '#')
23-
end
24-
}
25-
})]],
26-
alter_slashes(snippy_src .. '/test/snippets/')
27-
))
28-
end
2912

3013
before_each(function()
31-
clear()
32-
screen = Screen.new(50, 5)
33-
screen:attach()
34-
35-
local defaults = {
36-
[1] = { foreground = Screen.colors.Blue1, bold = true },
37-
[2] = { bold = true },
38-
[3] = { background = Screen.colors.LightGrey },
39-
}
40-
41-
if eval('has("nvim-0.10")') > 0 then
42-
command('colorscheme vim')
43-
defaults[3] = { background = Screen.colors.LightGrey, foreground = Screen.colors.Black }
44-
end
45-
46-
screen:set_default_attr_ids(defaults)
47-
48-
command('set rtp=$VIMRUNTIME')
49-
command('set rtp+=' .. alter_slashes(snippy_src))
50-
command('runtime plugin/snippy.lua')
51-
command('lua snippy = require("snippy")')
52-
exec_lua([[snippy.setup({ choice_delay = 0 })]])
14+
helpers.before_each()
15+
screen = helpers.screen
5316
end)
5417

5518
after_each(function()
@@ -274,7 +237,7 @@ describe('Core', function()
274237
})
275238

276239
eq(true, exec_lua([[return snippy.is_active()]]))
277-
ok(exec_lua([[return snippy.can_jump(1)]]))
240+
eq(true, exec_lua([[return snippy.can_jump(1)]]))
278241
feed('<plug>(snippy-next)')
279242

280243
screen:expect({
@@ -288,7 +251,7 @@ describe('Core', function()
288251
})
289252

290253
eq(true, exec_lua([[return snippy.is_active()]]))
291-
ok(exec_lua([[return snippy.can_jump(1)]]))
254+
eq(true, exec_lua([[return snippy.can_jump(1)]]))
292255
feed('<plug>(snippy-next)')
293256

294257
screen:expect({
@@ -321,7 +284,7 @@ describe('Core', function()
321284
})
322285

323286
eq(true, exec_lua([[return snippy.is_active()]]))
324-
ok(exec_lua([[return snippy.can_jump(1)]]))
287+
eq(true, exec_lua([[return snippy.can_jump(1)]]))
325288
feed('<plug>(snippy-next)')
326289

327290
screen:expect({

test/functional/helpers.lua

+55-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,62 @@
1+
local luassert = require('luassert')
2+
local Screen = require('test.functional.ui.screen')
3+
14
local ok, helpers = pcall(require, 'test.functional.testnvim')
25
if not ok then
36
ok, helpers = pcall(require, 'test.functional.helpers')
47
end
5-
helpers = helpers()
68

7-
local ok2, testutil = pcall(require, 'test.testutil')
8-
if ok2 then
9-
helpers.eq = testutil.eq
10-
helpers.neq = testutil.neq
11-
helpers.ok = testutil.ok
9+
local snippy_src = os.getenv('SNIPPY_PATH') or '.'
10+
11+
local H = helpers()
12+
13+
function H.eq(expected, actual, context)
14+
return luassert.are.same(expected, actual, context)
15+
end
16+
17+
function H.neq(expected, actual, context)
18+
return luassert.are_not.same(expected, actual, context)
19+
end
20+
21+
H.setup_test_snippets = function()
22+
H.exec_lua(string.format(
23+
[[
24+
snippy.setup({
25+
snippet_dirs = '%s',
26+
enable_auto = true,
27+
expand_options = {
28+
c = function()
29+
return vim.startswith(vim.api.nvim_get_current_line(), '#')
30+
end
31+
}
32+
})]],
33+
H.alter_slashes(snippy_src .. '/test/snippets/')
34+
))
35+
end
36+
37+
H.before_each = function()
38+
H.clear()
39+
H.screen = Screen.new(50, 5)
40+
H.screen:attach()
41+
42+
local defaults = {
43+
[1] = { foreground = Screen.colors.Blue1, bold = true },
44+
[2] = { bold = true },
45+
[3] = { background = Screen.colors.LightGrey },
46+
}
47+
48+
if H.fn.has('nvim-0.10') > 0 then
49+
H.command('colorscheme vim')
50+
defaults[3] = { background = Screen.colors.LightGrey, foreground = Screen.colors.Black }
51+
end
52+
53+
H.screen:set_default_attr_ids(defaults)
54+
55+
H.command('set rtp=$VIMRUNTIME')
56+
H.command('set rtp+=' .. H.alter_slashes(snippy_src))
57+
H.command('runtime plugin/snippy.lua')
58+
H.command('lua snippy = require("snippy")')
59+
H.exec_lua([[snippy.setup({ choice_delay = 0 })]])
1260
end
1361

14-
return helpers
62+
return H

test/functional/markers_spec.lua

+3-25
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
local helpers = require('helpers')
2-
local Screen = require('test.functional.ui.screen')
3-
local clear, command, eval = helpers.clear, helpers.command, helpers.eval
4-
local alter_slashes = helpers.alter_slashes
2+
local command, eval = helpers.command, helpers.eval
53
local exec_lua = helpers.exec_lua
64

75
describe('Virtual markers', function()
86
local screen
9-
local snippy_src = os.getenv('SNIPPY_PATH') or '.'
107

118
before_each(function()
12-
clear()
13-
screen = Screen.new(50, 5)
14-
screen:attach()
15-
16-
local defaults = {
17-
[1] = { foreground = Screen.colors.Blue1, bold = true },
18-
[2] = { bold = true },
19-
[3] = { background = Screen.colors.LightGrey },
20-
}
21-
22-
if eval('has("nvim-0.10")') > 0 then
23-
command('colorscheme vim')
24-
defaults[3] = { background = Screen.colors.LightGrey, foreground = Screen.colors.Black }
25-
end
26-
27-
screen:set_default_attr_ids(defaults)
28-
command('set rtp=$VIMRUNTIME')
29-
command('set rtp+=' .. alter_slashes(snippy_src))
30-
command('runtime plugin/snippy.lua')
31-
command('lua snippy = require("snippy")')
32-
exec_lua([[snippy.setup({ choice_delay = 0 })]])
9+
helpers.before_each()
10+
screen = helpers.screen
3311
end)
3412

3513
after_each(function()

test/functional/options_spec.lua

+7-43
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,17 @@
11
local helpers = require('helpers')
2-
local Screen = require('test.functional.ui.screen')
3-
local clear, command, eval = helpers.clear, helpers.command, helpers.eval
4-
local feed, alter_slashes = helpers.feed, helpers.alter_slashes
2+
local command = helpers.command
3+
local feed = helpers.feed
54
local insert = helpers.insert
6-
local eq, neq, ok, skip = helpers.eq, helpers.neq, helpers.ok, helpers.skip
7-
local sleep, exec_lua = helpers.sleep, helpers.exec_lua
5+
local eq = helpers.eq
6+
local exec_lua = helpers.exec_lua
7+
local setup_test_snippets = helpers.setup_test_snippets
88

99
describe('Options', function()
1010
local screen
11-
local snippy_src = os.getenv('SNIPPY_PATH') or '.'
12-
13-
local function setup_test_snippets()
14-
exec_lua(string.format(
15-
[[
16-
snippy.setup({
17-
snippet_dirs = '%s',
18-
enable_auto = true,
19-
expand_options = {
20-
c = function()
21-
return vim.startswith(vim.api.nvim_get_current_line(), '#')
22-
end
23-
}
24-
})]],
25-
alter_slashes(snippy_src .. '/test/snippets/')
26-
))
27-
end
2811

2912
before_each(function()
30-
clear()
31-
screen = Screen.new(50, 5)
32-
screen:attach()
33-
34-
local defaults = {
35-
[1] = { foreground = Screen.colors.Blue1, bold = true },
36-
[2] = { bold = true },
37-
[3] = { background = Screen.colors.LightGrey },
38-
}
39-
40-
if eval('has("nvim-0.10")') > 0 then
41-
command('colorscheme vim')
42-
defaults[3] = { background = Screen.colors.LightGrey, foreground = Screen.colors.Black }
43-
end
44-
45-
screen:set_default_attr_ids(defaults)
46-
command('set rtp=$VIMRUNTIME')
47-
command('set rtp+=' .. alter_slashes(snippy_src))
48-
command('runtime plugin/snippy.lua')
49-
command('lua snippy = require("snippy")')
50-
exec_lua([[snippy.setup({ choice_delay = 0 })]])
13+
helpers.before_each()
14+
screen = helpers.screen
5115
end)
5216

5317
after_each(function()

0 commit comments

Comments
 (0)