summaryrefslogtreecommitdiffstats
path: root/test/json_test.js
blob: 50521431f87f3b37af40829609ddfe266d3779e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Asserts that an object can be encoded to an expected string.
 */
new sjcl.test.TestCase("JSON Encode Test", function (cb) {
	if(!sjcl.json) {
		this.unimplemented();
		cb && cb();
		return;
	}

	//Build up a standard object for encoding, this includes a nice wide variety of properties.
	var obj = new Object();
	obj.int = 4;
	obj.nint = -5;
	obj.str = 'string';
	obj.iv = [ -95577995, -949876189, 1443400017, 697058741 ];
	obj.truth = true;
	obj.lie = false;

	try {
		var str = sjcl.json.encode(obj);
		this.require(!(!str)); //Check for non-'falsey'
	}
	catch (e) {
		//The standard object should encode just fine, so this is out of place. Fail.
		this.fail(e);
	}
	cb && cb();
});


/*
 * Asserts that a JSON string can be decoded to an expected object.
 */
new sjcl.test.TestCase("JSON Decode Test", function (cb) {
	if(!sjcl.json) {
		this.unimplemented();
		cb && cb();
		return;
	}
	
	var str = ''; var i;
	str = '{"int":4,"nint":-5,"str":"string","iv":"/////wAAAAAAAAABAAAAAg==","truth":true,"lie":false}';

	try { 
		var obj = sjcl.json.decode(str);
		this.require(obj.int === 4);
		this.require(obj.nint === -5);
		this.require(obj.str === 'string');
		this.require(obj.truth === true);
		this.require(obj.lie === false);
		for(i in obj.iv) {
			this.require(obj.iv[i] == (i-1)); //Array in iv is [-1,0,1,2]
		}
	} catch (e) { this.fail(e); }

	str = '{ "int" : 4,  "nint"  :  -5,"str":"string",  "iv":  "/////wAAAAAAAAABAAAAAg==","truth": true,"lie":  false  }';
	try { 
		var obj = sjcl.json.decode(str);
		this.require(obj.int === 4);
		this.require(obj.nint === -5);
		this.require(obj.str === 'string');
		this.require(obj.truth === true);
		this.require(obj.lie === false);
		for(i in obj.iv) {
			this.require(obj.iv[i] == (i-1)); //Array in iv is [-1,0,1,2]
		}
	} catch (e) { this.fail(e); }
	
	//Tests passed, return.
	cb && cb();
});


/*
 * Asserts that an Object can be Encoded to a string that can be decoded to an equivalent object
 * as well as the converse.
 */
new sjcl.test.TestCase("JSON Commutative Test", function (cb) {
	if(!sjcl.json) {
		this.unimplemented();
		cb && cb();
		return;
	}

	var obj1 = new Object();
	obj1.int = 4;
	obj1.nint = -5;
	obj1.str = 'string';
	obj1.iv = [ -95577995, -949876189, 1443400017, 697058741 ];
	obj1.truth = true;
	obj1.lie = false;

	var str1 = '';
	var str2 = '';
	var obj2;
	try {
		str1 = sjcl.json.encode(obj1);
		obj2 = sjcl.json.decode(str1);
		str2 = sjcl.json.encode(obj2);
	}
	catch (e) {
		this.fail(e);
	}

	try {
		this.require(str1 === str2);
		this.require(obj1.int == obj2.int);
		this.require(obj1.str == obj2.str);
		this.require(obj1.lie == obj2.lie);
		this.require(obj1.nint == obj2.nint);
		this.require(obj1.truth == obj2.truth);

		var i;
		for(i in obj1.iv)
		{
			this.require(obj1.iv[i] == obj2.iv[i]);
		}
	}
	catch (e) {
		this.fail(e);
	}

	//Tests passed.
	cb && cb();
});