printf_spec.lua (3208B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local clear = n.clear 5 local eq = t.eq 6 local eval = n.eval 7 local fn = n.fn 8 local api = n.api 9 local exc_exec = n.exc_exec 10 11 describe('printf()', function() 12 setup(clear) 13 14 it('works with zero and %b', function() 15 eq('0', fn.printf('%lb', 0)) 16 eq('0', fn.printf('%llb', 0)) 17 eq('0', fn.printf('%zb', 0)) 18 end) 19 it('works with one and %b', function() 20 eq('1', fn.printf('%b', 1)) 21 eq('1', fn.printf('%lb', 1)) 22 eq('1', fn.printf('%llb', 1)) 23 eq('1', fn.printf('%zb', 1)) 24 end) 25 it('works with 0xff and %b', function() 26 eq('11111111', fn.printf('%b', 0xff)) 27 eq('11111111', fn.printf('%lb', 0xff)) 28 eq('11111111', fn.printf('%llb', 0xff)) 29 eq('11111111', fn.printf('%zb', 0xff)) 30 end) 31 it('accepts width modifier with %b', function() 32 eq(' 1', fn.printf('%3b', 1)) 33 end) 34 it('accepts prefix modifier with %b', function() 35 eq('0b1', fn.printf('%#b', 1)) 36 end) 37 it('writes capital B with %B', function() 38 eq('0B1', fn.printf('%#B', 1)) 39 end) 40 it('accepts prefix, zero-fill and width modifiers with %b', function() 41 eq('0b001', fn.printf('%#05b', 1)) 42 end) 43 it('accepts prefix and width modifiers with %b', function() 44 eq(' 0b1', fn.printf('%#5b', 1)) 45 end) 46 it('does not write prefix for zero with prefix and width modifier used with %b', function() 47 eq(' 0', fn.printf('%#5b', 0)) 48 end) 49 it('accepts precision modifier with %b', function() 50 eq('00000', fn.printf('%.5b', 0)) 51 end) 52 it('accepts all modifiers with %b at once', function() 53 -- zero-fill modifier is ignored when used with left-align 54 -- force-sign and add-blank are ignored 55 -- use-grouping-characters modifier is ignored always 56 eq('0b00011 ', fn.printf("% '+#0-10.5b", 3)) 57 end) 58 it('errors out when %b modifier is used for a list', function() 59 eq('Vim(call):E745: Using a List as a Number', exc_exec('call printf("%b", [])')) 60 end) 61 it('errors out when %b modifier is used for a float', function() 62 eq('Vim(call):E805: Using a Float as a Number', exc_exec('call printf("%b", 3.1415926535)')) 63 end) 64 it('works with %p correctly', function() 65 local null_ret = nil 66 local seen_rets = {} 67 -- Collect all args in an array to avoid possible allocation of the same 68 -- address after freeing unreferenced values. 69 api.nvim_set_var('__args', {}) 70 local function check_printf(expr, is_null) 71 eq(0, exc_exec('call add(__args, ' .. expr .. ')')) 72 eq(0, exc_exec('let __result = printf("%p", __args[-1])')) 73 local id_ret = eval('id(__args[-1])') 74 eq(id_ret, api.nvim_get_var('__result')) 75 if is_null then 76 if null_ret then 77 eq(null_ret, id_ret) 78 else 79 null_ret = id_ret 80 end 81 else 82 eq(nil, seen_rets[id_ret]) 83 seen_rets[id_ret] = expr 84 end 85 api.nvim_del_var('__result') 86 end 87 check_printf('v:_null_string', true) 88 check_printf('v:_null_list', true) 89 check_printf('v:_null_dict', true) 90 check_printf('v:_null_blob', true) 91 check_printf('[]') 92 check_printf('{}') 93 check_printf('0z') 94 check_printf('function("tr", ["a"])') 95 end) 96 end)