UNPKG

1.13 kBJavaScriptView Raw
1"use strict";
2
3var assert = require("assert");
4
5var arbitraryBless = require("./arbitraryBless.js");
6var generator = require("./generator.js");
7var show = require("./show.js");
8var shrink = require("./shrink.js");
9
10/**
11 - `bless(arb: {...}): arbitrary a`
12
13 Bless almost arbitrary structure to be proper arbitrary. *Note*: this function mutates argument.
14
15 #### Example:
16
17 ```js
18 var arbTokens = jsc.bless({
19 generator: function () {
20 switch (jsc.random(0, 2)) {
21 case 0: return "foo";
22 case 1: return "bar";
23 case 2: return "quux";
24 }
25 }
26 });
27 ```
28*/
29function bless(arb) {
30 assert(arb !== null && typeof arb === "object", "bless: arb should be an object");
31 assert(typeof arb.generator === "function", "bless: arb.generator should be a function");
32
33 // default shrink
34 if (typeof arb.shrink !== "function") {
35 arb.shrink = shrink.noop;
36 }
37
38 // default show
39 if (typeof arb.show !== "function") {
40 arb.show = show.def;
41 }
42
43 generator.bless(arb.generator);
44 shrink.bless(arb.shrink);
45
46 arbitraryBless(arb);
47 return arb;
48}
49
50module.exports = bless;