From f11a647bb72d98f030d10b8b3e0301a509e34175 Mon Sep 17 00:00:00 2001 From: Leon van Kammen Date: Thu, 23 Nov 2023 11:25:52 +0100 Subject: [PATCH] hardened filter tests --- src/Test.hx | 4 ++-- src/spec/filter.selectors.json | 20 ++++++++++++-------- src/xrfragment/Filter.hx | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Test.hx b/src/Test.hx index e167e15..7878d72 100644 --- a/src/Test.hx +++ b/src/Test.hx @@ -31,7 +31,7 @@ class Test { var valid:Bool = false; var item:Dynamic = spec[i]; f = new Filter(item.data); - res = URI.parse(item.data,0); + 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] ); @@ -45,7 +45,7 @@ class Test { 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 = item.expect.out == f.get()[ item.expect.input[0] ].root; + if( item.expect.fn == "testFilterRoot" ) valid = res.exists(item.expect.input[0]) && res.get(item.expect.input[0]).filter.get().root == 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; diff --git a/src/spec/filter.selectors.json b/src/spec/filter.selectors.json index 053908f..5da1d62 100644 --- a/src/spec/filter.selectors.json +++ b/src/spec/filter.selectors.json @@ -2,12 +2,16 @@ {"fn":"url","data":"http://foo.com?foo=1#foo*&-sometag&-someid&myid", "expect":{ "fn":"testParsed", "input":"myid","out":true},"label":"myid exists"}, {"fn":"url","data":"http://foo.com?foo=1#tag=bar", "expect":{ "fn":"testParsed", "input":"tag", "out":true},"label":"tag exists"}, {"fn":"url","data":"http://foo.com?foo=1#-tag=bar", "expect":{ "fn":"testParsed", "input":"tag", "out":true},"label":"tag exists"}, - {"fn":"url","data":"http://foo.com?foo=1#price=>2", "expect":{ "fn":"testParsed", "input":"price","out":true},"label":"query test"}, - {"fn":"query","data":"tag=bar", "expect":{ "fn":"testProperty","input":["tag","bar"],"out":true}}, - {"fn":"query","data":"-tag=foo", "expect":{ "fn":"testProperty","input":["tag","foo"],"out":false}}, - {"fn":"query","data":"-tag*=foo", "expect":{ "fn":"testProperty","input":["tag","foo"],"out":false}}, - {"fn":"query","data":"-tag=>2", "expect":{ "fn":"testProperty","input":["tag","3"],"out":false}}, - {"fn":"query","data":"price=>2", "expect":{ "fn":"testProperty","input":["price","1"],"out":false}}, - {"fn":"query","data":"price=<2", "expect":{ "fn":"testProperty","input":["price","5"],"out":false}}, - {"fn":"query","data":"price=<2", "expect":{ "fn":"testProperty","input":["price","1"],"out":true}} + {"fn":"url","data":"http://foo.com?foo=1#price=>2", "expect":{ "fn":"testParsed", "input":"price","out":true},"label":"filter test"}, + {"fn":"filter","data":"tag=bar", "expect":{ "fn":"testProperty","input":["tag","bar"],"out":true}}, + {"fn":"filter","data":"-tag=foo", "expect":{ "fn":"testProperty","input":["tag","foo"],"out":false}}, + {"fn":"filter","data":"-tag*=foo", "expect":{ "fn":"testProperty","input":["tag","foo"],"out":false}}, + {"fn":"filter","data":"-tag=>2", "expect":{ "fn":"testProperty","input":["tag","3"],"out":false}}, + {"fn":"filter","data":"price=>2", "expect":{ "fn":"testProperty","input":["price","1"],"out":false}}, + {"fn":"filter","data":"price=<2", "expect":{ "fn":"testProperty","input":["price","5"],"out":false}}, + {"fn":"filter","data":"price=<2", "expect":{ "fn":"testProperty","input":["price","1"],"out":true}}, + {"fn":"url","data":"#/foo", "expect":{ "fn":"testFilterRoot","input":["foo"],"out":true},"label":"foo should be root-only"}, + {"fn":"url","data":"#/foo&foo","expect":{ "fn":"testFilterRoot","input":["foo"],"out":false},"label":"foo should recursively selected"}, + {"fn":"url","data":"#/foo&foo&/bar", "expect":{ "fn":"testFilterRoot","input":["foo"],"out":false},"label":"bar should be root-only"}, + {"fn":"url","data":"#-/foo", "expect":{ "fn":"testFilterRoot","input":["foo"],"out":true},"label":"foo should be root-only"} ] diff --git a/src/xrfragment/Filter.hx b/src/xrfragment/Filter.hx index 5eb62f0..1094987 100644 --- a/src/xrfragment/Filter.hx +++ b/src/xrfragment/Filter.hx @@ -98,7 +98,7 @@ class Filter { else rule[oper] = v; q.set('expr',rule); }else{ // 1. ELSE we are dealing with an object - q.set("root", isRoot.match(str) ? true : false ); // 1. and we set `root` to `true` or `false` (true=`/` root selector is present) + q.set("root", isRoot.match(str) ? true : false ); // 1. and we set `root` to `true` or `false` (true=`/` root selector is present) } q.set("show", isExclude.match(str) ? false : true ); // 1. therefore we we set `show` to `true` or `false` (false=excluder `-`) q.set("key", operators.replace(k,'') );