From 522e1099266741004fb099e6db486d5237ed3afc Mon Sep 17 00:00:00 2001 From: dbs Date: Tue, 29 Mar 2011 13:55:09 +0000 Subject: [PATCH] Make javascript unit tests more granular to ease tracking down errors Noticed a failure when running with Rhino 1.7RC2 that wasn't happening with version of Rhino packaged in shrinksafe.jar, and it was a bit tedious narrowing down which test was actually failing. Breaking up the tests into smaller sets will make this process easier and might invite the submission of other tests to target areas that are not yet covered. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@2222 9efc2488-bf62-4759-914b-345cdb29e865 --- src/javascript/tests/testJSON_v1.js | 56 +++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/javascript/tests/testJSON_v1.js b/src/javascript/tests/testJSON_v1.js index 1cd7d10..19aee2c 100644 --- a/src/javascript/tests/testJSON_v1.js +++ b/src/javascript/tests/testJSON_v1.js @@ -16,45 +16,65 @@ doh.register("JSONTests", [ doh.assertTrue(encodeJS(null) === null); doh.assertTrue(encodeJS("") === ""); }, - function test_js2JSON() { + function test_js2JSON_strict() { // Solo nulls and booleans are stringified XXX doh.assertTrue(js2JSON(null) === "null"); doh.assertTrue(js2JSON(true) === "true"); doh.assertTrue(js2JSON(false) === "false"); + }, + function test_js2JSON_numbers() { doh.assertTrue(js2JSON(0) === 0); doh.assertTrue(js2JSON(1.5) === 1.5); doh.assertTrue(js2JSON(.7) === .7); + }, + function test_js2JSON_strings() { doh.assertTrue(js2JSON("") == '""'); doh.assertTrue(js2JSON("foo") == '"foo"'); // Escape sequences doh.assertTrue(js2JSON("foo\n\t\n") == '"foo\\n\\t\\n"'); + }, + function test_js2JSON_arrays() { + doh.assertTrue(js2JSON([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]'); + }, + function test_js2JSON_objects() { doh.assertTrue(js2JSON({"foo":"bar"}) == '{"foo":"bar"}'); doh.assertTrue(js2JSON({"foo":true}) == '{"foo":true}'); doh.assertTrue(js2JSON({"foo":0}) == '{"foo":0}'); - doh.assertTrue(js2JSON([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]'); + }, + function test_js2JSON_objects_ordered() { // Order of object attributes is not guaranteed - doh.assertTrue(js2JSON({"foo":{"one":null,"two":2}}) == '{"foo":{"two":2,"one":null}}'); + doh.assertTrue(js2JSON({"foo":{"one":null,"two":2}}) == '{"foo":{"one":null,"two":2}}'); }, - function test_js2JSONRaw() { + function test_js2JSONRaw_strict() { // Solo nulls and booleans are stringified XXX doh.assertTrue(js2JSONRaw(null) === "null"); doh.assertTrue(js2JSONRaw(true) === "true"); doh.assertTrue(js2JSONRaw(false) === "false"); + }, + function test_js2JSONRaw_numbers() { doh.assertTrue(js2JSONRaw(0) === 0); doh.assertTrue(js2JSONRaw(1.5) === 1.5); doh.assertTrue(js2JSONRaw(.7) === .7); + }, + function test_js2JSONRaw_strings() { doh.assertTrue(js2JSONRaw("") == '""'); doh.assertTrue(js2JSONRaw("foo") == '"foo"'); // Escape sequences doh.assertTrue(js2JSONRaw("foo\n\t\n") == '"foo\\n\\t\\n"'); + }, + function test_js2JSONRaw_arrays() { + doh.assertTrue(js2JSONRaw([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]'); + }, + function test_js2JSONRaw_objects() { doh.assertTrue(js2JSONRaw({"foo":"bar"}) == '{"foo":"bar"}'); doh.assertTrue(js2JSONRaw({"foo":true}) == '{"foo":true}'); doh.assertTrue(js2JSONRaw({"foo":0}) == '{"foo":0}'); - doh.assertTrue(js2JSONRaw([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]'); + }, + function test_js2JSONRaw_objects_ordered() { // Order of object attributes is not guaranteed - doh.assertTrue(js2JSONRaw({"foo":{"one":null,"two":2}}) == '{"foo":{"two":2,"one":null}}'); + doh.assertTrue(js2JSONRaw({"foo":{"one":null,"two":2}}) == '{"foo":{"one":null,"two":2}}'); }, - function test_JSON2jsRaw() { + function test_JSON2jsRaw_strict() { // Standalone quoted nulls and booleans are converted to primitives doh.assertTrue(JSON2jsRaw(null) === null); doh.assertTrue(JSON2jsRaw("null") === null); @@ -62,20 +82,30 @@ doh.register("JSONTests", [ doh.assertTrue(JSON2jsRaw("true") === true); doh.assertTrue(JSON2jsRaw(false) === false); doh.assertTrue(JSON2jsRaw("false") === false); + }, + function test_JSON2jsRaw_numbers() { // Zero is zero and only zero doh.assertTrue(JSON2jsRaw(0) === 0); + doh.assertTrue(JSON2jsRaw(1.5) === 1.5); + doh.assertTrue(JSON2jsRaw(.5) === .5); + }, + function test_JSON2jsRaw_strings() { // Empty string doh.assertTrue(JSON2jsRaw('""') === ""); // String doh.assertTrue(JSON2jsRaw('"foo"') == "foo"); + }, + function test_JSON2jsRaw_arrays() { // Array; access an index doh.assertTrue(JSON2jsRaw('[0,1,2,3,4,5]')[1] == 1); + }, + function test_JSON2jsRaw_objects() { // Object; access a key doh.assertTrue(JSON2jsRaw('{"foo":"bar"}').foo == "bar"); doh.assertTrue(JSON2jsRaw('{"foo":{"two":2,"one":null}}').foo.one === null); doh.assertTrue(JSON2jsRaw('{"foo":{"two":2,"one":"null"}}').foo.one === "null"); }, - function test_JSON2js() { + function test_JSON2js_strict() { // Standalone quoted nulls and booleans are converted to primitives doh.assertTrue(JSON2js(null) === null); doh.assertTrue(JSON2js("null") === null); @@ -83,14 +113,24 @@ doh.register("JSONTests", [ doh.assertTrue(JSON2js("true") === true); doh.assertTrue(JSON2js(false) === false); doh.assertTrue(JSON2js("false") === false); + }, + function test_JSON2js_numbers() { // Zero is zero and only zero doh.assertTrue(JSON2js(0) === 0); + doh.assertTrue(JSON2js(1.5) === 1.5); + doh.assertTrue(JSON2js(.5) === .5); + }, + function test_JSON2js_strings() { // Empty string doh.assertTrue(JSON2js('""') === ""); // String doh.assertTrue(JSON2js('"foo"') == "foo"); + }, + function test_JSON2js_arrays() { // Array; access an index doh.assertTrue(JSON2js('[0,1,2,3,4,5]')[1] == 1); + }, + function test_JSON2js_objects() { // Object; access a key doh.assertTrue(JSON2js('{"foo":"bar"}').foo == "bar"); doh.assertTrue(JSON2js('{"foo":{"two":2,"one":null}}').foo.one === null); -- 2.11.0