1" Tests for the +clientserver feature. 2 3source check.vim 4CheckFeature job 5 6if !has('clientserver') 7 call assert_fails('call remote_startserver("local")', 'E942:') 8endif 9 10CheckFeature clientserver 11 12source shared.vim 13 14func Check_X11_Connection() 15 if has('x11') 16 if empty($DISPLAY) 17 throw 'Skipped: $DISPLAY is not set' 18 endif 19 try 20 call remote_send('xxx', '') 21 catch 22 if v:exception =~ 'E240:' 23 throw 'Skipped: no connection to the X server' 24 endif 25 " ignore other errors 26 endtry 27 endif 28endfunc 29 30func Test_client_server() 31 let cmd = GetVimCommand() 32 if cmd == '' 33 throw 'GetVimCommand() failed' 34 endif 35 call Check_X11_Connection() 36 37 let name = 'XVIMTEST' 38 let cmd .= ' --servername ' . name 39 let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'}) 40 call WaitForAssert({-> assert_equal("run", job_status(job))}) 41 42 " Takes a short while for the server to be active. 43 " When using valgrind it takes much longer. 44 call WaitForAssert({-> assert_match(name, serverlist())}) 45 46 eval name->remote_foreground() 47 48 call remote_send(name, ":let testvar = 'yes'\<CR>") 49 call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"') 50 call assert_equal('yes', remote_expr(name, "testvar", "", 2)) 51 call assert_fails("let x=remote_expr(name, '2+x')", 'E449:') 52 call assert_fails("let x=remote_expr('[], '2+2')", 'E116:') 53 54 if has('unix') && has('gui') && !has('gui_running') 55 " Running in a terminal and the GUI is available: Tell the server to open 56 " the GUI and check that the remote command still works. 57 " Need to wait for the GUI to start up, otherwise the send hangs in trying 58 " to send to the terminal window. 59 if has('gui_athena') || has('gui_motif') 60 " For those GUIs, ignore the 'failed to create input context' error. 61 call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>") 62 else 63 call remote_send(name, ":gui -f\<CR>") 64 endif 65 " Wait for the server to be up and answering requests. 66 " When using valgrind this can be very, very slow. 67 sleep 1 68 call WaitForAssert({-> assert_match('\d', name->remote_expr("v:version", "", 1))}, 10000) 69 70 call remote_send(name, ":let testvar = 'maybe'\<CR>") 71 call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))}) 72 endif 73 74 call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241:') 75 76 " Expression evaluated locally. 77 if v:servername == '' 78 eval 'MYSELF'->remote_startserver() 79 " May get MYSELF1 when running the test again. 80 call assert_match('MYSELF', v:servername) 81 call assert_fails("call remote_startserver('MYSELF')", 'E941:') 82 endif 83 let g:testvar = 'myself' 84 call assert_equal('myself', remote_expr(v:servername, 'testvar')) 85 call remote_send(v:servername, ":let g:testvar2 = 75\<CR>") 86 call feedkeys('', 'x') 87 call assert_equal(75, g:testvar2) 88 call assert_fails('let v = remote_expr(v:servername, "/2")', ['E15:.*/2']) 89 90 call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid') 91 call assert_equal('got it', g:myserverid->remote_read(2)) 92 93 call remote_send(name, ":eval expand('<client>')->server2client('another')\<CR>", 'g:myserverid') 94 let peek_result = 'nothing' 95 let r = g:myserverid->remote_peek('peek_result') 96 " unpredictable whether the result is already available. 97 if r > 0 98 call assert_equal('another', peek_result) 99 elseif r == 0 100 call assert_equal('nothing', peek_result) 101 else 102 call assert_report('remote_peek() failed') 103 endif 104 let g:peek_result = 'empty' 105 call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0') 106 call assert_equal('another', g:peek_result) 107 call assert_equal('another', remote_read(g:myserverid, 2)) 108 109 if !has('gui_running') 110 " In GUI vim, the following tests display a dialog box 111 112 let cmd = GetVimProg() .. ' --servername ' .. name 113 114 " Run a separate instance to send a command to the server 115 call remote_expr(name, 'execute("only")') 116 call system(cmd .. ' --remote-send ":new Xfile<CR>"') 117 call assert_equal('2', remote_expr(name, 'winnr("$")')) 118 call assert_equal('Xfile', remote_expr(name, 'winbufnr(1)->bufname()')) 119 call remote_expr(name, 'execute("only")') 120 121 " Invoke a remote-expr. On MS-Windows, the returned value has a carriage 122 " return. 123 let l = system(cmd .. ' --remote-expr "2 + 2"') 124 call assert_equal(['4'], split(l, "\n")) 125 126 " Edit multiple files using --remote 127 call system(cmd .. ' --remote Xfile1 Xfile2 Xfile3') 128 call assert_equal("Xfile1\nXfile2\nXfile3\n", remote_expr(name, 'argv()')) 129 eval name->remote_send(":%bw!\<CR>") 130 131 " Edit files in separate tab pages 132 call system(cmd .. ' --remote-tab Xfile1 Xfile2 Xfile3') 133 call WaitForAssert({-> assert_equal('3', remote_expr(name, 'tabpagenr("$")'))}) 134 call assert_equal('Xfile2', remote_expr(name, 'bufname(tabpagebuflist(2)[0])')) 135 eval name->remote_send(":%bw!\<CR>") 136 137 " Edit a file using --remote-wait 138 eval name->remote_send(":source $VIMRUNTIME/plugin/rrhelper.vim\<CR>") 139 call system(cmd .. ' --remote-wait +enew Xfile1') 140 call assert_equal("Xfile1", remote_expr(name, 'bufname("#")')) 141 eval name->remote_send(":%bw!\<CR>") 142 143 " Edit files using --remote-tab-wait 144 call system(cmd .. ' --remote-tabwait +tabonly\|enew Xfile1 Xfile2') 145 call assert_equal('1', remote_expr(name, 'tabpagenr("$")')) 146 eval name->remote_send(":%bw!\<CR>") 147 148 " Error cases 149 if v:lang == "C" || v:lang =~ '^[Ee]n' 150 let l = split(system(cmd .. ' --remote +pwd'), "\n") 151 call assert_equal("Argument missing after: \"+pwd\"", l[1]) 152 endif 153 let l = system(cmd .. ' --remote-expr "abcd"') 154 call assert_match('^E449: ', l) 155 endif 156 157 eval name->remote_send(":%bw!\<CR>") 158 eval name->remote_send(":qa!\<CR>") 159 try 160 call WaitForAssert({-> assert_equal("dead", job_status(job))}) 161 finally 162 if job_status(job) != 'dead' 163 call assert_report('Server did not exit') 164 call job_stop(job, 'kill') 165 endif 166 endtry 167 168 call assert_fails('call remote_startserver([])', 'E730:') 169 call assert_fails("let x = remote_peek([])", 'E730:') 170 call assert_fails("let x = remote_read('vim10')", 171 \ has('unix') ? ['E573:.*vim10'] : 'E277:') 172 call assert_fails("call server2client('abc', 'xyz')", 173 \ has('unix') ? ['E573:.*abc'] : 'E258:') 174endfunc 175 176" Uncomment this line to get a debugging log 177" call ch_logfile('channellog', 'w') 178 179" vim: shiftwidth=2 sts=2 expandtab 180