Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions tests/tests/swfs/from_avmplus/ecma3/JSON/AS3Types/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package {
import flash.display.MovieClip;
public class Test extends MovieClip {}
}


import avmplus.*;
import flash.utils.*;
import com.adobe.test.Assert;

// var SECTION = "15.2";
// var VERSION = "ECMA_5";
// var TITLE = "JSON AS3 specific types";



function removeExceptionDetail(s:String) {
var fnd=s.indexOf(" ");
if (fnd>-1) {
if (s.indexOf(':',fnd)>-1) {
s=s.substring(0,s.indexOf(':',fnd));
}
}
return s;
}

function sortObject(o:Object) {
var keys=[];
var key;
for ( key in o ) {
if (o[key]===undefined) {
continue;
}
keys[keys.length]=key;
}
keys.sort();
var ret="{";
var value;
for (var i in keys) {
key=keys[i];
value=o[key];
if (value is String) {
value='"'+value+'"';
} else if (value is Array) {
value='['+value+']';
} else if (value is Object) {
}
ret += '"'+key+'":'+value+",";
}
ret=ret.substring(0,ret.length-1);
ret+="}";
return ret;
}

// Vector tests

Assert.expectEq("Vectors are stringified to JSON Array syntax",'[1,"2",true,4,"a"]',JSON.stringify(Vector.<*>([1,"2",true,4.0,"a"])));
Assert.expectEq("Vectors of int are stringified to JSON Array syntax",'[1,-2,3,-4]',JSON.stringify(Vector.<int>([1,-2,3,-4])));
Assert.expectEq("Vectors of uint are stringified to JSON Array syntax",'[1,2,3,4]',JSON.stringify(Vector.<uint>([1,2,3,4])));
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])));
Assert.expectEq("Vectors of Boolean are stringified to JSON Array syntax",'[true,false,true,false]',JSON.stringify(Vector.<Boolean>([true,false,true,false])));
Assert.expectEq("uninitialized Vector is stringified to []","[]",JSON.stringify(new Vector.<*>()));
Assert.expectEq("Vector of edge case values",'[null,null,null,null,""]',JSON.stringify(Vector.<*>([null,undefined,-Infinity,NaN,""])));

// Array tests
Assert.expectEq("Arrays are parsed correctly, empty []","",JSON.parse("[]").toString());
Assert.expectEq("Arrays are parsed correctly, [1]","1",JSON.parse("[1]").toString());
Assert.expectEq("Arrays are parsed correctly, [1,2,3]","1,2,3",JSON.parse("[1,2,3]").toString());

JSONSyntaxError="SyntaxError: Error #1132";

// crashes
exception1="no exception";
try {
JSON.parse("[");
} catch(e) {
exception1=removeExceptionDetail(e.toString());
}
Assert.expectEq("Arrays errors are detected, [",JSONSyntaxError,exception1);

exception2="no exception";
try {
JSON.parse("[1,2,3,fals]");
} catch(e) {
exception2=removeExceptionDetail(e.toString());
}
Assert.expectEq("Arrays errors are detected, [1,2,3,fals]",JSONSyntaxError,exception2);

exception3="no exception";
try {
JSON.parse("[1,2,]");
} catch(e) {
exception3=removeExceptionDetail(e.toString());
}
Assert.expectEq("Arrays errors are detected, [1,2,]",JSONSyntaxError,exception3);

exception4="no exception";
try {
JSON.parse("[1,2,3");
} catch(e) {
exception4=removeExceptionDetail(e.toString());
}
Assert.expectEq("Arrays errors are detected, [1,2,3",JSONSyntaxError,exception4);

var star:*="anytype";
Assert.expectEq("Type * is like an untyped variable",'"anytype"',JSON.stringify(star));


// Dictionary tests
var d:Dictionary=new Dictionary();

class Foo extends Object {
static var timecounter = 1;
public var timestamp;
var name;
function Foo(name) { this.name = name; this.timestamp = timecounter++; }
public function toString():String { return this.name; }
}
var o1 = new Foo("o");
var p2 = new Foo("p");
var o3 = new Foo("o");
d[o3]="value";
Assert.expectEq("stringify a Dictionary object",'"Dictionary"',JSON.stringify(d));

