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

Delta Between Two Patch Sets: qunit/tests/filterValidation.js

Issue 5279235799252992: Issue 491 - Validate custom filters (Closed)
Left Patch Set: Addressed comment and added tests Created Nov. 20, 2014, 12:45 p.m.
Right Patch Set: Improved test messages Created Nov. 21, 2014, 8:07 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « qunit/index.html ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 module( 1 (function()
2 "filter validation", 2 {
3 var filterValidation = require("filterValidation");
4 var parseFilter = filterValidation.parseFilter;
5 var parseFilters = filterValidation.parseFilters;
6
7 var filterClasses = require("filterClasses");
8 var BlockingFilter = filterClasses.BlockingFilter;
9 var ElemHideFilter = filterClasses.ElemHideFilter;
10 var CommentFilter = filterClasses.CommentFilter;
11
12 module("Filter validation");
13
14 test("Detecting invalid filters", function()
3 { 15 {
4 setup: function() 16 throws(parseFilter.bind(null, "||example.com^$unknown"), "unknown option");
5 { 17 throws(parseFilter.bind(null, "[foobar]"), "filter list header");
6 var filterValidation = require("filterValidation"); 18 throws(parseFilter.bind(null, "##[foo"), "invalid selector");
7 this.parseFilter = filterValidation.parseFilter;
8 this.parseFilters = filterValidation.parseFilters;
9 19
10 var filterClasses = require("filterClasses"); 20 throws(parseFilters.bind(null, "!comment\r\n||example.com^\n\n##/"), /\b4\b/ , "error contains corresponding line number");
11 this.BlockingFilter = filterClasses.BlockingFilter; 21 });
12 this.ElemHideFilter = filterClasses.ElemHideFilter;
13 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
14 }
15 }
16 );
17 22
18 test("detecting invalid filters", function() { 23 test("Allowing valid 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.
19 throws(this.parseFilter.bind(null, "||example.com$unknown"), "unkown option"); 24 {
Wladimir Palant 2014/11/20 14:10:06 Typo: "unkown"
Sebastian Noack 2014/11/20 15:36:29 Done.
20 throws(this.parseFilter.bind(null, "[foobar]"), "filter list header"); 25 var text, filter;
21 throws(this.parseFilter.bind(null, "##[foo"), "invalid selector");
22 });
23 26
24 test("allowing valid filters", function() { 27 text = "||example.com^";
25 var text, filter; 28 filter = parseFilter(text);
29 ok(filter instanceof BlockingFilter, "blocking filter parsed");
30 equal(filter.text, text, "blocking filter text matches");
26 31
27 text = "||example.com"; 32 text = '##div:first-child a[src="http://example.com"] > .foo + #bar'
Wladimir Palant 2014/11/20 14:10:06 Nit: Better "||example.com^" - a separator placeho
Sebastian Noack 2014/11/20 15:36:29 Done.
28 filter = this.parseFilter(text); 33 filter = parseFilter(text);
29 ok(filter instanceof this.BlockingFilter && filter.text == text, "blocking fil ter"); 34 ok(filter instanceof ElemHideFilter, "elemhide filter parsed");
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.
35 equal(filter.text, text, "elemhide filter text matches");
30 36
31 text = '##div:first-child a[src="http://example.com"] > .foo + #bar' 37 text = "! foo bar"
32 filter = this.parseFilter(text); 38 filter = parseFilter(text);
33 ok(filter instanceof this.ElemHideFilter && filter.text == text, "elemhide fil ter"); 39 ok(filter instanceof CommentFilter, "comment filter parsed");
40 equal(filter.text, text, "comment filter text matches");
34 41
35 text = "! foo bar" 42 equal(parseFilter(""), null, "empty filter parsed as 'null'");
36 filter = this.parseFilter(text); 43 });
37 ok(filter instanceof this.CommentFilter && filter.text == text, "comment filte r");
38 44
39 equal(this.parseFilter(""), null, "empty filter"); 45 test("Normalizing filters", function()
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.
40 }); 46 {
47 var ws = " \t\r\n";
41 48
42 test("parsing multiple filters", function() 49 equal(parseFilter(ws + "@@" + ws + "||" + ws + "example.com" + ws + "^" + ws ).text, "@@||example.com^", "unnecessary spaces");
43 { 50 equal(parseFilter(ws), null, "only spaces");
44 var filters; 51 });
45 52
46 filters = this.parseFilters("||example.com\n \n###foobar\r\n! foo bar\n"); 53 test("Parsing multiple filters", function()
47 ok( 54 {
48 filters.length == 3 && 55 var filters = parseFilters("||example.com^\n \n###foobar\r\n! foo bar\n");
49 56
50 filters[0] instanceof this.BlockingFilter && 57 equal(filters.length, 3, "all filters parsed");
51 filters[0].text == "||example.com" &&
52 58
53 filters[1] instanceof this.ElemHideFilter && 59 ok(filters[0] instanceof BlockingFilter, "1st filter is blocking");
54 filters[1].text == "###foobar" && 60 equal(filters[0].text, "||example.com^", "1st filter text matches");
55 61
56 filters[2] instanceof this.CommentFilter && 62 ok(filters[1] instanceof ElemHideFilter, "2nd filter is elemhide");
57 filters[2].text == "! foo bar", 63 equal(filters[1].text, "###foobar", "2nd filter text matches");
58 64
59 "parse filters and comments, stripping empty lines" 65 ok(filters[2] instanceof CommentFilter, "3rd filter is comment");
60 ); 66 equal(filters[2].text, "! foo bar", "3rd filter text matches");
67 });
61 68
62 filters = this.parseFilters("[foobar]\n \n||example.com\r\n! foo bar\n", true) ; 69 test("Parsing multiple filters, stripping filter list headers", function()
63 ok( 70 {
64 filters.length == 2 && 71 var filters = parseFilters("[foobar]\n \n||example.com^\r\n! foo bar\n", tru e);
65 72
66 filters[0] instanceof this.BlockingFilter && 73 equal(filters.length, 2, "all filters parsed");
67 filters[0].text == "||example.com" &&
68 74
69 filters[1] instanceof this.CommentFilter && 75 ok(filters[0] instanceof BlockingFilter, "1st filter is blocking");
70 filters[1].text == "! foo bar", 76 equal(filters[0].text, "||example.com^", "1st filter text matches");
71 77
72 "parse filters and comments, stripping empty lines and filter list headers" 78 ok(filters[1] instanceof CommentFilter, "2nd filter is comment");
73 ); 79 equal(filters[1].text, "! foo bar", "2nd filter text matches");
74 80 });
75 throws( 81 })();
76 this.parseFilters.bind(null, "!comment\r\n||example.com\n\n##/"),
77 /^([^:]+ )?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
78 "throw error with corresponding line number"
79 );
80 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld