Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Generate dangerous strings by default #3043

Merged
merged 17 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .yarn/versions/82fcb103.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
releases:
fast-check: minor

declined:
- "@fast-check/monorepo"
- "@fast-check/examples"
- "@fast-check/ava"
- "@fast-check/jest"
- "@fast-check/packaged"
- "@fast-check/test-ava-bundle-cjs"
- "@fast-check/test-ava-bundle-esm"
- "@fast-check/test-bundle-esbuild-with-import"
- "@fast-check/test-bundle-esbuild-with-require"
- "@fast-check/test-bundle-node-extension-cjs"
- "@fast-check/test-bundle-node-extension-mjs"
- "@fast-check/test-bundle-node-with-import"
- "@fast-check/test-bundle-node-with-require"
- "@fast-check/test-bundle-rollup-with-import"
- "@fast-check/test-bundle-rollup-with-require"
- "@fast-check/test-bundle-webpack-with-import"
- "@fast-check/test-bundle-webpack-with-require"
- "@fast-check/test-jest-bundle-cjs"
- "@fast-check/test-jest-bundle-esm"
- "@fast-check/test-minimal-support"
- "@fast-check/test-types"
198 changes: 102 additions & 96 deletions packages/fast-check/documentation/Arbitraries.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Arbitrary } from '../../../check/arbitrary/definition/Arbitrary';

const dangerousStrings = [
// Default attributes on raw Object (from ({}).*)
'__defineGetter__',
'__defineSetter__',
'__lookupGetter__',
'__lookupSetter__',
'__proto__',
'constructor',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'toLocaleString',
'toString',
'valueOf',
// Other classical defaults (from MyClassName.*)
'apply',
'arguments',
'bind',
'call',
'caller',
'length',
'name',
'prototype',
// React
'key',
'ref',
];

/** @internal */
function computeCandidateString(
dangerous: string,
charArbitrary: Arbitrary<string>,
stringSplitter: (value: string) => string[]
): string[] | undefined {
let candidate: string[];
try {
candidate = stringSplitter(dangerous);
} catch (err) {
// No split found for `dangerous`, `dangerous` cannot be shrunk by arrays made of `charArbitrary`
return undefined;
}
for (const entry of candidate) {
if (!charArbitrary.canShrinkWithoutContext(entry)) {
// Item `entry` cannot be shrunk by `charArbitrary` thus we cannot keep this candidate
// Remark: depending on the passed `stringSplitter` this check may already have been done
return undefined;
}
}
return candidate;
}

/** @internal */
export function createSlicesForString(
charArbitrary: Arbitrary<string>,
stringSplitter: (value: string) => string[]
): string[][] {
const slicesForString: string[][] = [];
for (const dangerous of dangerousStrings) {
const candidate = computeCandidateString(dangerous, charArbitrary, stringSplitter);
if (candidate) {
dubzzz marked this conversation as resolved.
Show resolved Hide resolved
slicesForString.push(candidate);
}
}
return slicesForString;
}
8 changes: 6 additions & 2 deletions packages/fast-check/src/arbitrary/asciiString.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { array } from './array';
import { array, ArrayConstraintsInternal } from './array';
import { ascii } from './ascii';
import { StringSharedConstraints } from './_shared/StringSharedConstraints';
import { codePointsToStringMapper, codePointsToStringUnmapper } from './_internals/mappers/CodePointsToString';
import { createSlicesForString } from './_internals/helpers/SlicesForStringBuilder';
export { StringSharedConstraints } from './_shared/StringSharedConstraints';

/**
Expand All @@ -14,5 +15,8 @@ export { StringSharedConstraints } from './_shared/StringSharedConstraints';
* @public
*/
export function asciiString(constraints: StringSharedConstraints = {}): Arbitrary<string> {
return array(ascii(), constraints).map(codePointsToStringMapper, codePointsToStringUnmapper);
const charArbitrary = ascii();
const experimentalCustomSlices = createSlicesForString(charArbitrary, codePointsToStringUnmapper);
const enrichedConstraints: ArrayConstraintsInternal<string> = { ...constraints, experimentalCustomSlices };
return array(charArbitrary, enrichedConstraints).map(codePointsToStringMapper, codePointsToStringUnmapper);
}
13 changes: 11 additions & 2 deletions packages/fast-check/src/arbitrary/base64String.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { array } from './array';
import { array, ArrayConstraintsInternal } from './array';
import { base64 } from './base64';
import { MaxLengthUpperBound } from './_internals/helpers/MaxLengthFromMinLength';
import { StringSharedConstraints } from './_shared/StringSharedConstraints';
import { codePointsToStringMapper, codePointsToStringUnmapper } from './_internals/mappers/CodePointsToString';
import { stringToBase64Mapper, stringToBase64Unmapper } from './_internals/mappers/StringToBase64';
import { createSlicesForString } from './_internals/helpers/SlicesForStringBuilder';
export { StringSharedConstraints } from './_shared/StringSharedConstraints';

