2016-04-23 16:53:11 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
2017-02-11 11:47:02 -07:00
|
|
|
|
2015-12-29 16:17:16 -07:00
|
|
|
local clear = helpers.clear
|
|
|
|
local eq = helpers.eq
|
2017-02-11 11:47:02 -07:00
|
|
|
local eval = helpers.eval
|
2024-01-12 10:59:57 -07:00
|
|
|
local fn = helpers.fn
|
|
|
|
local api = helpers.api
|
2015-12-29 16:17:16 -07:00
|
|
|
local exc_exec = helpers.exc_exec
|
|
|
|
|
|
|
|
describe('printf()', function()
|
2016-01-20 23:24:10 -07:00
|
|
|
before_each(clear)
|
|
|
|
|
2015-12-29 16:17:16 -07:00
|
|
|
it('works with zero and %b', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('0', fn.printf('%lb', 0))
|
|
|
|
eq('0', fn.printf('%llb', 0))
|
|
|
|
eq('0', fn.printf('%zb', 0))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('works with one and %b', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('1', fn.printf('%b', 1))
|
|
|
|
eq('1', fn.printf('%lb', 1))
|
|
|
|
eq('1', fn.printf('%llb', 1))
|
|
|
|
eq('1', fn.printf('%zb', 1))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('works with 0xff and %b', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('11111111', fn.printf('%b', 0xff))
|
|
|
|
eq('11111111', fn.printf('%lb', 0xff))
|
|
|
|
eq('11111111', fn.printf('%llb', 0xff))
|
|
|
|
eq('11111111', fn.printf('%zb', 0xff))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('accepts width modifier with %b', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(' 1', fn.printf('%3b', 1))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('accepts prefix modifier with %b', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('0b1', fn.printf('%#b', 1))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('writes capital B with %B', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('0B1', fn.printf('%#B', 1))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('accepts prefix, zero-fill and width modifiers with %b', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('0b001', fn.printf('%#05b', 1))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('accepts prefix and width modifiers with %b', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(' 0b1', fn.printf('%#5b', 1))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('does not write prefix for zero with prefix and width modifier used with %b', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(' 0', fn.printf('%#5b', 0))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('accepts precision modifier with %b', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('00000', fn.printf('%.5b', 0))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('accepts all modifiers with %b at once', function()
|
|
|
|
-- zero-fill modifier is ignored when used with left-align
|
|
|
|
-- force-sign and add-blank are ignored
|
|
|
|
-- use-grouping-characters modifier is ignored always
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('0b00011 ', fn.printf("% '+#0-10.5b", 3))
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|
|
|
|
it('errors out when %b modifier is used for a list', function()
|
|
|
|
eq('Vim(call):E745: Using a List as a Number', exc_exec('call printf("%b", [])'))
|
|
|
|
end)
|
|
|
|
it('errors out when %b modifier is used for a float', function()
|
|
|
|
eq('Vim(call):E805: Using a Float as a Number', exc_exec('call printf("%b", 3.1415926535)'))
|
|
|
|
end)
|
2017-02-11 11:47:02 -07:00
|
|
|
it('works with %p correctly', function()
|
|
|
|
local null_ret = nil
|
|
|
|
local seen_rets = {}
|
|
|
|
-- Collect all args in an array to avoid possible allocation of the same
|
|
|
|
-- address after freeing unreferenced values.
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('__args', {})
|
2017-02-11 11:47:02 -07:00
|
|
|
local function check_printf(expr, is_null)
|
|
|
|
eq(0, exc_exec('call add(__args, ' .. expr .. ')'))
|
|
|
|
eq(0, exc_exec('let __result = printf("%p", __args[-1])'))
|
|
|
|
local id_ret = eval('id(__args[-1])')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(id_ret, api.nvim_get_var('__result'))
|
2017-02-11 11:47:02 -07:00
|
|
|
if is_null then
|
|
|
|
if null_ret then
|
|
|
|
eq(null_ret, id_ret)
|
|
|
|
else
|
|
|
|
null_ret = id_ret
|
|
|
|
end
|
|
|
|
else
|
|
|
|
eq(nil, seen_rets[id_ret])
|
|
|
|
seen_rets[id_ret] = expr
|
|
|
|
end
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_del_var('__result')
|
2017-02-11 11:47:02 -07:00
|
|
|
end
|
|
|
|
check_printf('v:_null_list', true)
|
|
|
|
check_printf('v:_null_dict', true)
|
|
|
|
check_printf('[]')
|
|
|
|
check_printf('{}')
|
|
|
|
check_printf('function("tr", ["a"])')
|
|
|
|
end)
|
2015-12-29 16:17:16 -07:00
|
|
|
end)
|