summaryrefslogtreecommitdiff
path: root/vim/pack/downloads/opt/lsp/test/rust_tests.vim
blob: 6dc227733a548f8f1caf2a515ffe27320ce2d2b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
vim9script
# Unit tests for LSP rust-analyzer client

source common.vim
source term_util.vim
source screendump.vim

var lspServers = [{
      filetype: ['rust'],
      path: exepath('rust-analyzer'),
      args: []
  }]
call LspAddServer(lspServers)
echomsg systemlist($'{lspServers[0].path} --version')

def g:Test_LspGotoDef()
  settagstack(0, {items: []})
  :cd xrust_tests/src
  try
    silent! edit ./main.rs
    deletebufline('%', 1, '$')
    g:WaitForServerFileLoad(0)
    var lines: list<string> =<< trim END
      fn main() {
      }
      fn foo() {
      }
      fn bar() {
        foo();
      }
    END
    setline(1, lines)
    g:WaitForServerFileLoad(0)
    cursor(6, 5)
    :LspGotoDefinition
    assert_equal([3, 4], [line('.'), col('.')])
    :%bw!
  finally
    :cd ../..
  endtry
enddef

# Test for :LspCodeAction creating a file in the current directory
def g:Test_LspCodeAction_CreateFile()
  :cd xrust_tests/src
  try
    silent! edit ./main.rs
    deletebufline('%', 1, '$')
    g:WaitForServerFileLoad(0)
    var lines: list<string> =<< trim END
      mod foo;
      fn main() {
      }
    END
    setline(1, lines)
    g:WaitForServerFileLoad(1)
    cursor(1, 1)
    :LspCodeAction 1
    g:WaitForServerFileLoad(0)
    assert_true(filereadable('foo.rs'))
    :%bw!
    delete('foo.rs')
  finally
    :cd ../..
  endtry
enddef

# Test for :LspCodeAction creating a file in a subdirectory
def g:Test_LspCodeAction_CreateFile_Subdir()
  :cd xrust_tests/src
  try
    silent! edit ./main.rs
    deletebufline('%', 1, '$')
    g:WaitForServerFileLoad(0)
    var lines: list<string> =<< trim END
      mod baz;
      fn main() {
      }
    END
    setline(1, lines)
    g:WaitForServerFileLoad(1)
    cursor(1, 1)
    :LspCodeAction 2
    g:WaitForServerFileLoad(0)
    assert_true(filereadable('baz/mod.rs'))
    :%bw!
    delete('baz', 'rf')
  finally
    :cd ../..
  endtry
enddef

# Test for :LspCodeAction renaming a file
def g:Test_LspCodeAction_RenameFile()
  :cd xrust_tests/src
  try
    silent! edit ./main.rs
    deletebufline('%', 1, '$')
    g:WaitForServerFileLoad(0)
    writefile([], 'foobar.rs')
    var lines: list<string> =<< trim END
      mod foobar;
      fn main() {
      }
    END
    setline(1, lines)
    g:WaitForServerFileLoad(0)
    cursor(1, 5)
    :LspRename foobaz
    g:WaitForServerFileLoad(0)
    assert_true(filereadable('foobaz.rs'))
    :%bw!
    delete('foobaz.rs')
  finally
    :cd ../..
  endtry
enddef

def g:Test_ZZZ_Cleanup()
  delete('./xrust_tests', 'rf')
enddef

# Start the rust-analyzer language server.  Returns true on success and false
# on failure.
def g:StartLangServer(): bool
  system('cargo new xrust_tests')
  :cd xrust_tests/src
  var status = false
  try
    status = g:StartLangServerWithFile('./main.rs')
  finally
    :cd ../..
  endtry
  return status
enddef

# vim: shiftwidth=2 softtabstop=2 noexpandtab