Skip to content

Commit

Permalink
And split the abbreviations tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian committed Oct 20, 2023
1 parent 56c006e commit d88b276
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 93 deletions.
180 changes: 87 additions & 93 deletions lua/tests/abbreviations/abbreviations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,107 +11,101 @@ end

require('lean').setup {}

for _, ft in pairs{'lean3', 'lean'} do

describe('unicode abbreviation expansion', function()
describe(ft, function()
it('autoexpands abbreviations', helpers.clean_buffer(ft, '', function()
helpers.insert[[\a]]
assert.contents.are[[α]]
it('autoexpands abbreviations', helpers.clean_buffer('lean', '', function()
helpers.insert[[\a]]
assert.contents.are[[α]]
end))

describe('explicit triggers', function()
it('inserts a space on <Space>', helpers.clean_buffer('lean', '', function()
helpers.insert[[\e<Space>]]
wait_for_expansion()
assert.contents.are[[ε ]]
end))

it('inserts a newline on <CR>', helpers.clean_buffer('lean', '', function()
helpers.insert[[\e<CR>]]
wait_for_expansion()
assert.contents.are('ε\n')
end))

describe('explicit triggers', function()
it('inserts a space on <Space>', helpers.clean_buffer(ft, '', function()
helpers.insert[[\e<Space>]]
wait_for_expansion()
assert.contents.are[[ε ]]
end))

it('inserts a newline on <CR>', helpers.clean_buffer(ft, '', function()
helpers.insert[[\e<CR>]]
wait_for_expansion()
assert.contents.are('ε\n')
end))

it('inserts nothing on <Tab>', helpers.clean_buffer(ft, '', function()
helpers.insert[[\e<Tab>]]
wait_for_expansion()
assert.contents.are[[ε]]
end))

it('leaves the cursor in the right spot on <Tab>', helpers.clean_buffer(ft, '', function()
helpers.insert[[\<<Tab>abc]]
wait_for_expansion()
assert.contents.are[[⟨abc]]
end))

it('inserts nothing on <Tab> mid-line', helpers.clean_buffer(ft, 'foo bar baz quux,', function()
vim.cmd('normal $')
helpers.insert[[ \comp<Tab> spam]]
wait_for_expansion()
assert.contents.are[[foo bar baz quux ∘ spam,]]
end))

it('does not interfere with existing mappings', helpers.clean_buffer(ft, '', function()
vim.api.nvim_buf_set_keymap(
0,
'i',
'<Tab>',
'<C-o>:lua vim.b.foo = 12<CR>',
{ noremap = true }
)
helpers.insert[[\e<Tab>]]
wait_for_expansion()
assert.contents.are[[ε]]
assert.falsy(vim.b.foo)
helpers.insert[[<Tab>]]
assert.contents.are[[ε]]
assert.are.same(vim.b.foo, 12)

vim.api.nvim_buf_del_keymap(0, 'i', '<Tab>')
end))

it('does not interfere with existing lua mappings', helpers.clean_buffer(ft, '', function()
vim.b.foo = 0
local inc = function() vim.b.foo = vim.b.foo + 1 end

assert.is.equal(0, #vim.api.nvim_buf_get_keymap(0, 'i'))
vim.keymap.set('i', '<Tab>', inc, { buffer = 0, noremap = true })
assert.is.equal(1, #vim.api.nvim_buf_get_keymap(0, 'i'))

assert.are.same(vim.b.foo, 0)
helpers.insert[[<Tab>]]
assert.are.same(vim.b.foo, 1)

helpers.insert[[\e<Tab>]]
wait_for_expansion()
assert.contents.are[[ε]]
assert.are.same(vim.b.foo, 1)
helpers.insert[[<Tab>]]
assert.contents.are[[ε]]
assert.are.same(vim.b.foo, 2)
vim.api.nvim_buf_del_keymap(0, 'i', '<Tab>')
end))
end)

-- Really this needs to place the cursor too, but for now we just strip
it('handles placing the $CURSOR', helpers.clean_buffer(ft, '', function()
helpers.insert[[foo \<><Tab>bar, baz]]
assert.is.equal('foo ⟨bar, baz⟩', vim.api.nvim_get_current_line())
it('inserts nothing on <Tab>', helpers.clean_buffer('lean', '', function()
helpers.insert[[\e<Tab>]]
wait_for_expansion()
assert.contents.are[[ε]]
end))

it('expands mid-word', helpers.clean_buffer(ft, '', function()
helpers.insert[[(\a]]
assert.contents.are[[]]
it('leaves the cursor in the right spot on <Tab>', helpers.clean_buffer('lean', '', function()
helpers.insert[[\<<Tab>abc]]
wait_for_expansion()
assert.contents.are[[⟨abc]]
end))

it('expands abbreviations in command mode', helpers.clean_buffer(ft, '', function()
helpers.insert[[foo ε bar]]
it('inserts nothing on <Tab> mid-line', helpers.clean_buffer('lean', 'foo bar baz quux,', function()
vim.cmd('normal $')
helpers.feed[[q/a\e<Space><CR>ibaz]]
assert.is.equal('foo bazε bar', vim.api.nvim_get_current_line())
helpers.insert[[ \comp<Tab> spam]]
wait_for_expansion()
assert.contents.are[[foo bar baz quux ∘ spam,]]
end))

it('does not interfere with existing mappings', helpers.clean_buffer('lean', '', function()
vim.api.nvim_buf_set_keymap(
0,
'i',
'<Tab>',
'<C-o>:lua vim.b.foo = 12<CR>',
{ noremap = true }
)
helpers.insert[[\e<Tab>]]
wait_for_expansion()
assert.contents.are[[ε]]
assert.falsy(vim.b.foo)
helpers.insert[[<Tab>]]
assert.contents.are[[ε]]
assert.are.same(vim.b.foo, 12)

vim.api.nvim_buf_del_keymap(0, 'i', '<Tab>')
end))

it('does not interfere with existing lua mappings', helpers.clean_buffer('lean', '', function()
vim.b.foo = 0
local inc = function() vim.b.foo = vim.b.foo + 1 end

assert.is.equal(0, #vim.api.nvim_buf_get_keymap(0, 'i'))
vim.keymap.set('i', '<Tab>', inc, { buffer = 0, noremap = true })
assert.is.equal(1, #vim.api.nvim_buf_get_keymap(0, 'i'))

assert.are.same(vim.b.foo, 0)
helpers.insert[[<Tab>]]
assert.are.same(vim.b.foo, 1)

helpers.insert[[\e<Tab>]]
wait_for_expansion()
assert.contents.are[[ε]]
assert.are.same(vim.b.foo, 1)
helpers.insert[[<Tab>]]
assert.contents.are[[ε]]
assert.are.same(vim.b.foo, 2)
vim.api.nvim_buf_del_keymap(0, 'i', '<Tab>')
end))
end)
end)

end
-- Really this needs to place the cursor too, but for now we just strip
it('handles placing the $CURSOR', helpers.clean_buffer('lean', '', function()
helpers.insert[[foo \<><Tab>bar, baz]]
assert.is.equal('foo ⟨bar, baz⟩', vim.api.nvim_get_current_line())
end))

it('expands mid-word', helpers.clean_buffer('lean', '', function()
helpers.insert[[(\a]]
assert.contents.are[[]]
end))

it('expands abbreviations in command mode', helpers.clean_buffer('lean', '', function()
helpers.insert[[foo ε bar]]
vim.cmd('normal $')
helpers.feed[[q/a\e<Space><CR>ibaz]]
assert.is.equal('foo bazε bar', vim.api.nvim_get_current_line())
end))
end)
111 changes: 111 additions & 0 deletions lua/tests/lean3/abbreviations_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
local helpers = require('tests.helpers')

-- Wait some time for the abbreviation to have been expanded.
-- This is very naive, it just ensures the line contains no `\`.
local function wait_for_expansion()
vim.wait(1000, function()
local contents = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), '\n')
return not contents:match[[\]]
end)
end

require('lean').setup {}

describe('unicode abbreviation expansion', function()
it('autoexpands abbreviations', helpers.clean_buffer('lean3', '', function()
helpers.insert[[\a]]
assert.contents.are[[α]]
end))

describe('explicit triggers', function()
it('inserts a space on <Space>', helpers.clean_buffer('lean3', '', function()
helpers.insert[[\e<Space>]]
wait_for_expansion()
assert.contents.are[[ε ]]
end))

it('inserts a newline on <CR>', helpers.clean_buffer('lean3', '', function()
helpers.insert[[\e<CR>]]
wait_for_expansion()
assert.contents.are('ε\n')
end))

it('inserts nothing on <Tab>', helpers.clean_buffer('lean3', '', function()
helpers.insert[[\e<Tab>]]
wait_for_expansion()
assert.contents.are[[ε]]
end))

it('leaves the cursor in the right spot on <Tab>', helpers.clean_buffer('lean3', '', function()
helpers.insert[[\<<Tab>abc]]
wait_for_expansion()
assert.contents.are[[⟨abc]]
end))

it('inserts nothing on <Tab> mid-line', helpers.clean_buffer('lean3', 'foo bar baz quux,', function()
vim.cmd('normal $')
helpers.insert[[ \comp<Tab> spam]]
wait_for_expansion()
assert.contents.are[[foo bar baz quux ∘ spam,]]
end))

it('does not interfere with existing mappings', helpers.clean_buffer('lean3', '', function()
vim.api.nvim_buf_set_keymap(
0,
'i',
'<Tab>',
'<C-o>:lua vim.b.foo = 12<CR>',
{ noremap = true }
)
helpers.insert[[\e<Tab>]]
wait_for_expansion()
assert.contents.are[[ε]]
assert.falsy(vim.b.foo)
helpers.insert[[<Tab>]]
assert.contents.are[[ε]]
assert.are.same(vim.b.foo, 12)

vim.api.nvim_buf_del_keymap(0, 'i', '<Tab>')
end))

it('does not interfere with existing lua mappings', helpers.clean_buffer('lean3', '', function()
vim.b.foo = 0
local inc = function() vim.b.foo = vim.b.foo + 1 end

assert.is.equal(0, #vim.api.nvim_buf_get_keymap(0, 'i'))
vim.keymap.set('i', '<Tab>', inc, { buffer = 0, noremap = true })
assert.is.equal(1, #vim.api.nvim_buf_get_keymap(0, 'i'))

assert.are.same(vim.b.foo, 0)
helpers.insert[[<Tab>]]
assert.are.same(vim.b.foo, 1)

helpers.insert[[\e<Tab>]]
wait_for_expansion()
assert.contents.are[[ε]]
assert.are.same(vim.b.foo, 1)
helpers.insert[[<Tab>]]
assert.contents.are[[ε]]
assert.are.same(vim.b.foo, 2)
vim.api.nvim_buf_del_keymap(0, 'i', '<Tab>')
end))
end)

-- Really this needs to place the cursor too, but for now we just strip
it('handles placing the $CURSOR', helpers.clean_buffer('lean3', '', function()
helpers.insert[[foo \<><Tab>bar, baz]]
assert.is.equal('foo ⟨bar, baz⟩', vim.api.nvim_get_current_line())
end))

it('expands mid-word', helpers.clean_buffer('lean3', '', function()
helpers.insert[[(\a]]
assert.contents.are[[]]
end))

it('expands abbreviations in command mode', helpers.clean_buffer('lean3', '', function()
helpers.insert[[foo ε bar]]
vim.cmd('normal $')
helpers.feed[[q/a\e<Space><CR>ibaz]]
assert.is.equal('foo bazε bar', vim.api.nvim_get_current_line())
end))
end)

0 comments on commit d88b276

Please sign in to comment.