Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import * as fastCheck from 'fast-check';
import test from 'ava';
import queryString from '..';
// Valid query parameters must follow:
// - key can be any unicode string (not empty)
// - value must be one of:
// --> any unicode string
// --> null
// --> array containing values defined above (at least two items)
const queryParamsArbitrary = fastCheck.dictionary(
fastCheck.fullUnicodeString(1, 10),
fastCheck.oneof(
fastCheck.fullUnicodeString(),
fastCheck.constant(null),
fastCheck.array(fastCheck.oneof(fastCheck.fullUnicodeString(), fastCheck.constant(null)), 2, 10)
)
);
const optionsArbitrary = fastCheck.record({
arrayFormat: fastCheck.constantFrom('bracket', 'index', 'none'),
strict: fastCheck.boolean(),
encode: fastCheck.constant(true),
sort: fastCheck.constant(false)
}, {withDeletedKeys: true});
test('should read correctly from stringified query params', t => {
t.notThrows(() => {
fastCheck.assert(
fastCheck.property(
queryParamsArbitrary,
optionsArbitrary,
import deepEqual from 'deep-equal';
import * as fastCheck from 'fast-check';
import test from 'ava';
import queryString from '..';
// Valid query parameters must follow:
// - key can be any unicode string (not empty)
// - value must be one of:
// --> any unicode string
// --> null
// --> array containing values defined above (at least two items)
const queryParamsArbitrary = fastCheck.dictionary(
fastCheck.fullUnicodeString(1, 10),
fastCheck.oneof(
fastCheck.fullUnicodeString(),
fastCheck.constant(null),
fastCheck.array(fastCheck.oneof(fastCheck.fullUnicodeString(), fastCheck.constant(null)), 2, 10)
)
);
const optionsArbitrary = fastCheck.record({
arrayFormat: fastCheck.constantFrom('bracket', 'index', 'none'),
strict: fastCheck.boolean(),
encode: fastCheck.constant(true),
sort: fastCheck.constant(false)
}, {withDeletedKeys: true});
test('should read correctly from stringified query params', t => {
t.notThrows(() => {
fastCheck.assert(
const wrapArb = orString === true ? arb => fc.oneof(arb, fc.string()) : arb => arb;
switch (typeof payload) {
return fc.dictionary(getArbitrary(type.domain), getArbitrary(type.codomain)) as any;
case 'InterfaceType':
case 'PartialType':
case 'ExactType':
return fc.record(record.map(getProps(type), getArbitrary as any) as any) as any;
case 'TupleType':
return (fc.tuple as any)(...type.types.map(getArbitrary));
case 'UnionType':
return fc.oneof(...type.types.map(getArbitrary)) as any;
case 'IntersectionType':
const isObjectIntersection = objectTypes.includes(type.types[0]._tag);
return isObjectIntersection
? (fc.tuple as any)(...type.types.map(t => getArbitrary(t)))
.map((values: Array) => Object.assign({}, ...values))
.filter(type.is)
: fc.oneof(...type.types.map(t => getArbitrary(t))).filter(type.is);
case 'RefinementType':
return getArbitrary(type.type).filter(type.predicate) as any;
}
}
export function getEither(leftArb: fc.Arbitrary, rightArb: fc.Arbitrary<a>): fc.Arbitrary> {
return fc.oneof(getLeft(leftArb), getRight(rightArb))
}
</a>
export function getValidation(
failureArb: fc.Arbitrary,
successArb: fc.Arbitrary<a>
): fc.Arbitrary> {
return fc.oneof(getFailure(failureArb), getSuccess(successArb))
}
</a>
export function getOption<a>(arb: fc.Arbitrary</a><a>): fc.Arbitrary> {
return fc.oneof(getNone(), getSome(arb))
}
</a>
export const binaryTree = (maxDepth: number): fc.Arbitrary> => {
const valueArbitrary = fc.integer();
if (maxDepth <= 0) {
return fc.record({
value: valueArbitrary,
left: fc.constant(null),
right: fc.constant(null)
});
}
const subTree = fc.oneof(fc.constant(null), binaryTree(maxDepth - 1));
return fc.record({
value: valueArbitrary,
left: subTree,
right: subTree
});
};
return valueArbitrary.chain(rootValue => {
const leftArb = binarySearchTree(maxDepth - 1, minValue, rootValue);
const rightArb = rootValue < maxValue ? binarySearchTree(maxDepth - 1, rootValue + 1, maxValue) : fc.constant(null);
return fc.record({
value: fc.constant(rootValue),
left: fc.oneof(fc.constant(null), leftArb),
right: fc.oneof(fc.constant(null), rightArb)
});
});
};