Bram Moolenaar | fabaf75 | 2017-12-23 17:26:11 +0100 | [diff] [blame] | 1 | " Tests for various eval things. |
| 2 | |
| 3 | function s:foo() abort |
| 4 | try |
| 5 | return [] == 0 |
| 6 | catch |
| 7 | return 1 |
| 8 | endtry |
| 9 | endfunction |
| 10 | |
| 11 | func Test_catch_return_with_error() |
| 12 | call assert_equal(1, s:foo()) |
| 13 | endfunc |
Bram Moolenaar | 2be5733 | 2018-02-13 18:05:18 +0100 | [diff] [blame] | 14 | |
| 15 | func Test_nocatch_restore_silent_emsg() |
| 16 | silent! try |
| 17 | throw 1 |
| 18 | catch |
| 19 | endtry |
| 20 | echoerr 'wrong' |
| 21 | let c1 = nr2char(screenchar(&lines, 1)) |
| 22 | let c2 = nr2char(screenchar(&lines, 2)) |
| 23 | let c3 = nr2char(screenchar(&lines, 3)) |
| 24 | let c4 = nr2char(screenchar(&lines, 4)) |
| 25 | let c5 = nr2char(screenchar(&lines, 5)) |
| 26 | call assert_equal('wrong', c1 . c2 . c3 . c4 . c5) |
| 27 | endfunc |
Bram Moolenaar | 78a16b0 | 2018-04-14 13:51:55 +0200 | [diff] [blame] | 28 | |
| 29 | func Test_mkdir_p() |
| 30 | call mkdir('Xmkdir/nested', 'p') |
| 31 | call assert_true(isdirectory('Xmkdir/nested')) |
| 32 | try |
| 33 | " Trying to make existing directories doesn't error |
| 34 | call mkdir('Xmkdir', 'p') |
| 35 | call mkdir('Xmkdir/nested', 'p') |
| 36 | catch /E739:/ |
| 37 | call assert_report('mkdir(..., "p") failed for an existing directory') |
| 38 | endtry |
| 39 | " 'p' doesn't suppress real errors |
| 40 | call writefile([], 'Xfile') |
| 41 | call assert_fails('call mkdir("Xfile", "p")', 'E739') |
| 42 | call delete('Xfile') |
| 43 | call delete('Xmkdir', 'rf') |
| 44 | endfunc |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 45 | |
| 46 | func Test_line_continuation() |
| 47 | let array = [5, |
| 48 | "\ ignore this |
| 49 | \ 6, |
| 50 | "\ more to ignore |
| 51 | "\ more moreto ignore |
| 52 | \ ] |
| 53 | "\ and some more |
| 54 | call assert_equal([5, 6], array) |
| 55 | endfunc |
Bram Moolenaar | 88b53fd | 2018-12-05 18:43:28 +0100 | [diff] [blame] | 56 | |
| 57 | func Test_E963() |
| 58 | " These commands used to cause an internal error prior to vim 8.1.0563 |
| 59 | let v_e = v:errors |
| 60 | let v_o = v:oldfiles |
| 61 | call assert_fails("let v:errors=''", 'E963:') |
| 62 | call assert_equal(v_e, v:errors) |
| 63 | call assert_fails("let v:oldfiles=''", 'E963:') |
| 64 | call assert_equal(v_o, v:oldfiles) |
| 65 | endfunc |
Bram Moolenaar | c0f5a78 | 2019-01-13 15:16:13 +0100 | [diff] [blame] | 66 | |
| 67 | func Test_for_invalid() |
| 68 | call assert_fails("for x in 99", 'E714:') |
| 69 | call assert_fails("for x in 'asdf'", 'E714:') |
| 70 | call assert_fails("for x in {'a': 9}", 'E714:') |
| 71 | endfunc |
Bram Moolenaar | 05500ec | 2019-01-13 19:10:33 +0100 | [diff] [blame] | 72 | |
| 73 | func Test_readfile_binary() |
| 74 | new |
| 75 | call setline(1, ['one', 'two', 'three']) |
| 76 | setlocal ff=dos |
| 77 | write XReadfile |
| 78 | let lines = readfile('XReadfile') |
| 79 | call assert_equal(['one', 'two', 'three'], lines) |
| 80 | let lines = readfile('XReadfile', '', 2) |
| 81 | call assert_equal(['one', 'two'], lines) |
| 82 | let lines = readfile('XReadfile', 'b') |
| 83 | call assert_equal(["one\r", "two\r", "three\r", ""], lines) |
| 84 | let lines = readfile('XReadfile', 'b', 2) |
| 85 | call assert_equal(["one\r", "two\r"], lines) |
| 86 | |
| 87 | bwipe! |
| 88 | call delete('XReadfile') |
| 89 | endfunc |
Bram Moolenaar | 4b9e91f | 2019-01-24 13:58:11 +0100 | [diff] [blame] | 90 | |
| 91 | func Test_let_errmsg() |
| 92 | call assert_fails('let v:errmsg = []', 'E730:') |
| 93 | let v:errmsg = '' |
| 94 | call assert_fails('let v:errmsg = []', 'E730:') |
| 95 | let v:errmsg = '' |
| 96 | endfunc |
Bram Moolenaar | 0f248b0 | 2019-04-04 15:36:05 +0200 | [diff] [blame] | 97 | |
| 98 | func Test_string_concatenation() |
| 99 | call assert_equal('ab', 'a'.'b') |
| 100 | call assert_equal('ab', 'a' .'b') |
| 101 | call assert_equal('ab', 'a'. 'b') |
| 102 | call assert_equal('ab', 'a' . 'b') |
| 103 | |
| 104 | call assert_equal('ab', 'a'..'b') |
| 105 | call assert_equal('ab', 'a' ..'b') |
| 106 | call assert_equal('ab', 'a'.. 'b') |
| 107 | call assert_equal('ab', 'a' .. 'b') |
| 108 | |
| 109 | let a = 'a' |
| 110 | let b = 'b' |
| 111 | let a .= b |
| 112 | call assert_equal('ab', a) |
| 113 | |
| 114 | let a = 'a' |
| 115 | let a.=b |
| 116 | call assert_equal('ab', a) |
| 117 | |
| 118 | let a = 'a' |
| 119 | let a ..= b |
| 120 | call assert_equal('ab', a) |
| 121 | |
| 122 | let a = 'a' |
| 123 | let a..=b |
| 124 | call assert_equal('ab', a) |
| 125 | endfunc |
Bram Moolenaar | 558ca4a | 2019-04-04 18:15:38 +0200 | [diff] [blame] | 126 | |
| 127 | scriptversion 2 |
| 128 | func Test_string_concat_scriptversion2() |
Bram Moolenaar | 93a4879 | 2019-04-20 21:54:28 +0200 | [diff] [blame] | 129 | call assert_true(has('vimscript-2')) |
Bram Moolenaar | 558ca4a | 2019-04-04 18:15:38 +0200 | [diff] [blame] | 130 | let a = 'a' |
| 131 | let b = 'b' |
| 132 | |
| 133 | call assert_fails('echo a . b', 'E15:') |
| 134 | call assert_fails('let a .= b', 'E985:') |
| 135 | call assert_fails('let vers = 1.2.3', 'E15:') |
| 136 | |
| 137 | if has('float') |
| 138 | let f = .5 |
| 139 | call assert_equal(0.5, f) |
| 140 | endif |
| 141 | endfunc |
| 142 | |
| 143 | scriptversion 1 |
| 144 | func Test_string_concat_scriptversion1() |
Bram Moolenaar | 93a4879 | 2019-04-20 21:54:28 +0200 | [diff] [blame] | 145 | call assert_true(has('vimscript-1')) |
Bram Moolenaar | 558ca4a | 2019-04-04 18:15:38 +0200 | [diff] [blame] | 146 | let a = 'a' |
| 147 | let b = 'b' |
| 148 | |
| 149 | echo a . b |
| 150 | let a .= b |
| 151 | let vers = 1.2.3 |
| 152 | call assert_equal('123', vers) |
| 153 | |
| 154 | if has('float') |
| 155 | call assert_fails('let f = .5', 'E15:') |
| 156 | endif |
| 157 | endfunc |
| 158 | |
Bram Moolenaar | d2e716e | 2019-04-20 14:39:52 +0200 | [diff] [blame] | 159 | scriptversion 3 |
| 160 | func Test_vvar_scriptversion3() |
Bram Moolenaar | 93a4879 | 2019-04-20 21:54:28 +0200 | [diff] [blame] | 161 | call assert_true(has('vimscript-3')) |
Bram Moolenaar | d2e716e | 2019-04-20 14:39:52 +0200 | [diff] [blame] | 162 | call assert_fails('echo version', 'E121:') |
| 163 | call assert_false(exists('version')) |
| 164 | let version = 1 |
| 165 | call assert_equal(1, version) |
| 166 | endfunc |
| 167 | |
| 168 | scriptversion 2 |
| 169 | func Test_vvar_scriptversion2() |
| 170 | call assert_true(exists('version')) |
| 171 | echo version |
| 172 | call assert_fails('let version = 1', 'E46:') |
| 173 | call assert_equal(v:version, version) |
Bram Moolenaar | 37df9a4 | 2019-06-14 14:39:51 +0200 | [diff] [blame] | 174 | |
| 175 | call assert_equal(v:version, v:versionlong / 10000) |
| 176 | call assert_true(v:versionlong > 8011525) |
Bram Moolenaar | d2e716e | 2019-04-20 14:39:52 +0200 | [diff] [blame] | 177 | endfunc |
| 178 | |
Bram Moolenaar | 61343f0 | 2019-07-20 21:11:13 +0200 | [diff] [blame] | 179 | func Test_dict_access_scriptversion2() |
| 180 | let l:x = {'foo': 1} |
| 181 | |
| 182 | call assert_false(0 && l:x.foo) |
| 183 | call assert_true(1 && l:x.foo) |
| 184 | endfunc |
| 185 | |
Bram Moolenaar | 558ca4a | 2019-04-04 18:15:38 +0200 | [diff] [blame] | 186 | func Test_scriptversion() |
| 187 | call writefile(['scriptversion 9'], 'Xversionscript') |
| 188 | call assert_fails('source Xversionscript', 'E999:') |
| 189 | call delete('Xversionscript') |
| 190 | endfunc |
Bram Moolenaar | 6064073 | 2019-06-07 23:15:22 +0200 | [diff] [blame] | 191 | |
| 192 | " Test fix for issue #4507 |
| 193 | func Test_skip_after_throw() |
| 194 | try |
| 195 | throw 'something' |
| 196 | let x = wincol() || &ts |
| 197 | catch /something/ |
| 198 | endtry |
| 199 | endfunc |