Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: qunit/tests/filterValidation.js

Issue 5279235799252992: Issue 491 - Validate custom filters (Closed)
Patch Set: Addressed comments Created Nov. 20, 2014, 3:35 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « qunit/index.html ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: qunit/tests/filterValidation.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/qunit/tests/filterValidation.js
@@ -0,0 +1,81 @@
+(function()
+{
+ var filterValidation = require("filterValidation");
+ var parseFilter = filterValidation.parseFilter;
+ var parseFilters = filterValidation.parseFilters;
+
+ var filterClasses = require("filterClasses");
+ var BlockingFilter = filterClasses.BlockingFilter;
+ var ElemHideFilter = filterClasses.ElemHideFilter;
+ var CommentFilter = filterClasses.CommentFilter;
+
+ module("Filter validation");
+
+ test("Detecting invalid filters", function()
+ {
+ throws(parseFilter.bind(null, "||example.com^$unknown"), "unknown option");
+ throws(parseFilter.bind(null, "[foobar]"), "filter list header");
+ throws(parseFilter.bind(null, "##[foo"), "invalid selector");
+
+ throws(parseFilters.bind(null, "!comment\r\n||example.com^\n\n##/"), /\b4\b/, "error contains corresponding line number");
+ });
+
+ test("Allowing valid filters", function()
+ {
+ var text, filter;
+
+ text = "||example.com^";
+ filter = parseFilter(text);
+ ok(filter instanceof BlockingFilter, "blocking filter");
+ equal(filter.text, text, "blocking filter text");
+
+ text = '##div:first-child a[src="http://example.com"] > .foo + #bar'
+ filter = parseFilter(text);
+ ok(filter instanceof ElemHideFilter, "elemhide filter");
+ equal(filter.text, text, "elemhide filter text");
+
+ text = "! foo bar"
+ filter = parseFilter(text);
+ ok(filter instanceof CommentFilter, "comment filter");
+ equal(filter.text, text, "comment filter text");
+
+ equal(parseFilter(""), null, "empty filter");
+ });
+
+ test("Normalizing filters", function()
+ {
+ var ws = " \t\r\n";
+
+ equal(parseFilter(ws + "@@" + ws + "||" + ws + "example.com" + ws + "^" + ws).text, "@@||example.com^", "unnecessary spaces");
+ equal(parseFilter(ws), null, "only spaces");
+ });
+
+ test("Parsing multiple filters", function()
+ {
+ var filters = parseFilters("||example.com^\n \n###foobar\r\n! foo bar\n");
+
+ equal(filters.length, 3, "there are 3 filters");
Wladimir Palant 2014/11/20 16:58:20 Nit: You are duplicating the expected result in th
Sebastian Noack 2014/11/21 08:08:08 Done, and improved a few other messages as well.
+
+ ok(filters[0] instanceof BlockingFilter, "1st filter is blocking");
+ equal(filters[0].text, "||example.com^", "1st filter text matches");
+
+ ok(filters[1] instanceof ElemHideFilter, "2nd filter is elemhide");
+ equal(filters[1].text, "###foobar", "2nd filter text matches");
+
+ ok(filters[2] instanceof CommentFilter, "3rd filter is comment");
+ equal(filters[2].text, "! foo bar", "3rd filter text matches");
+ });
+
+ test("Parsing multiple filters, stripping filter list headers", function()
+ {
+ var filters = parseFilters("[foobar]\n \n||example.com^\r\n! foo bar\n", true);
+
+ equal(filters.length, 2, "there are 2 filters");
+
+ ok(filters[0] instanceof BlockingFilter, "1st filter is blocking");
+ equal(filters[0].text, "||example.com^", "1st filter text matches");
+
+ ok(filters[1] instanceof CommentFilter, "2nd filter is comment");
+ equal(filters[1].text, "! foo bar", "2nd filter text matches");
+ });
+})();
« no previous file with comments | « qunit/index.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld