2016-08-29 22:49:24 +02:00
|
|
|
/* vi:set ts=8 sts=4 sw=4 noet:
|
2016-02-02 18:20:08 +01:00
|
|
|
*
|
|
|
|
* VIM - Vi IMproved by Bram Moolenaar
|
|
|
|
*
|
|
|
|
* Do ":help uganda" in Vim to read copying and usage conditions.
|
|
|
|
* Do ":help credits" in Vim to see a list of people who contributed.
|
|
|
|
* See README.txt for an overview of the Vim source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* json_test.c: Unittests for json.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
/* Must include main.c because it contains much more than just main() */
|
|
|
|
#define NO_VIM_MAIN
|
|
|
|
#include "main.c"
|
|
|
|
|
|
|
|
/* This file has to be included because the tested functions are static */
|
|
|
|
#include "json.c"
|
|
|
|
|
2016-02-02 19:15:38 +01:00
|
|
|
#if defined(FEAT_EVAL)
|
2016-02-02 18:20:08 +01:00
|
|
|
/*
|
|
|
|
* Test json_find_end() with imcomplete items.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
test_decode_find_end(void)
|
|
|
|
{
|
|
|
|
js_read_T reader;
|
|
|
|
|
|
|
|
reader.js_fill = NULL;
|
|
|
|
reader.js_used = 0;
|
|
|
|
|
|
|
|
/* string and incomplete string */
|
|
|
|
reader.js_buf = (char_u *)"\"hello\"";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" \"hello\" ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"\"hello";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
|
|
|
|
/* number and dash (incomplete number) */
|
|
|
|
reader.js_buf = (char_u *)"123";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"-";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
|
|
|
|
/* false, true and null, also incomplete */
|
|
|
|
reader.js_buf = (char_u *)"false";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"f";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"fa";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"fal";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"fals";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
|
|
|
|
reader.js_buf = (char_u *)"true";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"t";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"tr";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"tru";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
|
|
|
|
reader.js_buf = (char_u *)"null";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"n";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"nu";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"nul";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
|
|
|
|
/* object without white space */
|
|
|
|
reader.js_buf = (char_u *)"{\"a\":123}";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"{\"a\":123";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"{\"a\":";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"{\"a\"";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"{\"a";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"{\"";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"{";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
|
|
|
|
/* object with white space */
|
|
|
|
reader.js_buf = (char_u *)" { \"a\" : 123 } ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" { \"a\" : 123 ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" { \"a\" : ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" { \"a\" ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" { \"a ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" { ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
|
2017-01-22 15:56:26 +01:00
|
|
|
/* JS object with white space */
|
|
|
|
reader.js_buf = (char_u *)" { a : 123 } ";
|
|
|
|
assert(json_find_end(&reader, JSON_JS) == OK);
|
|
|
|
reader.js_buf = (char_u *)" { a : ";
|
|
|
|
assert(json_find_end(&reader, JSON_JS) == MAYBE);
|
|
|
|
|
2016-02-02 18:20:08 +01:00
|
|
|
/* array without white space */
|
|
|
|
reader.js_buf = (char_u *)"[\"a\",123]";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"[\"a\",123";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"[\"a\",";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"[\"a\"";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"[\"a";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"[\"";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)"[";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
|
|
|
|
/* array with white space */
|
|
|
|
reader.js_buf = (char_u *)" [ \"a\" , 123 ] ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" [ \"a\" , 123 ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" [ \"a\" , ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" [ \"a\" ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" [ \"a ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" [ ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == MAYBE);
|
2016-02-02 18:20:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fill_from_cookie(js_read_T *reader)
|
|
|
|
{
|
|
|
|
reader->js_buf = reader->js_cookie;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test json_find_end with an incomplete array, calling the fill function.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
test_fill_called_on_find_end(void)
|
|
|
|
{
|
|
|
|
js_read_T reader;
|
|
|
|
|
|
|
|
reader.js_fill = fill_from_cookie;
|
|
|
|
reader.js_used = 0;
|
|
|
|
reader.js_buf = (char_u *)" [ \"a\" , 123 ";
|
2018-02-27 17:27:13 +01:00
|
|
|
reader.js_cookie = " [ \"a\" , 123 ] ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" [ \"a\" , ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" [ \"a\" ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" [ \"a";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
reader.js_buf = (char_u *)" [ ";
|
2016-02-07 19:19:53 +01:00
|
|
|
assert(json_find_end(&reader, 0) == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test json_find_end with an incomplete string, calling the fill function.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
test_fill_called_on_string(void)
|
|
|
|
{
|
|
|
|
js_read_T reader;
|
|
|
|
|
|
|
|
reader.js_fill = fill_from_cookie;
|
|
|
|
reader.js_used = 0;
|
|
|
|
reader.js_buf = (char_u *)" \"foo";
|
|
|
|
reader.js_end = reader.js_buf + STRLEN(reader.js_buf);
|
2018-02-27 17:27:13 +01:00
|
|
|
reader.js_cookie = " \"foobar\" ";
|
2017-01-11 21:50:08 +01:00
|
|
|
assert(json_decode_string(&reader, NULL, '"') == OK);
|
2016-02-02 18:20:08 +01:00
|
|
|
}
|
2016-02-02 19:15:38 +01:00
|
|
|
#endif
|
2016-02-02 18:20:08 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2016-02-02 19:15:38 +01:00
|
|
|
#if defined(FEAT_EVAL)
|
2016-02-02 18:20:08 +01:00
|
|
|
test_decode_find_end();
|
|
|
|
test_fill_called_on_find_end();
|
|
|
|
test_fill_called_on_string();
|
2016-02-02 19:15:38 +01:00
|
|
|
#endif
|
2016-02-02 18:20:08 +01:00
|
|
|
return 0;
|
|
|
|
}
|