Initial sketch of automated indent tests

This commit is contained in:
Jędrzej Boczar 2021-03-13 22:46:45 +01:00 committed by Kiyan
parent eefa5662e6
commit 67f2c7149c
14 changed files with 257 additions and 0 deletions

View 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

View file

@ -0,0 +1,11 @@
def aligned_indent(arg1,
arg2):
pass
aligned_indent(1,
2)
aligned_indent(1,
2
)

View 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

View file

@ -0,0 +1,23 @@
# list
a = [
1, 2,
3
]
# set
b = {
3,
4,
}
# dict
c = {
'a': 'b',
'c': 1,
}
# tuple
d = (
1,
2,
)

View 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

View 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)
]

View 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

View file

@ -0,0 +1,6 @@
def hanging_indent(
arg1, arg2):
pass
hanging_indent(
1, 2)

View file

@ -0,0 +1,8 @@
a = 2 \
+ 2
b = 'hello' \
'world'
c = lambda x: \
x + 3

View 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
)

View file

@ -0,0 +1,17 @@
a = """
String A
"""
b = """
String B
"""
c = """
String C
"""
d = """
String D
String D
String D
"""

View 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
View 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
View 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' }"