xrfragment/src/Test.hx

108 lines
6 KiB
Haxe
Raw Normal View History

import xrfragment.Filter;
2023-03-31 14:47:54 +02:00
import xrfragment.URI;
2023-04-14 17:37:33 +02:00
import xrfragment.XRF;
2023-03-09 19:58:08 +01:00
2023-03-17 16:06:24 +01:00
class Spec {
macro public static function load(path : String) {
var value = sys.io.File.getContent(path),
json = haxe.Json.parse(value);
return macro $v{json};
}
}
2023-03-09 19:58:08 +01:00
class Test {
static var errors:Int = 0;
2023-03-09 19:58:08 +01:00
static public function main():Void {
2024-02-02 11:13:07 +00:00
test( "url.json", Spec.load("src/spec/url.json") );
test( "pos.json", Spec.load("src/spec/pos.json") );
2023-10-30 16:15:08 +01:00
test( "t.json", Spec.load("src/spec/t.json") );
2024-02-08 19:40:43 +01:00
test( "xywh.json", Spec.load("src/spec/xywh.json") );
2024-02-05 14:11:29 +01:00
test( "s.json", Spec.load("src/spec/s.json") );
2024-02-08 19:40:43 +01:00
test( "suv.json", Spec.load("src/spec/suv.json") );
test( "suv.json", Spec.load("src/spec/uv.json") );
2024-02-02 11:13:07 +00:00
test( "filter.selectors.json", Spec.load("src/spec/filter.selectors.json") );
2023-03-21 17:57:54 +01:00
//test( Spec.load("src/spec/tmp.json") );
if( errors > 1 ) trace("\n-----\n[ ] "+errors+" errors :/");
2023-03-10 18:49:16 +01:00
}
2023-03-09 19:58:08 +01:00
2023-10-30 16:15:08 +01:00
static public function test( topic:String, spec:Array<Dynamic>):Void {
2024-02-08 19:40:43 +01:00
trace("\n[ . ] running "+topic);
var Filter = xrfragment.Filter;
2023-03-17 16:06:24 +01:00
for( i in 0...spec.length ){
var f:Filter = null;
2023-03-17 16:06:24 +01:00
var res:haxe.DynamicAccess<Dynamic> = null;
var valid:Bool = false;
var item:Dynamic = spec[i];
f = new Filter(item.data);
2023-11-23 11:25:52 +01:00
res = URI.parse(item.data,null);
if( item.expect.fn == "test" ) valid = item.expect.out == f.test( item.expect.input[0] );
if( item.expect.fn == "testProperty" ) valid = item.expect.out == f.testProperty( item.expect.input[0], item.expect.input[1] );
if( item.expect.fn == "testPropertyInt" ) valid = item.expect.out == f.testProperty( item.expect.input[0], item.expect.input[1] );
if( item.expect.fn == "testPropertyExclude" ) valid = item.expect.out == f.testProperty( item.expect.input[0], item.expect.input[1], true );
if( item.expect.fn == "testParsed" ) valid = item.expect.out == res.exists(item.expect.input);
if( item.expect.fn == "testPredefinedView" ) valid = res.exists(item.expect.input) && item.expect.out == res.get(item.expect.input).is( XRF.PV_EXECUTE) ;
if( item.expect.fn == "testPropertyAssign" ) valid = res.exists(item.expect.input) && item.expect.out == res.get(item.expect.input).is( XRF.PROP_BIND) ;
if( item.expect.fn == "testBrowserOverride" ) valid = item.expect.out == (URI.parse(item.data,XRF.NAVIGATOR)).exists(item.expect.input);
if( item.expect.fn == "testEmbedOverride" ) valid = item.expect.out == (URI.parse(item.data,XRF.METADATA)).exists(item.expect.input);
if( item.expect.fn == "equal.string" ) valid = res.get(item.expect.input) && item.expect.out == res.get(item.expect.input).string;
if( item.expect.fn == "equal.x" ) valid = equalX(res,item);
if( item.expect.fn == "equal.xy" ) valid = equalXY(res,item);
if( item.expect.fn == "equal.xyz" ) valid = equalXYZ(res,item);
2024-02-02 11:13:07 +00:00
if( item.expect.fn == "equal.mediafragmentT" ) valid = equalMediaFragment(res,item,"t");
if( item.expect.fn == "equal.mediafragmentXYWH") valid = equalMediaFragment(res,item,"xywh");
2024-02-05 14:11:29 +01:00
if( item.expect.fn == "equal.mediafragmentS") valid = equalMediaFragment(res,item,"s");
2024-02-08 19:40:43 +01:00
if( item.expect.fn == "equal.mediafragmentSUV") valid = equalMediaFragment(res,item,"suv");
if( item.expect.fn == "testFilterRoot" ) valid = res.exists(item.expect.input[0]) && res.get(item.expect.input[0]).filter.get().root == item.expect.out;
if( item.expect.fn == "testFilterDeep" ) valid = res.exists(item.expect.input[0]) && res.get(item.expect.input[0]).filter.get().deep == item.expect.out;
2023-03-17 16:06:24 +01:00
var ok:String = valid ? "[ ] " : "[ ] ";
trace( ok + item.fn + ": '" + item.data + "'" + (item.label ? " (" + (item.label?item.label:item.expect.fn) +")" : ""));
2023-03-17 16:06:24 +01:00
if( !valid ) errors += 1;
}
}
2023-10-30 16:15:08 +01:00
static public function equalX(res:haxe.DynamicAccess<Dynamic>, item:Dynamic):Bool {
if( !item.expect.out && !res.get(item.expect.input) ) return true;
else return res.get(item.expect.input) && item.expect.out == (Std.string(res.get(item.expect.input).x) );
}
2023-04-14 15:19:52 +02:00
static public function equalXY(res:haxe.DynamicAccess<Dynamic>, item:Dynamic):Bool {
if( !item.expect.out && !res.get(item.expect.input) ) return true;
else return res.get(item.expect.input) && item.expect.out == (Std.string(res.get(item.expect.input).x) +","+ Std.string(res.get(item.expect.input).y) );
}
static public function equalXYZ(res:haxe.DynamicAccess<Dynamic>, item:Dynamic):Bool {
if( !item.expect.out && !res.get(item.expect.input) ) return true;
else return res.get(item.expect.input) && item.expect.out == (Std.string(res.get(item.expect.input).x) +","+ Std.string(res.get(item.expect.input).y)+","+ Std.string(res.get(item.expect.input).z));
}
2024-02-02 11:13:07 +00:00
static public function equalMediaFragment(res:haxe.DynamicAccess<Dynamic>, item:Dynamic, key:String):Bool {
if( !item.expect.out && !res.get(item.expect.input) ) return true;
else return res.get( key ).floats[ Std.parseInt(item.expect.input) ] == Std.parseFloat(item.expect.out);
}
2023-03-10 18:49:16 +01:00
static public function testUrl():Void {
2023-03-31 14:47:54 +02:00
var Uri = xrfragment.URI;
var url:String = "http://foo.com?foo=1#bar=flop&a=1,2&b=c|d|1,2,3";
trace(url);
trace( Uri.parse(url,0) );
2023-03-10 18:49:16 +01:00
}
static public function testFilter():Void {
var Filter = xrfragment.Filter;
trace( (new Filter("foo or bar")).toObject() );
trace( (new Filter("class:fopoer or bar foo:bar")).toObject().or[0] );
trace( (new Filter("-skybox class:foo")).toObject().or[0] );
trace( (new Filter("foo/flop moo or bar")).toObject().or[0] );
trace( (new Filter("-foo/flop moo or bar")).toObject().or[0] );
trace( (new Filter("price:>4 moo or bar")).toObject().or[0] );
trace( (new Filter("price:>=4 moo or bar")).toObject().or[0] );
trace( (new Filter("price:<=4 moo or bar")).toObject().or[0] );
trace( (new Filter("price:!=4 moo or bar")).toObject().or[0] );
2023-03-09 19:58:08 +01:00
trace("all tests passed");
}
}