// Change toJSON to show object values, where distinct objects
// with identical toString output are distinguished (if possible)
// by the timestamps they were constructed with.
var origDictionarytoJSON=Dictionary.prototype.toJSON;
Dictionary.prototype.toJSON=function():* {
var x = {};
for (var i in this) {
if ("timestamp" in i) {
x[String(i)+"_"+i.timestamp] = this[i];
} else {
x[String(i)] = this[i];
}
}
return x; // x is not a String!
};

// The simple case: a single element Dictionary won't run into
// issues of unspecified order of rendered key/value entries.
Assert.expectEq("stringify a 1-elem Dictionary with customized toJSON",
'{"o_3":"value"}',
JSON.stringify(d));

d[o1]="o1-value";
d[p2]="p2-value";
d[o3]="o3-value";

// Trickier case: multi-element Dictionary may present entries in
// arbitrary order; normalize it by reparsing and sorting entries.
Assert.expectEq("stringify a 3-elem Dictionary with customized toJSON",
'{"o_1":"o1-value","o_3":"o3-value","p_2":"p2-value"}',
sortObject(JSON.parse(JSON.stringify(d))));

// Restore original toJSON and its trivial behavior.
Dictionary.prototype.toJSON=origDictionarytoJSON;
Assert.expectEq("stringify a Dictionary restored original toJSON",'"Dictionary"',JSON.stringify(d));

// ByteArray tests
var b:ByteArray=new ByteArray();
b.writeUTF("byte array string");
Assert.expectEq("stringify a ByteArray object",'"ByteArray"',JSON.stringify(b));

var origByteArraytoJSON=ByteArray.prototype.toJSON;
ByteArray.prototype.toJSON=function() {
return this.toString().substring(2);
}
Assert.expectEq("stringify a ByteArray object with custom toJSON",'"byte array string"',JSON.stringify(b));
ByteArray.prototype.toJSON=origByteArraytoJSON;
Assert.expectEq("stringify a ByteArray object with restored toJSON",'"ByteArray"',JSON.stringify(b));

// XML

var x:XML=<root><element1 prop1="one"/></root>;
Assert.expectEq("stringify XML object",'"XML"',JSON.stringify(x));

// Date
var dt:Date=new Date(2011,3,26,10,33,0,111);
Assert.expectEq("stringify a Date object",true,JSON.stringify(dt).indexOf('"Tue Apr 26 10:33:00')>-1);
var origDatetoJSON=Date.prototype.toJSON;
Date.prototype.toJSON=function() {
return ""+this.getFullYear()+"-"+(this.getMonth()+1)+"-"+this.getDate()+"T"+this.getHours()+":"+this.getMinutes()+":"+this.getSeconds()+"."+this.getMilliseconds()+"Z";
}
Assert.expectEq("stringify a Date object with custom toJSON",'"2011-4-26T10:33:0.111Z"',JSON.stringify(dt));
Date.prototype.toJSON=origDatetoJSON;
Assert.expectEq("stringify a Date object with restored toJSON",true,JSON.stringify(dt).indexOf('"Tue Apr 26 10:33:00')>-1);


