Skip to content

Commit

Permalink
Adding stringOf Arbitrary
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed May 6, 2018
1 parent 0dd1e66 commit cc73ab3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/check/arbitrary/StringArbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ function Base64StringArbitrary(minLength: number, maxLength: number) {
});
}

/**
* Arbitrary producing string using the characters produced by `charArb`
*/
function stringOf(charArb: Arbitrary<string>): Arbitrary<string>;
/**
* Arbitrary producing string using the characters produced by `charArb`
* @param maxLength Upper bound of the generated string length
*/
function stringOf(charArb: Arbitrary<string>, maxLength: number): Arbitrary<string>;
/**
* Arbitrary producing string using the characters produced by `charArb`
* @param minLength Lower bound of the generated string length
* @param maxLength Upper bound of the generated string length
*/
function stringOf(charArb: Arbitrary<string>, minLength: number, maxLength: number): Arbitrary<string>;
function stringOf(charArb: Arbitrary<string>, aLength?: number, bLength?: number): Arbitrary<string> {
return StringArbitrary(charArb, aLength, bLength);
}

/**
* Arbitrary producing string of {@link char}
*/
Expand Down Expand Up @@ -171,4 +190,4 @@ function base64String(aLength?: number, bLength?: number): Arbitrary<string> {
return Base64StringArbitrary(minLength + 3 - (minLength + 3) % 4, maxLength - maxLength % 4); // base64 length is always a multiple of 4
}

export { string, asciiString, string16bits, unicodeString, fullUnicodeString, hexaString, base64String };
export { stringOf, string, asciiString, string16bits, unicodeString, fullUnicodeString, hexaString, base64String };
1 change: 1 addition & 0 deletions src/fast-check-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
hexaString,
string,
string16bits,
stringOf,
unicodeString
} from './check/arbitrary/StringArbitrary';
import { genericTuple, tuple } from './check/arbitrary/TupleArbitrary';
Expand Down
41 changes: 41 additions & 0 deletions test/unit/check/arbitrary/StringArbitrary.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as assert from 'assert';
import * as fc from '../../../../lib/fast-check';

import { constantFrom } from '../../../../src/check/arbitrary/ConstantArbitrary';
import {
stringOf,
string,
asciiString,
string16bits,
Expand All @@ -10,13 +12,52 @@ import {
base64String
} from '../../../../src/check/arbitrary/StringArbitrary';

import * as genericHelper from './generic/GenericArbitraryHelper';

import * as stubRng from '../../stubs/generators';

const minMax = fc
.tuple(fc.integer(0, 10000), fc.integer(0, 10000))
.map(t => (t[0] < t[1] ? { min: t[0], max: t[1] } : { min: t[1], max: t[0] }));

describe('StringArbitrary', () => {
describe('stringOf', () => {
describe('Given no length constraints', () => {
genericHelper.isValidArbitrary(() => stringOf(constantFrom('\u{1f431}', 'D', '1').noShrink()), {
isStrictlySmallerValue: (g1, g2) => [...g1].length < [...g2].length,
isValidValue: (g: string) =>
typeof g === 'string' && [...g].every(c => c === '\u{1f431}' || c === 'D' || c === '1')
});
});
describe('Given maximal length only', () => {
genericHelper.isValidArbitrary(
(maxLength: number) => stringOf(constantFrom('\u{1f431}', 'D', '1').noShrink(), maxLength),
{
seedGenerator: fc.nat(100),
isStrictlySmallerValue: (g1, g2) => [...g1].length < [...g2].length,
isValidValue: (g: string, maxLength: number) =>
typeof g === 'string' &&
[...g].length <= maxLength &&
[...g].every(c => c === '\u{1f431}' || c === 'D' || c === '1')
}
);
});
describe('Given minimal and maximal lengths', () => {
genericHelper.isValidArbitrary(
(constraints: { min: number; max: number }) =>
stringOf(constantFrom('\u{1f431}', 'D', '1').noShrink(), constraints.min, constraints.max),
{
seedGenerator: genericHelper.minMax(fc.nat(100)),
isStrictlySmallerValue: (g1, g2) => [...g1].length < [...g2].length,
isValidValue: (g: string, constraints: { min: number; max: number }) =>
typeof g === 'string' &&
[...g].length >= constraints.min &&
[...g].length <= constraints.max &&
[...g].every(c => c === '\u{1f431}' || c === 'D' || c === '1')
}
);
});
});
describe('string', () => {
it('Should generate printable characters', () =>
fc.assert(
Expand Down

0 comments on commit cc73ab3

Please sign in to comment.