@@ -12,18 +12,30 @@ const Exclusion = rules.Exclusion,
1212describe ( 'rules.js' , function ( ) {
1313 let test_str = 'test' ;
1414
15+ describe ( 'nullIterable' , function ( ) {
16+ it ( 'is iterable zero times and is size 0' , function ( ) {
17+ let count = 0 ;
18+ for ( let _ of rules . nullIterable ) { // eslint-disable-line no-unused-vars
19+ count += 1 ;
20+ }
21+ assert . strictEqual ( count , 0 ) ;
22+ assert . strictEqual ( rules . nullIterable . size , 0 ) ;
23+ assert . isEmpty ( rules . nullIterable ) ;
24+ } ) ;
25+ } ) ;
26+
1527 describe ( 'Exclusion' , function ( ) {
1628 it ( 'constructs' , function ( ) {
1729 let exclusion = new Exclusion ( test_str ) ;
18- assert ( exclusion . pattern_c . test ( test_str ) , true ) ;
30+ assert . isTrue ( exclusion . pattern_c . test ( test_str ) , true ) ;
1931 } ) ;
2032 } ) ;
2133
2234 describe ( 'Rule' , function ( ) {
2335 it ( 'constructs trivial rule' , function ( ) {
2436 let rule = new Rule ( '^http:' , 'https:' ) ;
25- assert ( rule . to , rules . trivial_rule_to ) ;
26- assert ( rule . from_c , rules . trivial_rule_from ) ;
37+ assert . equal ( rule . to , rules . trivial_rule_to ) ;
38+ assert . equal ( rule . from_c , rules . trivial_rule_from_c ) ;
2739 } ) ;
2840 } ) ;
2941
@@ -41,7 +53,7 @@ describe('rules.js', function() {
4153 it ( 'rewrites uris' , function ( ) {
4254 let rule = new Rule ( '^http:' , 'https:' ) ;
4355 this . ruleset . rules . push ( rule ) ;
44- assert ( this . ruleset . apply ( 'http://example.com/' ) , 'https://example.com/' ) ;
56+ assert . equal ( this . ruleset . apply ( 'http://example.com/' ) , 'https://example.com/' ) ;
4557 } ) ;
4658
4759 it ( 'does nothing when empty' , function ( ) {
@@ -85,28 +97,70 @@ describe('rules.js', function() {
8597
8698 describe ( 'RuleSets' , function ( ) {
8799 describe ( '#potentiallyApplicableRulesets' , function ( ) {
88- let example_host = 'example.com' ;
100+ let host = 'example.com' ,
101+ value = [ host ] ;
89102
90103 beforeEach ( function ( ) {
91104 this . rsets = new RuleSets ( ) ;
92105 } ) ;
93106
94107 it ( 'returns nothing when empty' , function ( ) {
95- assert . isEmpty ( this . rsets . potentiallyApplicableRulesets ( example_host ) ) ;
108+ assert . isEmpty ( this . rsets . potentiallyApplicableRulesets ( host ) ) ;
109+ } ) ;
110+
111+ it ( 'returns nothing for malformed hosts' , function ( ) {
112+ assert . isEmpty ( this . rsets . potentiallyApplicableRulesets ( '....' ) ) ;
96113 } ) ;
97114
98115 it ( 'returns cached rulesets' , function ( ) {
99- this . rsets . ruleCache . set ( example_host , ' value' ) ;
100- assert ( this . rsets . potentiallyApplicableRulesets ( example_host ) , ' value' ) ;
116+ this . rsets . ruleCache . set ( host , value ) ;
117+ assert . deepEqual ( this . rsets . potentiallyApplicableRulesets ( host ) , value ) ;
101118 } ) ;
102119
103- it ( 'returns applicable rule sets' , function ( ) {
104- let target = [ 'value' ] ;
105- this . rsets . targets . set ( example_host , target ) ;
120+ it ( 'caches results' , function ( ) {
121+ this . rsets . targets . set ( host , value ) ;
122+
123+ assert . isEmpty ( this . rsets . ruleCache ) ;
124+ this . rsets . potentiallyApplicableRulesets ( host ) ;
125+ assert . isTrue ( this . rsets . ruleCache . has ( host ) ) ;
126+ } ) ;
127+
128+ describe ( 'wildcard matching' , function ( ) {
129+
130+ it ( 'no wildcard' , function ( ) {
131+ let target = host ;
132+ this . rsets . targets . set ( target , value ) ;
133+
134+ let result = this . rsets . potentiallyApplicableRulesets ( target ) ,
135+ expected = new Set ( value ) ;
136+
137+ assert . deepEqual ( result , expected ) ;
138+ } ) ;
139+
140+ it ( 'matches left hand side wildcards' , function ( ) {
141+ let target = '*.' + host ;
142+ this . rsets . targets . set ( target , value ) ;
143+
144+ let res1 = this . rsets . potentiallyApplicableRulesets ( 'sub.' + host ) ;
145+ assert . deepEqual ( res1 , new Set ( value ) , 'default case' ) ;
146+
147+ let res2 = this . rsets . potentiallyApplicableRulesets ( host ) ;
148+ assert . isEmpty ( res2 , 'wildcard does not match parent domains' ) ;
149+
150+ let res3 = this . rsets . potentiallyApplicableRulesets ( 'moresub.sub.' + host ) ;
151+ assert . deepEqual ( res3 , new Set ( value ) , 'wildcard matches sub domains' ) ;
152+ } ) ;
153+
154+ it ( 'matches middle wildcards' , function ( ) {
155+ let target = 'sub.*.' + host ;
156+ this . rsets . targets . set ( target , value ) ;
157+
158+ let res1 = this . rsets . potentiallyApplicableRulesets ( 'sub.star.' + host ) ;
159+ assert . deepEqual ( res1 , new Set ( value ) , 'default case' ) ;
106160
107- let result = this . rsets . potentiallyApplicableRulesets ( example_host ) ,
108- expected = new Set ( target ) ;
109- assert . deepEqual ( result , expected ) ;
161+ let res2 = this . rsets . potentiallyApplicableRulesets ( 'sub.foo.bar.' + host ) ;
162+ assert . isEmpty ( res2 , new Set ( value ) , 'only matches one label' ) ;
163+ } ) ;
110164 } ) ;
111165 } ) ;
112166 } ) ;
0 commit comments