From 774f7fc7ed8887f180d722c544122349d65370f8 Mon Sep 17 00:00:00 2001 From: Leon van Kammen Date: Thu, 1 Feb 2024 15:16:16 +0000 Subject: [PATCH] updated spec with media fragment extension --- src/Test.hx | 50 +++++++++++++++++++++++++++---------------- src/spec/t.json | 4 +++- src/xrfragment/XRF.hx | 6 +++--- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/Test.hx b/src/Test.hx index f77962f..277ba55 100644 --- a/src/Test.hx +++ b/src/Test.hx @@ -15,10 +15,10 @@ class Test { static var errors:Int = 0; static public function main():Void { - test( "url.json", Spec.load("src/spec/url.json") ); - test( "pos.json", Spec.load("src/spec/pos.json") ); + //test( "url.json", Spec.load("src/spec/url.json") ); + //test( "pos.json", Spec.load("src/spec/pos.json") ); test( "t.json", Spec.load("src/spec/t.json") ); - test( "filter.selectors.json", Spec.load("src/spec/filter.selectors.json") ); + //test( "filter.selectors.json", Spec.load("src/spec/filter.selectors.json") ); //test( Spec.load("src/spec/tmp.json") ); if( errors > 1 ) trace("\n-----\n[ ❌] "+errors+" errors :/"); } @@ -33,21 +33,23 @@ class Test { var item:Dynamic = spec[i]; f = new Filter(item.data); 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); - 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; + 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); + if( item.expect.fn == "equal.mediafragment" ) valid = equalMediaFragment(res,item); + if( item.expect.fn == "equal.mediafragmentSpd") valid = equalMediaFragmentSpd(res,item); + 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; var ok:String = valid ? "[ ✔ ] " : "[ ❌] "; trace( ok + item.fn + ": '" + item.data + "'" + (item.label ? " (" + (item.label?item.label:item.expect.fn) +")" : "")); if( !valid ) errors += 1; @@ -69,6 +71,18 @@ class Test { 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)); } + static public function equalMediaFragment(res:haxe.DynamicAccess, item:Dynamic):Bool { + if( !item.expect.out && !res.get(item.expect.input) ) return true; + else return res.get('t').floats[ Std.parseInt(item.expect.input) ] == Std.parseInt(item.expect.out); + } + + static public function equalMediaFragmentSpd(res:haxe.DynamicAccess, item:Dynamic):Bool { + if( !item.expect.out && !res.get(item.expect.input) ) return true; + else{ + return res.get('t').speed[ Std.parseInt(item.expect.input) ] == Std.parseFloat(item.expect.out); + } + } + static public function testUrl():Void { var Uri = xrfragment.URI; var url:String = "http://foo.com?foo=1#bar=flop&a=1,2&b=c|d|1,2,3"; diff --git a/src/spec/t.json b/src/spec/t.json index 3489795..4a4accd 100644 --- a/src/spec/t.json +++ b/src/spec/t.json @@ -6,5 +6,7 @@ {"fn":"url","data":"http://foo.com?foo=1#t=1,2,3", "expect":{ "fn":"equal.xyz", "input":"t","out":"1,2,3"},"label":"a equal.xyz"}, {"fn":"url","data":"http://foo.com?foo=1#t=1,-2,3", "expect":{ "fn":"equal.xyz", "input":"t","out":"1,-2,3"},"label":"a equal.xyz"}, {"fn":"url","data":"http://foo.com?foo=1#t=1,100", "expect":{ "fn":"equal.xy", "input":"t","out":"1,100"},"label":"a equal.xy"}, - {"fn":"url","data":"http://foo.com?foo=1#t=2,500", "expect":{ "fn":"testBrowserOverride", "input":"t","out":true},"label":"browser URI can override t (defined in asset)"} + {"fn":"url","data":"http://foo.com?foo=1#t=2,500", "expect":{ "fn":"testBrowserOverride", "input":"t","out":true},"label":"browser URI can override t (defined in asset)"}, + {"fn":"url","data":"http://foo.com?foo=1#t=1,100,400,500*1.2,2.3", "expect":{ "fn":"equal.mediafragment", "input":"3","out":"500"},"label":"a equal.mediafragment"}, + {"fn":"url","data":"http://foo.com?foo=1#t=1,100,400,500*1.2,2.3", "expect":{ "fn":"equal.mediafragmentSpd", "input":"1","out":"2.3"},"label":"a equal.mediafragmentSpeed"} ] diff --git a/src/xrfragment/XRF.hx b/src/xrfragment/XRF.hx index 540ae24..a1e149d 100644 --- a/src/xrfragment/XRF.hx +++ b/src/xrfragment/XRF.hx @@ -105,7 +105,7 @@ class XRF { if( xyzn.length > 2 ) v.z = Std.parseFloat(xyzn[2]); // for( i in 0...xyzn.length ){ v.floats.push( Std.parseFloat(xyzn[i]) ); - } + } } // > the xrfragment specification should stay simple enough // > for anyone to write a parser using either regexes or grammar/lexers if( isColor.match(str) ) v.color = str; // > therefore expressions/comprehensions are not supported (max wildcard/comparison operators for queries e.g.) @@ -120,8 +120,8 @@ class XRF { if( isMediaFrag.match(str) ){ var speed:Array = str.split("*"); v.speed = new Array(); - if( speed.length > 0 ){ - var values:Array = str.split(","); + if( speed.length > 1 ){ + var values:Array = speed[1].split(","); for( i in 0...values.length ){ v.speed.push( Std.parseFloat(values[i]) ); }