In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

WonderflAPI Test

Test for http://github.com/mash/WonderflUtils

net.wonderfl.data.*;
net.wonderfl.utils.WonderflAPI

is written by http://wonderfl.net/user/bkzen
Get Adobe Flash player
by 9re 10 Sep 2010
/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/pFQB
 */

package {
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.text.TextField;
    import flash.display.Sprite;
    import net.wonderfl.data.*;
    import net.wonderfl.utils.WonderflAPI;
    /**
     * @author Taro KOBAYASHI
     */
    public class WonderflAPITest extends Sprite {
        public function WonderflAPITest() {
            tf = new TextField;
            tf.wordWrap = true;
            tf.width = tf.height = 465;
            addChild(tf);
            
            var params:Object = loaderInfo.parameters;
            var api:WonderflAPI = new WonderflAPI(params);
            
            testSuit(
                new ApiTest(api.apiCode(), APICodeData),
                new ApiTest(api.apiForks(), APIForksData),
                new ApiTest(api.apiUser(), APIUserData),
                new ApiTest(api.apiUserCodes(), APIUserCodesData),
                new TestComplete
            );
        }
    }
}
import com.adobe.serialization.json.JSON;
import flash.events.Event;
import flash.net.*;
import flash.text.TextField;

var tf:TextField;
function testSuit(...tests:Array):void {
    for (var i:int = 0; i < tests.length - 1; ++i) 
        tests[i].next = tests[i + 1];
        
    tests[0].exec();
}

function trace(...o):void {
    tf.appendText(o + "\n");
    tf.scrollV = tf.maxScrollV;
}

function assertAHasB(a:Object, b:Object):void {
    var value:*;
    for (var key:String in b) {
        value = a[key];
        if (typeof value == 'object') {
            assertAHasB(value, b[key]);
        } else {
            if (value != b[key]) throw new Error('doesnot match ' + key);
            trace(key + ' matched');
        }
    }
}

class Test {
    protected var _exec:Function;
    public function Test($exec:Function):void {
        _exec = $exec;
    }
    public function exec():void {
        _exec();
    }
    public var next:Test;
}

class TestComplete extends Test {
    public function TestComplete() {
        super(function testComplete():void {
            trace('------------------------------');
            trace('all tests passed. ok');
        });
    }
}

class ApiTest extends Test {
    public function ApiTest(apiRequest:URLRequest, resultClass:Class) {
        var ldr:URLLoader = new URLLoader;
        ldr.addEventListener(Event.COMPLETE, function _complete(e:*):void {
            ldr.removeEventListener(Event.COMPLETE, _complete);
            var rawData:Object = JSON.decode(ldr.data);
            
            assertAHasB(new resultClass(rawData), rawData);
            
            trace(resultClass + ' test ok');
            trace();
            
            if (next != null) next.exec();
        });
        super(function ():void {
            trace('------------------------------');
            trace('loading data from ... ' + apiRequest.url);
            ldr.load(apiRequest);
        });
    }
}