Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 1 | " Test for reading and writing .viminfo |
| 2 | |
| 3 | function Test_read_and_write() |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 4 | call histdel(':') |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 5 | let lines = [ |
| 6 | \ '# comment line', |
| 7 | \ '*encoding=utf-8', |
| 8 | \ '~MSle0~/asdf', |
| 9 | \ '|copied as-is', |
| 10 | \ '|and one more', |
| 11 | \ ] |
| 12 | call writefile(lines, 'Xviminfo') |
| 13 | rviminfo Xviminfo |
| 14 | call assert_equal('asdf', @/) |
| 15 | |
| 16 | wviminfo Xviminfo |
| 17 | let lines = readfile('Xviminfo') |
| 18 | let done = 0 |
| 19 | for line in lines |
| 20 | if line[0] == '|' |
| 21 | if done == 0 |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 22 | call assert_equal('|1,2', line) |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 23 | elseif done == 1 |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 24 | call assert_equal('|copied as-is', line) |
| 25 | elseif done == 2 |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 26 | call assert_equal('|and one more', line) |
| 27 | endif |
| 28 | let done += 1 |
| 29 | endif |
| 30 | endfor |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 31 | call assert_equal(3, done) |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 32 | |
| 33 | call delete('Xviminfo') |
| 34 | endfunc |
| 35 | |
| 36 | func Test_global_vars() |
| 37 | let test_dict = {'foo': 1, 'bar': 0, 'longvarible': 1000} |
| 38 | let g:MY_GLOBAL_DICT = test_dict |
| 39 | " store a really long list, so line wrapping will occur in viminfo file |
| 40 | let test_list = range(1,100) |
| 41 | let g:MY_GLOBAL_LIST = test_list |
Bram Moolenaar | e9c0727 | 2016-03-30 20:50:46 +0200 | [diff] [blame] | 42 | set viminfo='100,<50,s10,h,!,nviminfo |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 43 | wv! Xviminfo |
| 44 | unlet g:MY_GLOBAL_DICT |
| 45 | unlet g:MY_GLOBAL_LIST |
| 46 | |
| 47 | rv! Xviminfo |
| 48 | call assert_equal(test_dict, g:MY_GLOBAL_DICT) |
| 49 | call assert_equal(test_list, g:MY_GLOBAL_LIST) |
| 50 | |
| 51 | call delete('Xviminfo') |
| 52 | set viminfo-=! |
| 53 | endfunc |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 54 | |
| 55 | func Test_cmdline_history() |
| 56 | call histdel(':') |
| 57 | call test_settime(11) |
| 58 | call histadd(':', "echo 'one'") |
| 59 | call test_settime(12) |
| 60 | " split into two lines |
| 61 | let long800 = repeat(" 'eight'", 100) |
| 62 | call histadd(':', "echo " . long800) |
| 63 | call test_settime(13) |
| 64 | " split into three lines |
| 65 | let long1400 = repeat(" 'fourteeeeen'", 100) |
| 66 | call histadd(':', "echo " . long1400) |
| 67 | wviminfo Xviminfo |
| 68 | let lines = readfile('Xviminfo') |
| 69 | let done_colon = 0 |
| 70 | let done_bar = 0 |
| 71 | let lnum = 0 |
| 72 | while lnum < len(lines) |
| 73 | let line = lines[lnum] | let lnum += 1 |
| 74 | if line[0] == ':' |
| 75 | if done_colon == 0 |
| 76 | call assert_equal(":\x161408", line) |
| 77 | let line = lines[lnum] | let lnum += 1 |
| 78 | call assert_equal('<echo ' . long1400, line) |
| 79 | elseif done_colon == 1 |
| 80 | call assert_equal(":\x16808", line) |
| 81 | let line = lines[lnum] | let lnum += 1 |
| 82 | call assert_equal("<echo " . long800, line) |
| 83 | elseif done_colon == 2 |
| 84 | call assert_equal(":echo 'one'", line) |
| 85 | endif |
| 86 | let done_colon += 1 |
| 87 | elseif line[0:4] == '|2,0,' |
| 88 | if done_bar == 0 |
| 89 | call assert_equal("|2,0,13,,>1407", line) |
| 90 | let line = lines[lnum] | let lnum += 1 |
| 91 | call assert_equal('|<"echo ' . long1400[0:484], line) |
| 92 | let line = lines[lnum] | let lnum += 1 |
| 93 | call assert_equal('|<' . long1400[485:974], line) |
| 94 | let line = lines[lnum] | let lnum += 1 |
| 95 | call assert_equal('|<' . long1400[975:] . '"', line) |
| 96 | elseif done_bar == 1 |
| 97 | call assert_equal('|2,0,12,,>807', line) |
| 98 | let line = lines[lnum] | let lnum += 1 |
| 99 | call assert_equal('|<"echo ' . long800[0:484], line) |
| 100 | let line = lines[lnum] | let lnum += 1 |
| 101 | call assert_equal('|<' . long800[485:] . '"', line) |
| 102 | elseif done_bar == 2 |
| 103 | call assert_equal("|2,0,11,,\"echo 'one'\"", line) |
| 104 | endif |
| 105 | let done_bar += 1 |
| 106 | endif |
| 107 | endwhile |
| 108 | call assert_equal(3, done_colon) |
| 109 | call assert_equal(3, done_bar) |
| 110 | |
| 111 | call histdel(':') |
| 112 | rviminfo Xviminfo |
| 113 | call assert_equal("echo " . long1400, histget(':', -1)) |
| 114 | call assert_equal("echo " . long800, histget(':', -2)) |
| 115 | call assert_equal("echo 'one'", histget(':', -3)) |
| 116 | |
| 117 | call delete('Xviminfo') |
| 118 | endfunc |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 119 | |
| 120 | func Test_cmdline_history_order() |
| 121 | call histdel(':') |
| 122 | call test_settime(11) |
| 123 | call histadd(':', "echo '11'") |
| 124 | call test_settime(22) |
| 125 | call histadd(':', "echo '22'") |
| 126 | call test_settime(33) |
| 127 | call histadd(':', "echo '33'") |
| 128 | wviminfo Xviminfo |
| 129 | |
| 130 | call histdel(':') |
| 131 | " items go in between |
| 132 | call test_settime(15) |
| 133 | call histadd(':', "echo '15'") |
| 134 | call test_settime(27) |
| 135 | call histadd(':', "echo '27'") |
| 136 | |
| 137 | rviminfo Xviminfo |
| 138 | call assert_equal("echo '33'", histget(':', -1)) |
| 139 | call assert_equal("echo '27'", histget(':', -2)) |
| 140 | call assert_equal("echo '22'", histget(':', -3)) |
| 141 | call assert_equal("echo '15'", histget(':', -4)) |
| 142 | call assert_equal("echo '11'", histget(':', -5)) |
| 143 | |
| 144 | call histdel(':') |
| 145 | " items go before and after |
| 146 | call test_settime(8) |
| 147 | call histadd(':', "echo '8'") |
| 148 | call test_settime(39) |
| 149 | call histadd(':', "echo '39'") |
| 150 | |
| 151 | rviminfo Xviminfo |
| 152 | call assert_equal("echo '39'", histget(':', -1)) |
| 153 | call assert_equal("echo '33'", histget(':', -2)) |
| 154 | call assert_equal("echo '22'", histget(':', -3)) |
| 155 | call assert_equal("echo '11'", histget(':', -4)) |
| 156 | call assert_equal("echo '8'", histget(':', -5)) |
| 157 | |
| 158 | " Check sorting works when writing with merge. |
| 159 | call histdel(':') |
| 160 | call test_settime(8) |
| 161 | call histadd(':', "echo '8'") |
| 162 | call test_settime(15) |
| 163 | call histadd(':', "echo '15'") |
| 164 | call test_settime(27) |
| 165 | call histadd(':', "echo '27'") |
| 166 | call test_settime(39) |
| 167 | call histadd(':', "echo '39'") |
| 168 | wviminfo Xviminfo |
| 169 | |
| 170 | call histdel(':') |
| 171 | rviminfo Xviminfo |
| 172 | call assert_equal("echo '39'", histget(':', -1)) |
| 173 | call assert_equal("echo '33'", histget(':', -2)) |
| 174 | call assert_equal("echo '27'", histget(':', -3)) |
| 175 | call assert_equal("echo '22'", histget(':', -4)) |
| 176 | call assert_equal("echo '15'", histget(':', -5)) |
| 177 | call assert_equal("echo '11'", histget(':', -6)) |
| 178 | call assert_equal("echo '8'", histget(':', -7)) |
| 179 | |
| 180 | call delete('Xviminfo') |
| 181 | endfunc |
Bram Moolenaar | 0122709 | 2016-06-11 14:47:40 +0200 | [diff] [blame] | 182 | |
| 183 | func Test_viminfo_encoding() |
| 184 | if !has('multi_byte') |
| 185 | return |
| 186 | endif |
| 187 | set enc=latin1 |
| 188 | call histdel(':') |
| 189 | call histadd(':', "echo '\xe9'") |
| 190 | wviminfo Xviminfo |
| 191 | |
| 192 | set fencs=utf-8,latin1 |
| 193 | set enc=utf-8 |
| 194 | sp Xviminfo |
| 195 | call assert_equal('latin1', &fenc) |
| 196 | close |
| 197 | |
| 198 | call histdel(':') |
| 199 | rviminfo Xviminfo |
| 200 | call assert_equal("echo 'é'", histget(':', -1)) |
| 201 | |
| 202 | call delete('Xviminfo') |
| 203 | endfunc |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 204 | |
| 205 | func Test_viminfo_bad_syntax() |
| 206 | let lines = [] |
| 207 | call add(lines, '|<') " empty continuation line |
| 208 | call add(lines, '|234234234234234324,nothing') |
| 209 | call add(lines, '|1+"no comma"') |
| 210 | call add(lines, '|1,2,3,4,5,6,7') " too many items |
| 211 | call add(lines, '|1,"string version"') |
| 212 | call add(lines, '|1,>x') " bad continuation line |
| 213 | call add(lines, '|1,"x') " missing quote |
| 214 | call add(lines, '|1,"x\') " trailing backslash |
| 215 | call add(lines, '|1,,,,') "trailing comma |
| 216 | call add(lines, '|1,>234') " trailing continuation line |
| 217 | call writefile(lines, 'Xviminfo') |
| 218 | call assert_fails('rviminfo Xviminfo', 'E685:') |
| 219 | |
| 220 | call delete('Xviminfo') |
| 221 | endfunc |
| 222 | |