13 changes: 13 additions & 0 deletions tests/tests/swfs/from_avmplus/ecma3/JSON/AS3Types/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<flex-config>
<compiler>
<source-path>
<path-element>.</path-element>
<path-element>../../../lib</path-element>
</source-path>
<debug>false</debug>
<omit-trace-statements>false</omit-trace-statements>
<show-actionscript-warnings>false</show-actionscript-warnings>
<strict>false</strict>
</compiler>
<output>test.swf</output>
</flex-config>
26 changes: 26 additions & 0 deletions tests/tests/swfs/from_avmplus/ecma3/JSON/AS3Types/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Vectors are stringified to JSON Array syntax PASSED!
Vectors of int are stringified to JSON Array syntax PASSED!
Vectors of uint are stringified to JSON Array syntax PASSED!
Vectors of Number are stringified to JSON Array syntax PASSED!
Vectors of Boolean are stringified to JSON Array syntax PASSED!
uninitialized Vector is stringified to [] PASSED!
Vector of edge case values PASSED!
Arrays are parsed correctly, empty [] PASSED!
Arrays are parsed correctly, [1] PASSED!
Arrays are parsed correctly, [1,2,3] PASSED!
Arrays errors are detected, [ PASSED!
Arrays errors are detected, [1,2,3,fals] PASSED!
Arrays errors are detected, [1,2,] PASSED!
Arrays errors are detected, [1,2,3 PASSED!
Type * is like an untyped variable PASSED!
stringify a Dictionary object PASSED!
stringify a 1-elem Dictionary with customized toJSON PASSED!
stringify a 3-elem Dictionary with customized toJSON PASSED!
stringify a Dictionary restored original toJSON PASSED!
stringify a ByteArray object PASSED!
stringify a ByteArray object with custom toJSON PASSED!
stringify a ByteArray object with restored toJSON PASSED!
stringify XML object PASSED!
stringify a Date object PASSED!
stringify a Date object with custom toJSON PASSED!
stringify a Date object with restored toJSON PASSED!
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/tests/swfs/from_avmplus/ecma3/JSON/AS3Types/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
num_ticks = 1
known_failure = true
83 changes: 83 additions & 0 deletions tests/tests/swfs/from_avmplus/ecma3/JSON/Callbacks/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package {
import flash.display.MovieClip;
public class Test extends MovieClip {}
}

import com.adobe.test.Assert;

//package {
// var SECTION = "15.2";
// var VERSION = "ECMA_5";
// var TITLE = "JSON AS3 Callback tests";



function removeExceptionDetail(s:String) {
var fnd=s.indexOf(" ");
if (fnd>-1) {
if (s.indexOf(':',fnd)>-1) {
s=s.substring(0,s.indexOf(':',fnd));
}
}
return s;
}

function sortObject(o:Object) {
var keys=[];
var key;
for ( key in o ) {
if (o[key]===undefined) {
continue;
}
keys[keys.length]=key;
}
keys.sort();
var ret="{";
var value;
for (var i in keys) {
key=keys[i];
value=o[key];
if (value is String) {
value='"'+value+'"';
} else if (value is Array) {
value='['+value+']';
} else if (value is Object) {
}
ret += '"'+key+'":'+value+",";
}
ret=ret.substring(0,ret.length-1);
ret+="}";
return ret;
}


Assert.expectEq('simple reviver',true,JSON.parse('[1,2,3]',function f() { return true; }));

var keys=[];
JSON.parse('[1,2,3]',function f(key,value) { keys[keys.length]=key; });
keys.sort();
Assert.expectEq('simple reviver test keys',',0,1,2',keys.toString());

var values=[];
JSON.parse('[1,2,3]',function f(key,value) { values[values.length]=value; });
values.sort();
Assert.expectEq('simple reviver test values',",,,1,2,3",values.toString());

var keys2=[];
JSON.parse('{"a":1,"b":2,"c":3}',function f(key,value) { keys2[keys2.length]=key; });
keys2.sort();
Assert.expectEq('simple reviver test keys on Object',',a,b,c',keys2.toString());

var values2=[];
JSON.parse('{"a":1,"b":2,"c":3}',function f(key,value) { values2[values2.length]=value; });
values2.sort();
Assert.expectEq('simple reviver test values on Object',"1,2,3,[object Object]",values2.toString());

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());

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; })));

//}
13 changes: 13 additions & 0 deletions tests/tests/swfs/from_avmplus/ecma3/JSON/Callbacks/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<flex-config>
<compiler>
<source-path>
<path-element>.</path-element>
<path-element>../../../lib</path-element>
</source-path>
<debug>false</debug>
<omit-trace-statements>false</omit-trace-statements>
<show-actionscript-warnings>false</show-actionscript-warnings>
<strict>false</strict>
</compiler>
<output>test.swf</output>
</flex-config>
7 changes: 7 additions & 0 deletions tests/tests/swfs/from_avmplus/ecma3/JSON/Callbacks/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
simple reviver PASSED!
simple reviver test keys PASSED!
simple reviver test values PASSED!
simple reviver test keys on Object PASSED!
simple reviver test values on Object PASSED!
simple reviver replaces values PASSED!
simple reviver undefined removes values PASSED!
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
num_ticks = 1
Loading