/**
Expand All @@ -28,7 +29,15 @@ function base64String(constraints: StringSharedConstraints = {}): Arbitrary<stri
if (minLength % 4 !== 0) throw new Error('Minimal length of base64 strings must be a multiple of 4');
if (maxLength % 4 !== 0) throw new Error('Maximal length of base64 strings must be a multiple of 4');

return array(base64(), { minLength, maxLength, size: requestedSize })
const charArbitrary = base64();
const experimentalCustomSlices = createSlicesForString(charArbitrary, codePointsToStringUnmapper);
const enrichedConstraints: ArrayConstraintsInternal<string> = {
minLength,
maxLength,
size: requestedSize,
experimentalCustomSlices,
};
return array(charArbitrary, enrichedConstraints)
.map(codePointsToStringMapper, codePointsToStringUnmapper)
.map(stringToBase64Mapper, stringToBase64Unmapper);
}
Expand Down
8 changes: 6 additions & 2 deletions packages/fast-check/src/arbitrary/fullUnicodeString.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { array } from './array';
import { array, ArrayConstraintsInternal } from './array';
import { fullUnicode } from './fullUnicode';
import { StringSharedConstraints } from './_shared/StringSharedConstraints';
import { codePointsToStringMapper, codePointsToStringUnmapper } from './_internals/mappers/CodePointsToString';
import { createSlicesForString } from './_internals/helpers/SlicesForStringBuilder';
export { StringSharedConstraints } from './_shared/StringSharedConstraints';

/**
Expand All @@ -14,5 +15,8 @@ export { StringSharedConstraints } from './_shared/StringSharedConstraints';
* @public
*/
export function fullUnicodeString(constraints: StringSharedConstraints = {}): Arbitrary<string> {
return array(fullUnicode(), constraints).map(codePointsToStringMapper, codePointsToStringUnmapper);
const charArbitrary = fullUnicode();
const experimentalCustomSlices = createSlicesForString(charArbitrary, codePointsToStringUnmapper);
const enrichedConstraints: ArrayConstraintsInternal<string> = { ...constraints, experimentalCustomSlices };
return array(charArbitrary, enrichedConstraints).map(codePointsToStringMapper, codePointsToStringUnmapper);
}
8 changes: 6 additions & 2 deletions packages/fast-check/src/arbitrary/hexaString.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { array } from './array';
import { array, ArrayConstraintsInternal } from './array';
import { hexa } from './hexa';
import { StringSharedConstraints } from './_shared/StringSharedConstraints';
import { codePointsToStringMapper, codePointsToStringUnmapper } from './_internals/mappers/CodePointsToString';
import { createSlicesForString } from './_internals/helpers/SlicesForStringBuilder';
export { StringSharedConstraints } from './_shared/StringSharedConstraints';

/**
Expand All @@ -14,6 +15,9 @@ export { StringSharedConstraints } from './_shared/StringSharedConstraints';
* @public
*/
function hexaString(constraints: StringSharedConstraints = {}): Arbitrary<string> {
return array(hexa(), constraints).map(codePointsToStringMapper, codePointsToStringUnmapper);
const charArbitrary = hexa();
const experimentalCustomSlices = createSlicesForString(charArbitrary, codePointsToStringUnmapper);
const enrichedConstraints: ArrayConstraintsInternal<string> = { ...constraints, experimentalCustomSlices };
return array(charArbitrary, enrichedConstraints).map(codePointsToStringMapper, codePointsToStringUnmapper);
}
export { hexaString };
8 changes: 6 additions & 2 deletions packages/fast-check/src/arbitrary/string.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { array } from './array';
import { array, ArrayConstraintsInternal } from './array';
import { char } from './char';
import { StringSharedConstraints } from './_shared/StringSharedConstraints';
import { codePointsToStringMapper, codePointsToStringUnmapper } from './_internals/mappers/CodePointsToString';
import { createSlicesForString } from './_internals/helpers/SlicesForStringBuilder';
export { StringSharedConstraints } from './_shared/StringSharedConstraints';

