|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('chai').assert, |
| 4 | + rules = require('../rules'); |
| 5 | + |
| 6 | +const Exclusion = rules.Exclusion, |
| 7 | + Rule = rules.Rule, |
| 8 | + RuleSet = rules.RuleSet, |
| 9 | + RuleSets = rules.RuleSets; |
| 10 | + |
| 11 | + |
| 12 | +describe('rules.js', function() { |
| 13 | + let test_str = 'test'; |
| 14 | + |
| 15 | + describe('Exclusion', function() { |
| 16 | + it('constructs', function() { |
| 17 | + let exclusion = new Exclusion(test_str); |
| 18 | + assert(exclusion.pattern_c.test(test_str), true); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('Rule', function() { |
| 23 | + it('constructs trivial rule', function() { |
| 24 | + let rule = new Rule('^http:', 'https:'); |
| 25 | + assert(rule.to, rules.trivial_rule_to); |
| 26 | + assert(rule.from_c, rules.trivial_rule_from); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('RuleSet', function() { |
| 31 | + beforeEach(function() { |
| 32 | + this.ruleset = new RuleSet('set_name', true, 'note'); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('#apply', function() { |
| 36 | + it('excludes excluded uris', function() { |
| 37 | + this.ruleset.exclusions = [new Exclusion(test_str)]; |
| 38 | + assert.isNull(this.ruleset.apply(test_str)); |
| 39 | + }); |
| 40 | + |
| 41 | + it('rewrites uris', function() { |
| 42 | + let rule = new Rule('^http:', 'https:'); |
| 43 | + this.ruleset.rules.push(rule); |
| 44 | + assert(this.ruleset.apply('http://example.com/'), 'https://example.com/'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('does nothing when empty', function() { |
| 48 | + assert.isNull(this.ruleset.apply('http://example.com/')); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('#isEquivalentTo', function() { |
| 53 | + let inputs = ['a', 'b', 'c']; |
| 54 | + |
| 55 | + it('not equivalent with different input', function() { |
| 56 | + let rs = new RuleSet(...inputs); |
| 57 | + assert.isFalse( |
| 58 | + rs.isEquivalentTo(new RuleSet('e', 'f', 'g')) |
| 59 | + ); |
| 60 | + }); |
| 61 | + it('not equivalent with different exclusions', function() { |
| 62 | + let rs_a = new RuleSet(...inputs), |
| 63 | + rs_b = new RuleSet(...inputs); |
| 64 | + rs_a.exclusions = [new Exclusion('foo')]; |
| 65 | + rs_b.exclusions = [new Exclusion('bar')]; |
| 66 | + |
| 67 | + assert.isFalse(rs_a.isEquivalentTo(rs_b)); |
| 68 | + }); |
| 69 | + |
| 70 | + it('not equivalent with different rules', function() { |
| 71 | + let rs_a = new RuleSet(...inputs), |
| 72 | + rs_b = new RuleSet(...inputs); |
| 73 | + rs_a.rules.push(new Rule('a', 'a')); |
| 74 | + rs_b.rules.push(new Rule('b', 'b')); |
| 75 | + |
| 76 | + assert.isFalse(rs_a.isEquivalentTo(rs_b)); |
| 77 | + }); |
| 78 | + |
| 79 | + it('equivalent to self', function() { |
| 80 | + let rs = new RuleSet(...inputs); |
| 81 | + assert.isTrue(rs.isEquivalentTo(rs)); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }) |
| 85 | + |
| 86 | + describe('RuleSets', function() { |
| 87 | + describe('#potentiallyApplicableRulesets', function() { |
| 88 | + let example_host = 'example.com'; |
| 89 | + |
| 90 | + beforeEach(function() { |
| 91 | + this.rsets = new RuleSets(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('returns nothing when empty', function() { |
| 95 | + assert.isEmpty(this.rsets.potentiallyApplicableRulesets(example_host)); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns cached rulesets', function() { |
| 99 | + this.rsets.ruleCache.set(example_host, 'value'); |
| 100 | + assert(this.rsets.potentiallyApplicableRulesets(example_host), 'value'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('returns applicable rule sets', function() { |
| 104 | + let target = ['value']; |
| 105 | + this.rsets.targets.set(example_host, target); |
| 106 | + |
| 107 | + let result = this.rsets.potentiallyApplicableRulesets(example_host), |
| 108 | + expected = new Set(target); |
| 109 | + assert.deepEqual(result, expected); |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}) |
0 commit comments