Skip to content

Commit

Permalink
feat: use defaultRange for negated sets (fixes #25)
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Feb 10, 2018
1 parent 5b17582 commit f571b6d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 38 deletions.
20 changes: 10 additions & 10 deletions lib/randexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ module.exports = class RandExp {

// This allows expanding to include additional characters
// for instance: RandExp.defaultRange.add(0, 65535);
if (regexp.defaultRange) {
this.defaultRange = regexp.defaultRange;
}
this.defaultRange = regexp.defaultRange ?
regexp.defaultRange : this.defaultRange.clone();

if (regexp.randInt) {
this.randInt = regexp.randInt;
}
Expand Down Expand Up @@ -179,14 +179,14 @@ module.exports = class RandExp {
} else if (token.type === ret.types.RANGE) {
return new DRange(token.from, token.to);
} else {
var drange = new DRange();
for (var i = 0; i < token.set.length; i++) {
var subrange = this._expand(token.set[i]);
let drange = new DRange();
for (let i = 0; i < token.set.length; i++) {
let subrange = this._expand(token.set[i]);
drange.add(subrange);
if (this.ignoreCase) {
for (var j = 0; j < subrange.length; j++) {
var code = subrange.index(j);
var otherCaseCode = this._toOtherCase(code);
for (let j = 0; j < subrange.length; j++) {
let code = subrange.index(j);
let otherCaseCode = this._toOtherCase(code);
if (code !== otherCaseCode) {
drange.add(otherCaseCode);
}
Expand All @@ -196,7 +196,7 @@ module.exports = class RandExp {
if (token.not) {
return this.defaultRange.clone().subtract(drange);
} else {
return drange;
return this.defaultRange.clone().intersect(drange);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"lib": "./lib"
},
"dependencies": {
"discontinuous-range": "1.0.0",
"discontinuous-range": "fent/discontinuous-range#feature\/intersect",
"ret": "~0.2.0"
},
"devDependencies": {
Expand Down
80 changes: 53 additions & 27 deletions test/custom-range-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,61 @@ const assert = require('assert');
const RandExp = require('..');


function genMaxChar(re) {
var output = re.gen();
var maxChar = 0;
for (var i = 0; i < output.length; i++) {
maxChar = Math.max(maxChar, output.charCodeAt(i));
}
return maxChar;
}

describe('Modify Range', () => {
it('Should generate unicode when we expand its range', () => {
var re = new RandExp(/.{100}/);
re.defaultRange.subtract(0, 126);
re.defaultRange.add(127, 65535);
var output = re.gen();
var maxChar = 0;
for (var i = 0; i < output.length; i++) {
maxChar = Math.max(maxChar, output.charCodeAt(i));
}
assert.ok(maxChar > 127, 'non-ascii characters should have been generated');
describe('Globally from prototpye', () => {
it('Should generate unicode when we expand its range', () => {
RandExp.prototype.defaultRange.subtract(0, 126);
RandExp.prototype.defaultRange.add(127, 65535);
var re = new RandExp(/.{100}/);
RandExp.prototype.defaultRange.add(0, 126);
RandExp.prototype.defaultRange.subtract(127, 65535);
var maxChar = genMaxChar(re);
assert.ok(maxChar >= 127, 'non-ascii characters should have been generated');
});
});

describe('From a regexp instance', () => {
it('Should generate unicode when we expand its range', () => {
var r = /[\d\D]{100}/;
r.defaultRange = RandExp.prototype.defaultRange.clone();
r.defaultRange.subtract(0, 126);
r.defaultRange.add(127, 65535);
var re = new RandExp(r);
var maxChar = genMaxChar(re);
assert.ok(maxChar >= 127, 'non-ascii characters should have been generated');
});
});

var r = /.{100}/;
r.defaultRange = RandExp.prototype.defaultRange.clone();
r.defaultRange.subtract(0, 126);
r.defaultRange.add(127, 65535);
output = RandExp.randexp(r);
maxChar = 0;
for (i = 0; i < output.length; i++) {
maxChar = Math.max(maxChar, output.charCodeAt(i));
}
assert.ok(maxChar > 127, 'non-ascii characters should have been generated');
describe('From a randexp instance', () => {
it('Should generate unicode when we expand its range', () => {
var re = new RandExp(/[\s\S]{100}/);
var maxChar = genMaxChar(re);
assert.ok(maxChar < 127, 'ascii characters should have been generated');
re.defaultRange.subtract(0, 126);
re.defaultRange.add(127, 65535);
maxChar = genMaxChar(re);
assert.ok(maxChar >= 127, 'non-ascii characters should have been generated');
});

re = new RandExp(/.{100}/);
output = re.gen();
maxChar = 0;
for (i = 0; i < output.length; i++) {
maxChar = Math.max(maxChar, output.charCodeAt(i));
}
assert.ok(maxChar <= 127, 'ascii characters should have been generated');
describe('With a negated set', () => {
it('Should still generate only ascii', () => {
var re = new RandExp(/[^a]{100}/);
var maxChar = genMaxChar(re);
assert.ok(maxChar < 127, 'ascii characters should have been generated');
re.defaultRange.subtract(0, 126);
re.defaultRange.add(127, 65535);
maxChar = genMaxChar(re);
assert.ok(maxChar >= 127, 'non-ascii characters should have been generated');
});
});
});
});

0 comments on commit f571b6d

Please sign in to comment.