2021-07-22 17:33:33 +00:00
|
|
|
import { expect } from 'chai';
|
2021-07-22 17:54:02 +00:00
|
|
|
import { array, defaultReportMessage, integer, lorem, tuple, uuid } from 'fast-check';
|
2022-02-15 14:37:22 +00:00
|
|
|
import { describe } from 'mocha';
|
2021-07-21 04:20:39 +00:00
|
|
|
|
2021-07-25 22:40:20 +00:00
|
|
|
import { over } from '../src/index.js';
|
2022-10-18 22:19:35 +00:00
|
|
|
import { itFails, itPasses } from './helpers.js';
|
2021-07-21 04:20:39 +00:00
|
|
|
|
2021-07-22 17:33:33 +00:00
|
|
|
const LARGE_VALUE = Math.floor(Math.random() * 1_000_000_000);
|
|
|
|
|
|
|
|
describe('example properties', () => {
|
2022-10-18 22:19:35 +00:00
|
|
|
it('number arbitrary', (done) => {
|
|
|
|
over('some numbers', integer(), (it) => {
|
|
|
|
it('should be a small number', (n: number) => {
|
|
|
|
return n < LARGE_VALUE;
|
|
|
|
});
|
|
|
|
}, {}, itFails('by returning false', done));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('another number arbitrary', (done) => {
|
|
|
|
over('some numbers', integer(), (it) => {
|
|
|
|
it('should be even', (n: number) => {
|
|
|
|
return n % 2 === 0;
|
|
|
|
});
|
|
|
|
}, {}, itFails('by returning false', done));
|
|
|
|
});
|
2021-07-21 04:54:15 +00:00
|
|
|
|
2022-10-18 22:19:35 +00:00
|
|
|
it('yet another number arbitrary', (done) => {
|
|
|
|
over('some numbers', integer(), (it) => {
|
|
|
|
it('should not throw', (n: number) => {
|
|
|
|
throw new Error('bad number!');
|
|
|
|
});
|
|
|
|
}, {}, itFails('by throwing an error', done));
|
|
|
|
});
|
2021-07-21 23:28:22 +00:00
|
|
|
|
2021-07-22 17:54:02 +00:00
|
|
|
|
2022-10-18 22:19:35 +00:00
|
|
|
it('many number arbitrary', (done) => {
|
|
|
|
over('some numbers', integer(), (it) => {
|
|
|
|
it('should resolve async checks', async (n: number) => {
|
|
|
|
expect(n).to.be.lessThanOrEqual(90);
|
|
|
|
});
|
|
|
|
}, {}, itFails('by throwing an error', done));
|
2021-07-21 04:20:39 +00:00
|
|
|
});
|
2021-07-21 04:50:21 +00:00
|
|
|
|
2022-10-18 22:19:35 +00:00
|
|
|
it('UUID arbitrary', (done) => {
|
|
|
|
over('some IDs', uuid(), (it) => {
|
|
|
|
// beforeEach hooks work normally, since the wrapped it calls through to real it
|
|
|
|
beforeEach(() => {
|
|
|
|
console.log('before each ID test');
|
|
|
|
});
|
2021-07-21 23:28:22 +00:00
|
|
|
|
2022-10-18 22:19:35 +00:00
|
|
|
it('should be long enough', (id: string) => {
|
|
|
|
expect(id).to.have.length.greaterThan(2);
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
// fast-check parameters are supported, like examples
|
|
|
|
// examples: ['a', 'b'],
|
|
|
|
numRuns: 1_000,
|
|
|
|
}, itPasses(done));
|
|
|
|
});
|
2021-07-21 23:28:22 +00:00
|
|
|
|
2022-10-18 22:19:35 +00:00
|
|
|
it('UUID arbitrary failure', (done) => {
|
|
|
|
over('some IDs', uuid(), (it) => {
|
|
|
|
it('should be a good one', (id: string) => {
|
|
|
|
return id[9] !== 'a';
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
// fast-check parameters are supported, like examples
|
|
|
|
examples: ['a', 'b'],
|
|
|
|
numRuns: 1_000,
|
|
|
|
}, itFails('by returning false', done));
|
2021-07-21 04:50:21 +00:00
|
|
|
});
|
2021-07-21 05:13:20 +00:00
|
|
|
|
2022-10-18 22:19:35 +00:00
|
|
|
it('even numbers', (done) => {
|
|
|
|
over('even numbers', integer().filter(n => n % 2 === 0), (it) => {
|
|
|
|
it('should be even', (n: number) => {
|
|
|
|
return n % 2 === 0;
|
|
|
|
});
|
|
|
|
}, {}, itPasses(done));
|
2021-07-21 05:13:20 +00:00
|
|
|
});
|
|
|
|
|
2022-10-18 22:19:35 +00:00
|
|
|
it('mapped properties', (done) => {
|
2022-10-18 23:39:30 +00:00
|
|
|
over('mapped properties', tuple(lorem(), integer()).map(([a, b]) => a.substring(b)), (it) => {
|
2022-10-18 22:19:35 +00:00
|
|
|
it('should have content', (text: string) => {
|
|
|
|
return text.length > 0;
|
|
|
|
});
|
|
|
|
}, {
|
2022-10-18 22:28:50 +00:00
|
|
|
examples: [''],
|
2022-10-18 22:19:35 +00:00
|
|
|
// error formatting can be overridden with a custom handler, or fast-check's default
|
|
|
|
errorReporter: defaultReportMessage,
|
|
|
|
}, itFails('after 1 tests', done));
|
2021-07-22 17:33:33 +00:00
|
|
|
});
|
|
|
|
|
2022-10-18 22:19:35 +00:00
|
|
|
it('equal tuples', (done) => {
|
|
|
|
over('tuples', tuple(integer(), integer()), (it) => {
|
|
|
|
// tuple properties are passed as a single parameter
|
|
|
|
it('should not be equal', ([a, b]) => {
|
|
|
|
return a === b;
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
examples: [[1, 2]]
|
|
|
|
}, itFails('by returning false', done));
|
|
|
|
});
|
2021-07-22 17:33:33 +00:00
|
|
|
|
2022-10-18 22:19:35 +00:00
|
|
|
it('uneven tuples', (done) => {
|
|
|
|
over('tuples', tuple(integer(), integer()), (it) => {
|
|
|
|
it('should be uneven', ([a, b]) => {
|
|
|
|
return a < b;
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
examples: [[1, 2]]
|
|
|
|
}, itFails('by returning false', done));
|
2021-07-22 17:33:33 +00:00
|
|
|
});
|
|
|
|
|
2022-10-18 22:19:35 +00:00
|
|
|
it('arrays', (done) => {
|
|
|
|
over('arrays', array(integer()), (it) => {
|
|
|
|
it('should have items', (t: Array<number>) => {
|
|
|
|
expect(t).to.have.length.lessThan(5_000);
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
numRuns: 1_000,
|
|
|
|
}, itPasses(done));
|
2021-07-21 05:13:20 +00:00
|
|
|
});
|
2021-07-21 04:20:39 +00:00
|
|
|
});
|