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 comment and added tests Created Nov. 20, 2014, 12:45 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,80 @@
+module(
+ "filter validation",
+ {
+ setup: function()
+ {
+ var filterValidation = require("filterValidation");
+ this.parseFilter = filterValidation.parseFilter;
+ this.parseFilters = filterValidation.parseFilters;
+
+ var filterClasses = require("filterClasses");
+ this.BlockingFilter = filterClasses.BlockingFilter;
+ this.ElemHideFilter = filterClasses.ElemHideFilter;
+ this.CommentFilter = filterClasses.CommentFilter;
Wladimir Palant 2014/11/20 14:10:06 It makes relatively little sense to require module
Sebastian Noack 2014/11/20 15:36:29 Done, but I used an IFEE, to don't pollute the glo
Wladimir Palant 2014/11/20 16:58:20 Right, they don't run in their own context... Actu
+ }
+ }
+);
+
+test("detecting invalid filters", function() {
Wladimir Palant 2014/11/20 14:10:06 Nit: Here and below, test names are usually capita
Sebastian Noack 2014/11/20 15:36:29 Done.
+ throws(this.parseFilter.bind(null, "||example.com$unknown"), "unkown option");
Wladimir Palant 2014/11/20 14:10:06 Typo: "unkown"
Sebastian Noack 2014/11/20 15:36:29 Done.
+ throws(this.parseFilter.bind(null, "[foobar]"), "filter list header");
+ throws(this.parseFilter.bind(null, "##[foo"), "invalid selector");
+});
+
+test("allowing valid filters", function() {
+ var text, filter;
+
+ text = "||example.com";
Wladimir Palant 2014/11/20 14:10:06 Nit: Better "||example.com^" - a separator placeho
Sebastian Noack 2014/11/20 15:36:29 Done.
+ filter = this.parseFilter(text);
+ ok(filter instanceof this.BlockingFilter && filter.text == text, "blocking filter");
Wladimir Palant 2014/11/20 14:10:06 Here and everywhere else, please use a separate ch
Sebastian Noack 2014/11/20 15:36:29 Done.
+
+ text = '##div:first-child a[src="http://example.com"] > .foo + #bar'
+ filter = this.parseFilter(text);
+ ok(filter instanceof this.ElemHideFilter && filter.text == text, "elemhide filter");
+
+ text = "! foo bar"
+ filter = this.parseFilter(text);
+ ok(filter instanceof this.CommentFilter && filter.text == text, "comment filter");
+
+ equal(this.parseFilter(""), null, "empty filter");
Wladimir Palant 2014/11/20 14:10:06 Might be a good idea to test that filter normaliza
Sebastian Noack 2014/11/20 15:36:29 Done.
+});
+
+test("parsing multiple filters", function()
+{
+ var filters;
+
+ filters = this.parseFilters("||example.com\n \n###foobar\r\n! foo bar\n");
+ ok(
+ filters.length == 3 &&
+
+ filters[0] instanceof this.BlockingFilter &&
+ filters[0].text == "||example.com" &&
+
+ filters[1] instanceof this.ElemHideFilter &&
+ filters[1].text == "###foobar" &&
+
+ filters[2] instanceof this.CommentFilter &&
+ filters[2].text == "! foo bar",
+
+ "parse filters and comments, stripping empty lines"
+ );
+
+ filters = this.parseFilters("[foobar]\n \n||example.com\r\n! foo bar\n", true);
+ ok(
+ filters.length == 2 &&
+
+ filters[0] instanceof this.BlockingFilter &&
+ filters[0].text == "||example.com" &&
+
+ filters[1] instanceof this.CommentFilter &&
+ filters[1].text == "! foo bar",
+
+ "parse filters and comments, stripping empty lines and filter list headers"
+ );
+
+ throws(
+ this.parseFilters.bind(null, "!comment\r\n||example.com\n\n##/"),
+ /^([^:]+ )?4( [^:]+)?:/,
Wladimir Palant 2014/11/20 14:10:06 No point overcomplicating this, how about /\b4\b/?
Sebastian Noack 2014/11/20 15:36:29 My concern was that there can be digits occurring
+ "throw error with corresponding line number"
+ );
+});
« 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