/**
Expand All @@ -14,5 +15,8 @@ export { StringSharedConstraints } from './_shared/StringSharedConstraints';
* @public
*/
export function string(constraints: StringSharedConstraints = {}): Arbitrary<string> {
return array(char(), constraints).map(codePointsToStringMapper, codePointsToStringUnmapper);
const charArbitrary = char();
const experimentalCustomSlices = createSlicesForString(charArbitrary, codePointsToStringUnmapper);
const enrichedConstraints: ArrayConstraintsInternal<string> = { ...constraints, experimentalCustomSlices };
return array(charArbitrary, enrichedConstraints).map(codePointsToStringMapper, codePointsToStringUnmapper);
}
8 changes: 6 additions & 2 deletions packages/fast-check/src/arbitrary/string16bits.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { array } from './array';
import { array, ArrayConstraintsInternal } from './array';
import { char16bits } from './char16bits';
import { StringSharedConstraints } from './_shared/StringSharedConstraints';
import { charsToStringMapper, charsToStringUnmapper } from './_internals/mappers/CharsToString';
import { createSlicesForString } from './_internals/helpers/SlicesForStringBuilder';
export { StringSharedConstraints } from './_shared/StringSharedConstraints';

/**
Expand All @@ -14,5 +15,8 @@ export { StringSharedConstraints } from './_shared/StringSharedConstraints';
* @public
*/
export function string16bits(constraints: StringSharedConstraints = {}): Arbitrary<string> {
return array(char16bits(), constraints).map(charsToStringMapper, charsToStringUnmapper);
const charArbitrary = char16bits();
const experimentalCustomSlices = createSlicesForString(charArbitrary, charsToStringUnmapper);
const enrichedConstraints: ArrayConstraintsInternal<string> = { ...constraints, experimentalCustomSlices };
return array(charArbitrary, enrichedConstraints).map(charsToStringMapper, charsToStringUnmapper);
}
8 changes: 6 additions & 2 deletions packages/fast-check/src/arbitrary/stringOf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { array } from './array';
import { array, ArrayConstraintsInternal } from './array';
import { StringSharedConstraints } from './_shared/StringSharedConstraints';
import { patternsToStringMapper, patternsToStringUnmapperFor } from './_internals/mappers/PatternsToString';
import { createSlicesForString } from './_internals/helpers/SlicesForStringBuilder';
export { StringSharedConstraints } from './_shared/StringSharedConstraints';

/**
Expand All @@ -14,5 +15,8 @@ export { StringSharedConstraints } from './_shared/StringSharedConstraints';
* @public
*/
export function stringOf(charArb: Arbitrary<string>, constraints: StringSharedConstraints = {}): Arbitrary<string> {
return array(charArb, constraints).map(patternsToStringMapper, patternsToStringUnmapperFor(charArb, constraints));
const unmapper = patternsToStringUnmapperFor(charArb, constraints);
const experimentalCustomSlices = createSlicesForString(charArb, unmapper);
const enrichedConstraints: ArrayConstraintsInternal<string> = { ...constraints, experimentalCustomSlices };
return array(charArb, enrichedConstraints).map(patternsToStringMapper, unmapper);
}
8 changes: 6 additions & 2 deletions packages/fast-check/src/arbitrary/unicodeString.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { array } from './array';
import { array, ArrayConstraintsInternal } from './array';
import { unicode } from './unicode';
import { StringSharedConstraints } from './_shared/StringSharedConstraints';
import { codePointsToStringMapper, codePointsToStringUnmapper } from './_internals/mappers/CodePointsToString';
import { createSlicesForString } from './_internals/helpers/SlicesForStringBuilder';
export { StringSharedConstraints } from './_shared/StringSharedConstraints';

/**
Expand All @@ -14,5 +15,8 @@ export { StringSharedConstraints } from './_shared/StringSharedConstraints';
* @public
*/
export function unicodeString(constraints: StringSharedConstraints = {}): Arbitrary<string> {
return array(unicode(), constraints).map(codePointsToStringMapper, codePointsToStringUnmapper);
const charArbitrary = unicode();
const experimentalCustomSlices = createSlicesForString(charArbitrary, codePointsToStringUnmapper);
const enrichedConstraints: ArrayConstraintsInternal<string> = { ...constraints, experimentalCustomSlices };
return array(charArbitrary, enrichedConstraints).map(codePointsToStringMapper, codePointsToStringUnmapper);
}
Loading