jsarbit
is tiny random value generator.
It is not test framework, but you can use with any test library such as mocha
, chai
and any other, if you want to property-based test simply.
npm install jsarbit
var arb = require('jsarbit');
var assert = require('chai').assert;
describe('example', function() {
it('Choose one of', function() {
var values = ['love', 'hate', 'apathy'];
var enums = arb.gen.oneOf(values);
arb.forAll(enums, function(v) {
// value is one of enums.
assert(values.indexOf(v) >= 0);
});
});
});
gen
is set of helper functions to create Arbitrary.
-
gen.of(value: any)
- Asis
value
generator.
- Asis
-
gen.int(min: number, max: number)
-
gen.float(min: number, max: number)
-
gen.posNum()
-
gen.posNum(max: number)
-
gen.negNum()
-
gen.negNum(min: number)
- Generator of numbers in range.
-
gen.array(element: Arbitrary or any value, sizeMin: number, sizeMax: number)
-
gen.array(element: Arbitrary or any value, sizeMax: number)
-
Generator of array.
-
Example:
var dogOrCatArray = gen.array(choose('dog', 'cat'), 10); var someOfPizza = gen.array('pizza', 1, 10);
-
-
gen.array(element: Arbitrary or any value, sizeMin: number, sizeMax: number)
-
gen.array(element: Arbitrary or any value, sizeMax: number)
- Generator of array.
-
gen.arrayOfN(size: number, arb: Arbitrary or value)
- Generator of array which has
size
number of elements generated byarb
(orarb
value ifarb
is not Arbitrary).
- Generator of array which has
-
gen.obj(propArbitrary: object)
-
Generator of object.
-
propArbitrary
is object which property takes Arbitrary. -
Example:
var arb = gen.obj({ "name": gen.oneOf("Alice", "Bob"), "address": { "country": "Japan", // Not Arbitrary property "zip": gen.numericStr(3) }, }); var sample = arb.sample(); /* { "name": "Alice", "address": { "country": "Japan", // as it is. "zip": "491" } } */
-
-
gen.choose(min: number, max: number)
- Generator of arbitrary number in range.
-
gen.oneOf(ary: array)
- Generator which choose a element of
ary
randomly.
- Generator which choose a element of
-
gen.bool()
- Generator of boolean.
-
gen.alphaLowerChar()
-
gen.alphaUpperChar()
-
gen.alphaChar()
-
gen.numericChar()
-
gen.alphaNumericChar()
- Generator of a char.
-
gen.charSeqOf(arb: Arbitrary which generates of char, minLen, maxLen)
-
gen.charSeqOf(arb: Arbitrary, minLen)
- Generator of string consisted of
arb
's sample.
- Generator of string consisted of
-
gen.numericStr(minLen, maxLen)
-
gen.numericStr(maxLen)
-
gen.identifier(minLen, maxLen)
-
gen.identifier(maxLen)
- Generator of string.
-
gen.frequency(freq: array)
-
Chooses one of the given generators with a weighted random distribution.
-
Example
var arb = gen.frequency([ [1, gen.oneOf(['a', 'b', 'c'])], [9, gen.choose(1,10)] ]); forAll(arb, function (value) { console.log(value); // char generated 10% of all. });
-
Arbitrary
is an arbitrary value generator class.
new Arbitrary(sampleFunc: function)
- Creates new instance which has
sample()
function assampleFunc
.
- Creates new instance which has
Arbitrary.sample()
- Generates arbitrary value.
Arbitrary.map(func: function)
- Returns new Arbitrary which
sample()
appliedfunc
tothis.sample()
's return value.
- Returns new Arbitrary which
Arbitrary.flatMap(func: function)
- Returns new Arbitrary which returned by
func
applied withthis.sample()
's return value.
- Returns new Arbitrary which returned by
function to generate with Arbitrary.
forAll(arb: Arbitrary, func: function, count=100)
-
Call
func
number ofcount
times with arbitrary value generated byarb
. -
Example
forAll(choose(0, 50), function(num) { assert(num >= 0 && num <= 50); });
-
MIT