robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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