robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local describe , it , setup =t . describe , t . it , t . setup 3local pcall_err =t . pcall_err 4local n =require ( 'test.functional.testnvim' )() 5 6local clear =n . clear 7local fn =n . fn 8local eval , eq =n . eval , t . eq 9local command =n . command 10local api =n . api 11 12describe ( 'msgpack*() functions' , function () 13setup ( clear ) 14 15local obj_test =function ( msg , obj ) 16it ( msg , function () 17api . nvim_set_var ( 'obj' , obj ) 18eq ( obj , eval ( 'msgpackparse(msgpackdump(g:obj))' )) 19eq ( obj , eval ( 'msgpackparse(msgpackdump(g:obj, "B"))' )) 20end ) 21end 22 23-- Regression test: msgpack_list_write was failing to write buffer with zero 24-- length. 25obj_test ( 'are able to dump and restore {"file": ""}' , { { file ='' } } ) 26-- Regression test: msgpack_list_write was failing to write buffer with NL at 27-- the end. 28obj_test ( 'are able to dump and restore {0, "echo mpack"}' , { { 0 , 'echo mpack' } } ) 29obj_test ( 'are able to dump and restore "Test\\n"' , { 'Test\n' } ) 30-- Regression test: msgpack_list_write was failing to write buffer with NL 31-- inside. 32obj_test ( 'are able to dump and restore "Test\\nTest 2"' , { 'Test\nTest 2' } ) 33-- Test that big objects (requirement: dump to something that is bigger then 34-- IOSIZE) are also fine. This particular object is obtained by concatenating 35-- 5 identical shada files. 36-- stylua: ignore 37local big_obj ={ 381 , 1436711454 , 78 , { 39encoding ="utf-8" , 40max_kbyte =10 , 41pid =19269 , 42version ="NVIM 0.0.0-alpha+201507121634" 43} , 448 , 1436711451 , 40 , { file ="/home/zyx/.nvim/shada/main.shada" } , 458 , 1436711391 , 8 , { file ="" } , 464 , 1436700940 , 30 , { 0 , "call mkdir('/tmp/tty/tty')" } , 474 , 1436701355 , 35 , { 0 , "call mkdir('/tmp/tty/tty', 'p')" } , 484 , 1436701368 , 24 , { 0 , "call mkdir('/', 'p')" } , 494 , 1436701375 , 26 , { 0 , "call mkdir('/tty/tty')" } , 504 , 1436701383 , 30 , { 0 , "call mkdir('/tty/tty/tty')" } , 514 , 1436701407 , 35 , { 0 , "call mkdir('/usr/tty/tty', 'p')" } , 524 , 1436701666 , 35 , { 0 , "call mkdir('/tty/tty/tty', 'p')" } , 534 , 1436708101 , 25 , { 0 , "echo msgpackdump([1])" } , 544 , 1436708966 , 6 , { 0 , "cq" } , 554 , 1436709606 , 25 , { 0 , "echo msgpackdump([5])" } , 564 , 1436709610 , 26 , { 0 , "echo msgpackdump([10])" } , 574 , 1436709615 , 31 , { 0 , "echo msgpackdump([5, 5, 5])" } , 584 , 1436709618 , 35 , { 0 , "echo msgpackdump([5, 5, 5, 10])" } , 594 , 1436709634 , 57 , { 600 , 61"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])" 62} , 634 , 1436709651 , 67 , { 640 , 65"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])" 66} , 674 , 1436709660 , 70 , { 680 , 69"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])" 70} , 714 , 1436710095 , 29 , { 0 , "echo msgpackparse([\"\\n\"])" } , 724 , 1436710100 , 28 , { 0 , "echo msgpackparse([\"j\"])" } , 734 , 1436710109 , 31 , { 0 , "echo msgpackparse([\"\", \"\"])" } , 744 , 1436710424 , 33 , { 0 , "echo msgpackparse([\"\", \"\\n\"])" } , 754 , 1436710428 , 32 , { 0 , "echo msgpackparse([\"\", \"j\"])" } , 764 , 1436711142 , 14 , { 0 , "echo mpack" } , 774 , 1436711196 , 45 , { 0 , "let lengths = map(mpack[:], 'len(v:val)')" } , 784 , 1436711206 , 16 , { 0 , "echo lengths" } , 794 , 1436711244 , 92 , { 800 , 81( "let sum = len(lengths) - 1 | call map(copy(lengths), " 82.. "'extend(g:, {\"sum\": sum + v:val})')" ) 83} , 844 , 1436711245 , 12 , { 0 , "echo sum" } , 854 , 1436711398 , 10 , { 0 , "echo s" } , 864 , 1436711404 , 41 , { 0 , "let mpack = readfile('/tmp/foo', 'b')" } , 874 , 1436711408 , 41 , { 0 , "let shada_objects=msgpackparse(mpack)" } , 884 , 1436711415 , 22 , { 0 , "echo shada_objects" } , 894 , 1436711451 , 30 , { 0 , "e ~/.nvim/shada/main.shada" } , 904 , 1436711454 , 6 , { 0 , "qa" } , 914 , 1436711442 , 9 , { 1 , "test" , 47 } , 924 , 1436711443 , 15 , { 1 , "aontsuesan" , 47 } , 932 , 1436711443 , 38 , { hlsearch =1 , pat ="aontsuesan" , smartcase =1 } , 942 , 0 , 31 , { islast =0 , pat ="" , smartcase =1 , sub =1 } , 953 , 0 , 3 , { "" } , 9610 , 1436711451 , 40 , { file ="/home/zyx/.nvim/shada/main.shada" } , 971 , 1436711454 , 78 , { 98encoding ="utf-8" , 99max_kbyte =10 , 100pid =19269 , 101version ="NVIM 0.0.0-alpha+201507121634" 102} , 1038 , 1436711451 , 40 , { file ="/home/zyx/.nvim/shada/main.shada" } , 1048 , 1436711391 , 8 , { file ="" } , 1054 , 1436700940 , 30 , { 0 , "call mkdir('/tmp/tty/tty')" } , 1064 , 1436701355 , 35 , { 0 , "call mkdir('/tmp/tty/tty', 'p')" } , 1074 , 1436701368 , 24 , { 0 , "call mkdir('/', 'p')" } , 1084 , 1436701375 , 26 , { 0 , "call mkdir('/tty/tty')" } , 1094 , 1436701383 , 30 , { 0 , "call mkdir('/tty/tty/tty')" } , 1104 , 1436701407 , 35 , { 0 , "call mkdir('/usr/tty/tty', 'p')" } , 1114 , 1436701666 , 35 , { 0 , "call mkdir('/tty/tty/tty', 'p')" } , 1124 , 1436708101 , 25 , { 0 , "echo msgpackdump([1])" } , 1134 , 1436708966 , 6 , { 0 , "cq" } , 1144 , 1436709606 , 25 , { 0 , "echo msgpackdump([5])" } , 1154 , 1436709610 , 26 , { 0 , "echo msgpackdump([10])" } , 1164 , 1436709615 , 31 , { 0 , "echo msgpackdump([5, 5, 5])" } , 1174 , 1436709618 , 35 , { 0 , "echo msgpackdump([5, 5, 5, 10])" } , 1184 , 1436709634 , 57 , { 1190 , 120"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])" 121} , 1224 , 1436709651 , 67 , { 1230 , 124"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])" 125} , 1264 , 1436709660 , 70 , { 1270 , 128"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])" 129} , 1304 , 1436710095 , 29 , { 0 , "echo msgpackparse([\"\\n\"])" } , 1314 , 1436710100 , 28 , { 0 , "echo msgpackparse([\"j\"])" } , 1324 , 1436710109 , 31 , { 0 , "echo msgpackparse([\"\", \"\"])" } , 1334 , 1436710424 , 33 , { 0 , "echo msgpackparse([\"\", \"\\n\"])" } , 1344 , 1436710428 , 32 , { 0 , "echo msgpackparse([\"\", \"j\"])" } , 1354 , 1436711142 , 14 , { 0 , "echo mpack" } , 1364 , 1436711196 , 45 , { 0 , "let lengths = map(mpack[:], 'len(v:val)')" } , 1374 , 1436711206 , 16 , { 0 , "echo lengths" } , 1384 , 1436711244 , 92 , { 1390 , 140( "let sum = len(lengths) - 1 | call map(copy(lengths), " 141.. "'extend(g:, {\"sum\": sum + v:val})')" ) 142} , 1434 , 1436711245 , 12 , { 0 , "echo sum" } , 1444 , 1436711398 , 10 , { 0 , "echo s" } , 1454 , 1436711404 , 41 , { 0 , "let mpack = readfile('/tmp/foo', 'b')" } , 1464 , 1436711408 , 41 , { 0 , "let shada_objects=msgpackparse(mpack)" } , 1474 , 1436711415 , 22 , { 0 , "echo shada_objects" } , 1484 , 1436711451 , 30 , { 0 , "e ~/.nvim/shada/main.shada" } , 1494 , 1436711454 , 6 , { 0 , "qa" } , 1504 , 1436711442 , 9 , { 1 , "test" , 47 } , 1514 , 1436711443 , 15 , { 1 , "aontsuesan" , 47 } , 1522 , 1436711443 , 38 , { hlsearch =1 , pat ="aontsuesan" , smartcase =1 } , 1532 , 0 , 31 , { islast =0 , pat ="" , smartcase =1 , sub =1 } , 1543 , 0 , 3 , { "" } , 15510 , 1436711451 , 40 , { file ="/home/zyx/.nvim/shada/main.shada" } , 1561 , 1436711454 , 78 , { 157encoding ="utf-8" , 158max_kbyte =10 , 159pid =19269 , 160version ="NVIM 0.0.0-alpha+201507121634" 161} , 1628 , 1436711451 , 40 , { file ="/home/zyx/.nvim/shada/main.shada" } , 1638 , 1436711391 , 8 , { file ="" } , 1644 , 1436700940 , 30 , { 0 , "call mkdir('/tmp/tty/tty')" } , 1654 , 1436701355 , 35 , { 0 , "call mkdir('/tmp/tty/tty', 'p')" } , 1664 , 1436701368 , 24 , { 0 , "call mkdir('/', 'p')" } , 1674 , 1436701375 , 26 , { 0 , "call mkdir('/tty/tty')" } , 1684 , 1436701383 , 30 , { 0 , "call mkdir('/tty/tty/tty')" } , 1694 , 1436701407 , 35 , { 0 , "call mkdir('/usr/tty/tty', 'p')" } , 1704 , 1436701666 , 35 , { 0 , "call mkdir('/tty/tty/tty', 'p')" } , 1714 , 1436708101 , 25 , { 0 , "echo msgpackdump([1])" } , 1724 , 1436708966 , 6 , { 0 , "cq" } , 1734 , 1436709606 , 25 , { 0 , "echo msgpackdump([5])" } , 1744 , 1436709610 , 26 , { 0 , "echo msgpackdump([10])" } , 1754 , 1436709615 , 31 , { 0 , "echo msgpackdump([5, 5, 5])" } , 1764 , 1436709618 , 35 , { 0 , "echo msgpackdump([5, 5, 5, 10])" } , 1774 , 1436709634 , 57 , { 1780 , 179"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])" 180} , 1814 , 1436709651 , 67 , { 1820 , 183"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])" 184} , 1854 , 1436709660 , 70 , { 1860 , 187"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])" 188} , 1894 , 1436710095 , 29 , { 0 , "echo msgpackparse([\"\\n\"])" } , 1904 , 1436710100 , 28 , { 0 , "echo msgpackparse([\"j\"])" } , 1914 , 1436710109 , 31 , { 0 , "echo msgpackparse([\"\", \"\"])" } , 1924 , 1436710424 , 33 , { 0 , "echo msgpackparse([\"\", \"\\n\"])" } , 1934 , 1436710428 , 32 , { 0 , "echo msgpackparse([\"\", \"j\"])" } , 1944 , 1436711142 , 14 , { 0 , "echo mpack" } , 1954 , 1436711196 , 45 , { 0 , "let lengths = map(mpack[:], 'len(v:val)')" } , 1964 , 1436711206 , 16 , { 0 , "echo lengths" } , 1974 , 1436711244 , 92 , { 1980 , 199( "let sum = len(lengths) - 1 | call map(copy(lengths), " 200.. "'extend(g:, {\"sum\": sum + v:val})')" ) 201} , 2024 , 1436711245 , 12 , { 0 , "echo sum" } , 2034 , 1436711398 , 10 , { 0 , "echo s" } , 2044 , 1436711404 , 41 , { 0 , "let mpack = readfile('/tmp/foo', 'b')" } , 2054 , 1436711408 , 41 , { 0 , "let shada_objects=msgpackparse(mpack)" } , 2064 , 1436711415 , 22 , { 0 , "echo shada_objects" } , 2074 , 1436711451 , 30 , { 0 , "e ~/.nvim/shada/main.shada" } , 2084 , 1436711454 , 6 , { 0 , "qa" } , 2094 , 1436711442 , 9 , { 1 , "test" , 47 } , 2104 , 1436711443 , 15 , { 1 , "aontsuesan" , 47 } , 2112 , 1436711443 , 38 , { hlsearch =1 , pat ="aontsuesan" , smartcase =1 } , 2122 , 0 , 31 , { islast =0 , pat ="" , smartcase =1 , sub =1 } , 2133 , 0 , 3 , { "" } , 21410 , 1436711451 , 40 , { file ="/home/zyx/.nvim/shada/main.shada" } , 2151 , 1436711454 , 78 , { 216encoding ="utf-8" , 217max_kbyte =10 , 218pid =19269 , 219version ="NVIM 0.0.0-alpha+201507121634" 220} , 2218 , 1436711451 , 40 , { file ="/home/zyx/.nvim/shada/main.shada" } , 2228 , 1436711391 , 8 , { file ="" } , 2234 , 1436700940 , 30 , { 0 , "call mkdir('/tmp/tty/tty')" } , 2244 , 1436701355 , 35 , { 0 , "call mkdir('/tmp/tty/tty', 'p')" } , 2254 , 1436701368 , 24 , { 0 , "call mkdir('/', 'p')" } , 2264 , 1436701375 , 26 , { 0 , "call mkdir('/tty/tty')" } , 2274 , 1436701383 , 30 , { 0 , "call mkdir('/tty/tty/tty')" } , 2284 , 1436701407 , 35 , { 0 , "call mkdir('/usr/tty/tty', 'p')" } , 2294 , 1436701666 , 35 , { 0 , "call mkdir('/tty/tty/tty', 'p')" } , 2304 , 1436708101 , 25 , { 0 , "echo msgpackdump([1])" } , 2314 , 1436708966 , 6 , { 0 , "cq" } , 2324 , 1436709606 , 25 , { 0 , "echo msgpackdump([5])" } , 2334 , 1436709610 , 26 , { 0 , "echo msgpackdump([10])" } , 2344 , 1436709615 , 31 , { 0 , "echo msgpackdump([5, 5, 5])" } , 2354 , 1436709618 , 35 , { 0 , "echo msgpackdump([5, 5, 5, 10])" } , 2364 , 1436709634 , 57 , { 2370 , 238"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])" 239} , 2404 , 1436709651 , 67 , { 2410 , 242"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])" 243} , 2444 , 1436709660 , 70 , { 2450 , 246"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])" 247} , 2484 , 1436710095 , 29 , { 0 , "echo msgpackparse([\"\\n\"])" } , 2494 , 1436710100 , 28 , { 0 , "echo msgpackparse([\"j\"])" } , 2504 , 1436710109 , 31 , { 0 , "echo msgpackparse([\"\", \"\"])" } , 2514 , 1436710424 , 33 , { 0 , "echo msgpackparse([\"\", \"\\n\"])" } , 2524 , 1436710428 , 32 , { 0 , "echo msgpackparse([\"\", \"j\"])" } , 2534 , 1436711142 , 14 , { 0 , "echo mpack" } , 2544 , 1436711196 , 45 , { 0 , "let lengths = map(mpack[:], 'len(v:val)')" } , 2554 , 1436711206 , 16 , { 0 , "echo lengths" } , 2564 , 1436711244 , 92 , { 2570 , 258( "let sum = len(lengths) - 1 | call map(copy(lengths), " 259.. "'extend(g:, {\"sum\": sum + v:val})')" ) 260} , 2614 , 1436711245 , 12 , { 0 , "echo sum" } , 2624 , 1436711398 , 10 , { 0 , "echo s" } , 2634 , 1436711404 , 41 , { 0 , "let mpack = readfile('/tmp/foo', 'b')" } , 2644 , 1436711408 , 41 , { 0 , "let shada_objects=msgpackparse(mpack)" } , 2654 , 1436711415 , 22 , { 0 , "echo shada_objects" } , 2664 , 1436711451 , 30 , { 0 , "e ~/.nvim/shada/main.shada" } , 2674 , 1436711454 , 6 , { 0 , "qa" } , 2684 , 1436711442 , 9 , { 1 , "test" , 47 } , 2694 , 1436711443 , 15 , { 1 , "aontsuesan" , 47 } , 2702 , 1436711443 , 38 , { hlsearch =1 , pat ="aontsuesan" , smartcase =1 } , 2712 , 0 , 31 , { islast =0 , pat ="" , smartcase =1 , sub =1 } , 2723 , 0 , 3 , { "" } , 27310 , 1436711451 , 40 , { file ="/home/zyx/.nvim/shada/main.shada" } , 2741 , 1436711454 , 78 , { 275encoding ="utf-8" , 276max_kbyte =10 , 277pid =19269 , 278version ="NVIM 0.0.0-alpha+201507121634" 279} , 2808 , 1436711451 , 40 , { file ="/home/zyx/.nvim/shada/main.shada" } , 2818 , 1436711391 , 8 , { file ="" } , 2824 , 1436700940 , 30 , { 0 , "call mkdir('/tmp/tty/tty')" } , 2834 , 1436701355 , 35 , { 0 , "call mkdir('/tmp/tty/tty', 'p')" } , 2844 , 1436701368 , 24 , { 0 , "call mkdir('/', 'p')" } , 2854 , 1436701375 , 26 , { 0 , "call mkdir('/tty/tty')" } , 2864 , 1436701383 , 30 , { 0 , "call mkdir('/tty/tty/tty')" } , 2874 , 1436701407 , 35 , { 0 , "call mkdir('/usr/tty/tty', 'p')" } , 2884 , 1436701666 , 35 , { 0 , "call mkdir('/tty/tty/tty', 'p')" } , 2894 , 1436708101 , 25 , { 0 , "echo msgpackdump([1])" } , 2904 , 1436708966 , 6 , { 0 , "cq" } , 2914 , 1436709606 , 25 , { 0 , "echo msgpackdump([5])" } , 2924 , 1436709610 , 26 , { 0 , "echo msgpackdump([10])" } , 2934 , 1436709615 , 31 , { 0 , "echo msgpackdump([5, 5, 5])" } , 2944 , 1436709618 , 35 , { 0 , "echo msgpackdump([5, 5, 5, 10])" } , 2954 , 1436709634 , 57 , { 2960 , 297"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])" 298} , 2994 , 1436709651 , 67 , { 3000 , 301"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])" 302} , 3034 , 1436709660 , 70 , { 3040 , 305"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])" 306} , 3074 , 1436710095 , 29 , { 0 , "echo msgpackparse([\"\\n\"])" } , 3084 , 1436710100 , 28 , { 0 , "echo msgpackparse([\"j\"])" } , 3094 , 1436710109 , 31 , { 0 , "echo msgpackparse([\"\", \"\"])" } , 3104 , 1436710424 , 33 , { 0 , "echo msgpackparse([\"\", \"\\n\"])" } , 3114 , 1436710428 , 32 , { 0 , "echo msgpackparse([\"\", \"j\"])" } , 3124 , 1436711142 , 14 , { 0 , "echo mpack" } , 3134 , 1436711196 , 45 , { 0 , "let lengths = map(mpack[:], 'len(v:val)')" } , 3144 , 1436711206 , 16 , { 0 , "echo lengths" } , 3154 , 1436711244 , 92 , { 3160 , 317( "let sum = len(lengths) - 1 | call map(copy(lengths), " 318.. "'extend(g:, {\"sum\": sum + v:val})')" ) 319} , 3204 , 1436711245 , 12 , { 0 , "echo sum" } , 3214 , 1436711398 , 10 , { 0 , "echo s" } , 3224 , 1436711404 , 41 , { 0 , "let mpack = readfile('/tmp/foo', 'b')" } , 3234 , 1436711408 , 41 , { 0 , "let shada_objects=msgpackparse(mpack)" } , 3244 , 1436711415 , 22 , { 0 , "echo shada_objects" } , 3254 , 1436711451 , 30 , { 0 , "e ~/.nvim/shada/main.shada" } , 3264 , 1436711454 , 6 , { 0 , "qa" } , 3274 , 1436711442 , 9 , { 1 , "test" , 47 } , 3284 , 1436711443 , 15 , { 1 , "aontsuesan" , 47 } , 3292 , 1436711443 , 38 , { hlsearch =1 , pat ="aontsuesan" , smartcase =1 } , 3302 , 0 , 31 , { islast =0 , pat ="" , smartcase =1 , sub =1 } , 3313 , 0 , 3 , { "" } , 33210 , 1436711451 , 40 , { file ="/home/zyx/.nvim/shada/main.shada" } 333} 334obj_test ( 'are able to dump and restore rather big object' , big_obj ) 335 336obj_test ( 'are able to dump and restore floating-point value' , { 0.125 } ) 337 338it ( 'can restore and dump UINT64_MAX' , function () 339command ( 'let dumped = ["\\xCF" . repeat("\\xFF", 8)]' ) 340command ( 'let parsed = msgpackparse(dumped)' ) 341command ( 'let dumped2 = msgpackdump(parsed)' ) 342eq ( 1 , eval ( 'type(parsed[0]) == type(0) ' .. '|| parsed[0]._TYPE is v:msgpack_types.integer' )) 343if eval ( 'type(parsed[0]) == type(0)' ) == 1 then 344command ( 'call assert_equal(0xFFFFFFFFFFFFFFFF, parsed[0])' ) 345eq ( {} , eval ( 'v:errors' )) 346else 347eq ( { _TYPE ={} , _VAL ={ 1 , 3 , 0x7FFFFFFF , 0x7FFFFFFF } } , eval ( 'parsed[0]' )) 348end 349eq ( 1 , eval ( 'dumped ==# dumped2' )) 350end ) 351 352it ( 'can restore and dump INT64_MIN' , function () 353command ( 'let dumped = ["\\xD3\\x80" . repeat("\\n", 7)]' ) 354command ( 'let parsed = msgpackparse(dumped)' ) 355command ( 'let dumped2 = msgpackdump(parsed)' ) 356eq ( 1 , eval ( 'type(parsed[0]) == type(0) ' .. '|| parsed[0]._TYPE is v:msgpack_types.integer' )) 357if eval ( 'type(parsed[0]) == type(0)' ) == 1 then 358command ( 'call assert_equal(-0x7fffffffffffffff - 1, parsed[0])' ) 359eq ( {} , eval ( 'v:errors' )) 360else 361eq ( { _TYPE ={} , _VAL ={ - 1 , 2 , 0 , 0 } } , eval ( 'parsed[0]' )) 362end 363eq ( 1 , eval ( 'dumped ==# dumped2' )) 364end ) 365 366it ( 'can restore and dump BIN string with zero byte' , function () 367command ( 'let dumped = ["\\xC4\\x01\\n"]' ) 368command ( 'let parsed = msgpackparse(dumped)' ) 369command ( 'let dumped2 = msgpackdump(parsed)' ) 370eq ( { '\000' } , eval ( 'parsed' )) 371eq ( 1 , eval ( 'dumped ==# dumped2' )) 372end ) 373 374it ( 'can restore and dump STR string contents with zero byte' , function () 375command ( 'let dumped = ["\\xA1\\n"]' ) 376command ( 'let parsed = msgpackparse(dumped)' ) 377command ( 'let dumped2 = msgpackdump(parsed)' ) 378eq ( { '\000' } , eval ( 'parsed' )) 379eq ( eval ( 'v:t_blob' ), eval ( 'type(parsed[0])' )) 380-- type is not preserved: prefer BIN for binary contents 381eq ( 0 , eval ( 'dumped ==# dumped2' )) 382end ) 383 384it ( 'can restore and dump BIN string with NL' , function () 385command ( 'let dumped = ["\\xC4\\x01", ""]' ) 386command ( 'let parsed = msgpackparse(dumped)' ) 387command ( 'let dumped2 = msgpackdump(parsed)' ) 388eq ( { '\n' } , eval ( 'parsed' )) 389eq ( 1 , eval ( 'dumped ==# dumped2' )) 390end ) 391 392it ( 'dump and restore special mapping with floating-point value' , function () 393command ( 'let todump = {"_TYPE": v:msgpack_types.float, "_VAL": 0.125}' ) 394eq ( { 0.125 } , eval ( 'msgpackparse(msgpackdump([todump]))' )) 395end ) 396end ) 397 398local blobstr =function ( list ) 399local l ={} 400for i , v in ipairs ( list ) do 401l [ i ] =v : gsub ( '\n' , '\000' ) 402end 403return table . concat ( l , '\n' ) 404end 405 406-- Test msgpackparse() with a readfile()-style list and a blob argument 407local parse_eq =function ( expect , list_arg ) 408local blob_expr ='0z' 409.. blobstr ( list_arg ): gsub ( '(.)' , function ( c ) 410return ( '%.2x' ): format ( c : byte ()) 411end ) 412eq ( expect , fn . msgpackparse ( list_arg )) 413command ( 'let g:parsed = msgpackparse(' .. blob_expr .. ')' ) 414eq ( expect , eval ( 'g:parsed' )) 415end 416 417describe ( 'msgpackparse() function' , function () 418setup ( clear ) 419 420it ( 'restores nil as v:null' , function () 421parse_eq ( eval ( '[v:null]' ), { '\192' } ) 422end ) 423 424it ( 'restores boolean false as v:false' , function () 425parse_eq ( { false } , { '\194' } ) 426end ) 427 428it ( 'restores boolean true as v:true' , function () 429parse_eq ( { true } , { '\195' } ) 430end ) 431 432it ( 'restores FIXSTR as string' , function () 433parse_eq ( { 'ab' } , { '\162ab' } ) 434end ) 435 436it ( 'restores BIN 8 as string' , function () 437parse_eq ( { 'ab' } , { '\196\002ab' } ) 438end ) 439 440it ( 'restores FIXEXT1 as special dict' , function () 441parse_eq ( { { _TYPE ={} , _VAL ={ 0x10 , { '' , '' } } } } , { '\212\016' , '' } ) 442eq ( 1 , eval ( 'g:parsed[0]._TYPE is v:msgpack_types.ext' )) 443end ) 444 445it ( 'restores MAP with BIN key as ordinary dict' , function () 446parse_eq ( { { a ='' } } , { '\129\196\001a\196\n' } ) 447end ) 448 449it ( 'restores MAP with duplicate STR keys as special dict' , function () 450command ( 'let dumped = ["\\x82\\xA1a\\xC4\\n\\xA1a\\xC4\\n"]' ) 451-- FIXME Internal error bug, can't use parse_eq() here 452command ( 'silent! let parsed = msgpackparse(dumped)' ) 453eq ( { 454{ 455_TYPE ={} , 456_VAL ={ 457{ 'a' , '' } , 458{ 'a' , '' } , 459} , 460} , 461} , eval ( 'parsed' )) 462eq ( 1 , eval ( 'g:parsed[0]._TYPE is v:msgpack_types.map' )) 463eq ( eval ( 'v:t_string' ), eval ( 'type(g:parsed[0]._VAL[0][0])' )) 464eq ( eval ( 'v:t_string' ), eval ( 'type(g:parsed[0]._VAL[1][0])' )) 465end ) 466 467it ( 'restores MAP with MAP key as special dict' , function () 468parse_eq ( { { _TYPE ={} , _VAL ={ { {} , '' } } } } , { '\129\128\196\n' } ) 469eq ( 1 , eval ( 'g:parsed[0]._TYPE is v:msgpack_types.map' )) 470end ) 471 472it ( 'msgpackparse(systemlist(...)) does not segfault. #3135' , function () 473local cmd ="sort(keys(msgpackparse(systemlist('" .. n . nvim_prog .. " --api-info'))[0]))" 474eval ( cmd ) 475eval ( cmd ) -- do it again (try to force segfault) 476local api_info =eval ( cmd ) -- do it again 477n . assert_alive () 478eq ( { 'error_types' , 'functions' , 'types' , 'ui_events' , 'ui_options' , 'version' } , api_info ) 479end ) 480 481it ( 'fails when called with no arguments' , function () 482eq ( 483'Vim(call):E119: Not enough arguments for function: msgpackparse' , 484pcall_err ( command , 'call msgpackparse()' ) 485) 486end ) 487 488it ( 'fails when called with two arguments' , function () 489eq ( 490'Vim(call):E118: Too many arguments for function: msgpackparse' , 491pcall_err ( command , 'call msgpackparse(["", ""], 1)' ) 492) 493end ) 494 495it ( 'fails to parse a string' , function () 496eq ( 497'Vim(call):E899: Argument of msgpackparse() must be a List or Blob' , 498pcall_err ( command , 'call msgpackparse("abcdefghijklmnopqrstuvwxyz")' ) 499) 500end ) 501 502it ( 'fails to parse a number' , function () 503eq ( 504'Vim(call):E899: Argument of msgpackparse() must be a List or Blob' , 505pcall_err ( command , 'call msgpackparse(127)' ) 506) 507end ) 508 509it ( 'fails to parse a dict' , function () 510eq ( 511'Vim(call):E899: Argument of msgpackparse() must be a List or Blob' , 512pcall_err ( command , 'call msgpackparse({})' ) 513) 514end ) 515 516it ( 'fails to parse a funcref' , function () 517eq ( 518'Vim(call):E899: Argument of msgpackparse() must be a List or Blob' , 519pcall_err ( command , 'call msgpackparse(function("tr"))' ) 520) 521end ) 522 523it ( 'fails to parse a partial' , function () 524command ( 'function! T() dict\nendfunction' ) 525eq ( 526'Vim(call):E899: Argument of msgpackparse() must be a List or Blob' , 527pcall_err ( command , 'call msgpackparse(function("T", [1, 2], {}))' ) 528) 529end ) 530 531it ( 'fails to parse a float' , function () 532eq ( 533'Vim(call):E899: Argument of msgpackparse() must be a List or Blob' , 534pcall_err ( command , 'call msgpackparse(0.0)' ) 535) 536end ) 537 538it ( 'fails on incomplete msgpack string' , function () 539local expected ='Vim(call):E475: Invalid argument: Incomplete msgpack string' 540eq ( expected , pcall_err ( command , [[call msgpackparse(["\xc4"])]] )) 541eq ( expected , pcall_err ( command , [[call msgpackparse(["\xca", "\x02\x03"])]] )) 542eq ( expected , pcall_err ( command , 'call msgpackparse(0zc4)' )) 543eq ( expected , pcall_err ( command , 'call msgpackparse(0zca0a0203)' )) 544end ) 545 546it ( 'fails when unable to parse msgpack string' , function () 547local expected ='Vim(call):E475: Invalid argument: Failed to parse msgpack string' 548eq ( expected , pcall_err ( command , [[call msgpackparse(["\xc1"])]] )) 549eq ( expected , pcall_err ( command , 'call msgpackparse(0zc1)' )) 550end ) 551end ) 552 553describe ( 'msgpackdump() function' , function () 554setup ( clear ) 555 556local dump_eq =function ( exp_list , arg_expr ) 557eq ( exp_list , eval ( 'msgpackdump(' .. arg_expr .. ')' )) 558eq ( blobstr ( exp_list ), eval ( 'msgpackdump(' .. arg_expr .. ', "B")' )) 559end 560 561it ( 'dumps string as BIN 8' , function () 562dump_eq ( { '\196\004Test' } , '["Test"]' ) 563end ) 564 565it ( 'dumps blob as BIN 8' , function () 566dump_eq ( { '\196\005Bl\nb!' } , '[0z426c006221]' ) 567end ) 568 569it ( 'can dump generic mapping with generic mapping keys and values' , function () 570command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}' ) 571command ( 'let todumpv1 = {"_TYPE": v:msgpack_types.map, "_VAL": []}' ) 572command ( 'let todumpv2 = {"_TYPE": v:msgpack_types.map, "_VAL": []}' ) 573command ( 'call add(todump._VAL, [todumpv1, todumpv2])' ) 574dump_eq ( { '\129\128\128' } , '[todump]' ) 575end ) 576 577it ( 'can dump v:true' , function () 578dump_eq ( { '\195' } , '[v:true]' ) 579end ) 580 581it ( 'can dump v:false' , function () 582dump_eq ( { '\194' } , '[v:false]' ) 583end ) 584 585it ( 'can dump v:null' , function () 586dump_eq ( { '\192' } , '[v:null]' ) 587end ) 588 589it ( 'can dump special bool mapping (true)' , function () 590command ( 'let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 1}' ) 591dump_eq ( { '\195' } , '[todump]' ) 592end ) 593 594it ( 'can dump special bool mapping (false)' , function () 595command ( 'let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 0}' ) 596dump_eq ( { '\194' } , '[todump]' ) 597end ) 598 599it ( 'can dump special nil mapping' , function () 600command ( 'let todump = {"_TYPE": v:msgpack_types.nil, "_VAL": 0}' ) 601dump_eq ( { '\192' } , '[todump]' ) 602end ) 603 604it ( 'can dump special ext mapping' , function () 605command ( 'let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}' ) 606dump_eq ( { '\212\005' , '' } , '[todump]' ) 607end ) 608 609it ( 'can dump special array mapping' , function () 610command ( 'let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}' ) 611dump_eq ( { '\146\005\145\196\n' } , '[todump]' ) 612end ) 613 614it ( 'can dump special UINT64_MAX mapping' , function () 615command ( 'let todump = {"_TYPE": v:msgpack_types.integer}' ) 616command ( 'let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]' ) 617dump_eq ( { '\207\255\255\255\255\255\255\255\255' } , '[todump]' ) 618end ) 619 620it ( 'can dump special INT64_MIN mapping' , function () 621command ( 'let todump = {"_TYPE": v:msgpack_types.integer}' ) 622command ( 'let todump._VAL = [-1, 2, 0, 0]' ) 623dump_eq ( { '\211\128\n\n\n\n\n\n\n' } , '[todump]' ) 624end ) 625 626it ( 'fails to dump a function reference' , function () 627command ( 'let Todump = function("tr")' ) 628eq ( 629'Vim(call):E5004: Error while dumping msgpackdump() argument, index 0, itself: attempt to dump function reference' , 630pcall_err ( command , 'call msgpackdump([Todump])' ) 631) 632end ) 633 634it ( 'fails to dump a partial' , function () 635command ( 'function! T() dict\nendfunction' ) 636command ( 'let Todump = function("T", [1, 2], {})' ) 637eq ( 638'Vim(call):E5004: Error while dumping msgpackdump() argument, index 0, itself: attempt to dump function reference' , 639pcall_err ( command , 'call msgpackdump([Todump])' ) 640) 641end ) 642 643it ( 'fails to dump a function reference in a list' , function () 644command ( 'let todump = [function("tr")]' ) 645eq ( 646'Vim(call):E5004: Error while dumping msgpackdump() argument, index 0, index 0: attempt to dump function reference' , 647pcall_err ( command , 'call msgpackdump([todump])' ) 648) 649end ) 650 651it ( 'fails to dump a recursive list' , function () 652command ( 'let todump = [[[]]]' ) 653command ( 'call add(todump[0][0], todump)' ) 654eq ( 655'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in index 0, index 0, index 0' , 656pcall_err ( command , 'call msgpackdump([todump])' ) 657) 658end ) 659 660it ( 'fails to dump a recursive dict' , function () 661command ( 'let todump = {"d": {"d": {}}}' ) 662command ( 'call extend(todump.d.d, {"d": todump})' ) 663eq ( 664"Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in key 'd', key 'd', key 'd'" , 665pcall_err ( command , 'call msgpackdump([todump])' ) 666) 667end ) 668 669it ( 'can dump dict with two same dicts inside' , function () 670command ( 'let inter = {}' ) 671command ( 'let todump = {"a": inter, "b": inter}' ) 672dump_eq ( { '\130\161a\128\161b\128' } , '[todump]' ) 673end ) 674 675it ( 'can dump list with two same lists inside' , function () 676command ( 'let inter = []' ) 677command ( 'let todump = [inter, inter]' ) 678dump_eq ( { '\146\144\144' } , '[todump]' ) 679end ) 680 681it ( 'fails to dump a recursive list in a special dict' , function () 682command ( 'let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}' ) 683command ( 'call add(todump._VAL, todump)' ) 684eq ( 685'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in index 0' , 686pcall_err ( command , 'call msgpackdump([todump])' ) 687) 688end ) 689 690it ( 'fails to dump a recursive (key) map in a special dict' , function () 691command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}' ) 692command ( 'call add(todump._VAL, [todump, 0])' ) 693eq ( 694'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in index 0' , 695pcall_err ( command , 'call msgpackdump([todump])' ) 696) 697end ) 698 699it ( 'fails to dump a recursive (val) map in a special dict' , function () 700command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}' ) 701command ( 'call add(todump._VAL, [0, todump])' ) 702eq ( 703'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in key 0 at index 0 from special map' , 704pcall_err ( command , 'call msgpackdump([todump])' ) 705) 706end ) 707 708it ( 'fails to dump a recursive (key) map in a special dict, _VAL reference' , function () 709command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[[], []]]}' ) 710command ( 'call add(todump._VAL[0][0], todump._VAL)' ) 711eq ( 712'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in key [[[[...@0], []]]] at index 0 from special map, index 0' , 713pcall_err ( command , 'call msgpackdump([todump])' ) 714) 715end ) 716 717it ( 'fails to dump a recursive (val) map in a special dict, _VAL reference' , function () 718command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[[], []]]}' ) 719command ( 'call add(todump._VAL[0][1], todump._VAL)' ) 720eq ( 721'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in key [] at index 0 from special map, index 0' , 722pcall_err ( command , 'call msgpackdump([todump])' ) 723) 724end ) 725 726it ( 'fails to dump a recursive (val) special list in a special dict' , function () 727command ( 'let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}' ) 728command ( 'call add(todump._VAL, [0, todump._VAL])' ) 729eq ( 730'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in index 0, index 1' , 731pcall_err ( command , 'call msgpackdump([todump])' ) 732) 733end ) 734 735it ( 'fails when called with no arguments' , function () 736eq ( 737'Vim(call):E119: Not enough arguments for function: msgpackdump' , 738pcall_err ( command , 'call msgpackdump()' ) 739) 740end ) 741 742it ( 'fails when called with three arguments' , function () 743eq ( 744'Vim(call):E118: Too many arguments for function: msgpackdump' , 745pcall_err ( command , 'call msgpackdump(["", ""], 1, 2)' ) 746) 747end ) 748 749it ( 'fails to dump a string' , function () 750eq ( 751'Vim(call):E686: Argument of msgpackdump() must be a List' , 752pcall_err ( command , 'call msgpackdump("abcdefghijklmnopqrstuvwxyz")' ) 753) 754end ) 755 756it ( 'fails to dump a number' , function () 757eq ( 758'Vim(call):E686: Argument of msgpackdump() must be a List' , 759pcall_err ( command , 'call msgpackdump(127)' ) 760) 761end ) 762 763it ( 'fails to dump a dict' , function () 764eq ( 765'Vim(call):E686: Argument of msgpackdump() must be a List' , 766pcall_err ( command , 'call msgpackdump({})' ) 767) 768end ) 769 770it ( 'fails to dump a funcref' , function () 771eq ( 772'Vim(call):E686: Argument of msgpackdump() must be a List' , 773pcall_err ( command , 'call msgpackdump(function("tr"))' ) 774) 775end ) 776 777it ( 'fails to dump a partial' , function () 778command ( 'function! T() dict\nendfunction' ) 779eq ( 780'Vim(call):E686: Argument of msgpackdump() must be a List' , 781pcall_err ( command , 'call msgpackdump(function("T", [1, 2], {}))' ) 782) 783end ) 784 785it ( 'fails to dump a float' , function () 786eq ( 787'Vim(call):E686: Argument of msgpackdump() must be a List' , 788pcall_err ( command , 'call msgpackdump(0.0)' ) 789) 790end ) 791 792it ( 'fails to dump special value' , function () 793for _ , val in ipairs ( { 'v:true' , 'v:false' , 'v:null' } ) do 794eq ( 795'Vim(call):E686: Argument of msgpackdump() must be a List' , 796pcall_err ( command , 'call msgpackdump(' .. val .. ')' ) 797) 798end 799end ) 800 801it ( 'can dump NULL string' , function () 802dump_eq ( { '\196\n' } , '[$XXX_UNEXISTENT_VAR_XXX]' ) 803dump_eq ( { '\196\n' } , '[v:_null_blob]' ) 804dump_eq ( { '\160' } , '[{"_TYPE": v:msgpack_types.string, "_VAL": [$XXX_UNEXISTENT_VAR_XXX]}]' ) 805end ) 806 807it ( 'can dump NULL blob' , function () 808eq ( { '\196\n' } , eval ( 'msgpackdump([v:_null_blob])' )) 809end ) 810 811it ( 'can dump NULL list' , function () 812eq ( { '\144' } , eval ( 'msgpackdump([v:_null_list])' )) 813end ) 814 815it ( 'can dump NULL dict' , function () 816eq ( { '\128' } , eval ( 'msgpackdump([v:_null_dict])' )) 817end ) 818end )