summaryrefslogtreecommitdiff
path: root/src/json_test.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2016-02-02 18:20:08 +0100
committerBram Moolenaar <Bram@vim.org>2016-02-02 18:20:08 +0100
commit56ead341a75e1a0395eee94a3280c67e2278a57e (patch)
tree52c81e0242666bf75c227a392473bf5ea26cf6dd /src/json_test.c
parentd9ea9069f5ef5b8b9f9e0d0daecdd124e2dcd818 (diff)
downloadvim-git-56ead341a75e1a0395eee94a3280c67e2278a57e.tar.gz
patch 7.4.1238v7.4.1238
Problem: Can't handle two messages right after each other. Solution: Find the end of the JSON. Read more when incomplete. Add a C test for the JSON decoding.
Diffstat (limited to 'src/json_test.c')
-rw-r--r--src/json_test.c193
1 files changed, 193 insertions, 0 deletions
diff --git a/src/json_test.c b/src/json_test.c
new file mode 100644
index 000000000..041654392
--- /dev/null
+++ b/src/json_test.c
@@ -0,0 +1,193 @@
+/* vi:set ts=8 sts=4 sw=4:
+ *
+ * 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"
+
+/*
+ * 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\"";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)" \"hello\" ";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)"\"hello";
+ assert(json_find_end(&reader) == MAYBE);
+
+ /* number and dash (incomplete number) */
+ reader.js_buf = (char_u *)"123";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)"-";
+ assert(json_find_end(&reader) == MAYBE);
+
+ /* false, true and null, also incomplete */
+ reader.js_buf = (char_u *)"false";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)"f";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"fa";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"fal";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"fals";
+ assert(json_find_end(&reader) == MAYBE);
+
+ reader.js_buf = (char_u *)"true";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)"t";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"tr";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"tru";
+ assert(json_find_end(&reader) == MAYBE);
+
+ reader.js_buf = (char_u *)"null";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)"n";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"nu";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"nul";
+ assert(json_find_end(&reader) == MAYBE);
+
+ /* object without white space */
+ reader.js_buf = (char_u *)"{\"a\":123}";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)"{\"a\":123";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"{\"a\":";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"{\"a\"";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"{\"a";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"{\"";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"{";
+ assert(json_find_end(&reader) == MAYBE);
+
+ /* object with white space */
+ reader.js_buf = (char_u *)" { \"a\" : 123 } ";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)" { \"a\" : 123 ";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)" { \"a\" : ";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)" { \"a\" ";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)" { \"a ";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)" { ";
+ assert(json_find_end(&reader) == MAYBE);
+
+ /* array without white space */
+ reader.js_buf = (char_u *)"[\"a\",123]";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)"[\"a\",123";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"[\"a\",";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"[\"a\"";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"[\"a";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"[\"";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)"[";
+ assert(json_find_end(&reader) == MAYBE);
+
+ /* array with white space */
+ reader.js_buf = (char_u *)" [ \"a\" , 123 ] ";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)" [ \"a\" , 123 ";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)" [ \"a\" , ";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)" [ \"a\" ";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)" [ \"a ";
+ assert(json_find_end(&reader) == MAYBE);
+ reader.js_buf = (char_u *)" [ ";
+ assert(json_find_end(&reader) == MAYBE);
+}
+
+ 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 ";
+ reader.js_cookie = " [ \"a\" , 123 ] ";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)" [ \"a\" , ";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)" [ \"a\" ";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)" [ \"a";
+ assert(json_find_end(&reader) == OK);
+ reader.js_buf = (char_u *)" [ ";
+ assert(json_find_end(&reader) == OK);
+}
+
+/*
+ * 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);
+ reader.js_cookie = " \"foobar\" ";
+ assert(json_decode_string(&reader, NULL) == OK);
+}
+
+ int
+main(void)
+{
+ test_decode_find_end();
+ test_fill_called_on_find_end();
+ test_fill_called_on_string();
+ return 0;
+}