|
| 1 | +var expect = require('chai').expect; |
| 2 | + |
| 3 | +var sequentialChars = require('../../lib/rules/sequentialChars'); |
| 4 | + |
| 5 | +function sequentialCharsMessage(x, verified) { |
| 6 | + var example = ''; |
| 7 | + for (var i = 0; i < x + 1; i++) { |
| 8 | + example += String.fromCharCode('a'.charCodeAt(0) + i); |
| 9 | + } |
| 10 | + var msg = 'No more than %d sequential alphanumeric characters (e.g., "%s" not allowed)'; // updated wording |
| 11 | + var d = {message: msg, format: [x, example], code: 'sequentialChars'}; |
| 12 | + if (verified !== undefined) { |
| 13 | + d.verified = verified; |
| 14 | + } |
| 15 | + return d; |
| 16 | +} |
| 17 | + |
| 18 | +function sequentialCharsValidate (max) { |
| 19 | + return function () { |
| 20 | + return sequentialChars.validate({max: max}); |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +describe('"sequential characters" rule (alphanumeric only)', function () { |
| 25 | + describe('validate', function () { |
| 26 | + it ('should fail if max is not a number or less than 2', function () { |
| 27 | + const errorRegex = /max should be a number greater than or equal to 2/; |
| 28 | + |
| 29 | + expect(sequentialCharsValidate(false)).to.throw(errorRegex); |
| 30 | + expect(sequentialCharsValidate(0)).to.throw(errorRegex); |
| 31 | + expect(sequentialCharsValidate(1)).to.throw(errorRegex); |
| 32 | + expect(sequentialCharsValidate('hello')).to.throw(errorRegex); |
| 33 | + expect(sequentialCharsValidate(undefined)).to.throw(errorRegex); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should work otherwise', function () { |
| 37 | + expect(sequentialCharsValidate(2)).not.to.throw(); |
| 38 | + expect(sequentialCharsValidate(5)).not.to.throw(); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('explain', function () { |
| 43 | + it('should return description with message', function () { |
| 44 | + expect(sequentialChars.explain({max: 3})).to.be.deep.equal(sequentialCharsMessage(3)); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('missing', function () { |
| 49 | + it('should inform that the rule is not verified for ascending sequences', function () { |
| 50 | + expect(sequentialChars.missing({max: 3}, 'abcd')).to.be.deep.equal(sequentialCharsMessage(3, false)); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should inform that the rule is not verified for descending sequences', function () { |
| 54 | + expect(sequentialChars.missing({max: 3}, 'dcba')).to.be.deep.equal(sequentialCharsMessage(3, false)); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should work otherwise', function () { |
| 58 | + expect(sequentialChars.missing({max: 3}, 'abce')).to.be.deep.equal(sequentialCharsMessage(3, true)); |
| 59 | + expect(sequentialChars.missing({max: 3}, 'acbd')).to.be.deep.equal(sequentialCharsMessage(3, true)); |
| 60 | + expect(sequentialChars.missing({max: 2}, 'abc')).to.be.deep.equal(sequentialCharsMessage(2, false)); |
| 61 | + expect(sequentialChars.missing({max: 2}, 'abd')).to.be.deep.equal(sequentialCharsMessage(2, true)); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('assert', function () { |
| 66 | + it('should return false on ascending letters', function () { |
| 67 | + expect(sequentialChars.assert({max: 2}, 'abcd')).to.be.equal(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return false on descending letters', function () { |
| 71 | + expect(sequentialChars.assert({max: 2}, 'dcba')).to.be.equal(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should return true on success for letters', function () { |
| 75 | + expect(sequentialChars.assert({max: 3}, 'abce')).to.be.equal(true); |
| 76 | + expect(sequentialChars.assert({max: 3}, 'acbd')).to.be.equal(true); |
| 77 | + expect(sequentialChars.assert({max: 2}, 'abd')).to.be.equal(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return false on ascending digits', function () { |
| 81 | + expect(sequentialChars.assert({max: 2}, '012')).to.be.equal(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should return false on descending digits', function () { |
| 85 | + expect(sequentialChars.assert({max: 2}, '654')).to.be.equal(false); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return true on success for digits', function () { |
| 89 | + expect(sequentialChars.assert({max: 3}, '0324')).to.be.equal(true); |
| 90 | + expect(sequentialChars.assert({max: 3}, '5321')).to.be.equal(true); |
| 91 | + expect(sequentialChars.assert({max: 2}, '134')).to.be.equal(true); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('non-alphanumeric', function () { |
| 96 | + it('should treat hyphen as a break in sequence', function () { |
| 97 | + expect(sequentialChars.assert({max: 2}, 'ab-cd')).to.be.equal(true); // 'ab' len2 ok, '-' break, 'cd' len2 ok |
| 98 | + }); |
| 99 | + |
| 100 | + it('should treat underscore as a break in sequence', function () { |
| 101 | + expect(sequentialChars.assert({max: 2}, 'ab_cd')).to.be.equal(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should break at leading non-alphanumeric', function () { |
| 105 | + expect(sequentialChars.assert({max: 2}, '#ab')).to.be.equal(true); // only 'ab' len2 |
| 106 | + }); |
| 107 | + |
| 108 | + it('should still fail if post-break sequence exceeds max', function () { |
| 109 | + expect(sequentialChars.assert({max: 2}, '#abc')).to.be.equal(false); // '#'(break) then 'abc' len3 > max |
| 110 | + }); |
| 111 | + |
| 112 | + it('should allow separated digit sequences each within limit', function () { |
| 113 | + expect(sequentialChars.assert({max: 2}, '01-23')).to.be.equal(true); // '01' len2 ok, '-' break, '23' len2 ok |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('preceding non-alphanumeric with +/- 1 charCode', function () { |
| 118 | + it('should not count preceding "`" (charCode 96) as part of ascending sequence starting at a (97)', function () { |
| 119 | + expect(sequentialChars.assert({max: 2}, '`ab')).to.be.equal(true); // only 'ab' counted, length 2 OK |
| 120 | + }); |
| 121 | + |
| 122 | + it('should fail only due to actual sequence after break, not including preceding "`"', function () { |
| 123 | + expect(sequentialChars.assert({max: 2}, '`abc')).to.be.equal(false); // 'abc' len3 > max; '`' ignored |
| 124 | + }); |
| 125 | + |
| 126 | + it('should not count preceding "{" (charCode 123) as part of descending sequence starting at z (122)', function () { |
| 127 | + expect(sequentialChars.assert({max: 2}, '{zy')).to.be.equal(true); // 'zy' len2 OK |
| 128 | + }); |
| 129 | + |
| 130 | + it('should fail when descending sequence after break exceeds max, excluding preceding "{"', function () { |
| 131 | + expect(sequentialChars.assert({max: 2}, '{zyx')).to.be.equal(false); // 'zyx' len3 > max |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments