Merge pull request #3034 from ZyX-I/msgpack-eval

msgpack viml functions for dump/restore
This commit is contained in:
Justin M. Keyes 2015-08-03 00:24:10 -04:00
commit 9f350e615c
7 changed files with 1646 additions and 14 deletions

View File

@ -1517,6 +1517,12 @@ v:mouse_col Column number for a mouse click obtained with |getchar()|.
This is the screen column number, like with |virtcol()|. The
value is zero when there was no mouse button click.
*v:msgpack_types* *msgpack_types-variable*
v:msgpack_types Dictionary containing msgpack types used by |msgpackparse()|
and |msgpackdump()|. All types inside dictionary are fixed
(not editable) empty lists. To check whether some list is one
of msgpack types, use |is| operator.
*v:oldfiles* *oldfiles-variable*
v:oldfiles List of file names that is loaded from the |viminfo| file on
startup. These are the files that Vim remembers marks for.
@ -1904,6 +1910,8 @@ min( {list}) Number minimum value of items in {list}
mkdir( {name} [, {path} [, {prot}]])
Number create directory {name}
mode( [expr]) String current editing mode
msgpackdump( {list}) List dump a list of objects to msgpack
msgpackparse( {list}) List parse msgpack to a list of objects
nextnonblank( {lnum}) Number line nr of non-blank line >= {lnum}
nr2char( {expr}[, {utf8}]) String single char with ASCII/UTF8 value {expr}
or( {expr}, {expr}) Number bitwise OR
@ -4625,6 +4633,91 @@ mode([expr]) Return a string that indicates the current mode.
"c" or "n".
Also see |visualmode()|.
msgpackdump({list}) {Nvim} *msgpackdump()*
Convert a list of VimL objects to msgpack. Returned value is
|readfile()|-style list. Example: >
call writefile(msgpackdump([{}]), 'fname.mpack', 'b')
< This will write the single 0x80 byte to `fname.mpack` file
(dictionary with zero items is represented by 0x80 byte in
messagepack).
Limitations:
1. |Funcref|s cannot be dumped.
2. Containers that reference themselves cannot be dumped.
3. Dictionary keys are always dumped as STR strings.
4. Other strings are always dumped as BIN strings.
5. Points 3. and 4. do not apply to |msgpack-special-dict|s.
msgpackparse({list}) {Nvim} *msgpackparse()*
Convert a |readfile()|-style list to a list of VimL objects.
Example: >
let fname = expand('~/.nvim/shada/main.shada')
let mpack = readfile(fname, 'b')
let shada_objects = msgpackparse(mpack)
< This will read |shada-file| to `shada_objects` list.
Limitations:
1. Mapping ordering is not preserved unless messagepack
mapping is dumped using generic mapping
(|msgpack-special-map|).
2. Since the parser aims to preserve all data untouched
(except for 1.) some strings are parsed to
|msgpack-special-dict| format which is not convenient to
use.
*msgpack-special-dict*
Some messagepack strings may be parsed to special
dictionaries. Special dictionaries are dictionaries which
1. Contain exactly two keys: `_TYPE` and `_VALUE`.
2. `_TYPE` key is one of the types found in |v:msgpack_types|
variable.
3. Value for `_VALUE` has the following format (Key column
contains name of the key from |v:msgpack_types|):
Key Value ~
nil Zero, ignored when dumping.
boolean One or zero. When dumping it is only checked that
value is a |Number|.
integer |List| with four numbers: sign (-1 or 1), highest two
bits, number with bits from 62nd to 31st, lowest 31
bits. I.e. to get actual number one will need to use
code like >
_VALUE[0] * ((_VALUE[1] << 62)
& (_VALUE[2] << 31)
& _VALUE[3])
< Special dictionary with this type will appear in
|msgpackparse()| output under one of the following
circumstances:
1. |Number| is 32-bit and value is either above
INT32_MAX or below INT32_MIN.
2. |Number| is 64-bit and value is above INT64_MAX. It
cannot possibly be below INT64_MIN because msgpack
C parser does not support such values.
float |Float|. This value cannot possibly appear in
|msgpackparse()| output.
string |readfile()|-style list of strings. This value will
appear in |msgpackparse()| output if string contains
zero byte or if string is a mapping key and mapping is
being represented as special dictionary for other
reasons.
binary |readfile()|-style list of strings. This value will
appear in |msgpackparse()| output if binary string
contains zero byte.
array |List|. This value cannot appear in |msgpackparse()|
output.
*msgpack-special-map*
map |List| of |List|s with two items (key and value) each.
This value will appear in |msgpackparse()| output if
parsed mapping contains one of the following keys:
1. Any key that is not a string (including keys which
are binary strings).
2. String with NUL byte inside.
3. Duplicate key.
4. Empty key.
ext |List| with two values: first is a signed integer
representing extension type. Second is
|readfile()|-style list of strings.
nextnonblank({lnum}) *nextnonblank()*
Return the line number of the first line at or below {lnum}
that is not blank. Example: >

File diff suppressed because it is too large Load Diff

View File

@ -65,6 +65,7 @@ enum {
VV_PROGPATH,
VV_COMMAND_OUTPUT,
VV_COMPLETED_ITEM,
VV_MSGPACK_TYPES,
VV_LEN, /* number of v: vars */
};

View File

@ -1,11 +1,16 @@
#ifndef NVIM_EVAL_DEFS_H
#define NVIM_EVAL_DEFS_H
#include <limits.h>
#include "nvim/hashtab.h"
typedef int varnumber_T;
typedef double float_T;
#define VARNUMBER_MAX INT_MAX
#define VARNUMBER_MIN INT_MIN
typedef struct listvar_S list_T;
typedef struct dictvar_S dict_T;

View File

@ -6125,7 +6125,7 @@ char_u * sign_typenr2name(int typenr)
/*
* Undefine/free all signs.
*/
void free_signs()
void free_signs(void)
{
while (first_sign != NULL)
sign_undefine(first_sign, NULL);

View File

@ -2624,7 +2624,7 @@ char_u *get_scriptname(scid_T id)
}
# if defined(EXITFREE)
void free_scriptnames()
void free_scriptnames(void)
{
# define FREE_SCRIPTNAME(item) xfree((item)->sn_name)
GA_DEEP_CLEAR(&script_items, scriptitem_T, FREE_SCRIPTNAME);

View File

@ -0,0 +1,606 @@
local helpers = require('test.functional.helpers')
local clear, feed, execute = helpers.clear, helpers.feed, helpers.execute
local eval, eq, neq = helpers.eval, helpers.eq, helpers.neq
local execute, source = helpers.execute, helpers.source
local nvim = helpers.nvim
describe('msgpack*() functions', function()
before_each(function()
clear()
end)
local obj_test = function(msg, obj)
it(msg, function()
nvim('set_var', 'obj', obj)
eq(obj, eval('msgpackparse(msgpackdump(g:obj))'))
end)
end
-- Regression test: msgpack_list_write was failing to write buffer with zero
-- length.
obj_test('are able to dump and restore {"file": ""}', {{file=''}})
-- Regression test: msgpack_list_write was failing to write buffer with NL at
-- the end.
obj_test('are able to dump and restore {0, "echo mpack"}', {{0, 'echo mpack'}})
obj_test('are able to dump and restore "Test\\n"', {'Test\n'})
-- Regression test: msgpack_list_write was failing to write buffer with NL
-- inside.
obj_test('are able to dump and restore "Test\\nTest 2"', {'Test\nTest 2'})
-- Test that big objects (requirement: dump to something that is bigger then
-- IOSIZE) are also fine. This particular object is obtained by concatenating
-- 5 identical shada files.
local big_obj = {
1, 1436711454, 78, {
encoding="utf-8",
max_kbyte=10,
pid=19269,
version="NVIM 0.0.0-alpha+201507121634"
},
8, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
8, 1436711391, 8, { file="" },
4, 1436700940, 30, { 0, "call mkdir('/tmp/tty/tty')" },
4, 1436701355, 35, { 0, "call mkdir('/tmp/tty/tty', 'p')" },
4, 1436701368, 24, { 0, "call mkdir('/', 'p')" },
4, 1436701375, 26, { 0, "call mkdir('/tty/tty')" },
4, 1436701383, 30, { 0, "call mkdir('/tty/tty/tty')" },
4, 1436701407, 35, { 0, "call mkdir('/usr/tty/tty', 'p')" },
4, 1436701666, 35, { 0, "call mkdir('/tty/tty/tty', 'p')" },
4, 1436708101, 25, { 0, "echo msgpackdump([1])" },
4, 1436708966, 6, { 0, "cq" },
4, 1436709606, 25, { 0, "echo msgpackdump([5])" },
4, 1436709610, 26, { 0, "echo msgpackdump([10])" },
4, 1436709615, 31, { 0, "echo msgpackdump([5, 5, 5])" },
4, 1436709618, 35, { 0, "echo msgpackdump([5, 5, 5, 10])" },
4, 1436709634, 57, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])"
},
4, 1436709651, 67, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])"
},
4, 1436709660, 70, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])"
},
4, 1436710095, 29, { 0, "echo msgpackparse([\"\\n\"])" },
4, 1436710100, 28, { 0, "echo msgpackparse([\"j\"])" },
4, 1436710109, 31, { 0, "echo msgpackparse([\"\", \"\"])" },
4, 1436710424, 33, { 0, "echo msgpackparse([\"\", \"\\n\"])" },
4, 1436710428, 32, { 0, "echo msgpackparse([\"\", \"j\"])" },
4, 1436711142, 14, { 0, "echo mpack" },
4, 1436711196, 45, { 0, "let lengths = map(mpack[:], 'len(v:val)')" },
4, 1436711206, 16, { 0, "echo lengths" },
4, 1436711244, 92, {
0,
("let sum = len(lengths) - 1 | call map(copy(lengths), "
.. "'extend(g:, {\"sum\": sum + v:val})')")
},
4, 1436711245, 12, { 0, "echo sum" },
4, 1436711398, 10, { 0, "echo s" },
4, 1436711404, 41, { 0, "let mpack = readfile('/tmp/foo', 'b')" },
4, 1436711408, 41, { 0, "let shada_objects=msgpackparse(mpack)" },
4, 1436711415, 22, { 0, "echo shada_objects" },
4, 1436711451, 30, { 0, "e ~/.nvim/shada/main.shada" },
4, 1436711454, 6, { 0, "qa" },
4, 1436711442, 9, { 1, "test", 47 },
4, 1436711443, 15, { 1, "aontsuesan", 47 },
2, 1436711443, 38, { hlsearch=1, pat="aontsuesan", smartcase=1 },
2, 0, 31, { islast=0, pat="", smartcase=1, sub=1 },
3, 0, 3, { "" },
10, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
1, 1436711454, 78, {
encoding="utf-8",
max_kbyte=10,
pid=19269,
version="NVIM 0.0.0-alpha+201507121634"
},
8, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
8, 1436711391, 8, { file="" },
4, 1436700940, 30, { 0, "call mkdir('/tmp/tty/tty')" },
4, 1436701355, 35, { 0, "call mkdir('/tmp/tty/tty', 'p')" },
4, 1436701368, 24, { 0, "call mkdir('/', 'p')" },
4, 1436701375, 26, { 0, "call mkdir('/tty/tty')" },
4, 1436701383, 30, { 0, "call mkdir('/tty/tty/tty')" },
4, 1436701407, 35, { 0, "call mkdir('/usr/tty/tty', 'p')" },
4, 1436701666, 35, { 0, "call mkdir('/tty/tty/tty', 'p')" },
4, 1436708101, 25, { 0, "echo msgpackdump([1])" },
4, 1436708966, 6, { 0, "cq" },
4, 1436709606, 25, { 0, "echo msgpackdump([5])" },
4, 1436709610, 26, { 0, "echo msgpackdump([10])" },
4, 1436709615, 31, { 0, "echo msgpackdump([5, 5, 5])" },
4, 1436709618, 35, { 0, "echo msgpackdump([5, 5, 5, 10])" },
4, 1436709634, 57, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])"
},
4, 1436709651, 67, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])"
},
4, 1436709660, 70, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])"
},
4, 1436710095, 29, { 0, "echo msgpackparse([\"\\n\"])" },
4, 1436710100, 28, { 0, "echo msgpackparse([\"j\"])" },
4, 1436710109, 31, { 0, "echo msgpackparse([\"\", \"\"])" },
4, 1436710424, 33, { 0, "echo msgpackparse([\"\", \"\\n\"])" },
4, 1436710428, 32, { 0, "echo msgpackparse([\"\", \"j\"])" },
4, 1436711142, 14, { 0, "echo mpack" },
4, 1436711196, 45, { 0, "let lengths = map(mpack[:], 'len(v:val)')" },
4, 1436711206, 16, { 0, "echo lengths" },
4, 1436711244, 92, {
0,
("let sum = len(lengths) - 1 | call map(copy(lengths), "
.. "'extend(g:, {\"sum\": sum + v:val})')")
},
4, 1436711245, 12, { 0, "echo sum" },
4, 1436711398, 10, { 0, "echo s" },
4, 1436711404, 41, { 0, "let mpack = readfile('/tmp/foo', 'b')" },
4, 1436711408, 41, { 0, "let shada_objects=msgpackparse(mpack)" },
4, 1436711415, 22, { 0, "echo shada_objects" },
4, 1436711451, 30, { 0, "e ~/.nvim/shada/main.shada" },
4, 1436711454, 6, { 0, "qa" },
4, 1436711442, 9, { 1, "test", 47 },
4, 1436711443, 15, { 1, "aontsuesan", 47 },
2, 1436711443, 38, { hlsearch=1, pat="aontsuesan", smartcase=1 },
2, 0, 31, { islast=0, pat="", smartcase=1, sub=1 },
3, 0, 3, { "" },
10, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
1, 1436711454, 78, {
encoding="utf-8",
max_kbyte=10,
pid=19269,
version="NVIM 0.0.0-alpha+201507121634"
},
8, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
8, 1436711391, 8, { file="" },
4, 1436700940, 30, { 0, "call mkdir('/tmp/tty/tty')" },
4, 1436701355, 35, { 0, "call mkdir('/tmp/tty/tty', 'p')" },
4, 1436701368, 24, { 0, "call mkdir('/', 'p')" },
4, 1436701375, 26, { 0, "call mkdir('/tty/tty')" },
4, 1436701383, 30, { 0, "call mkdir('/tty/tty/tty')" },
4, 1436701407, 35, { 0, "call mkdir('/usr/tty/tty', 'p')" },
4, 1436701666, 35, { 0, "call mkdir('/tty/tty/tty', 'p')" },
4, 1436708101, 25, { 0, "echo msgpackdump([1])" },
4, 1436708966, 6, { 0, "cq" },
4, 1436709606, 25, { 0, "echo msgpackdump([5])" },
4, 1436709610, 26, { 0, "echo msgpackdump([10])" },
4, 1436709615, 31, { 0, "echo msgpackdump([5, 5, 5])" },
4, 1436709618, 35, { 0, "echo msgpackdump([5, 5, 5, 10])" },
4, 1436709634, 57, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])"
},
4, 1436709651, 67, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])"
},
4, 1436709660, 70, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])"
},
4, 1436710095, 29, { 0, "echo msgpackparse([\"\\n\"])" },
4, 1436710100, 28, { 0, "echo msgpackparse([\"j\"])" },
4, 1436710109, 31, { 0, "echo msgpackparse([\"\", \"\"])" },
4, 1436710424, 33, { 0, "echo msgpackparse([\"\", \"\\n\"])" },
4, 1436710428, 32, { 0, "echo msgpackparse([\"\", \"j\"])" },
4, 1436711142, 14, { 0, "echo mpack" },
4, 1436711196, 45, { 0, "let lengths = map(mpack[:], 'len(v:val)')" },
4, 1436711206, 16, { 0, "echo lengths" },
4, 1436711244, 92, {
0,
("let sum = len(lengths) - 1 | call map(copy(lengths), "
.. "'extend(g:, {\"sum\": sum + v:val})')")
},
4, 1436711245, 12, { 0, "echo sum" },
4, 1436711398, 10, { 0, "echo s" },
4, 1436711404, 41, { 0, "let mpack = readfile('/tmp/foo', 'b')" },
4, 1436711408, 41, { 0, "let shada_objects=msgpackparse(mpack)" },
4, 1436711415, 22, { 0, "echo shada_objects" },
4, 1436711451, 30, { 0, "e ~/.nvim/shada/main.shada" },
4, 1436711454, 6, { 0, "qa" },
4, 1436711442, 9, { 1, "test", 47 },
4, 1436711443, 15, { 1, "aontsuesan", 47 },
2, 1436711443, 38, { hlsearch=1, pat="aontsuesan", smartcase=1 },
2, 0, 31, { islast=0, pat="", smartcase=1, sub=1 },
3, 0, 3, { "" },
10, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
1, 1436711454, 78, {
encoding="utf-8",
max_kbyte=10,
pid=19269,
version="NVIM 0.0.0-alpha+201507121634"
},
8, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
8, 1436711391, 8, { file="" },
4, 1436700940, 30, { 0, "call mkdir('/tmp/tty/tty')" },
4, 1436701355, 35, { 0, "call mkdir('/tmp/tty/tty', 'p')" },
4, 1436701368, 24, { 0, "call mkdir('/', 'p')" },
4, 1436701375, 26, { 0, "call mkdir('/tty/tty')" },
4, 1436701383, 30, { 0, "call mkdir('/tty/tty/tty')" },
4, 1436701407, 35, { 0, "call mkdir('/usr/tty/tty', 'p')" },
4, 1436701666, 35, { 0, "call mkdir('/tty/tty/tty', 'p')" },
4, 1436708101, 25, { 0, "echo msgpackdump([1])" },
4, 1436708966, 6, { 0, "cq" },
4, 1436709606, 25, { 0, "echo msgpackdump([5])" },
4, 1436709610, 26, { 0, "echo msgpackdump([10])" },
4, 1436709615, 31, { 0, "echo msgpackdump([5, 5, 5])" },
4, 1436709618, 35, { 0, "echo msgpackdump([5, 5, 5, 10])" },
4, 1436709634, 57, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])"
},
4, 1436709651, 67, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])"
},
4, 1436709660, 70, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])"
},
4, 1436710095, 29, { 0, "echo msgpackparse([\"\\n\"])" },
4, 1436710100, 28, { 0, "echo msgpackparse([\"j\"])" },
4, 1436710109, 31, { 0, "echo msgpackparse([\"\", \"\"])" },
4, 1436710424, 33, { 0, "echo msgpackparse([\"\", \"\\n\"])" },
4, 1436710428, 32, { 0, "echo msgpackparse([\"\", \"j\"])" },
4, 1436711142, 14, { 0, "echo mpack" },
4, 1436711196, 45, { 0, "let lengths = map(mpack[:], 'len(v:val)')" },
4, 1436711206, 16, { 0, "echo lengths" },
4, 1436711244, 92, {
0,
("let sum = len(lengths) - 1 | call map(copy(lengths), "
.. "'extend(g:, {\"sum\": sum + v:val})')")
},
4, 1436711245, 12, { 0, "echo sum" },
4, 1436711398, 10, { 0, "echo s" },
4, 1436711404, 41, { 0, "let mpack = readfile('/tmp/foo', 'b')" },
4, 1436711408, 41, { 0, "let shada_objects=msgpackparse(mpack)" },
4, 1436711415, 22, { 0, "echo shada_objects" },
4, 1436711451, 30, { 0, "e ~/.nvim/shada/main.shada" },
4, 1436711454, 6, { 0, "qa" },
4, 1436711442, 9, { 1, "test", 47 },
4, 1436711443, 15, { 1, "aontsuesan", 47 },
2, 1436711443, 38, { hlsearch=1, pat="aontsuesan", smartcase=1 },
2, 0, 31, { islast=0, pat="", smartcase=1, sub=1 },
3, 0, 3, { "" },
10, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
1, 1436711454, 78, {
encoding="utf-8",
max_kbyte=10,
pid=19269,
version="NVIM 0.0.0-alpha+201507121634"
},
8, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
8, 1436711391, 8, { file="" },
4, 1436700940, 30, { 0, "call mkdir('/tmp/tty/tty')" },
4, 1436701355, 35, { 0, "call mkdir('/tmp/tty/tty', 'p')" },
4, 1436701368, 24, { 0, "call mkdir('/', 'p')" },
4, 1436701375, 26, { 0, "call mkdir('/tty/tty')" },
4, 1436701383, 30, { 0, "call mkdir('/tty/tty/tty')" },
4, 1436701407, 35, { 0, "call mkdir('/usr/tty/tty', 'p')" },
4, 1436701666, 35, { 0, "call mkdir('/tty/tty/tty', 'p')" },
4, 1436708101, 25, { 0, "echo msgpackdump([1])" },
4, 1436708966, 6, { 0, "cq" },
4, 1436709606, 25, { 0, "echo msgpackdump([5])" },
4, 1436709610, 26, { 0, "echo msgpackdump([10])" },
4, 1436709615, 31, { 0, "echo msgpackdump([5, 5, 5])" },
4, 1436709618, 35, { 0, "echo msgpackdump([5, 5, 5, 10])" },
4, 1436709634, 57, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])"
},
4, 1436709651, 67, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])"
},
4, 1436709660, 70, {
0,
"echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])"
},
4, 1436710095, 29, { 0, "echo msgpackparse([\"\\n\"])" },
4, 1436710100, 28, { 0, "echo msgpackparse([\"j\"])" },
4, 1436710109, 31, { 0, "echo msgpackparse([\"\", \"\"])" },
4, 1436710424, 33, { 0, "echo msgpackparse([\"\", \"\\n\"])" },
4, 1436710428, 32, { 0, "echo msgpackparse([\"\", \"j\"])" },
4, 1436711142, 14, { 0, "echo mpack" },
4, 1436711196, 45, { 0, "let lengths = map(mpack[:], 'len(v:val)')" },
4, 1436711206, 16, { 0, "echo lengths" },
4, 1436711244, 92, {
0,
("let sum = len(lengths) - 1 | call map(copy(lengths), "
.. "'extend(g:, {\"sum\": sum + v:val})')")
},
4, 1436711245, 12, { 0, "echo sum" },
4, 1436711398, 10, { 0, "echo s" },
4, 1436711404, 41, { 0, "let mpack = readfile('/tmp/foo', 'b')" },
4, 1436711408, 41, { 0, "let shada_objects=msgpackparse(mpack)" },
4, 1436711415, 22, { 0, "echo shada_objects" },
4, 1436711451, 30, { 0, "e ~/.nvim/shada/main.shada" },
4, 1436711454, 6, { 0, "qa" },
4, 1436711442, 9, { 1, "test", 47 },
4, 1436711443, 15, { 1, "aontsuesan", 47 },
2, 1436711443, 38, { hlsearch=1, pat="aontsuesan", smartcase=1 },
2, 0, 31, { islast=0, pat="", smartcase=1, sub=1 },
3, 0, 3, { "" },
10, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" }
}
obj_test('are able to dump and restore rather big object', big_obj)
obj_test('are able to dump and restore floating-point value', {0.125})
it('restore nil as special dict', function()
execute('let dumped = ["\\xC0"]')
execute('let parsed = msgpackparse(dumped)')
eq({{_TYPE={}, _VAL=0}}, eval('parsed'))
eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.nil'))
end)
it('restore boolean false as zero', function()
execute('let dumped = ["\\xC2"]')
execute('let parsed = msgpackparse(dumped)')
eq({{_TYPE={}, _VAL=0}}, eval('parsed'))
eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.boolean'))
end)
it('restore boolean true as one', function()
execute('let dumped = ["\\xC3"]')
execute('let parsed = msgpackparse(dumped)')
eq({{_TYPE={}, _VAL=1}}, eval('parsed'))
eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.boolean'))
end)
it('dump string as BIN 8', function()
nvim('set_var', 'obj', {'Test'})
eq({"\196\004Test"}, eval('msgpackdump(obj)'))
end)
it('restore FIXSTR as special dict', function()
execute('let dumped = ["\\xa2ab"]')
execute('let parsed = msgpackparse(dumped)')
eq({{_TYPE={}, _VAL={'ab'}}}, eval('parsed'))
eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.string'))
end)
it('restore BIN 8 as string', function()
execute('let dumped = ["\\xC4\\x02ab"]')
eq({'ab'}, eval('msgpackparse(dumped)'))
end)
it('restore FIXEXT1 as special dictionary', function()
execute('let dumped = ["\\xD4\\x10", ""]')
execute('let parsed = msgpackparse(dumped)')
eq({{_TYPE={}, _VAL={0x10, {"", ""}}}}, eval('parsed'))
eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.ext'))
end)
it('restore MAP with BIN key as special dictionary', function()
execute('let dumped = ["\\x81\\xC4\\x01a\\xC4\\n"]')
execute('let parsed = msgpackparse(dumped)')
eq({{_TYPE={}, _VAL={{'a', ''}}}}, eval('parsed'))
eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.map'))
end)
it('restore MAP with duplicate STR keys as special dictionary', function()
execute('let dumped = ["\\x82\\xA1a\\xC4\\n\\xA1a\\xC4\\n"]')
execute('let parsed = msgpackparse(dumped)')
eq({{_TYPE={}, _VAL={{{_TYPE={}, _VAL={'a'}}, ''},
{{_TYPE={}, _VAL={'a'}}, ''}}}}, eval('parsed'))
eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.map'))
eq(1, eval('g:parsed[0]._VAL[0][0]._TYPE is v:msgpack_types.string'))
eq(1, eval('g:parsed[0]._VAL[1][0]._TYPE is v:msgpack_types.string'))
end)
it('restore MAP with MAP key as special dictionary', function()
execute('let dumped = ["\\x81\\x80\\xC4\\n"]')
execute('let parsed = msgpackparse(dumped)')
eq({{_TYPE={}, _VAL={{{}, ''}}}}, eval('parsed'))
eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.map'))
end)
it('can restore and dump UINT64_MAX', function()
execute('let dumped = ["\\xCF" . repeat("\\xFF", 8)]')
execute('let parsed = msgpackparse(dumped)')
execute('let dumped2 = msgpackdump(parsed)')
eq(1, eval('type(parsed[0]) == type(0) ' ..
'|| parsed[0]._TYPE is v:msgpack_types.integer'))
if eval('type(parsed[0]) == type(0)') == 1 then
eq(1, eval('0xFFFFFFFFFFFFFFFF == parsed[0]'))
else
eq({_TYPE={}, _VAL={1, 3, 0x7FFFFFFF, 0x7FFFFFFF}}, eval('parsed[0]'))
end
eq(1, eval('dumped ==# dumped2'))
end)
it('can restore and dump INT64_MIN', function()
execute('let dumped = ["\\xD3\\x80" . repeat("\\n", 7)]')
execute('let parsed = msgpackparse(dumped)')
execute('let dumped2 = msgpackdump(parsed)')
eq(1, eval('type(parsed[0]) == type(0) ' ..
'|| parsed[0]._TYPE is v:msgpack_types.integer'))
if eval('type(parsed[0]) == type(0)') == 1 then
eq(1, eval('-0x8000000000000000 == parsed[0]'))
else
eq({_TYPE={}, _VAL={-1, 2, 0, 0}}, eval('parsed[0]'))
end
eq(1, eval('dumped ==# dumped2'))
end)
it('can restore and dump BIN string with zero byte', function()
execute('let dumped = ["\\xC4\\x01\\n"]')
execute('let parsed = msgpackparse(dumped)')
execute('let dumped2 = msgpackdump(parsed)')
eq({{_TYPE={}, _VAL={'\n'}}}, eval('parsed'))
eq(1, eval('parsed[0]._TYPE is v:msgpack_types.binary'))
eq(1, eval('dumped ==# dumped2'))
end)
it('can restore and dump STR string with zero byte', function()
execute('let dumped = ["\\xA1\\n"]')
execute('let parsed = msgpackparse(dumped)')
execute('let dumped2 = msgpackdump(parsed)')
eq({{_TYPE={}, _VAL={'\n'}}}, eval('parsed'))
eq(1, eval('parsed[0]._TYPE is v:msgpack_types.string'))
eq(1, eval('dumped ==# dumped2'))
end)
it('can restore and dump BIN string with NL', function()
execute('let dumped = ["\\xC4\\x01", ""]')
execute('let parsed = msgpackparse(dumped)')
execute('let dumped2 = msgpackdump(parsed)')
eq({"\n"}, eval('parsed'))
eq(1, eval('dumped ==# dumped2'))
end)
it('can dump generic mapping with generic mapping keys and values', function()
execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
execute('let todumpv1 = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
execute('let todumpv2 = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
execute('call add(todump._VAL, [todumpv1, todumpv2])')
eq({'\129\128\128'}, eval('msgpackdump([todump])'))
end)
it('can dump generic mapping with ext', function()
execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}')
eq({'\212\005', ''}, eval('msgpackdump([todump])'))
end)
it('can dump generic mapping with array', function()
execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}')
eq({'\146\005\145\196\n'}, eval('msgpackdump([todump])'))
end)
it('can dump generic mapping with UINT64_MAX', function()
execute('let todump = {"_TYPE": v:msgpack_types.integer}')
execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]')
eq({'\207\255\255\255\255\255\255\255\255'}, eval('msgpackdump([todump])'))
end)
it('can dump generic mapping with INT64_MIN', function()
execute('let todump = {"_TYPE": v:msgpack_types.integer}')
execute('let todump._VAL = [-1, 2, 0, 0]')
eq({'\211\128\n\n\n\n\n\n\n'}, eval('msgpackdump([todump])'))
end)
it('dump and restore generic mapping with floating-point value', function()
execute('let todump = {"_TYPE": v:msgpack_types.float, "_VAL": 0.125}')
eq({0.125}, eval('msgpackparse(msgpackdump([todump]))'))
end)
it('fails to dump a function reference', function()
execute('let Todump = function("tr")')
execute([[
try
let dumped = msgpackdump([Todump])
let exception = 0
catch
let exception = v:exception
endtry
]])
eq('Vim(let):E475: Invalid argument: attempt to dump function reference',
eval('exception'))
end)
it('fails to dump a function reference in a list', function()
execute('let todump = [function("tr")]')
execute([[
try
let dumped = msgpackdump([todump])
let exception = 0
catch
let exception = v:exception
endtry
]])
eq('Vim(let):E475: Invalid argument: attempt to dump function reference',
eval('exception'))
end)
it('fails to dump a recursive list', function()
execute('let todump = [[[]]]')
execute('call add(todump[0][0], todump)')
execute([[
try
let dumped = msgpackdump([todump])
let exception = 0
catch
let exception = v:exception
endtry
]])
eq('Vim(let):E475: Invalid argument: container references itself',
eval('exception'))
end)
it('fails to dump a recursive dict', function()
execute('let todump = {"d": {"d": {}}}')
execute('call extend(todump.d.d, {"d": todump})')
execute([[
try
let dumped = msgpackdump([todump])
let exception = 0
catch
let exception = v:exception
endtry
]])
eq('Vim(let):E475: Invalid argument: container references itself',
eval('exception'))
end)
it('fails to dump a recursive list in a special dict', function()
execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}')
execute('call add(todump._VAL, todump)')
execute([[
try
let dumped = msgpackdump([todump])
let exception = 0
catch
let exception = v:exception
endtry
]])
eq('Vim(let):E475: Invalid argument: container references itself',
eval('exception'))
end)
it('fails to dump a recursive (key) map in a special dict', function()
execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}')
execute('call add(todump._VAL, [todump, 0])')
execute([[
try
let dumped = msgpackdump([todump])
let exception = 0
catch
let exception = v:exception
endtry
]])
eq('Vim(let):E475: Invalid argument: container references itself',
eval('exception'))
end)
it('fails to dump a recursive (val) map in a special dict', function()
execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}')
execute('call add(todump._VAL, [0, todump])')
execute([[
try
let dumped = msgpackdump([todump])
let exception = 0
catch
let exception = v:exception
endtry
]])
eq('Vim(let):E475: Invalid argument: container references itself',
eval('exception'))
end)
it('fails to dump a recursive (val) special list in a special dict',
function()
execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}')
execute('call add(todump._VAL, [0, todump._VAL])')
execute([[
try
let dumped = msgpackdump([todump])
let exception = 0
catch
let exception = v:exception
endtry
]])
eq('Vim(let):E475: Invalid argument: container references itself',
eval('exception'))
end)
end)