blob: 801404d9c6f81bf1333c9aa7eec6f94da53c2ac2 [file] [log] [blame]
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001" Test commands that are not compiled in a :def function
2
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003source check.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004source vim9.vim
Bram Moolenaare9f262b2020-07-05 14:57:51 +02005source view_util.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006
7def Test_edit_wildcards()
8 let filename = 'Xtest'
9 edit `=filename`
10 assert_equal('Xtest', bufname())
11
12 let filenr = 123
13 edit Xtest`=filenr`
14 assert_equal('Xtest123', bufname())
15
16 filenr = 77
17 edit `=filename``=filenr`
18 assert_equal('Xtest77', bufname())
19
20 edit X`=filename`xx`=filenr`yy
21 assert_equal('XXtestxx77yy', bufname())
22enddef
23
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020024def Test_hardcopy_wildcards()
25 CheckUnix
26 CheckFeature postscript
27
28 let outfile = 'print'
29 hardcopy > X`=outfile`.ps
30 assert_true(filereadable('Xprint.ps'))
31
32 delete('Xprint.ps')
33enddef
34
35def Test_syn_include_wildcards()
36 writefile(['syn keyword Found found'], 'Xthemine.vim')
37 let save_rtp = &rtp
38 &rtp = '.'
39
40 let fname = 'mine'
41 syn include @Group Xthe`=fname`.vim
42 assert_match('Found.* contained found', execute('syn list Found'))
43
44 &rtp = save_rtp
45 delete('Xthemine.vim')
46enddef
47
Bram Moolenaar1cc2a942020-05-10 19:10:31 +020048def Test_assign_list()
49 let l: list<string> = []
50 l[0] = 'value'
51 assert_equal('value', l[0])
52
53 l[1] = 'asdf'
54 assert_equal('value', l[0])
55 assert_equal('asdf', l[1])
56 assert_equal('asdf', l[-1])
57 assert_equal('value', l[-2])
Bram Moolenaarf163bd52020-05-10 21:47:43 +020058
59 let nrl: list<number> = []
60 for i in range(5)
61 nrl[i] = i
62 endfor
63 assert_equal([0, 1, 2, 3, 4], nrl)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +020064enddef
65
66def Test_assign_dict()
67 let d: dict<string> = {}
68 d['key'] = 'value'
69 assert_equal('value', d['key'])
70
71 d[123] = 'qwerty'
72 assert_equal('qwerty', d[123])
73 assert_equal('qwerty', d['123'])
Bram Moolenaarf163bd52020-05-10 21:47:43 +020074
75 let nrd: dict<number> = {}
76 for i in range(3)
77 nrd[i] = i
78 endfor
79 assert_equal({'0': 0, '1': 1, '2': 2}, nrd)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +020080enddef
81
Bram Moolenaar7e8967f2020-06-27 21:56:17 +020082def Test_echo_linebreak()
83 let lines =<< trim END
84 vim9script
85 redir @a
86 echo 'one'
87 .. 'two'
88 redir END
89 assert_equal("\nonetwo", @a)
90 END
91 CheckScriptSuccess(lines)
92
93 lines =<< trim END
94 vim9script
95 redir @a
96 echo 11 +
97 77
98 - 22
99 redir END
100 assert_equal("\n66", @a)
101 END
102 CheckScriptSuccess(lines)
103enddef
104
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200105def Test_if_linebreak()
106 let lines =<< trim END
107 vim9script
108 if 1 &&
109 2
110 || 3
111 g:res = 42
112 endif
113 assert_equal(42, g:res)
114 END
115 CheckScriptSuccess(lines)
116 unlet g:res
117
118 lines =<< trim END
119 vim9script
120 if 1 &&
121 0
122 g:res = 0
123 elseif 0 ||
124 0
125 || 1
126 g:res = 12
127 endif
128 assert_equal(12, g:res)
129 END
130 CheckScriptSuccess(lines)
131 unlet g:res
132enddef
133
134def Test_while_linebreak()
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200135 let lines =<< trim END
136 vim9script
137 let nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200138 while nr <
139 10 + 3
140 nr = nr
141 + 4
142 endwhile
143 assert_equal(16, nr)
144 END
145 CheckScriptSuccess(lines)
146
147 lines =<< trim END
148 vim9script
149 let nr = 0
150 while nr
151 <
152 10
153 +
154 3
155 nr = nr
156 +
157 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200158 endwhile
159 assert_equal(16, nr)
160 END
161 CheckScriptSuccess(lines)
162enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200163
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200164def Test_for_linebreak()
165 let lines =<< trim END
166 vim9script
167 let nr = 0
168 for x
169 in
170 [1, 2, 3, 4]
171 nr = nr + x
172 endfor
173 assert_equal(10, nr)
174 END
175 CheckScriptSuccess(lines)
176
177 lines =<< trim END
178 vim9script
179 let nr = 0
180 for x
181 in
182 [1, 2,
183 3, 4
184 ]
185 nr = nr
186 +
187 x
188 endfor
189 assert_equal(10, nr)
190 END
191 CheckScriptSuccess(lines)
192enddef
193
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200194def Test_method_call_linebreak()
Bram Moolenaar5f195932020-07-01 20:07:14 +0200195 let lines =<< trim END
196 vim9script
197 let res = []
198 func RetArg(
199 arg
200 )
201 let s:res = a:arg
202 endfunc
203 [1,
204 2,
205 3]->RetArg()
206 assert_equal([1, 2, 3], res)
207 END
208 CheckScriptSuccess(lines)
209enddef
210
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200211def Test_dict_member()
212 let test: dict<list<number>> = {'data': [3, 1, 2]}
213 test.data->sort()
214 assert_equal(#{data: [1, 2, 3]}, test)
215 test.data
216 ->reverse()
217 assert_equal(#{data: [3, 2, 1]}, test)
218
219 let lines =<< trim END
220 vim9script
221 let test: dict<list<number>> = {'data': [3, 1, 2]}
222 test.data->sort()
223 assert_equal(#{data: [1, 2, 3]}, test)
224 END
225 CheckScriptSuccess(lines)
226enddef
227
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200228def Test_bar_after_command()
229 def RedrawAndEcho()
230 let x = 'did redraw'
231 redraw | echo x
232 enddef
233 RedrawAndEcho()
234 assert_match('did redraw', Screenline(&lines))
235
Bram Moolenaar788123c2020-07-05 15:32:17 +0200236 def CallAndEcho()
237 let x = 'did redraw'
238 reg_executing() | echo x
239 enddef
240 CallAndEcho()
241 assert_match('did redraw', Screenline(&lines))
242
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200243 if has('unix')
244 # bar in filter write command does not start new command
245 def WriteToShell()
246 new
247 setline(1, 'some text')
248 w !cat | cat > Xoutfile
249 bwipe!
250 enddef
251 WriteToShell()
252 assert_equal(['some text'], readfile('Xoutfile'))
253 delete('Xoutfile')
254
255 # bar in filter read command does not start new command
256 def ReadFromShell()
257 new
258 r! echo hello there | cat > Xoutfile
259 r !echo again | cat >> Xoutfile
260 bwipe!
261 enddef
262 ReadFromShell()
263 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
264 delete('Xoutfile')
265 endif
266enddef
267
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200268def Test_eval_command()
269 let from = 3
270 let to = 5
271 g:val = 111
272 def Increment(nrs: list<number>)
273 for nr in nrs
274 g:val += nr
275 endfor
276 enddef
277 eval range(from, to)
278 ->Increment()
279 assert_equal(111 + 3 + 4 + 5, g:val)
280 unlet g:val
281enddef
282
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200283
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200284" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker