Skip to content

Commit ce554ad

Browse files
committed
tests: Port ECMA3 JSON tests
These tests have been adapted from the avmplus repo. See https://github.com/adobe/avmplus/tree/858d034a3bd3a54d9b70909386435cf4aec81d21/test
1 parent f032b6a commit ce554ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2366
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
package {
5+
import flash.display.MovieClip;
6+
public class Test extends MovieClip {}
7+
}
8+
9+
10+
import avmplus.*;
11+
import flash.utils.*;
12+
import com.adobe.test.Assert;
13+
14+
// var SECTION = "15.2";
15+
// var VERSION = "ECMA_5";
16+
// var TITLE = "JSON AS3 specific types";
17+
18+
19+
20+
function removeExceptionDetail(s:String) {
21+
var fnd=s.indexOf(" ");
22+
if (fnd>-1) {
23+
if (s.indexOf(':',fnd)>-1) {
24+
s=s.substring(0,s.indexOf(':',fnd));
25+
}
26+
}
27+
return s;
28+
}
29+
30+
function sortObject(o:Object) {
31+
var keys=[];
32+
var key;
33+
for ( key in o ) {
34+
if (o[key]===undefined) {
35+
continue;
36+
}
37+
keys[keys.length]=key;
38+
}
39+
keys.sort();
40+
var ret="{";
41+
var value;
42+
for (var i in keys) {
43+
key=keys[i];
44+
value=o[key];
45+
if (value is String) {
46+
value='"'+value+'"';
47+
} else if (value is Array) {
48+
value='['+value+']';
49+
} else if (value is Object) {
50+
}
51+
ret += '"'+key+'":'+value+",";
52+
}
53+
ret=ret.substring(0,ret.length-1);
54+
ret+="}";
55+
return ret;
56+
}
57+
58+
// Vector tests
59+
60+
Assert.expectEq("Vectors are stringified to JSON Array syntax",'[1,"2",true,4,"a"]',JSON.stringify(Vector.<*>([1,"2",true,4.0,"a"])));
61+
Assert.expectEq("Vectors of int are stringified to JSON Array syntax",'[1,-2,3,-4]',JSON.stringify(Vector.<int>([1,-2,3,-4])));
62+
Assert.expectEq("Vectors of uint are stringified to JSON Array syntax",'[1,2,3,4]',JSON.stringify(Vector.<uint>([1,2,3,4])));
63+
Assert.expectEq("Vectors of Number are stringified to JSON Array syntax",'[1,2.2,3.33,4.444]',JSON.stringify(Vector.<Number>([1,2.2,3.33,4.444])));
64+
Assert.expectEq("Vectors of Boolean are stringified to JSON Array syntax",'[true,false,true,false]',JSON.stringify(Vector.<Boolean>([true,false,true,false])));
65+
Assert.expectEq("uninitialized Vector is stringified to []","[]",JSON.stringify(new Vector.<*>()));
66+
Assert.expectEq("Vector of edge case values",'[null,null,null,null,""]',JSON.stringify(Vector.<*>([null,undefined,-Infinity,NaN,""])));
67+
68+
// Array tests
69+
Assert.expectEq("Arrays are parsed correctly, empty []","",JSON.parse("[]").toString());
70+
Assert.expectEq("Arrays are parsed correctly, [1]","1",JSON.parse("[1]").toString());
71+
Assert.expectEq("Arrays are parsed correctly, [1,2,3]","1,2,3",JSON.parse("[1,2,3]").toString());
72+
73+
JSONSyntaxError="SyntaxError: Error #1132";
74+
75+
// crashes
76+
exception1="no exception";
77+
try {
78+
JSON.parse("[");
79+
} catch(e) {
80+
exception1=removeExceptionDetail(e.toString());
81+
}
82+
Assert.expectEq("Arrays errors are detected, [",JSONSyntaxError,exception1);
83+
84+
exception2="no exception";
85+
try {
86+
JSON.parse("[1,2,3,fals]");
87+
} catch(e) {
88+
exception2=removeExceptionDetail(e.toString());
89+
}
90+
Assert.expectEq("Arrays errors are detected, [1,2,3,fals]",JSONSyntaxError,exception2);
91+
92+
exception3="no exception";
93+
try {
94+
JSON.parse("[1,2,]");
95+
} catch(e) {
96+
exception3=removeExceptionDetail(e.toString());
97+
}
98+
Assert.expectEq("Arrays errors are detected, [1,2,]",JSONSyntaxError,exception3);
99+
100+
exception4="no exception";
101+
try {
102+
JSON.parse("[1,2,3");
103+
} catch(e) {
104+
exception4=removeExceptionDetail(e.toString());
105+
}
106+
Assert.expectEq("Arrays errors are detected, [1,2,3",JSONSyntaxError,exception4);
107+
108+
var star:*="anytype";
109+
Assert.expectEq("Type * is like an untyped variable",'"anytype"',JSON.stringify(star));
110+
111+
112+
// Dictionary tests
113+
var d:Dictionary=new Dictionary();
114+
115+
class Foo extends Object {
116+
static var timecounter = 1;
117+
public var timestamp;
118+
var name;
119+
function Foo(name) { this.name = name; this.timestamp = timecounter++; }
120+
public function toString():String { return this.name; }
121+
}
122+
var o1 = new Foo("o");
123+
var p2 = new Foo("p");
124+
var o3 = new Foo("o");
125+
d[o3]="value";
126+
Assert.expectEq("stringify a Dictionary object",'"Dictionary"',JSON.stringify(d));
127+
128+
// Change toJSON to show object values, where distinct objects
129+
// with identical toString output are distinguished (if possible)
130+
// by the timestamps they were constructed with.
131+
var origDictionarytoJSON=Dictionary.prototype.toJSON;
132+
Dictionary.prototype.toJSON=function():* {
133+
var x = {};
134+
for (var i in this) {
135+
if ("timestamp" in i) {
136+
x[String(i)+"_"+i.timestamp] = this[i];
137+
} else {
138+
x[String(i)] = this[i];
139+
}
140+
}
141+
return x; // x is not a String!
142+
};
143+
144+
// The simple case: a single element Dictionary won't run into
145+
// issues of unspecified order of rendered key/value entries.
146+
Assert.expectEq("stringify a 1-elem Dictionary with customized toJSON",
147+
'{"o_3":"value"}',
148+
JSON.stringify(d));
149+
150+
d[o1]="o1-value";
151+
d[p2]="p2-value";
152+
d[o3]="o3-value";
153+
154+
// Trickier case: multi-element Dictionary may present entries in
155+
// arbitrary order; normalize it by reparsing and sorting entries.
156+
Assert.expectEq("stringify a 3-elem Dictionary with customized toJSON",
157+
'{"o_1":"o1-value","o_3":"o3-value","p_2":"p2-value"}',
158+
sortObject(JSON.parse(JSON.stringify(d))));
159+
160+
// Restore original toJSON and its trivial behavior.
161+
Dictionary.prototype.toJSON=origDictionarytoJSON;
162+
Assert.expectEq("stringify a Dictionary restored original toJSON",'"Dictionary"',JSON.stringify(d));
163+
164+
// ByteArray tests
165+
var b:ByteArray=new ByteArray();
166+
b.writeUTF("byte array string");
167+
Assert.expectEq("stringify a ByteArray object",'"ByteArray"',JSON.stringify(b));
168+
169+
var origByteArraytoJSON=ByteArray.prototype.toJSON;
170+
ByteArray.prototype.toJSON=function() {
171+
return this.toString().substring(2);
172+
}
173+
Assert.expectEq("stringify a ByteArray object with custom toJSON",'"byte array string"',JSON.stringify(b));
174+
ByteArray.prototype.toJSON=origByteArraytoJSON;
175+
Assert.expectEq("stringify a ByteArray object with restored toJSON",'"ByteArray"',JSON.stringify(b));
176+
177+
// XML
178+
179+
var x:XML=<root><element1 prop1="one"/></root>;
180+
Assert.expectEq("stringify XML object",'"XML"',JSON.stringify(x));
181+
182+
// Date
183+
var dt:Date=new Date(2011,3,26,10,33,0,111);
184+
Assert.expectEq("stringify a Date object",true,JSON.stringify(dt).indexOf('"Tue Apr 26 10:33:00')>-1);
185+
var origDatetoJSON=Date.prototype.toJSON;
186+
Date.prototype.toJSON=function() {
187+
return ""+this.getFullYear()+"-"+(this.getMonth()+1)+"-"+this.getDate()+"T"+this.getHours()+":"+this.getMinutes()+":"+this.getSeconds()+"."+this.getMilliseconds()+"Z";
188+
}
189+
Assert.expectEq("stringify a Date object with custom toJSON",'"2011-4-26T10:33:0.111Z"',JSON.stringify(dt));
190+
Date.prototype.toJSON=origDatetoJSON;
191+
Assert.expectEq("stringify a Date object with restored toJSON",true,JSON.stringify(dt).indexOf('"Tue Apr 26 10:33:00')>-1);
192+
193+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<flex-config>
2+
<compiler>
3+
<source-path>
4+
<path-element>.</path-element>
5+
<path-element>../../../lib</path-element>
6+
</source-path>
7+
<debug>false</debug>
8+
<omit-trace-statements>false</omit-trace-statements>
9+
<show-actionscript-warnings>false</show-actionscript-warnings>
10+
<strict>false</strict>
11+
</compiler>
12+
<output>test.swf</output>
13+
</flex-config>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Vectors are stringified to JSON Array syntax PASSED!
2+
Vectors of int are stringified to JSON Array syntax PASSED!
3+
Vectors of uint are stringified to JSON Array syntax PASSED!
4+
Vectors of Number are stringified to JSON Array syntax PASSED!
5+
Vectors of Boolean are stringified to JSON Array syntax PASSED!
6+
uninitialized Vector is stringified to [] PASSED!
7+
Vector of edge case values PASSED!
8+
Arrays are parsed correctly, empty [] PASSED!
9+
Arrays are parsed correctly, [1] PASSED!
10+
Arrays are parsed correctly, [1,2,3] PASSED!
11+
Arrays errors are detected, [ PASSED!
12+
Arrays errors are detected, [1,2,3,fals] PASSED!
13+
Arrays errors are detected, [1,2,] PASSED!
14+
Arrays errors are detected, [1,2,3 PASSED!
15+
Type * is like an untyped variable PASSED!
16+
stringify a Dictionary object PASSED!
17+
stringify a 1-elem Dictionary with customized toJSON PASSED!
18+
stringify a 3-elem Dictionary with customized toJSON PASSED!
19+
stringify a Dictionary restored original toJSON PASSED!
20+
stringify a ByteArray object PASSED!
21+
stringify a ByteArray object with custom toJSON PASSED!
22+
stringify a ByteArray object with restored toJSON PASSED!
23+
stringify XML object PASSED!
24+
stringify a Date object PASSED!
25+
stringify a Date object with custom toJSON PASSED!
26+
stringify a Date object with restored toJSON PASSED!
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
num_ticks = 1
2+
known_failure = true
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
package {
5+
import flash.display.MovieClip;
6+
public class Test extends MovieClip {}
7+
}
8+
9+
import com.adobe.test.Assert;
10+
11+
//package {
12+
// var SECTION = "15.2";
13+
// var VERSION = "ECMA_5";
14+
// var TITLE = "JSON AS3 Callback tests";
15+
16+
17+
18+
function removeExceptionDetail(s:String) {
19+
var fnd=s.indexOf(" ");
20+
if (fnd>-1) {
21+
if (s.indexOf(':',fnd)>-1) {
22+
s=s.substring(0,s.indexOf(':',fnd));
23+
}
24+
}
25+
return s;
26+
}
27+
28+
function sortObject(o:Object) {
29+
var keys=[];
30+
var key;
31+
for ( key in o ) {
32+
if (o[key]===undefined) {
33+
continue;
34+
}
35+
keys[keys.length]=key;
36+
}
37+
keys.sort();
38+
var ret="{";
39+
var value;
40+
for (var i in keys) {
41+
key=keys[i];
42+
value=o[key];
43+
if (value is String) {
44+
value='"'+value+'"';
45+
} else if (value is Array) {
46+
value='['+value+']';
47+
} else if (value is Object) {
48+
}
49+
ret += '"'+key+'":'+value+",";
50+
}
51+
ret=ret.substring(0,ret.length-1);
52+
ret+="}";
53+
return ret;
54+
}
55+
56+
57+
Assert.expectEq('simple reviver',true,JSON.parse('[1,2,3]',function f() { return true; }));
58+
59+
var keys=[];
60+
JSON.parse('[1,2,3]',function f(key,value) { keys[keys.length]=key; });
61+
keys.sort();
62+
Assert.expectEq('simple reviver test keys',',0,1,2',keys.toString());
63+
64+
var values=[];
65+
JSON.parse('[1,2,3]',function f(key,value) { values[values.length]=value; });
66+
values.sort();
67+
Assert.expectEq('simple reviver test values',",,,1,2,3",values.toString());
68+
69+
var keys2=[];
70+
JSON.parse('{"a":1,"b":2,"c":3}',function f(key,value) { keys2[keys2.length]=key; });
71+
keys2.sort();
72+
Assert.expectEq('simple reviver test keys on Object',',a,b,c',keys2.toString());
73+
74+
var values2=[];
75+
JSON.parse('{"a":1,"b":2,"c":3}',function f(key,value) { values2[values2.length]=value; });
76+
values2.sort();
77+
Assert.expectEq('simple reviver test values on Object',"1,2,3,[object Object]",values2.toString());
78+
79+
Assert.expectEq('simple reviver replaces values','1,0,0,4,5',JSON.parse('[1,2,3,4,5]',function f(key,value) { if (value=='2' || value=='3') return 0; else return value; }).toString());
80+
81+
Assert.expectEq("simple reviver undefined removes values",'{"c":3,"d":4}',sortObject(JSON.parse('{"a":1,"b":2,"c":3,"d":4}',function f(key,value) { if (key=='a' || key=='b') return undefined; else return value; })));
82+
83+
//}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<flex-config>
2+
<compiler>
3+
<source-path>
4+
<path-element>.</path-element>
5+
<path-element>../../../lib</path-element>
6+
</source-path>
7+
<debug>false</debug>
8+
<omit-trace-statements>false</omit-trace-statements>
9+
<show-actionscript-warnings>false</show-actionscript-warnings>
10+
<strict>false</strict>
11+
</compiler>
12+
<output>test.swf</output>
13+
</flex-config>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
simple reviver PASSED!
2+
simple reviver test keys PASSED!
3+
simple reviver test values PASSED!
4+
simple reviver test keys on Object PASSED!
5+
simple reviver test values on Object PASSED!
6+
simple reviver replaces values PASSED!
7+
simple reviver undefined removes values PASSED!
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
num_ticks = 1

0 commit comments

Comments
 (0)