robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.8 KiB199 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local command = n.command
6local fn = n.fn
7local clear = n.clear
8local eval = n.eval
9local eq = t.eq
10local pcall_err = t.pcall_err
11local api = n.api
12local NIL = vim.NIL
13
14describe('Special values', function()
15  before_each(clear)
16
17  it('do not cause error when freed', function()
18    command([[
19      function Test()
20        try
21          return v:true
22        finally
23          return 'something else'
24        endtry
25      endfunction
26    ]])
27    command('call Test()')
28  end)
29
30  it('work with empty()', function()
31    eq(0, fn.empty(true))
32    eq(1, fn.empty(false))
33    eq(1, fn.empty(NIL))
34  end)
35
36  it('can be stringified and eval’ed back', function()
37    eq(true, fn.eval(fn.string(true)))
38    eq(false, fn.eval(fn.string(false)))
39    eq(NIL, fn.eval(fn.string(NIL)))
40  end)
41
42  it('work with is/isnot properly', function()
43    eq(1, eval('v:null is v:null'))
44    eq(0, eval('v:null is v:true'))
45    eq(0, eval('v:null is v:false'))
46    eq(1, eval('v:true is v:true'))
47    eq(0, eval('v:true is v:false'))
48    eq(1, eval('v:false is v:false'))
49
50    eq(0, eval('v:null  is 0'))
51    eq(0, eval('v:true  is 0'))
52    eq(0, eval('v:false is 0'))
53
54    eq(0, eval('v:null  is 1'))
55    eq(0, eval('v:true  is 1'))
56    eq(0, eval('v:false is 1'))
57
58    eq(0, eval('v:null  is ""'))
59    eq(0, eval('v:true  is ""'))
60    eq(0, eval('v:false is ""'))
61
62    eq(0, eval('v:null  is "null"'))
63    eq(0, eval('v:true  is "true"'))
64    eq(0, eval('v:false is "false"'))
65
66    eq(0, eval('v:null  is []'))
67    eq(0, eval('v:true  is []'))
68    eq(0, eval('v:false is []'))
69
70    eq(0, eval('v:null isnot v:null'))
71    eq(1, eval('v:null isnot v:true'))
72    eq(1, eval('v:null isnot v:false'))
73    eq(0, eval('v:true isnot v:true'))
74    eq(1, eval('v:true isnot v:false'))
75    eq(0, eval('v:false isnot v:false'))
76
77    eq(1, eval('v:null  isnot 0'))
78    eq(1, eval('v:true  isnot 0'))
79    eq(1, eval('v:false isnot 0'))
80
81    eq(1, eval('v:null  isnot 1'))
82    eq(1, eval('v:true  isnot 1'))
83    eq(1, eval('v:false isnot 1'))
84
85    eq(1, eval('v:null  isnot ""'))
86    eq(1, eval('v:true  isnot ""'))
87    eq(1, eval('v:false isnot ""'))
88
89    eq(1, eval('v:null  isnot "null"'))
90    eq(1, eval('v:true  isnot "true"'))
91    eq(1, eval('v:false isnot "false"'))
92
93    eq(1, eval('v:null  isnot []'))
94    eq(1, eval('v:true  isnot []'))
95    eq(1, eval('v:false isnot []'))
96  end)
97
98  it('work with +/-/* properly', function()
99    eq(1, eval('0 + v:true'))
100    eq(0, eval('0 + v:null'))
101    eq(0, eval('0 + v:false'))
102
103    eq(-1, eval('0 - v:true'))
104    eq(0, eval('0 - v:null'))
105    eq(0, eval('0 - v:false'))
106
107    eq(1, eval('1 * v:true'))
108    eq(0, eval('1 * v:null'))
109    eq(0, eval('1 * v:false'))
110  end)
111
112  it('does not work with +=/-=/.=', function()
113    api.nvim_set_var('true', true)
114    api.nvim_set_var('false', false)
115    command('let null = v:null')
116
117    eq('Vim(let):E734: Wrong variable type for +=', pcall_err(command, 'let true  += 1'))
118    eq('Vim(let):E734: Wrong variable type for +=', pcall_err(command, 'let false += 1'))
119    eq('Vim(let):E734: Wrong variable type for +=', pcall_err(command, 'let null  += 1'))
120
121    eq('Vim(let):E734: Wrong variable type for -=', pcall_err(command, 'let true  -= 1'))
122    eq('Vim(let):E734: Wrong variable type for -=', pcall_err(command, 'let false -= 1'))
123    eq('Vim(let):E734: Wrong variable type for -=', pcall_err(command, 'let null  -= 1'))
124
125    eq('Vim(let):E734: Wrong variable type for .=', pcall_err(command, 'let true  .= 1'))
126    eq('Vim(let):E734: Wrong variable type for .=', pcall_err(command, 'let false .= 1'))
127    eq('Vim(let):E734: Wrong variable type for .=', pcall_err(command, 'let null  .= 1'))
128  end)
129
130  it('work with . (concat) properly', function()
131    eq('v:true', eval('"" . v:true'))
132    eq('v:null', eval('"" . v:null'))
133    eq('v:false', eval('"" . v:false'))
134  end)
135
136  it('work with ?? (falsy operator)', function()
137    eq(true, eval('v:true ?? 42'))
138    eq(42, eval('v:false ?? 42'))
139    eq(42, eval('v:null ?? 42'))
140  end)
141
142  it('work with type()', function()
143    eq(6, fn.type(true))
144    eq(6, fn.type(false))
145    eq(7, fn.type(NIL))
146  end)
147
148  it('work with copy() and deepcopy()', function()
149    eq(true, fn.deepcopy(true))
150    eq(false, fn.deepcopy(false))
151    eq(NIL, fn.deepcopy(NIL))
152
153    eq(true, fn.copy(true))
154    eq(false, fn.copy(false))
155    eq(NIL, fn.copy(NIL))
156  end)
157
158  it('fails in index', function()
159    eq('Vim(echo):E909: Cannot index a special variable', pcall_err(command, 'echo v:true[0]'))
160    eq('Vim(echo):E909: Cannot index a special variable', pcall_err(command, 'echo v:false[0]'))
161    eq('Vim(echo):E909: Cannot index a special variable', pcall_err(command, 'echo v:null[0]'))
162  end)
163
164  it('is accepted by assert_true and assert_false', function()
165    fn.assert_false(false)
166    fn.assert_false(true)
167    fn.assert_false(NIL)
168
169    fn.assert_true(false)
170    fn.assert_true(true)
171    fn.assert_true(NIL)
172
173    eq({
174      'Expected False but got v:true',
175      'Expected False but got v:null',
176      'Expected True but got v:false',
177      'Expected True but got v:null',
178    }, api.nvim_get_vvar('errors'))
179  end)
180
181  describe('compat', function()
182    it('v:count is distinct from count', function()
183      command('let count = []') -- v:count is readonly
184      eq(1, eval('count is# g:["count"]'))
185    end)
186    it('v:errmsg is distinct from errmsg', function()
187      command('let errmsg = 1')
188      eq(1, eval('errmsg is# g:["errmsg"]'))
189    end)
190    it('v:shell_error is distinct from shell_error', function()
191      command('let shell_error = []') -- v:shell_error is readonly
192      eq(1, eval('shell_error is# g:["shell_error"]'))
193    end)
194    it('v:this_session is distinct from this_session', function()
195      command('let this_session = []')
196      eq(1, eval('this_session is# g:["this_session"]'))
197    end)
198  end)
199end)