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

Side by Side Diff: qunit/tests/filterValidation.js

Issue 5655474749833216: Issue 2658 - Got rid of try..catch for filter validation (Closed)
Patch Set: Improved comments Created June 8, 2015, 11:59 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « options.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 (function() 1 (function()
2 { 2 {
3 var filterValidation = require("filterValidation"); 3 var filterValidation = require("filterValidation");
4 var parseFilter = filterValidation.parseFilter; 4 var parseFilter = filterValidation.parseFilter;
5 var parseFilters = filterValidation.parseFilters; 5 var parseFilters = filterValidation.parseFilters;
6 6
7 var filterClasses = require("filterClasses"); 7 var filterClasses = require("filterClasses");
8 var BlockingFilter = filterClasses.BlockingFilter; 8 var BlockingFilter = filterClasses.BlockingFilter;
9 var ElemHideFilter = filterClasses.ElemHideFilter; 9 var ElemHideFilter = filterClasses.ElemHideFilter;
10 var CommentFilter = filterClasses.CommentFilter; 10 var CommentFilter = filterClasses.CommentFilter;
11 11
12 module("Filter validation"); 12 module("Filter validation");
13 13
14 test("Detecting invalid filters", function() 14 test("Detecting invalid filters", function()
15 { 15 {
16 throws(parseFilter.bind(null, "||example.com^$unknown"), "unknown option"); 16 ok(parseFilter("||example.com^$unknown").error, "unknown option");
17 throws(parseFilter.bind(null, "[foobar]"), "filter list header"); 17 ok(parseFilter("[foobar]").error, "filter list header");
18 throws(parseFilter.bind(null, "##[foo"), "invalid selector"); 18 ok(parseFilter("##[foo").error, "invalid selector");
19 19 ok(/\b4\b/.test(parseFilters("!comment\r\n||example.com^\n\n##/").error), "e rror contains corresponding line number");
20 throws(parseFilters.bind(null, "!comment\r\n||example.com^\n\n##/"), /\b4\b/ , "error contains corresponding line number");
21 }); 20 });
22 21
23 test("Allowing valid filters", function() 22 test("Allowing valid filters", function()
24 { 23 {
25 var text, filter; 24 var text, filter;
26 25
27 text = "||example.com^"; 26 text = "||example.com^";
28 filter = parseFilter(text); 27 filter = parseFilter(text).filter;
29 ok(filter instanceof BlockingFilter, "blocking filter parsed"); 28 ok(filter instanceof BlockingFilter, "blocking filter parsed");
30 equal(filter.text, text, "blocking filter text matches"); 29 equal(filter.text, text, "blocking filter text matches");
31 30
32 text = '##div:first-child a[src="http://example.com"] > .foo + #bar' 31 text = '##div:first-child a[src="http://example.com"] > .foo + #bar'
33 filter = parseFilter(text); 32 filter = parseFilter(text).filter;
34 ok(filter instanceof ElemHideFilter, "elemhide filter parsed"); 33 ok(filter instanceof ElemHideFilter, "elemhide filter parsed");
35 equal(filter.text, text, "elemhide filter text matches"); 34 equal(filter.text, text, "elemhide filter text matches");
36 35
37 text = "! foo bar" 36 text = "! foo bar"
38 filter = parseFilter(text); 37 filter = parseFilter(text).filter;
39 ok(filter instanceof CommentFilter, "comment filter parsed"); 38 ok(filter instanceof CommentFilter, "comment filter parsed");
40 equal(filter.text, text, "comment filter text matches"); 39 equal(filter.text, text, "comment filter text matches");
41 40
42 equal(parseFilter(""), null, "empty filter parsed as 'null'"); 41 equal(parseFilter("").filter, null, "empty filter parsed as 'null'");
43 }); 42 });
44 43
45 test("Normalizing filters", function() 44 test("Normalizing filters", function()
46 { 45 {
47 var ws = " \t\r\n"; 46 var ws = " \t\r\n";
48 47
49 equal(parseFilter(ws + "@@" + ws + "||" + ws + "example.com" + ws + "^" + ws ).text, "@@||example.com^", "unnecessary spaces"); 48 equal(parseFilter(ws + "@@" + ws + "||" + ws + "example.com" + ws + "^" + ws ).filter.text, "@@||example.com^", "unnecessary spaces");
50 equal(parseFilter(ws), null, "only spaces"); 49 equal(parseFilter(ws).filter, null, "only spaces");
51 }); 50 });
52 51
53 test("Parsing multiple filters", function() 52 test("Parsing multiple filters", function()
54 { 53 {
55 var filters = parseFilters("||example.com^\n \n###foobar\r\n! foo bar\n"); 54 var filters = parseFilters("||example.com^\n \n###foobar\r\n! foo bar\n").fi lters;
56 55
57 equal(filters.length, 3, "all filters parsed"); 56 equal(filters.length, 3, "all filters parsed");
58 57
59 ok(filters[0] instanceof BlockingFilter, "1st filter is blocking"); 58 ok(filters[0] instanceof BlockingFilter, "1st filter is blocking");
60 equal(filters[0].text, "||example.com^", "1st filter text matches"); 59 equal(filters[0].text, "||example.com^", "1st filter text matches");
61 60
62 ok(filters[1] instanceof ElemHideFilter, "2nd filter is elemhide"); 61 ok(filters[1] instanceof ElemHideFilter, "2nd filter is elemhide");
63 equal(filters[1].text, "###foobar", "2nd filter text matches"); 62 equal(filters[1].text, "###foobar", "2nd filter text matches");
64 63
65 ok(filters[2] instanceof CommentFilter, "3rd filter is comment"); 64 ok(filters[2] instanceof CommentFilter, "3rd filter is comment");
66 equal(filters[2].text, "! foo bar", "3rd filter text matches"); 65 equal(filters[2].text, "! foo bar", "3rd filter text matches");
67 }); 66 });
68 67
69 test("Parsing multiple filters, stripping filter list headers", function() 68 test("Parsing multiple filters, stripping filter list headers", function()
70 { 69 {
71 var filters = parseFilters("[foobar]\n \n||example.com^\r\n! foo bar\n", tru e); 70 var filters = parseFilters("[foobar]\n \n||example.com^\r\n! foo bar\n", tru e).filters;
72 71
73 equal(filters.length, 2, "all filters parsed"); 72 equal(filters.length, 2, "all filters parsed");
74 73
75 ok(filters[0] instanceof BlockingFilter, "1st filter is blocking"); 74 ok(filters[0] instanceof BlockingFilter, "1st filter is blocking");
76 equal(filters[0].text, "||example.com^", "1st filter text matches"); 75 equal(filters[0].text, "||example.com^", "1st filter text matches");
77 76
78 ok(filters[1] instanceof CommentFilter, "2nd filter is comment"); 77 ok(filters[1] instanceof CommentFilter, "2nd filter is comment");
79 equal(filters[1].text, "! foo bar", "2nd filter text matches"); 78 equal(filters[1].text, "! foo bar", "2nd filter text matches");
80 }); 79 });
81 })(); 80 })();
OLDNEW
« no previous file with comments | « options.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld