it('should implement a generator', () => { s.def('::ingredient2', s.cat(':quantity', isInteger, ':unit', isString)); expect(s.exercise('::ingredient2', 7)).to.have.length(7) .to.satisfy(sample => every(sample, ([[v1, v2]]) => isInteger(v1) || isString(v2))); });
describe('Test the exercise function', () => { it('should generate 10 integers', () => { expect(s.exercise(isInteger)).to.have.length(10); }); it('should generate 10 booleans', () => { expect(s.exercise(isBoolean)).to.have.length(10).to.satisfy(sample => every(sample, ([b]) => isBoolean(b))); }); it('should generate 5 random values from a set', () => { s.def('::color', ['Red', 'Blue', 'Dun']); expect(s.exercise('::color', 5)).to.have.length(5); }); it('should generate 7 random values from a named spec', () => { s.def('::pairs', s.cat(':n', isInteger, ':s', isString)); expect(s.exercise('::pairs', 7)).to.have.length(7).to.satisfy(sample => every(sample, ([[n, s]]) => isInteger(n) && isString(s))); }); it('should generate 10 random values from a spec object', () => { expect(s.exercise(s.nilable(isString))).to.have.length(10).to.satisfy(sample => every(sample, ([s]) => isNull(s) || isString(s))); }); it('should throw an exception', () => { s.def('::impossible', s.and(isInteger, isString)); expect(() => s.exercise('::impossible')).to.throw(Error, /sampleFromSpec failed.*/); }); });
describe('Test the conformer function', () => { it('should create a spec using a predicate function', () => { const pred = value => isInteger(value) ? value : invalidString; const spec = s.conformer(pred); expect(s.isValid(spec, 13)).to.be.true; expect(s.isValid(spec, '13')).to.be.false; }); it('should create a spec with an unform function', () => { const pred = value => isInteger(value) ? [value] : invalidString; const unform = ([value]) => value; const spec = s.conformer(pred, unform); expect(s.conform(spec, 13)).to.eql([13]); expect(s.unform(spec, [13])).to.equal(13); }); it('should implement a generator', () => { const spec = s.conformer(isString); expect(s.exercise(spec, 7)).to.have.length(7) .to.satisfy(sample => _.every(sample, ([v]) => isString(v))); }); });
if (isInteger(value)) { callback(); return;
.to.satisfy(sample => every(sample, ([v]) => isInteger(v) && isEven(v))); });
.to.satisfy(sample => every(sample, ([v]) => isInteger(v) || isString(v))); }); });