mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-18 19:30:02 -04:00
Initial sketch of automated indent tests
This commit is contained in:
parent
eefa5662e6
commit
67f2c7149c
14 changed files with 257 additions and 0 deletions
25
lua/nvim-treesitter/test_utils.lua
Normal file
25
lua/nvim-treesitter/test_utils.lua
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
function M.indent_whole_file(file)
|
||||||
|
assert.are.same(1, vim.fn.filereadable(file))
|
||||||
|
|
||||||
|
-- load reference file
|
||||||
|
vim.cmd(string.format('edit %s', file))
|
||||||
|
local reference = vim.api.nvim_buf_get_lines(0, 0, -1, true)
|
||||||
|
|
||||||
|
-- FIXME: why this is not set automatically?
|
||||||
|
vim.bo.indentexpr = 'nvim_treesitter#indent()'
|
||||||
|
assert.are.same('nvim_treesitter#indent()', vim.bo.indentexpr)
|
||||||
|
|
||||||
|
-- indent the whole file
|
||||||
|
vim.cmd 'normal gg=G'
|
||||||
|
local indented = vim.api.nvim_buf_get_lines(0, 0, -1, true)
|
||||||
|
|
||||||
|
-- clear any changes to avoid 'No write since last change (add ! to override)'
|
||||||
|
vim.cmd 'edit!'
|
||||||
|
|
||||||
|
-- compare before and after
|
||||||
|
assert.are.same(reference, indented)
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
11
lua/tests/indent/python/aligned_indent.py
Normal file
11
lua/tests/indent/python/aligned_indent.py
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
def aligned_indent(arg1,
|
||||||
|
arg2):
|
||||||
|
pass
|
||||||
|
|
||||||
|
aligned_indent(1,
|
||||||
|
2)
|
||||||
|
|
||||||
|
|
||||||
|
aligned_indent(1,
|
||||||
|
2
|
||||||
|
)
|
||||||
14
lua/tests/indent/python/basic_blocks.py
Normal file
14
lua/tests/indent/python/basic_blocks.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
from os import (
|
||||||
|
path,
|
||||||
|
name as OsName
|
||||||
|
)
|
||||||
|
|
||||||
|
def foo(x):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Foo:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def foo(self):
|
||||||
|
pass
|
||||||
23
lua/tests/indent/python/basic_collections.py
Normal file
23
lua/tests/indent/python/basic_collections.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
# list
|
||||||
|
a = [
|
||||||
|
1, 2,
|
||||||
|
3
|
||||||
|
]
|
||||||
|
|
||||||
|
# set
|
||||||
|
b = {
|
||||||
|
3,
|
||||||
|
4,
|
||||||
|
}
|
||||||
|
|
||||||
|
# dict
|
||||||
|
c = {
|
||||||
|
'a': 'b',
|
||||||
|
'c': 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
# tuple
|
||||||
|
d = (
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
)
|
||||||
27
lua/tests/indent/python/branches.py
Normal file
27
lua/tests/indent/python/branches.py
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
a = [
|
||||||
|
1, 2, 3]
|
||||||
|
|
||||||
|
b = [
|
||||||
|
x + 1
|
||||||
|
for x in range(3)]
|
||||||
|
|
||||||
|
c = [[[
|
||||||
|
1
|
||||||
|
]]]
|
||||||
|
|
||||||
|
d = [[[
|
||||||
|
4]]]
|
||||||
|
|
||||||
|
e = [[
|
||||||
|
1], 2, 3]
|
||||||
|
|
||||||
|
def foo(x, y):
|
||||||
|
pass
|
||||||
|
|
||||||
|
foo(
|
||||||
|
a,
|
||||||
|
b)
|
||||||
|
|
||||||
|
if (a and
|
||||||
|
b):
|
||||||
|
pass
|
||||||
25
lua/tests/indent/python/comprehensions.py
Normal file
25
lua/tests/indent/python/comprehensions.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
# list
|
||||||
|
a = [
|
||||||
|
x + 1 for x in range(3)
|
||||||
|
]
|
||||||
|
|
||||||
|
# dict
|
||||||
|
b = {
|
||||||
|
x: x + 1 for x in range(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
# generator
|
||||||
|
c = (
|
||||||
|
x * x for x in range(3)
|
||||||
|
)
|
||||||
|
|
||||||
|
# set
|
||||||
|
d = {
|
||||||
|
x + x for x in range(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
# other styles
|
||||||
|
e = [
|
||||||
|
x + 1 for x
|
||||||
|
in range(3)
|
||||||
|
]
|
||||||
24
lua/tests/indent/python/control_flow.py
Normal file
24
lua/tests/indent/python/control_flow.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
a, b, c, = 1, 2, 3
|
||||||
|
|
||||||
|
if a == a:
|
||||||
|
x = 1
|
||||||
|
elif b:
|
||||||
|
x = 2
|
||||||
|
else:
|
||||||
|
x = 3
|
||||||
|
|
||||||
|
while False:
|
||||||
|
pass
|
||||||
|
|
||||||
|
for _ in range(3):
|
||||||
|
pass
|
||||||
|
|
||||||
|
with open('/tmp/f', 'w') as f:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
pass
|
||||||
6
lua/tests/indent/python/hanging_indent.py
Normal file
6
lua/tests/indent/python/hanging_indent.py
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
def hanging_indent(
|
||||||
|
arg1, arg2):
|
||||||
|
pass
|
||||||
|
|
||||||
|
hanging_indent(
|
||||||
|
1, 2)
|
||||||
8
lua/tests/indent/python/join_lines.py
Normal file
8
lua/tests/indent/python/join_lines.py
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
a = 2 \
|
||||||
|
+ 2
|
||||||
|
|
||||||
|
b = 'hello' \
|
||||||
|
'world'
|
||||||
|
|
||||||
|
c = lambda x: \
|
||||||
|
x + 3
|
||||||
41
lua/tests/indent/python/nested_collections.py
Normal file
41
lua/tests/indent/python/nested_collections.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
a = [
|
||||||
|
1,
|
||||||
|
[
|
||||||
|
2,
|
||||||
|
[
|
||||||
|
3
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
b = [
|
||||||
|
1, [[
|
||||||
|
3
|
||||||
|
],
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
c = [[[
|
||||||
|
3
|
||||||
|
]]]
|
||||||
|
|
||||||
|
d = {
|
||||||
|
'a': [
|
||||||
|
2, 3
|
||||||
|
],
|
||||||
|
'c': (
|
||||||
|
[1, 2, 3],
|
||||||
|
[
|
||||||
|
2,
|
||||||
|
4
|
||||||
|
], {
|
||||||
|
6,
|
||||||
|
8
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
e = (1, 2,
|
||||||
|
3, 4,
|
||||||
|
5, 6
|
||||||
|
)
|
||||||
17
lua/tests/indent/python/strings.py
Normal file
17
lua/tests/indent/python/strings.py
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
a = """
|
||||||
|
String A
|
||||||
|
"""
|
||||||
|
|
||||||
|
b = """
|
||||||
|
String B
|
||||||
|
"""
|
||||||
|
|
||||||
|
c = """
|
||||||
|
String C
|
||||||
|
"""
|
||||||
|
|
||||||
|
d = """
|
||||||
|
String D
|
||||||
|
String D
|
||||||
|
String D
|
||||||
|
"""
|
||||||
19
lua/tests/indent/python_spec.lua
Normal file
19
lua/tests/indent/python_spec.lua
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
local whole_file = require('nvim-treesitter.test_utils').indent_whole_file
|
||||||
|
|
||||||
|
describe('indent python', function()
|
||||||
|
local files = {
|
||||||
|
'aligned_indent.py',
|
||||||
|
'basic_blocks.py',
|
||||||
|
'basic_collections.py',
|
||||||
|
'branches.py',
|
||||||
|
'comprehensions.py',
|
||||||
|
'control_flow.py',
|
||||||
|
'hanging_indent.py',
|
||||||
|
'join_lines.py',
|
||||||
|
'nested_collections.py',
|
||||||
|
'strings.py',
|
||||||
|
}
|
||||||
|
for _, file in ipairs(files) do
|
||||||
|
it(file, function() whole_file('lua/tests/indent/python/' .. file) end)
|
||||||
|
end
|
||||||
|
end)
|
||||||
10
scripts/minimal_init.lua
Normal file
10
scripts/minimal_init.lua
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
vim.cmd [[set runtimepath+=.]]
|
||||||
|
vim.cmd [[runtime! plugin/plenary.vim]]
|
||||||
|
vim.cmd [[runtime! plugin/nvim-treesitter.vim]]
|
||||||
|
|
||||||
|
vim.g.swapfile = false
|
||||||
|
|
||||||
|
require('nvim-treesitter.configs').setup {
|
||||||
|
ensure_installed = 'maintained',
|
||||||
|
indent = { enable = true },
|
||||||
|
}
|
||||||
7
scripts/run_tests.sh
Executable file
7
scripts/run_tests.sh
Executable file
|
|
@ -0,0 +1,7 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
HERE="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
|
cd $HERE/..
|
||||||
|
|
||||||
|
nvim --headless --noplugin -u scripts/minimal_init.lua \
|
||||||
|
-c "PlenaryBustedDirectory lua/tests/indent/ { minimal_init = './scripts/minimal_init.lua' }"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue