registers_spec.lua (5747B)
1 -- ShaDa registers saving/reading support 2 local t = require('test.testutil') 3 local n = require('test.functional.testnvim')() 4 local t_shada = require('test.functional.shada.testutil') 5 6 local nvim_command, fn, eq = n.command, n.fn, t.eq 7 local reset, clear = t_shada.reset, t_shada.clear 8 local expect_exit = n.expect_exit 9 10 local setreg = function(name, contents, typ) 11 if type(contents) == 'string' then 12 contents = { contents } 13 end 14 fn.setreg(name, contents, typ) 15 end 16 17 local getreg = function(name) 18 return { 19 fn.getreg(name, 1, 1), 20 fn.getregtype(name), 21 } 22 end 23 24 describe('ShaDa support code', function() 25 before_each(reset) 26 after_each(clear) 27 28 it('is able to dump and restore registers and their type', function() 29 setreg('c', { 'd', 'e', '' }, 'c') 30 setreg('l', { 'a', 'b', 'cde' }, 'l') 31 setreg('b', { 'bca', 'abc', 'cba' }, 'b3') 32 expect_exit(nvim_command, 'qall') 33 reset() 34 eq({ { 'd', 'e', '' }, 'v' }, getreg('c')) 35 eq({ { 'a', 'b', 'cde' }, 'V' }, getreg('l')) 36 eq({ { 'bca', 'abc', 'cba' }, '\0223' }, getreg('b')) 37 end) 38 39 it('does not dump registers with zero <', function() 40 nvim_command("set shada='0,<0") 41 setreg('c', { 'd', 'e', '' }, 'c') 42 setreg('l', { 'a', 'b', 'cde' }, 'l') 43 setreg('b', { 'bca', 'abc', 'cba' }, 'b3') 44 expect_exit(nvim_command, 'qall') 45 reset() 46 eq({ {}, '' }, getreg('c')) 47 eq({ {}, '' }, getreg('l')) 48 eq({ {}, '' }, getreg('b')) 49 end) 50 51 it('does restore registers with zero <', function() 52 setreg('c', { 'd', 'e', '' }, 'c') 53 setreg('l', { 'a', 'b', 'cde' }, 'l') 54 setreg('b', { 'bca', 'abc', 'cba' }, 'b3') 55 expect_exit(nvim_command, 'qall') 56 reset("set shada='0,<0") 57 eq({ { 'd', 'e', '' }, 'v' }, getreg('c')) 58 eq({ { 'a', 'b', 'cde' }, 'V' }, getreg('l')) 59 eq({ { 'bca', 'abc', 'cba' }, '\0223' }, getreg('b')) 60 end) 61 62 it('does not dump registers with zero "', function() 63 nvim_command('set shada=\'0,\\"0') 64 setreg('c', { 'd', 'e', '' }, 'c') 65 setreg('l', { 'a', 'b', 'cde' }, 'l') 66 setreg('b', { 'bca', 'abc', 'cba' }, 'b3') 67 expect_exit(nvim_command, 'qall') 68 reset() 69 eq({ {}, '' }, getreg('c')) 70 eq({ {}, '' }, getreg('l')) 71 eq({ {}, '' }, getreg('b')) 72 end) 73 74 it('does restore registers with zero "', function() 75 setreg('c', { 'd', 'e', '' }, 'c') 76 setreg('l', { 'a', 'b', 'cde' }, 'l') 77 setreg('b', { 'bca', 'abc', 'cba' }, 'b3') 78 expect_exit(nvim_command, 'qall') 79 reset('set shada=\'0,\\"0') 80 eq({ { 'd', 'e', '' }, 'v' }, getreg('c')) 81 eq({ { 'a', 'b', 'cde' }, 'V' }, getreg('l')) 82 eq({ { 'bca', 'abc', 'cba' }, '\0223' }, getreg('b')) 83 end) 84 85 it('does dump registers with zero ", but non-zero <', function() 86 nvim_command('set shada=\'0,\\"0,<50') 87 setreg('c', { 'd', 'e', '' }, 'c') 88 setreg('l', { 'a', 'b', 'cde' }, 'l') 89 setreg('b', { 'bca', 'abc', 'cba' }, 'b3') 90 expect_exit(nvim_command, 'qall') 91 reset() 92 eq({ { 'd', 'e', '' }, 'v' }, getreg('c')) 93 eq({ { 'a', 'b', 'cde' }, 'V' }, getreg('l')) 94 eq({ { 'bca', 'abc', 'cba' }, '\0223' }, getreg('b')) 95 end) 96 97 it('does limit number of lines according to <', function() 98 nvim_command("set shada='0,<2") 99 setreg('o', { 'd' }, 'c') 100 setreg('t', { 'a', 'b', 'cde' }, 'l') 101 expect_exit(nvim_command, 'qall') 102 reset() 103 eq({ { 'd' }, 'v' }, getreg('o')) 104 eq({ {}, '' }, getreg('t')) 105 end) 106 107 it('does limit number of lines according to "', function() 108 nvim_command('set shada=\'0,\\"2') 109 setreg('o', { 'd' }, 'c') 110 setreg('t', { 'a', 'b', 'cde' }, 'l') 111 expect_exit(nvim_command, 'qall') 112 reset() 113 eq({ { 'd' }, 'v' }, getreg('o')) 114 eq({ {}, '' }, getreg('t')) 115 end) 116 117 it('does limit number of lines according to < rather then "', function() 118 nvim_command('set shada=\'0,\\"2,<3') 119 setreg('o', { 'd' }, 'c') 120 setreg('t', { 'a', 'b', 'cde' }, 'l') 121 setreg('h', { 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' }, 'b3') 122 expect_exit(nvim_command, 'qall') 123 reset() 124 eq({ { 'd' }, 'v' }, getreg('o')) 125 eq({ { 'a', 'b', 'cde' }, 'V' }, getreg('t')) 126 eq({ {}, '' }, getreg('h')) 127 end) 128 129 it('dumps and loads register correctly with utf-8 contents', function() 130 reset() 131 setreg('e', { '«' }, 'c') 132 expect_exit(nvim_command, 'qall') 133 reset() 134 eq({ { '«' }, 'v' }, getreg('e')) 135 end) 136 137 it('dumps and loads history correctly with 8-bit single-byte', function() 138 reset() 139 -- \171 is U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK in latin1 140 setreg('e', { '\171«' }, 'c') 141 expect_exit(nvim_command, 'qall') 142 reset() 143 eq({ { '\171«' }, 'v' }, getreg('e')) 144 end) 145 146 it("has a blank unnamed register if it wasn't set and register 0 is empty", function() 147 setreg('1', { 'one' }, 'c') 148 setreg('2', { 'two' }, 'c') 149 setreg('a', { 'a' }, 'c') 150 expect_exit(nvim_command, 'qall') 151 reset() 152 eq({ {}, '' }, getreg('0')) 153 eq({ { 'one' }, 'v' }, getreg('1')) 154 eq({ {}, '' }, getreg('"')) 155 eq({ { 'a' }, 'v' }, getreg('a')) 156 end) 157 158 it("defaults the unnamed register to register 0 if it wasn't set", function() 159 setreg('0', { 'zero' }, 'c') 160 setreg('1', { 'one' }, 'c') 161 setreg('2', { 'two' }, 'c') 162 expect_exit(nvim_command, 'qall') 163 reset() 164 eq({ { 'zero' }, 'v' }, getreg('0')) 165 eq({ { 'one' }, 'v' }, getreg('1')) 166 eq({ { 'zero' }, 'v' }, getreg('"')) 167 end) 168 169 it('remembers which register was the unnamed register when loading', function() 170 setreg('0', { 'zero' }, 'c') 171 setreg('1', { 'one' }, 'cu') 172 setreg('2', { 'two' }, 'c') 173 expect_exit(nvim_command, 'qall') 174 reset() 175 eq({ { 'zero' }, 'v' }, getreg('0')) 176 eq({ { 'one' }, 'v' }, getreg('1')) 177 eq({ { 'one' }, 'v' }, getreg('"')) 178 end) 179 end)