From b8a09518b908969fa46787de495d18f075f3621c Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Mon, 25 Feb 2013 18:26:50 +0200 Subject: [PATCH] JSON.parse() testcases --- testcases/test-builtin-json-dec-array.js | 52 +++++++++++++++++++ testcases/test-builtin-json-dec-empty.js | 19 +++++++ testcases/test-builtin-json-dec-whitespace.js | 26 ++++++++++ 3 files changed, 97 insertions(+) create mode 100644 testcases/test-builtin-json-dec-array.js create mode 100644 testcases/test-builtin-json-dec-empty.js create mode 100644 testcases/test-builtin-json-dec-whitespace.js diff --git a/testcases/test-builtin-json-dec-array.js b/testcases/test-builtin-json-dec-array.js new file mode 100644 index 00000000..50d7bbbe --- /dev/null +++ b/testcases/test-builtin-json-dec-array.js @@ -0,0 +1,52 @@ +/*=== +SyntaxError +===*/ + +/* Don't allow empty object with just comma */ + +try { + print(JSON.parse('[,]')); +} catch (e) { + print(e.name); +} + +/*=== +SyntaxError +===*/ + +/* Don't allow initial comma */ + +try { + print(JSON.parse('[,1]')); +} catch (e) { + print(e.name); +} + + +/*=== +SyntaxError +===*/ + +/* Don't allow trailing comma. + * + * Rhino allows this. + */ + +try { + print(JSON.parse('[1,]')); +} catch (e) { + print(e.name); +} + +/*=== +SyntaxError +===*/ + +/* Don't allow successive commas (elisions) */ + +try { + print(JSON.parse('[1,,2]')); +} catch (e) { + print(e.name); +} + diff --git a/testcases/test-builtin-json-dec-empty.js b/testcases/test-builtin-json-dec-empty.js new file mode 100644 index 00000000..bd92beef --- /dev/null +++ b/testcases/test-builtin-json-dec-empty.js @@ -0,0 +1,19 @@ +/*=== +SyntaxError +SyntaxError +===*/ + +/* Empty or pure white space is a SyntaxError. */ + +try { + print(JSON.parse('')); +} catch (e) { + print(e.name); +} + +try { + print(JSON.parse(' \n\r\t')); +} catch (e) { + print(e.name); +} + diff --git a/testcases/test-builtin-json-dec-whitespace.js b/testcases/test-builtin-json-dec-whitespace.js new file mode 100644 index 00000000..62681142 --- /dev/null +++ b/testcases/test-builtin-json-dec-whitespace.js @@ -0,0 +1,26 @@ +/*=== +1 +1 +SyntaxError +===*/ + +/* Allow leading and trailing whitespace, but no other garbage */ + +try { + print(JSON.parse('1')); +} catch (e) { + print(e.name); +} + +try { + print(JSON.parse('\t\n 1 \t\n')); +} catch (e) { + print(e.name); +} + +try { + print(JSON.parse('1 x')); +} catch (e) { + print(e.name); +} +