mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
tests/indent: improve Rust tests
This commit is contained in:
parent
69dda8c09d
commit
1d7e5144ab
16 changed files with 217 additions and 13 deletions
11
lua/tests/indent/rust/array.rs
Normal file
11
lua/tests/indent/rust/array.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
const X: [i32; 2] = [
|
||||
1,
|
||||
2,
|
||||
];
|
||||
|
||||
fn foo() {
|
||||
let _x = [
|
||||
1,
|
||||
2,
|
||||
];
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
fn foo(x: i32) {
|
||||
if (x > 10) {
|
||||
return 10;
|
||||
} else {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
7
lua/tests/indent/rust/comment.rs
Normal file
7
lua/tests/indent/rust/comment.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/// Function foo
|
||||
///
|
||||
/// Description of
|
||||
/// function foo.
|
||||
fn foo(x: i32, y: i32) -> i32 {
|
||||
x + y
|
||||
}
|
||||
17
lua/tests/indent/rust/cond.rs
Normal file
17
lua/tests/indent/rust/cond.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
fn foo(mut x: i32) -> i32 {
|
||||
if x > 10 {
|
||||
return 10;
|
||||
} else if x == 10 {
|
||||
return 9;
|
||||
} else {
|
||||
x += 10;
|
||||
}
|
||||
|
||||
if x < 0 {
|
||||
if x == -1 {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
0
|
||||
}
|
||||
11
lua/tests/indent/rust/enum.rs
Normal file
11
lua/tests/indent/rust/enum.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
enum Foo {
|
||||
X,
|
||||
Y(
|
||||
char,
|
||||
char,
|
||||
),
|
||||
Z {
|
||||
x: u32,
|
||||
y: u32,
|
||||
},
|
||||
}
|
||||
10
lua/tests/indent/rust/func.rs
Normal file
10
lua/tests/indent/rust/func.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
fn foo() -> i32 {
|
||||
1
|
||||
}
|
||||
|
||||
fn foo(
|
||||
x: i32,
|
||||
y: i32
|
||||
) -> i32 {
|
||||
x + y
|
||||
}
|
||||
7
lua/tests/indent/rust/impl.rs
Normal file
7
lua/tests/indent/rust/impl.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
struct Foo;
|
||||
|
||||
impl Foo {
|
||||
fn foo() -> i32 {
|
||||
1
|
||||
}
|
||||
}
|
||||
19
lua/tests/indent/rust/loop.rs
Normal file
19
lua/tests/indent/rust/loop.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
fn foo(mut x: i32) {
|
||||
while x > 0 {
|
||||
x -= 1;
|
||||
}
|
||||
|
||||
for i in 0..3 {
|
||||
x += 1;
|
||||
}
|
||||
|
||||
loop {
|
||||
x += 1;
|
||||
|
||||
if x < 100 {
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
13
lua/tests/indent/rust/macro.rs
Normal file
13
lua/tests/indent/rust/macro.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
macro_rules! foo {
|
||||
($a:ident, $b:ident, $c:ident) => {
|
||||
struct $a;
|
||||
struct $b;
|
||||
},
|
||||
($a:ident) => {
|
||||
struct $a;
|
||||
},
|
||||
}
|
||||
|
||||
foo! {
|
||||
A
|
||||
}
|
||||
11
lua/tests/indent/rust/match.rs
Normal file
11
lua/tests/indent/rust/match.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fn foo(x: i32) -> i32 {
|
||||
match x {
|
||||
0 => 1,
|
||||
1 => {
|
||||
2
|
||||
},
|
||||
2 | 3 => {
|
||||
4
|
||||
}
|
||||
}
|
||||
}
|
||||
8
lua/tests/indent/rust/mod.rs
Normal file
8
lua/tests/indent/rust/mod.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
mod foo {
|
||||
const X: i32 = 1;
|
||||
|
||||
mod bar {
|
||||
|
||||
const Y: i32 = 1;
|
||||
}
|
||||
}
|
||||
12
lua/tests/indent/rust/string.rs
Normal file
12
lua/tests/indent/rust/string.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fn foo() {
|
||||
let a = "hello
|
||||
world";
|
||||
|
||||
let b = "hello\
|
||||
world";
|
||||
|
||||
let c = r#"
|
||||
hello
|
||||
world
|
||||
"#;
|
||||
}
|
||||
4
lua/tests/indent/rust/struct.rs
Normal file
4
lua/tests/indent/rust/struct.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
struct Foo {
|
||||
x: u32,
|
||||
y: u32,
|
||||
}
|
||||
11
lua/tests/indent/rust/trait.rs
Normal file
11
lua/tests/indent/rust/trait.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
struct Foo;
|
||||
|
||||
trait Bar {
|
||||
fn bar();
|
||||
}
|
||||
|
||||
impl Bar for Foo {
|
||||
fn bar() {
|
||||
|
||||
}
|
||||
}
|
||||
21
lua/tests/indent/rust/where.rs
Normal file
21
lua/tests/indent/rust/where.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
fn foo<T>(t: T) -> i32
|
||||
where
|
||||
T: Debug,
|
||||
{
|
||||
1
|
||||
}
|
||||
|
||||
fn foo<T>(t: T) -> i32 where
|
||||
T: Debug,
|
||||
{
|
||||
1
|
||||
}
|
||||
|
||||
struct Foo<T>(T);
|
||||
|
||||
impl<T> Write for Foo<T>
|
||||
where
|
||||
T: Debug,
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -1,18 +1,67 @@
|
|||
local whole_file = require('nvim-treesitter.test_utils').indent_whole_file
|
||||
local new_line = require('nvim-treesitter.test_utils').indent_new_line
|
||||
local scan_dir = require('plenary.scandir').scan_dir
|
||||
|
||||
local opts = {
|
||||
tabstop = 4,
|
||||
shiftwidth = 4,
|
||||
softtabstop = 0,
|
||||
expandtab = true,
|
||||
}
|
||||
|
||||
describe('indent Rust:', function()
|
||||
describe('whole file:', function()
|
||||
local files = scan_dir('lua/tests/indent/rust');
|
||||
for _, file in ipairs(files) do
|
||||
it(vim.fn.fnamemodify(file, ':t'), function()
|
||||
whole_file(file, {
|
||||
tabstop = 4,
|
||||
shiftwidth = 4,
|
||||
softtabstop = 0,
|
||||
expandtab = true,
|
||||
})
|
||||
whole_file(file, opts)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
describe('new line:', function()
|
||||
local run = function(file, spec, title)
|
||||
title = title and title or tostring(spec.on_line)
|
||||
it(string.format('%s[%s]', file, title), function()
|
||||
new_line('lua/tests/indent/rust/' .. file, spec, opts)
|
||||
end)
|
||||
end
|
||||
|
||||
run('array.rs', { on_line = 2, text = '0,', indent = 4 })
|
||||
run('array.rs', { on_line = 8, text = '0,', indent = 8 })
|
||||
run('comment.rs', { on_line = 3, text = 'a', indent = '/// ' })
|
||||
run('cond.rs', { on_line = 11, text = 'x += 1;', indent = 12 })
|
||||
run('cond.rs', { on_line = 2, text = 'x += 1;', indent = 8 })
|
||||
run('cond.rs', { on_line = 4, text = 'x += 1;', indent = 8 })
|
||||
run('cond.rs', { on_line = 6, text = 'x += 1;', indent = 8 })
|
||||
run('enum.rs', { on_line = 2, text = 'Q,', indent = 4 })
|
||||
run('enum.rs', { on_line = 4, text = 'i32,', indent = 8 })
|
||||
run('enum.rs', { on_line = 8, text = 'z: u32,', indent = 8 })
|
||||
run('func.rs', { on_line = 1, text = 'let _x = 1;', indent = 4 })
|
||||
run('func.rs', { on_line = 6, text = 'z: i32,', indent = 4 })
|
||||
run('impl.rs', { on_line = 3, text = 'const FOO: u32 = 1;', indent = 4 })
|
||||
run('impl.rs', { on_line = 4, text = 'let _x = 1;', indent = 8 })
|
||||
run('loop.rs', { on_line = 10, text = 'x += 1;', indent = 8 })
|
||||
run('loop.rs', { on_line = 2, text = 'x += 1;', indent = 8 })
|
||||
run('loop.rs', { on_line = 6, text = 'x += 1;', indent = 8 })
|
||||
run('macro.rs', { on_line = 1, text = '() => {},', indent = 4 })
|
||||
run('macro.rs', { on_line = 12, text = 'B C', indent = 4 })
|
||||
run('macro.rs', { on_line = 2, text = 'struct $c;', indent = 8 })
|
||||
run('match.rs', { on_line = 2, text = '-1 => -1,', indent = 8 })
|
||||
run('match.rs', { on_line = 7, text = 'let y = 1;', indent = 12 })
|
||||
run('mod.rs', { on_line = 1, text = 'const Z: i32 = 1;', indent = 4 })
|
||||
run('mod.rs', { on_line = 2, text = 'const Z: i32 = 1;', indent = 4 })
|
||||
run('mod.rs', { on_line = 6, text = 'const Z: i32 = 1;', indent = 8 })
|
||||
run('string.rs', { on_line = 2, text = 'brave new', indent = 0 })
|
||||
run('string.rs', { on_line = 5, text = 'brave new \\', indent = 8 })
|
||||
run('string.rs', { on_line = 9, text = 'brave new \\', indent = 8 })
|
||||
run('struct.rs', { on_line = 1, text = 'z: i32,', indent = 4 })
|
||||
run('struct.rs', { on_line = 2, text = 'z: i32,', indent = 4 })
|
||||
run('trait.rs', { on_line = 4, text = 'fn baz();', indent = 4 })
|
||||
run('trait.rs', { on_line = 7, text = 'fn baz();', indent = 4 })
|
||||
run('trait.rs', { on_line = 8, text = '()', indent = 8 })
|
||||
run('where.rs', { on_line = 17, text = 'T: Debug,', indent = 4 })
|
||||
run('where.rs', { on_line = 2, text = 'T: Debug,', indent = 4 })
|
||||
run('where.rs', { on_line = 9, text = 'T: Debug,', indent = 4 })
|
||||
end)
|
||||
end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue