]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/tests/testJSON_v1.js
9530fa0efa57406189dd21ff65f052430f8815e4
[OpenSRF.git] / src / javascript / tests / testJSON_v1.js
1 dojo.provide('opensrf.tests.testJSON_v1');
2
3 dojo.require('DojoSRF');
4
5 doh.register("JSONTests", [
6     function test_version() {
7         doh.assertTrue(JSON_version() == 'wrapper');
8     },
9     function test_decodeJS() {
10         doh.assertTrue(decodeJS(0) === 0);
11         doh.assertTrue(decodeJS(null) === null);
12         doh.assertTrue(decodeJS("") === "");
13     },
14     function test_encodeJS() {
15         doh.assertTrue(encodeJS(0) === 0);
16         doh.assertTrue(encodeJS(null) === null);
17         doh.assertTrue(encodeJS("") === "");
18     },
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");
24     },
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);
29     },
30     function test_js2JSON_strings() {
31         doh.assertTrue(js2JSON("") == '""');
32         doh.assertTrue(js2JSON("foo") == '"foo"');
33         // Escape sequences
34         doh.assertTrue(js2JSON("foo\n\t\n") == '"foo\\n\\t\\n"');
35     },
36     function test_js2JSON_arrays() {
37         doh.assertTrue(js2JSON([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]');
38     },
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}');
43     },
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]}}');
47     },
48     function test_js2JSONRaw_strict() {
49         // Solo nulls and booleans are stringified XXX
50         doh.assertTrue(js2JSONRaw(null) === "null");
51         doh.assertTrue(js2JSONRaw(true) === "true");
52         doh.assertTrue(js2JSONRaw(false) === "false");
53     },
54     function test_js2JSONRaw_numbers() {
55         doh.assertTrue(js2JSONRaw(0) === 0);
56         doh.assertTrue(js2JSONRaw(1.5) === 1.5);
57         doh.assertTrue(js2JSONRaw(.7) === .7);
58     },
59     function test_js2JSONRaw_strings() {
60         doh.assertTrue(js2JSONRaw("") == '""');
61         doh.assertTrue(js2JSONRaw("foo") == '"foo"');
62         // Escape sequences
63         doh.assertTrue(js2JSONRaw("foo\n\t\n") == '"foo\\n\\t\\n"');
64     },
65     function test_js2JSONRaw_arrays() {
66         doh.assertTrue(js2JSONRaw([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]');
67     },
68     function test_js2JSONRaw_objects() {
69         doh.assertTrue(js2JSONRaw({"foo":"bar"}) == '{"foo":"bar"}');
70         doh.assertTrue(js2JSONRaw({"foo":true}) == '{"foo":true}');
71         doh.assertTrue(js2JSONRaw({"foo":0}) == '{"foo":0}');
72     },
73     function test_js2JSONRaw_objects_ordered() {
74         // Order of object attributes is not guaranteed
75         doh.assertTrue(js2JSONRaw({"foo":{"one":[null,"two",2]}}) == '{"foo":{"one":[null,"two",2]}}');
76     },
77     function test_JSON2jsRaw_strict() {
78         // Standalone quoted nulls and booleans are converted to primitives
79         doh.assertTrue(JSON2jsRaw(null) === null);
80         doh.assertTrue(JSON2jsRaw("null") === null);
81         doh.assertTrue(JSON2jsRaw(true) === true);
82         doh.assertTrue(JSON2jsRaw("true") === true);
83         doh.assertTrue(JSON2jsRaw(false) === false);
84         doh.assertTrue(JSON2jsRaw("false") === false);
85     },
86     function test_JSON2jsRaw_numbers() {
87         // Zero is zero and only zero
88         doh.assertTrue(JSON2jsRaw(0) === 0);
89         doh.assertTrue(JSON2jsRaw(1.5) === 1.5);
90         doh.assertTrue(JSON2jsRaw(.5) === .5);
91     },
92     function test_JSON2jsRaw_strings() {
93         // Empty string
94         doh.assertTrue(JSON2jsRaw('""') === "");
95         // String
96         doh.assertTrue(JSON2jsRaw('"foo"') == "foo");
97     },
98     function test_JSON2jsRaw_arrays() {
99         // Array; access an index
100         doh.assertTrue(JSON2jsRaw('[0,1,2,3,4,5]')[1] == 1);
101     },
102     function test_JSON2jsRaw_objects() {
103         // Object; access a key
104         doh.assertTrue(JSON2jsRaw('{"foo":"bar"}').foo == "bar");
105         doh.assertTrue(JSON2jsRaw('{"foo":{"two":2,"one":null}}').foo.one === null);
106         doh.assertTrue(JSON2jsRaw('{"foo":{"two":2,"one":"null"}}').foo.one === "null");
107     },
108     function test_JSON2js_strict() {
109         // Standalone quoted nulls and booleans are converted to primitives
110         doh.assertTrue(JSON2js(null) === null);
111         doh.assertTrue(JSON2js("null") === null);
112         doh.assertTrue(JSON2js(true) === true);
113         doh.assertTrue(JSON2js("true") === true);
114         doh.assertTrue(JSON2js(false) === false);
115         doh.assertTrue(JSON2js("false") === false);
116     },
117     function test_JSON2js_numbers() {
118         // Zero is zero and only zero
119         doh.assertTrue(JSON2js(0) === 0);
120         doh.assertTrue(JSON2js(1.5) === 1.5);
121         doh.assertTrue(JSON2js(.5) === .5);
122     },
123     function test_JSON2js_strings() {
124         // Empty string
125         doh.assertTrue(JSON2js('""') === "");
126         // String
127         doh.assertTrue(JSON2js('"foo"') == "foo");
128     },
129     function test_JSON2js_arrays() {
130         // Array; access an index
131         doh.assertTrue(JSON2js('[0,1,2,3,4,5]')[1] == 1);
132     },
133     function test_JSON2js_objects() {
134         // Object; access a key
135         doh.assertTrue(JSON2js('{"foo":"bar"}').foo == "bar");
136         doh.assertTrue(JSON2js('{"foo":{"two":2,"one":null}}').foo.one === null);
137         doh.assertTrue(JSON2js('{"foo":{"two":2,"one":"null"}}').foo.one === "null");
138     }
139 ]);