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