Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('parse stringified object', () => {
const key = fc.fullUnicodeString()
const values = [
key,
fc.lorem(1000, false), // words
fc.lorem(100, true), // sentences
fc.boolean(),
fc.integer(),
fc.double(),
fc.constantFrom(null, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY)
]
const yamlArbitrary = fc.anything({ key: key, values: values })
const optionsArbitrary = fc.record(
{
keepBlobsInJSON: fc.boolean(),
keepCstNodes: fc.boolean(),
keepNodeTypes: fc.boolean(),
mapAsMap: fc.constant(false),
merge: fc.boolean(),
schema: fc.constantFrom('core', 'yaml-1.1') // ignore 'failsafe', 'json'
},
{ withDeletedKeys: true }
)
fc.assert(
fc.property(yamlArbitrary, optionsArbitrary, (obj, opts) => {
expect(YAML.parse(YAML.stringify(obj, opts), opts)).toStrictEqual(obj)
})
)
// 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,
(object, options) => deepEqual(queryString.parse(queryString.stringify(object, options), options), object)
)
);
});
it('should serialize using getSerializedArrayType', () => {
const schema = record({
type: constant<'array'>('array'),
items: record({
type: constant<'string'>('string'),
format: constant(none),
deprecated: constant(none),
}),
format: constant(none),
deprecated: constant(none),
});
assert(
property($refArbitrary, schema, string(), (from, schema, name) => {
const expected = pipe(
schema.items,
serializeSchemaObject(from, name),
either.map(getSerializedArrayType(name)),
);
it('should serialize using getSerializedArrayType', () => {
const schema = record({
type: constant<'array'>('array'),
items: record({
type: constant<'string'>('string'),
format: constant(none),
deprecated: constant(none),
}),
format: constant(none),
deprecated: constant(none),
});
assert(
property($refArbitrary, schema, string(), (from, schema, name) => {
const expected = pipe(
schema.items,
serializeSchemaObject(from, name),
either.map(getSerializedArrayType(name)),
);
const serialized = pipe(schema, serializeSchemaObject(from, name));
expect(serialized).toEqual(expected);
it('successful unary call', async () => {
await fc.assert(
fc.asyncProperty(
fc.record({
method: lowerCamelCase(),
requestContext: context(),
responseContext: context(),
useCompression: fc.boolean(),
request: fc.dictionary(fc.string(), fc.jsonObject()),
useRequestFunction: fc.boolean(),
}),
async arb => {
const app = express();
const handler: ModuleRpcServer.ServiceHandlerFor<
typeof unaryServiceDefinition,
any
> = {
async unary(request, requestContext) {
return {
fooRequest: request,
return wrapArb(fc.boolean());
case 'number':
return wrapArb(fc.integer());
case 'undefined':
return wrapArb(fc.constant(undefined));
case 'array':
return wrapArb(k, fc.generic_tuple(...payload[k].map(v => inferPayloadArbitrary(v, orString, withDeletedKeys))));
case 'string':
return wrapArb(fc.string());
case 'object':
if (payload === null) return fc.constant(null);
const recordConfiguration = {};
for (const k of Object.keys(payload)) {
recordConfiguration[k] = inferPayloadArbitrary(payload[k], orString, withDeletedKeys);
}
return fc.record(recordConfiguration, { withDeletedKeys });
}
return fc.constant(undefined);
};
export const binarySearchTree = (
maxDepth: number,
minValue: number = Number.MIN_SAFE_INTEGER,
maxValue: number = Number.MAX_SAFE_INTEGER
): fc.Arbitrary> => {
const valueArbitrary = fc.integer(minValue, maxValue);
if (maxDepth <= 0) {
return fc.record({
value: valueArbitrary,
left: fc.constant(null),
right: fc.constant(null)
});
}
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)
});
});
};
export const perfectBinaryTree = (depth: number): fc.Arbitrary> => {
const valueArbitrary = fc.integer();
if (depth <= 0) {
return fc.record({
value: valueArbitrary,
left: fc.constant(null),
right: fc.constant(null)
});
}
const subTree = perfectBinaryTree(depth - 1);
return fc.record({
value: valueArbitrary,
left: subTree,
right: subTree
});
};
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
});
};