1
local helpers = require('test.functional.helpers')
2
local clear = helpers.clear
4
local command = helpers.command
5
local meths = helpers.meths
6
local eval = helpers.eval
7
local exc_exec = helpers.exc_exec
8
local redir_exec = helpers.redir_exec
9
local funcs = helpers.funcs
10
local write_file = helpers.write_file
12
describe('string() function', function()
15
describe('used to represent floating-point values', function()
16
it('dumps NaN values', function()
17
eq('str2float(\'nan\')', eval('string(str2float(\'nan\'))'))
20
it('dumps infinite values', function()
21
eq('str2float(\'inf\')', eval('string(str2float(\'inf\'))'))
22
eq('-str2float(\'inf\')', eval('string(str2float(\'-inf\'))'))
25
it('dumps regular values', function()
26
eq('1.5', funcs.string(1.5))
27
eq('1.56e-20', funcs.string(1.56000e-020))
28
eq('0.0', eval('string(0.0)'))
31
it('dumps values with at most six digits after the decimal point',
33
eq('1.234568e-20', funcs.string(1.23456789123456789123456789e-020))
34
eq('1.234568', funcs.string(1.23456789123456789123456789))
37
it('dumps values with at most seven digits before the decimal point',
39
eq('1234567.891235', funcs.string(1234567.89123456789123456789))
40
eq('1.234568e7', funcs.string(12345678.9123456789123456789))
43
it('dumps negative values', function()
44
eq('-1.5', funcs.string(-1.5))
45
eq('-1.56e-20', funcs.string(-1.56000e-020))
46
eq('-1.234568e-20', funcs.string(-1.23456789123456789123456789e-020))
47
eq('-1.234568', funcs.string(-1.23456789123456789123456789))
48
eq('-1234567.891235', funcs.string(-1234567.89123456789123456789))
49
eq('-1.234568e7', funcs.string(-12345678.9123456789123456789))
53
describe('used to represent numbers', function()
54
it('dumps regular values', function()
55
eq('0', funcs.string(0))
56
eq('-1', funcs.string(-1))
57
eq('1', funcs.string(1))
60
it('dumps large values', function()
61
eq('2147483647', funcs.string(2^31-1))
62
eq('-2147483648', funcs.string(-2^31))
66
describe('used to represent strings', function()
67
it('dumps regular strings', function()
68
eq('\'test\'', funcs.string('test'))
71
it('dumps empty strings', function()
72
eq('\'\'', funcs.string(''))
75
it('dumps strings with \' inside', function()
76
eq('\'\'\'\'\'\'\'\'', funcs.string('\'\'\''))
77
eq('\'a\'\'b\'\'\'\'\'', funcs.string('a\'b\'\''))
78
eq('\'\'\'b\'\'\'\'d\'', funcs.string('\'b\'\'d'))
79
eq('\'a\'\'b\'\'c\'\'d\'', funcs.string('a\'b\'c\'d'))
83
describe('used to represent funcrefs', function()
84
local fname = 'Xtest-functional-eval-string_spec-fref-script.vim'
86
before_each(function()
97
let g:Test2_f = function('s:Test2')
99
command('source ' .. fname)
102
after_each(function()
106
it('dumps references to built-in functions', function()
107
eq('function(\'function\')', eval('string(function("function"))'))
110
it('dumps references to user functions', function()
111
eq('function(\'Test1\')', eval('string(function("Test1"))'))
112
eq('function(\'g:Test3\')', eval('string(function("g:Test3"))'))
115
it('dumps references to script functions', function()
116
eq('function(\'<SNR>1_Test2\')', eval('string(Test2_f)'))
120
describe('used to represent lists', function()
121
it('dumps empty list', function()
122
eq('[]', funcs.string({}))
125
it('dumps nested lists', function()
126
eq('[[[[[]]]]]', funcs.string({{{{{}}}}}))
129
it('dumps nested non-empty lists', function()
130
eq('[1, [[3, [[5], 4]], 2]]', funcs.string({1, {{3, {{5}, 4}}, 2}}))
133
it('errors when dumping recursive lists', function()
134
meths.set_var('l', {})
136
eq('Vim(echo):E724: unable to correctly dump variable with self-referencing container',
137
exc_exec('echo string(l)'))
140
it('dumps recursive lists despite the error', function()
141
meths.set_var('l', {})
143
eq('\nE724: unable to correctly dump variable with self-referencing container\n[{E724@0}]',
144
redir_exec('echo string(l)'))
145
eq('\nE724: unable to correctly dump variable with self-referencing container\n[[{E724@1}]]',
146
redir_exec('echo string([l])'))
150
describe('used to represent dictionaries', function()
151
it('dumps empty dictionary', function()
152
eq('{}', eval('string({})'))
155
it('dumps non-empty dictionary', function()
156
eq('{\'t\'\'est\': 1}', funcs.string({['t\'est']=1}))
159
it('errors when dumping recursive dictionaries', function()
160
meths.set_var('d', {d=1})
161
eval('extend(d, {"d": d})')
162
eq('Vim(echo):E724: unable to correctly dump variable with self-referencing container',
163
exc_exec('echo string(d)'))
166
it('dumps recursive dictionaries despite the error', function()
167
meths.set_var('d', {d=1})
168
eval('extend(d, {"d": d})')
169
eq('\nE724: unable to correctly dump variable with self-referencing container\n{\'d\': {E724@0}}',
170
redir_exec('echo string(d)'))
171
eq('\nE724: unable to correctly dump variable with self-referencing container\n{\'out\': {\'d\': {E724@1}}}',
172
redir_exec('echo string({"out": d})'))