robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Test suite for testing interactions with API bindings 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local clear =n . clear 7local eq =t . eq 8local exec_lua =n . exec_lua 9 10describe ( 'lua vim.mpack' , function () 11before_each ( clear ) 12it ( 'encodes vim.NIL' , function () 13eq ( 14{ true , true , true , true } , 15exec_lua ( function () 16local var =vim . mpack . decode ( vim . mpack . encode ( { 33 , vim . NIL , 77 } )) 17return { var [ 1 ] == 33 , var [ 2 ] == vim . NIL , var [ 3 ] == 77 , var [ 4 ] == nil } 18end ) 19) 20end ) 21 22it ( 'encodes vim.empty_dict()' , function () 23eq ( 24{ { {} , 'foo' , {} } , true , false } , 25exec_lua ( function () 26local var =vim . mpack . decode ( vim . mpack . encode ( { {} , 'foo' , vim . empty_dict () } )) 27return { var , vim . islist ( var [ 1 ]), vim . islist ( var [ 3 ]) } 28end ) 29) 30end ) 31 32it ( 'encodes negative integers at type boundaries correctly #37202' , function () 33-- Test boundary values between int8/int16/int32 34-- int8 range: -128 to -33 (fixint handles -32 to -1) 35-- int16 range: -32768 to -129 36-- int32 range: -2147483648 to -32769 37local result =exec_lua ( function () 38local tests ={ 39{ - 128 , - 128 } , -- int8 boundary (minimum int8) 40{ - 129 , - 129 } , -- int16 boundary (one past int8) 41{ - 32768 , - 32768 } , -- int16 boundary (minimum int16) 42{ - 32769 , - 32769 } , -- int32 boundary (one past int16) 43} 44local results ={} 45for _ , test in ipairs ( tests ) do 46local input , expected =test [ 1 ], test [ 2 ] 47local decoded =vim . mpack . decode ( vim . mpack . encode ( input )) 48table . insert ( results , { input =input , decoded =decoded , ok =decoded == expected } ) 49end 50return results 51end ) 52for _ , r in ipairs ( result ) do 53eq ( true , r . ok , string . format ( 'encode/decode %d returned %d' , r . input , r . decoded )) 54end 55end ) 56 57it ( 'encodes negative integers below -2^32 correctly' , function () 58eq ( 59{} , 60exec_lua ( function () 61local failures ={} 62for _ , v in ipairs ( { - ( 2 ^ 32 + 5 ), - ( 3 * 2 ^ 32 + 70000 ), - ( 2 ^ 53 - 1 ) } ) do 63local decoded =vim . mpack . decode ( vim . mpack . encode ( v )) 64if decoded ~= v then 65table . insert ( failures , ( '%.17g -> %.17g' ): format ( v , decoded )) 66end 67end 68return failures 69end ) 70) 71end ) 72 73it ( 'encodes numbers beyond 2^53 #32216' , function () 74eq ( 75{} , 76exec_lua ( function () 77local failures ={} 78local tests ={ 792 ^ 53 , 80- 2 ^ 53 , 812 ^ 64 - 2048 , -- largest uint64 double 82- 2 ^ 63 , -- smallest int64 83- 2 ^ 63 - 2048 , -- below int64, encoded as float 84- 1.5 * 2 ^ 63 , 852 ^ 64 , -- above uint64, encoded as float 861e300 , 87math . huge , 88- math . huge , 89} 90for _ , v in ipairs ( tests ) do 91local decoded =vim . mpack . decode ( vim . mpack . encode ( v )) 92if decoded ~= v then 93table . insert ( failures , ( '%.17g -> %.17g' ): format ( v , decoded )) 94end 95end 96local nan =vim . mpack . decode ( vim . mpack . encode ( 0 / 0 )) 97if nan == nan then 98table . insert ( failures , ( 'nan -> %.17g' ): format ( nan )) 99end 100return failures 101end ) 102) 103end ) 104 105it ( 'encodes dict keys of length 20-31 as fixstr #32784' , function () 106-- MessagePack fixstr format: 0xa0 | length (for lengths 0-31) 107-- Before #36737, strings 20-31 bytes were incorrectly encoded as str8 (0xd9, len) 108for len =20 , 31 do 109local expected_header =string . char ( 0xa0 + len ) -- fixstr header 110local result =exec_lua ( function ( keylen ) 111local key =string . rep ( 'x' , keylen ) 112return vim . mpack . encode ( { [ key ] =1 } ) 113end , len ) 114-- Byte 1 is fixmap header (0x81), byte 2 should be fixstr header for the key 115eq ( expected_header , result : sub ( 2 , 2 ), 'dict key length ' .. len .. ' should use fixstr' ) 116end 117end ) 118end )