1 dojo.provide('opensrf.tests.testJSON_v1');
3 dojo.require('DojoSRF');
5 doh.register("JSONTests", [
6 function test_version() {
7 doh.assertTrue(JSON_version() == 'wrapper');
9 function test_decodeJS() {
10 doh.assertTrue(decodeJS(0) === 0);
11 doh.assertTrue(decodeJS(null) === null);
12 doh.assertTrue(decodeJS("") === "");
14 function test_encodeJS() {
15 doh.assertTrue(encodeJS(0) === 0);
16 doh.assertTrue(encodeJS(null) === null);
17 doh.assertTrue(encodeJS("") === "");
19 function test_js2JSON_strict() {
20 // Solo nulls and booleans are stringified XXX
21 doh.assertTrue(js2JSON(null) === "null");
22 doh.assertTrue(js2JSON(true) === "true");
23 doh.assertTrue(js2JSON(false) === "false");
25 function test_js2JSON_numbers() {
26 doh.assertTrue(js2JSON(0) === 0);
27 doh.assertTrue(js2JSON(1.5) === 1.5);
28 doh.assertTrue(js2JSON(.7) === .7);
30 function test_js2JSON_strings() {
31 doh.assertTrue(js2JSON("") == '""');
32 doh.assertTrue(js2JSON("foo") == '"foo"');
34 doh.assertTrue(js2JSON("foo\n\t\n") == '"foo\\n\\t\\n"');
36 function test_js2JSON_arrays() {
37 doh.assertTrue(js2JSON([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]');
39 function test_js2JSON_objects() {
40 doh.assertTrue(js2JSON({"foo":"bar"}) == '{"foo":"bar"}');
41 doh.assertTrue(js2JSON({"foo":true}) == '{"foo":true}');
42 doh.assertTrue(js2JSON({"foo":0}) == '{"foo":0}');
44 function test_js2JSON_objects_ordered() {
45 // Order of object attributes is not guaranteed
46 doh.assertTrue(js2JSON({"foo":{"one":[null,"two",2]}}) == '{"foo":{"one":[null,"two",2]}}');
48 function test_JSON2js_strict() {
49 // Standalone quoted nulls and booleans are converted to primitives
50 doh.assertTrue(JSON2js(null) === null);
51 doh.assertTrue(JSON2js("null") === null);
52 doh.assertTrue(JSON2js(true) === true);
53 doh.assertTrue(JSON2js("true") === true);
54 doh.assertTrue(JSON2js(false) === false);
55 doh.assertTrue(JSON2js("false") === false);
57 function test_JSON2js_numbers() {
58 // Zero is zero and only zero
59 doh.assertTrue(JSON2js(0) === 0);
60 doh.assertTrue(JSON2js(1.5) === 1.5);
61 doh.assertTrue(JSON2js(.5) === .5);
63 function test_JSON2js_strings() {
65 doh.assertTrue(JSON2js('""') === "");
67 doh.assertTrue(JSON2js('"foo"') == "foo");
69 function test_JSON2js_arrays() {
70 // Array; access an index
71 doh.assertTrue(JSON2js('[0,1,2,3,4,5]')[1] == 1);
73 function test_JSON2js_objects() {
74 // Object; access a key
75 doh.assertTrue(JSON2js('{"foo":"bar"}').foo == "bar");
76 doh.assertTrue(JSON2js('{"foo":{"two":2,"one":null}}').foo.one === null);
77 doh.assertTrue(JSON2js('{"foo":{"two":2,"one":"null"}}').foo.one === "null");