chance-generators
Advanced tools
Comparing version 2.7.0 to 2.8.0
@@ -24,3 +24,3 @@ /*global define*/ | ||
function getMinNatural(value) { | ||
function getMinNatural (value) { | ||
if (typeof value === 'number') { | ||
@@ -37,14 +37,2 @@ return value | ||
function getMax(value) { | ||
if (typeof value === 'number') { | ||
return value | ||
} | ||
if (value && value.isGenerator && value.args[0] && typeof value.args[0].max === 'number') { | ||
return value.args[0].max | ||
} | ||
return Number.MAX_SAFE_INTEGER | ||
} | ||
function unwrap (v) { | ||
@@ -585,52 +573,84 @@ if (Array.isArray(v)) { | ||
var lastMagicValues = null | ||
var magicStringGenerator = null | ||
function getMagicStringGenerator (magicValues) { | ||
if (lastMagicValues && lastMagicValues.size === magicValues.size) { | ||
return magicStringGenerator | ||
} | ||
function hasMagicValues () { | ||
return typeof global === 'object' && | ||
global.recordLocation && | ||
global.recordLocation.magicValues | ||
} | ||
var magicStrings = Array.from(magicValues).filter(function (value) { | ||
return typeof value === 'string' | ||
}) | ||
var generatorCache = {} | ||
if (magicStrings.length === 0) { | ||
return that.string | ||
function createMagicValueGenerator (name, baseGeneratorName, predicate) { | ||
function createGenerator () { | ||
var magicValues = global.recordLocation.magicValues | ||
var cacheEntry = generatorCache[name] | ||
if (cacheEntry && cacheEntry.size === magicValues.size) { | ||
return cacheEntry.generator | ||
} | ||
var matchingMagicValues = Array.from(magicValues).filter(predicate) | ||
if (matchingMagicValues.length === 0) { | ||
return that[baseGeneratorName] | ||
} | ||
var generator = that.pickone(matchingMagicValues) | ||
generatorCache[name] = { | ||
size: magicValues.size, | ||
generator: generator | ||
} | ||
return generator | ||
} | ||
lastMagicValues = magicValues | ||
magicStringGenerator = that.pickone(magicStrings) | ||
that[name] = generatorFunction(name, [], function () { | ||
if (!hasMagicValues()) { | ||
return that[baseGeneratorName]() | ||
} | ||
return magicStringGenerator | ||
} | ||
var generator = createGenerator() | ||
function hasMagicValues () { | ||
return global.recordLocation && global.recordLocation.magicValues | ||
} | ||
return generator() | ||
}) | ||
that.magicString = generatorFunction('magicString', [], function () { | ||
if (!hasMagicValues()) { | ||
return that.string() | ||
installMapFunction(that[name]) | ||
var shrinker = shrinkers[baseGeneratorName] | ||
if (shrinker) { | ||
that[name].shrink = function (data) { | ||
return shrinker(that[name], data) | ||
} | ||
} | ||
var g = getMagicStringGenerator(global.recordLocation.magicValues) | ||
var expander = expanders[baseGeneratorName] | ||
if (expander) { | ||
that[name].expand = function (data) { | ||
return expander(that[name], data) | ||
} | ||
} | ||
} | ||
return g() | ||
createMagicValueGenerator('magicString', 'string', function (value) { | ||
return typeof value === 'string' | ||
}) | ||
installMapFunction(that.magicString) | ||
createMagicValueGenerator('magicInteger', 'integer', Number.isInteger) | ||
that.magicString.shrink = function (data) { | ||
return shrinkers.string(that.string, data) | ||
} | ||
createMagicValueGenerator('magicNatural', 'natural', function (value) { | ||
return Number.isInteger(value) && value >= 0 | ||
}) | ||
that.magicString.expand = function (data) { | ||
return expanders.string(that.string, data) | ||
} | ||
createMagicValueGenerator('magicFloating', 'floating', function (value) { | ||
return Number.isFinite(value) | ||
}) | ||
function getTextGenerator (magicValues) { | ||
createMagicValueGenerator('magicNumber', 'floating', function (value) { | ||
return typeof value === 'number' | ||
}) | ||
function getTextGenerator () { | ||
var shortString = that.string({ length: that.natural({ max: 7 }) }) | ||
magicStringGenerator = that.weighted([ | ||
return that.weighted([ | ||
that.string, | ||
@@ -642,6 +662,8 @@ that.sentence, | ||
that.magicString, | ||
that.array(that.weighted([that.magicString, shortString], [60, 40]), that.natural({ max: 10 })).map(strings => strings.join('')) | ||
that.array( | ||
that.weighted([that.magicString, shortString], [60, 40]), | ||
that.natural({ max: 10 })).map(function (strings) { | ||
strings.join('') | ||
}) | ||
], [20, 20, 10, 10, 10, 10, 5]) | ||
return magicStringGenerator | ||
} | ||
@@ -665,2 +687,42 @@ | ||
function getNumberGenerator () { | ||
var smallFloat = that.floating({ min: -100, max: 100 }) | ||
var smallInteger = that.integer({ min: -100, max: 100 }) | ||
var mediumFloat = that.floating({ min: -10000, max: 10000 }) | ||
var mediumInteger = that.integer({ min: -10000, max: 10000 }) | ||
return that.weighted([ | ||
smallFloat, | ||
smallInteger, | ||
mediumFloat, | ||
mediumInteger, | ||
that.integer, | ||
that.floating, | ||
that.magicNumber, | ||
that.magicInteger, | ||
that.magicInteger.map(function (value) { | ||
return value + 1 | ||
}), | ||
that.magicInteger.map(function (value) { | ||
return value - 1 | ||
}) | ||
], [30, 30, 20, 20, 5, 5, 10, 5, 5, 5]) | ||
} | ||
that.number = generatorFunction('number', [], function () { | ||
var g = getNumberGenerator() | ||
return g() | ||
}) | ||
installMapFunction(that.number) | ||
that.number.shrink = function (data) { | ||
return shrinkers.floating(that.floating, data) | ||
} | ||
that.number.expand = function (data) { | ||
return expanders.floating(that.floating, data) | ||
} | ||
that.sequence = generatorFunction('sequence', [], function (fn, count, options) { | ||
@@ -667,0 +729,0 @@ count = typeof count === 'undefined' |
{ | ||
"name": "chance-generators", | ||
"version": "2.7.0", | ||
"version": "2.8.0", | ||
"description": "Random generators based on changejs", | ||
"author": "Sune Simonsen", | ||
"keywords": [ | ||
"testing", | ||
"random" | ||
], | ||
"license": "MIT", | ||
"main": "lib/chance-generators.js", | ||
@@ -13,8 +19,2 @@ "scripts": { | ||
}, | ||
"keywords": [ | ||
"testing", | ||
"random" | ||
], | ||
"author": "Sune Simonsen", | ||
"license": "MIT", | ||
"dependencies": { | ||
@@ -31,8 +31,3 @@ "chance": "1.0.1" | ||
"unexpected-markdown": "1.6.1" | ||
}, | ||
"standard": { | ||
"globals": [ | ||
"beforeEach" | ||
] | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
/*global describe, it*/ | ||
/*global describe, it, beforeEach, afterEach*/ | ||
const Chance = require('../lib/chance-generators') | ||
@@ -1020,5 +1020,23 @@ const expect = require('unexpected') | ||
describe('magicString', () => { | ||
describe('when global.recordLocation.magicValues is not available', () => { | ||
it('generates the values with the string generator', () => { | ||
const generator = chance.magicString | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
'6(n25SSlGl', 'eH#ySk', 'Wbe)19*pan]nTwTM', 'FbvMT', 'kdv[BrHg6To' | ||
]) | ||
}) | ||
}) | ||
describe('when global.recordLocation.magicValues is available', () => { | ||
beforeEach(() => { | ||
global.recordLocation = { magicValues: new Set(['foo', 'bar', 'baz']) } | ||
global.recordLocation = { | ||
magicValues: new Set([1, 666, 'foo', 4.5, 'bar', -Infinity, 42, 'baz', Infinity]) | ||
} | ||
}) | ||
@@ -1046,2 +1064,229 @@ | ||
describe('magicInteger', () => { | ||
describe('when global.recordLocation.magicValues is not available', () => { | ||
it('generates the values with the integer generator', () => { | ||
const generator = chance.magicInteger | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
-2260084377780224, 5342043492581376, 8119347222413312, | ||
-5702731889115136, 4179231256870912 | ||
]) | ||
}) | ||
}) | ||
describe('when global.recordLocation.magicValues is available', () => { | ||
beforeEach(() => { | ||
global.recordLocation = { | ||
magicValues: new Set([1, 666, 'foo', 4.5, 'bar', -Infinity, 42, 'baz', Infinity]) | ||
} | ||
}) | ||
afterEach(() => { | ||
delete global.recordLocation | ||
}) | ||
it('randomly pick magic integers', () => { | ||
const generator = chance.magicInteger | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
666, 42, 42, 1, 42 | ||
]) | ||
}) | ||
}) | ||
}) | ||
describe('magicNatural', () => { | ||
describe('when global.recordLocation.magicValues is not available', () => { | ||
it('generates the values with the natural generator', () => { | ||
const generator = chance.magicNatural | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
3373557438480384, 7174621373661184, 8563273238577152, | ||
1652233682812928, 6593215255805952 | ||
]) | ||
}) | ||
}) | ||
describe('when global.recordLocation.magicValues is available', () => { | ||
beforeEach(() => { | ||
global.recordLocation = { | ||
magicValues: new Set([1, 666, 'foo', 4.5, 'bar', -Infinity, 6.66, 42, 'baz', Infinity]) | ||
} | ||
}) | ||
afterEach(() => { | ||
delete global.recordLocation | ||
}) | ||
it('randomly pick magic natural numbers', () => { | ||
const generator = chance.magicNatural | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
666, 42, 42, 1, 42 | ||
]) | ||
}) | ||
}) | ||
}) | ||
describe('magicFloating', () => { | ||
describe('when global.recordLocation.magicValues is not available', () => { | ||
it('generates the values with the floating generator', () => { | ||
const generator = chance.magicFloating | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
-226008437778.0224, 534204349258.1376, 811934722241.3312, | ||
-570273188911.5135, 417923125687.0912 | ||
]) | ||
}) | ||
}) | ||
describe('when global.recordLocation.magicValues is available', () => { | ||
beforeEach(() => { | ||
global.recordLocation = { | ||
magicValues: new Set([1, 666, 'foo', 4.5, 'bar', -Infinity, 6.66, 42, 'baz', Infinity]) | ||
} | ||
}) | ||
afterEach(() => { | ||
delete global.recordLocation | ||
}) | ||
it('randomly pick magic floating numbers', () => { | ||
const generator = chance.magicFloating | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
666, 6.66, 42, 1, 6.66 | ||
]) | ||
}) | ||
}) | ||
}) | ||
describe('magicNumber', () => { | ||
describe('when global.recordLocation.magicValues is not available', () => { | ||
it('generates the values with the floating generator', () => { | ||
const generator = chance.magicNumber | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
-226008437778.0224, 534204349258.1376, 811934722241.3312, | ||
-570273188911.5135, 417923125687.0912 | ||
]) | ||
}) | ||
}) | ||
describe('when global.recordLocation.magicValues is available', () => { | ||
beforeEach(() => { | ||
global.recordLocation = { | ||
magicValues: new Set([1, 666, 'foo', 4.5, 'bar', -Infinity, 6.66, 42, 'baz', Infinity]) | ||
} | ||
}) | ||
afterEach(() => { | ||
delete global.recordLocation | ||
}) | ||
it('randomly pick magic numbers', () => { | ||
const generator = chance.magicNumber | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
4.5, 42, Infinity, 666, 42 | ||
]) | ||
}) | ||
}) | ||
}) | ||
describe('number', () => { | ||
describe('when global.recordLocation.magicValues is not available', () => { | ||
it('generates the values with the floating generator', () => { | ||
const generator = chance.number | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
60, -5702731889115135, 5594, 1937, -10.8335 | ||
]) | ||
}) | ||
}) | ||
describe('when global.recordLocation.magicValues is available', () => { | ||
beforeEach(() => { | ||
global.recordLocation = { | ||
magicValues: new Set([1, 666, 'foo', 4.5, 'bar', -Infinity, 6.66, 42, 'baz', Infinity]) | ||
} | ||
}) | ||
afterEach(() => { | ||
delete global.recordLocation | ||
}) | ||
it('takes advantage of the magic numbers', () => { | ||
const generator = chance.number | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to satisfy', [ | ||
60, 2, 5594, 1937, -10.8335, -80.0051, -8.1502, 4.5 | ||
]) | ||
}) | ||
}) | ||
}) | ||
describe('text', () => { | ||
@@ -1068,3 +1313,5 @@ it('generates random text', () => { | ||
beforeEach(() => { | ||
global.recordLocation = { magicValues: new Set(['foo', 'bar', 'baz']) } | ||
global.recordLocation = { | ||
magicValues: new Set([1, 666, 'foo', 4.5, 'bar', -Infinity, 42, 'baz', Infinity]) | ||
} | ||
}) | ||
@@ -1071,0 +1318,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76379
12
1820