| Index: test/filterClasses.js |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/test/filterClasses.js |
| @@ -0,0 +1,182 @@ |
| +"use strict"; |
| + |
| +let { |
| + Filter, InvalidFilter, CommentFilter, RegExpFilter, WhiteListFilter, |
| + ElemHideFilter, ElemHideException |
| +} = require("../lib/filterClassesNew"); |
| + |
| +Filter.fromText("/asdf??+/"); |
| + |
| +exports.testFromText = function(test) |
| +{ |
| + let tests = [ |
| + ["!asdf", CommentFilter, "comment"], |
| + ["asdf", RegExpFilter, "blocking"], |
| + ["asdf$image,~collapse", RegExpFilter, "blocking"], |
| + ["/asdf/", RegExpFilter, "blocking"], |
| + ["/asdf??+/", InvalidFilter, "invalid"], |
| + ["@@asdf", WhiteListFilter, "whitelist"], |
| + ["@@asdf$image,~collapse", WhiteListFilter, "whitelist"], |
| + ["@@/asdf/", WhiteListFilter, "whitelist"], |
| + ["@@/asdf??+/", InvalidFilter, "invalid"], |
| + ["##asdf", ElemHideFilter, "elemhide"], |
| + ["#@#asdf", ElemHideException, "elemhideexception"], |
| + ["foobar##asdf", ElemHideFilter, "elemhide"], |
| + ["foobar#@#asdf", ElemHideException, "elemhideexception"], |
| + ["foobar##a", ElemHideFilter, "elemhide"], |
| + ["foobar#@#a", ElemHideException, "elemhideexception"], |
| + |
| + ["foobar#asdf", RegExpFilter, "blocking"], |
| + ["foobar|foobas##asdf", RegExpFilter, "blocking"], |
| + ["foobar##asdf{asdf}", RegExpFilter, "blocking"], |
| + ["foobar##", RegExpFilter, "blocking"], |
| + ["foobar#@#", RegExpFilter, "blocking"], |
| + ]; |
| + for (let [text, type, typeName, location] of tests) |
| + { |
| + let filter = Filter.fromText(text); |
| + try |
| + { |
| + test.ok(filter instanceof Filter, "Got filter for " + text); |
| + test.equal(filter.text, text, "Correct filter text for " + text); |
| + test.ok(filter instanceof type, "Correct filter type for " + text); |
| + test.equal(filter.type, typeName, "Type name for " + text + " is " + typeName); |
| + if (type == InvalidFilter) |
| + test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); |
| + } |
| + finally |
| + { |
| + filter.delete(); |
| + } |
| + } |
| + test.done(); |
| +}; |
| + |
| +exports.testNormalize = function(test) |
| +{ |
| + let tests = [ |
| + [" foo bar ", "foobar"], |
| + ["foobar", "foobar"], |
| + [" ! comment something ", "! comment something"], |
| + [" ! \n comment something ", "! comment something"], |
| + [" foo , bar ## foo > bar ", "foo,bar##foo > bar"], |
| + [" foo , bar #@# foo > bar ", "foo,bar#@#foo > bar"], |
| + ]; |
| + for (let [text, expected] of tests) |
| + test.equal(Filter.normalize(text), expected); |
| + test.done(); |
| +}; |
| + |
| +exports.testActiveFilter = function(test) |
| +{ |
| + let filter1 = Filter.fromText("asdf"); |
| + let filter1copy = Filter.fromText("asdf"); |
| + let filter2 = Filter.fromText("##foobar"); |
| + |
| + try |
| + { |
| + test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Filters are initially enabled"); |
| + filter1.disabled = true; |
| + test.ok(filter1.disabled, "Disabling filter works"); |
| + test.ok(filter1copy.disabled, "Filter copies are also disabled"); |
| + test.ok(!filter2.disabled, "Disabling one filter doesn't disable others"); |
| + |
| + test.ok(filter1.hitCount === 0 && filter1copy.hitCount === 0 && filter2.hitCount === 0, "Filters have no hit initially"); |
| + filter1.hitCount = 5; |
| + test.equal(filter1.hitCount, 5, "Changing hit count works"); |
| + test.equal(filter1copy.hitCount, 5, "Hit count of filter copies is also changed"); |
| + test.equal(filter2.hitCount, 0, "Hit count of other filters isn't affected"); |
| + |
| + test.ok(filter1.lastHit === 0 && filter1copy.lastHit === 0 && filter2.lastHit === 0, "Filters have no last hit time initially"); |
| + filter1.lastHit = 10; |
| + test.equal(filter1.lastHit, 10, "Changing last hit time works"); |
| + test.equal(filter1copy.lastHit, 10, "Last hit time of filter copies is also changed"); |
| + test.equal(filter2.lastHit, 0, "Last hit time of other filters isn't affected"); |
| + } |
| + finally |
| + { |
| + filter1.delete(); |
| + filter1copy.delete(); |
| + filter2.delete(); |
| + } |
| + |
| + test.done(); |
| +}; |
| + |
| +exports.testMatching = function(test) |
| +{ |
| + function testMatch(text, location, contentType, docDomain, thirdParty, sitekey, expected) |
| + { |
| + let filter = Filter.fromText(text); |
| + test.equal(filter.matches(location), expected, "Result of filter " + text + " matching " + location); |
| + filter.delete(); |
| + |
| + filter = Filter.fromText("@@" + text) |
| + test.equal(filter.matches(location), expected, "Result of filter @@" + text + " matching " + location); |
| + filter.delete(); |
| + } |
| + |
| + // Basic filters |
| + testMatch("abc", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("abc", "http://ABC/adf", "IMAGE", null, false, null, true); |
| + testMatch("abc", "http://abd/adf", "IMAGE", null, false, null, false); |
| + testMatch("|abc", "http://abc/adf", "IMAGE", null, false, null, false); |
| + testMatch("|http://abc", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("abc|", "http://abc/adf", "IMAGE", null, false, null, false); |
| + testMatch("abc/adf|", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("||example.com/foo", "http://example.com/foo/bar", "IMAGE", null, false, null, true); |
| + testMatch("||com/foo", "http://example.com/foo/bar", "IMAGE", null, false, null, true); |
| + testMatch("||mple.com/foo", "http://example.com/foo/bar", "IMAGE", null, false, null, false); |
| + testMatch("||/example.com/foo", "http://example.com/foo/bar", "IMAGE", null, false, null, false); |
| + testMatch("||example.com/foo/bar|", "http://example.com/foo/bar", "IMAGE", null, false, null, true); |
| + testMatch("||example.com/foo", "http://foo.com/http://example.com/foo/bar", "IMAGE", null, false, null, false); |
| + testMatch("||example.com/foo|", "http://example.com/foo/bar", "IMAGE", null, false, null, false); |
| + |
| + // Separator placeholders |
| + testMatch("abc^d", "http://abc/def", "IMAGE", null, false, null, true); |
| + testMatch("abc^e", "http://abc/def", "IMAGE", null, false, null, false); |
| + testMatch("def^", "http://abc/def", "IMAGE", null, false, null, true); |
| + testMatch("http://abc/d^f", "http://abc/def", "IMAGE", null, false, null, false); |
| + testMatch("http://abc/def^", "http://abc/def", "IMAGE", null, false, null, true); |
| + testMatch("^foo=bar^", "http://abc/?foo=bar", "IMAGE", null, false, null, true); |
| + testMatch("^foo=bar^", "http://abc/?a=b&foo=bar", "IMAGE", null, false, null, true); |
| + testMatch("^foo=bar^", "http://abc/?foo=bar&a=b", "IMAGE", null, false, null, true); |
| + testMatch("^foo=bar^", "http://abc/?notfoo=bar", "IMAGE", null, false, null, false); |
| + testMatch("^foo=bar^", "http://abc/?foo=barnot", "IMAGE", null, false, null, false); |
| + testMatch("^foo=bar^", "http://abc/?foo=bar%2Enot", "IMAGE", null, false, null, false); |
| + testMatch("||example.com^", "http://example.com/foo/bar", "IMAGE", null, false, null, true); |
| + testMatch("||example.com^", "http://example.company.com/foo/bar", "IMAGE", null, false, null, false); |
| + testMatch("||example.com^", "http://example.com:1234/foo/bar", "IMAGE", null, false, null, true); |
| + testMatch("||example.com^", "http://example.com.com/foo/bar", "IMAGE", null, false, null, false); |
| + testMatch("||example.com^", "http://example.com-company.com/foo/bar", "IMAGE", null, false, null, false); |
| + testMatch("||example.com^foo", "http://example.com/foo/bar", "IMAGE", null, false, null, true); |
| + testMatch("||пример.ру^", "http://пример.ру/foo/bar", "IMAGE", null, false, null, true); |
| + testMatch("||пример.ру^", "http://пример.руководитель.ру/foo/bar", "IMAGE", null, false, null, false); |
| + testMatch("||пример.ру^", "http://пример.ру:1234/foo/bar", "IMAGE", null, false, null, true); |
| + testMatch("||пример.ру^", "http://пример.ру.ру/foo/bar", "IMAGE", null, false, null, false); |
| + testMatch("||пример.ру^", "http://пример.ру-ководитель.ру/foo/bar", "IMAGE", null, false, null, false); |
| + testMatch("||пример.ру^foo", "http://пример.ру/foo/bar", "IMAGE", null, false, null, true); |
| + |
| + // Wildcard matching |
| + testMatch("abc*d", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("abc*d", "http://abcd/af", "IMAGE", null, false, null, true); |
| + testMatch("abc*d", "http://abc/d/af", "IMAGE", null, false, null, true); |
| + testMatch("abc*d", "http://dabc/af", "IMAGE", null, false, null, false); |
| + testMatch("*abc", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("abc*", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("|*abc", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("abc*|", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("abc***d", "http://abc/adf", "IMAGE", null, false, null, true); |
| + |
| + // Regular expressions |
| + testMatch("/abc/", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("/abc/", "http://abcd/adf", "IMAGE", null, false, null, true); |
| + testMatch("*/abc/", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("*/abc/", "http://abcd/adf", "IMAGE", null, false, null, false); |
| + testMatch("/a\\wc/", "http://abc/adf", "IMAGE", null, false, null, true); |
| + testMatch("/a\\wc/", "http://a1c/adf", "IMAGE", null, false, null, true); |
| + testMatch("/a\\wc/", "http://a_c/adf", "IMAGE", null, false, null, true); |
| + testMatch("/a\\wc/", "http://a%c/adf", "IMAGE", null, false, null, false); |
| + |
| + test.done(); |
| +}; |